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
45 package org.eclipse.jgit.dircache;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.assertFalse;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertNotSame;
51 import static org.junit.Assert.assertSame;
52 import static org.junit.Assert.assertTrue;
53 import static org.junit.Assert.fail;
54
55 import java.io.File;
56
57 import org.eclipse.jgit.events.IndexChangedEvent;
58 import org.eclipse.jgit.events.IndexChangedListener;
59 import org.eclipse.jgit.events.ListenerList;
60 import org.eclipse.jgit.junit.RepositoryTestCase;
61 import org.eclipse.jgit.lib.FileMode;
62 import org.eclipse.jgit.lib.ObjectId;
63 import org.junit.Test;
64
65 public class DirCacheBuilderTest extends RepositoryTestCase {
66 @Test
67 public void testBuildEmpty() throws Exception {
68 {
69 final DirCache dc = db.lockDirCache();
70 final DirCacheBuilder b = dc.builder();
71 assertNotNull(b);
72 b.finish();
73 dc.write();
74 assertTrue(dc.commit());
75 }
76 {
77 final DirCache dc = db.readDirCache();
78 assertEquals(0, dc.getEntryCount());
79 }
80 }
81
82 @Test
83 public void testBuildRejectsUnsetFileMode() throws Exception {
84 final DirCache dc = DirCache.newInCore();
85 final DirCacheBuilder b = dc.builder();
86 assertNotNull(b);
87
88 final DirCacheEntry e = new DirCacheEntry("a");
89 assertEquals(0, e.getRawMode());
90 try {
91 b.add(e);
92 } catch (IllegalArgumentException err) {
93 assertEquals("FileMode not set for path a", err.getMessage());
94 }
95 }
96
97 @Test
98 public void testBuildOneFile_FinishWriteCommit() throws Exception {
99 final String path = "a-file-path";
100 final FileMode mode = FileMode.REGULAR_FILE;
101 final long lastModified = 1218123387057L;
102 final int length = 1342;
103 final DirCacheEntry entOrig;
104 {
105 final DirCache dc = db.lockDirCache();
106 final DirCacheBuilder b = dc.builder();
107 assertNotNull(b);
108
109 entOrig = new DirCacheEntry(path);
110 entOrig.setFileMode(mode);
111 entOrig.setLastModified(lastModified);
112 entOrig.setLength(length);
113
114 assertNotSame(path, entOrig.getPathString());
115 assertEquals(path, entOrig.getPathString());
116 assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
117 assertEquals(mode.getBits(), entOrig.getRawMode());
118 assertEquals(0, entOrig.getStage());
119 assertEquals(lastModified, entOrig.getLastModified());
120 assertEquals(length, entOrig.getLength());
121 assertFalse(entOrig.isAssumeValid());
122 b.add(entOrig);
123
124 b.finish();
125 assertEquals(1, dc.getEntryCount());
126 assertSame(entOrig, dc.getEntry(0));
127
128 dc.write();
129 assertTrue(dc.commit());
130 }
131 {
132 final DirCache dc = db.readDirCache();
133 assertEquals(1, dc.getEntryCount());
134
135 final DirCacheEntry entRead = dc.getEntry(0);
136 assertNotSame(entOrig, entRead);
137 assertEquals(path, entRead.getPathString());
138 assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
139 assertEquals(mode.getBits(), entOrig.getRawMode());
140 assertEquals(0, entOrig.getStage());
141 assertEquals(lastModified, entOrig.getLastModified());
142 assertEquals(length, entOrig.getLength());
143 assertFalse(entOrig.isAssumeValid());
144 }
145 }
146
147 @Test
148 public void testBuildOneFile_Commit() throws Exception {
149 final String path = "a-file-path";
150 final FileMode mode = FileMode.REGULAR_FILE;
151 final long lastModified = 1218123387057L;
152 final int length = 1342;
153 final DirCacheEntry entOrig;
154 {
155 final DirCache dc = db.lockDirCache();
156 final DirCacheBuilder b = dc.builder();
157 assertNotNull(b);
158
159 entOrig = new DirCacheEntry(path);
160 entOrig.setFileMode(mode);
161 entOrig.setLastModified(lastModified);
162 entOrig.setLength(length);
163
164 assertNotSame(path, entOrig.getPathString());
165 assertEquals(path, entOrig.getPathString());
166 assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
167 assertEquals(mode.getBits(), entOrig.getRawMode());
168 assertEquals(0, entOrig.getStage());
169 assertEquals(lastModified, entOrig.getLastModified());
170 assertEquals(length, entOrig.getLength());
171 assertFalse(entOrig.isAssumeValid());
172 b.add(entOrig);
173
174 assertTrue(b.commit());
175 assertEquals(1, dc.getEntryCount());
176 assertSame(entOrig, dc.getEntry(0));
177 assertFalse(new File(db.getDirectory(), "index.lock").exists());
178 }
179 {
180 final DirCache dc = db.readDirCache();
181 assertEquals(1, dc.getEntryCount());
182
183 final DirCacheEntry entRead = dc.getEntry(0);
184 assertNotSame(entOrig, entRead);
185 assertEquals(path, entRead.getPathString());
186 assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
187 assertEquals(mode.getBits(), entOrig.getRawMode());
188 assertEquals(0, entOrig.getStage());
189 assertEquals(lastModified, entOrig.getLastModified());
190 assertEquals(length, entOrig.getLength());
191 assertFalse(entOrig.isAssumeValid());
192 }
193 }
194
195 @Test
196 public void testBuildOneFile_Commit_IndexChangedEvent()
197 throws Exception {
198 final class ReceivedEventMarkerException extends RuntimeException {
199 private static final long serialVersionUID = 1L;
200
201 }
202
203 final String path = "a-file-path";
204 final FileMode mode = FileMode.REGULAR_FILE;
205
206 final long lastModified = 1218123387057L;
207 final int length = 1342;
208 DirCacheEntry entOrig;
209 boolean receivedEvent = false;
210
211 DirCache dc = db.lockDirCache();
212 IndexChangedListener listener = new IndexChangedListener() {
213
214 @Override
215 public void onIndexChanged(IndexChangedEvent event) {
216 throw new ReceivedEventMarkerException();
217 }
218 };
219
220 ListenerList l = db.getListenerList();
221 l.addIndexChangedListener(listener);
222 DirCacheBuilder b = dc.builder();
223
224 entOrig = new DirCacheEntry(path);
225 entOrig.setFileMode(mode);
226 entOrig.setLastModified(lastModified);
227 entOrig.setLength(length);
228 b.add(entOrig);
229 try {
230 b.commit();
231 } catch (ReceivedEventMarkerException e) {
232 receivedEvent = true;
233 }
234 if (!receivedEvent)
235 fail("did not receive IndexChangedEvent");
236
237
238
239 dc = db.lockDirCache();
240 listener = new IndexChangedListener() {
241
242 @Override
243 public void onIndexChanged(IndexChangedEvent event) {
244 throw new ReceivedEventMarkerException();
245 }
246 };
247
248 l = db.getListenerList();
249 l.addIndexChangedListener(listener);
250 b = dc.builder();
251
252 entOrig = new DirCacheEntry(path);
253 entOrig.setFileMode(mode);
254 entOrig.setLastModified(lastModified);
255 entOrig.setLength(length);
256 b.add(entOrig);
257 try {
258 b.commit();
259 } catch (ReceivedEventMarkerException e) {
260 fail("unexpected IndexChangedEvent");
261 }
262 }
263
264 @Test
265 public void testFindSingleFile() throws Exception {
266 final String path = "a-file-path";
267 final DirCache dc = db.readDirCache();
268 final DirCacheBuilder b = dc.builder();
269 assertNotNull(b);
270
271 final DirCacheEntry entOrig = new DirCacheEntry(path);
272 entOrig.setFileMode(FileMode.REGULAR_FILE);
273 assertNotSame(path, entOrig.getPathString());
274 assertEquals(path, entOrig.getPathString());
275 b.add(entOrig);
276 b.finish();
277
278 assertEquals(1, dc.getEntryCount());
279 assertSame(entOrig, dc.getEntry(0));
280 assertEquals(0, dc.findEntry(path));
281
282 assertEquals(-1, dc.findEntry("@@-before"));
283 assertEquals(0, real(dc.findEntry("@@-before")));
284
285 assertEquals(-2, dc.findEntry("a-zoo"));
286 assertEquals(1, real(dc.findEntry("a-zoo")));
287
288 assertSame(entOrig, dc.getEntry(path));
289 }
290
291 @Test
292 public void testAdd_InGitSortOrder() throws Exception {
293 final DirCache dc = db.readDirCache();
294
295 final String[] paths = { "a-", "a.b", "a/b", "a0b" };
296 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
297 for (int i = 0; i < paths.length; i++) {
298 ents[i] = new DirCacheEntry(paths[i]);
299 ents[i].setFileMode(FileMode.REGULAR_FILE);
300 }
301
302 final DirCacheBuilder b = dc.builder();
303 for (int i = 0; i < ents.length; i++)
304 b.add(ents[i]);
305 b.finish();
306
307 assertEquals(paths.length, dc.getEntryCount());
308 for (int i = 0; i < paths.length; i++) {
309 assertSame(ents[i], dc.getEntry(i));
310 assertEquals(paths[i], dc.getEntry(i).getPathString());
311 assertEquals(i, dc.findEntry(paths[i]));
312 assertSame(ents[i], dc.getEntry(paths[i]));
313 }
314 }
315
316 @Test
317 public void testAdd_ReverseGitSortOrder() throws Exception {
318 final DirCache dc = db.readDirCache();
319
320 final String[] paths = { "a-", "a.b", "a/b", "a0b" };
321 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
322 for (int i = 0; i < paths.length; i++) {
323 ents[i] = new DirCacheEntry(paths[i]);
324 ents[i].setFileMode(FileMode.REGULAR_FILE);
325 }
326
327 final DirCacheBuilder b = dc.builder();
328 for (int i = ents.length - 1; i >= 0; i--)
329 b.add(ents[i]);
330 b.finish();
331
332 assertEquals(paths.length, dc.getEntryCount());
333 for (int i = 0; i < paths.length; i++) {
334 assertSame(ents[i], dc.getEntry(i));
335 assertEquals(paths[i], dc.getEntry(i).getPathString());
336 assertEquals(i, dc.findEntry(paths[i]));
337 assertSame(ents[i], dc.getEntry(paths[i]));
338 }
339 }
340
341 @Test
342 public void testBuilderClear() throws Exception {
343 final DirCache dc = db.readDirCache();
344
345 final String[] paths = { "a-", "a.b", "a/b", "a0b" };
346 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
347 for (int i = 0; i < paths.length; i++) {
348 ents[i] = new DirCacheEntry(paths[i]);
349 ents[i].setFileMode(FileMode.REGULAR_FILE);
350 }
351 {
352 final DirCacheBuilder b = dc.builder();
353 for (int i = 0; i < ents.length; i++)
354 b.add(ents[i]);
355 b.finish();
356 }
357 assertEquals(paths.length, dc.getEntryCount());
358 {
359 final DirCacheBuilder b = dc.builder();
360 b.finish();
361 }
362 assertEquals(0, dc.getEntryCount());
363 }
364
365 private static int real(int eIdx) {
366 if (eIdx < 0)
367 eIdx = -(eIdx + 1);
368 return eIdx;
369 }
370 }