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