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