1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.dircache;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertFalse;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51
52 import org.eclipse.jgit.lib.FileMode;
53 import org.eclipse.jgit.lib.ObjectId;
54 import org.junit.Test;
55
56 public class DirCacheEntryTest {
57 @Test
58 public void testIsValidPath() {
59 assertTrue(isValidPath("a"));
60 assertTrue(isValidPath("a/b"));
61 assertTrue(isValidPath("ab/cd/ef"));
62
63 assertFalse(isValidPath(""));
64 assertFalse(isValidPath("/a"));
65 assertFalse(isValidPath("a//b"));
66 assertFalse(isValidPath("ab/cd//ef"));
67 assertFalse(isValidPath("a/"));
68 assertFalse(isValidPath("ab/cd/ef/"));
69 assertFalse(isValidPath("a\u0000b"));
70 }
71
72 private static boolean isValidPath(final String path) {
73 try {
74 DirCacheCheckout.checkValidPath(path);
75 return true;
76 } catch (InvalidPathException e) {
77 return false;
78 }
79 }
80
81 @Test
82 public void testCreate_ByStringPath() {
83 assertEquals("a", new DirCacheEntry("a").getPathString());
84 assertEquals("a/b", new DirCacheEntry("a/b").getPathString());
85
86 try {
87 new DirCacheEntry("/a");
88 fail("Incorrectly created DirCacheEntry");
89 } catch (IllegalArgumentException err) {
90 assertEquals("Invalid path: /a", err.getMessage());
91 }
92 }
93
94 @Test
95 public void testCreate_ByStringPathAndStage() {
96 DirCacheEntry e;
97
98 e = new DirCacheEntry("a", 0);
99 assertEquals("a", e.getPathString());
100 assertEquals(0, e.getStage());
101
102 e = new DirCacheEntry("a/b", 1);
103 assertEquals("a/b", e.getPathString());
104 assertEquals(1, e.getStage());
105
106 e = new DirCacheEntry("a/c", 2);
107 assertEquals("a/c", e.getPathString());
108 assertEquals(2, e.getStage());
109
110 e = new DirCacheEntry("a/d", 3);
111 assertEquals("a/d", e.getPathString());
112 assertEquals(3, e.getStage());
113
114 try {
115 new DirCacheEntry("/a", 1);
116 fail("Incorrectly created DirCacheEntry");
117 } catch (IllegalArgumentException err) {
118 assertEquals("Invalid path: /a", err.getMessage());
119 }
120
121 try {
122 new DirCacheEntry("a", -11);
123 fail("Incorrectly created DirCacheEntry");
124 } catch (IllegalArgumentException err) {
125 assertEquals("Invalid stage -11 for path a", err.getMessage());
126 }
127
128 try {
129 new DirCacheEntry("a", 4);
130 fail("Incorrectly created DirCacheEntry");
131 } catch (IllegalArgumentException err) {
132 assertEquals("Invalid stage 4 for path a", err.getMessage());
133 }
134 }
135
136 @Test
137 public void testSetFileMode() {
138 final DirCacheEntry e = new DirCacheEntry("a");
139
140 assertEquals(0, e.getRawMode());
141
142 e.setFileMode(FileMode.REGULAR_FILE);
143 assertSame(FileMode.REGULAR_FILE, e.getFileMode());
144 assertEquals(FileMode.REGULAR_FILE.getBits(), e.getRawMode());
145
146 e.setFileMode(FileMode.EXECUTABLE_FILE);
147 assertSame(FileMode.EXECUTABLE_FILE, e.getFileMode());
148 assertEquals(FileMode.EXECUTABLE_FILE.getBits(), e.getRawMode());
149
150 e.setFileMode(FileMode.SYMLINK);
151 assertSame(FileMode.SYMLINK, e.getFileMode());
152 assertEquals(FileMode.SYMLINK.getBits(), e.getRawMode());
153
154 e.setFileMode(FileMode.GITLINK);
155 assertSame(FileMode.GITLINK, e.getFileMode());
156 assertEquals(FileMode.GITLINK.getBits(), e.getRawMode());
157
158 try {
159 e.setFileMode(FileMode.MISSING);
160 fail("incorrectly accepted FileMode.MISSING");
161 } catch (IllegalArgumentException err) {
162 assertEquals("Invalid mode 0 for path a", err.getMessage());
163 }
164
165 try {
166 e.setFileMode(FileMode.TREE);
167 fail("incorrectly accepted FileMode.TREE");
168 } catch (IllegalArgumentException err) {
169 assertEquals("Invalid mode 40000 for path a", err.getMessage());
170 }
171 }
172
173 @Test
174 public void testCopyMetaDataWithStage() {
175 copyMetaDataHelper(false);
176 }
177
178 @Test
179 public void testCopyMetaDataWithoutStage() {
180 copyMetaDataHelper(true);
181 }
182
183 private static void copyMetaDataHelper(final boolean keepStage) {
184 DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_2);
185 e.setAssumeValid(false);
186 e.setCreationTime(2L);
187 e.setFileMode(FileMode.EXECUTABLE_FILE);
188 e.setLastModified(3L);
189 e.setLength(100L);
190 e.setObjectId(ObjectId
191 .fromString("0123456789012345678901234567890123456789"));
192 e.setUpdateNeeded(true);
193
194 DirCacheEntry f = new DirCacheEntry("someother/path",
195 DirCacheEntry.STAGE_1);
196 f.setAssumeValid(true);
197 f.setCreationTime(10L);
198 f.setFileMode(FileMode.SYMLINK);
199 f.setLastModified(20L);
200 f.setLength(100000000L);
201 f.setObjectId(ObjectId
202 .fromString("1234567890123456789012345678901234567890"));
203 f.setUpdateNeeded(true);
204
205 e.copyMetaData(f, keepStage);
206 assertTrue(e.isAssumeValid());
207 assertEquals(10L, e.getCreationTime());
208 assertEquals(
209 ObjectId.fromString("1234567890123456789012345678901234567890"),
210 e.getObjectId());
211 assertEquals(FileMode.SYMLINK, e.getFileMode());
212 assertEquals(20L, e.getLastModified());
213 assertEquals(100000000L, e.getLength());
214 if (keepStage)
215 assertEquals(DirCacheEntry.STAGE_2, e.getStage());
216 else
217 assertEquals(DirCacheEntry.STAGE_1, e.getStage());
218 assertTrue(e.isUpdateNeeded());
219 assertEquals("some/path", e.getPathString());
220 }
221 }