1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.dircache;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertSame;
16
17 import org.eclipse.jgit.junit.RepositoryTestCase;
18 import org.eclipse.jgit.lib.FileMode;
19 import org.junit.Test;
20
21 public class DirCacheFindTest extends RepositoryTestCase {
22 @Test
23 public void testEntriesWithin() throws Exception {
24 final DirCache dc = db.readDirCache();
25
26 final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
27 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
28 for (int i = 0; i < paths.length; i++) {
29 ents[i] = new DirCacheEntry(paths[i]);
30 ents[i].setFileMode(FileMode.REGULAR_FILE);
31 }
32 final int aFirst = 1;
33 final int aLast = 3;
34
35 final DirCacheBuilder b = dc.builder();
36 for (DirCacheEntry ent : ents) {
37 b.add(ent);
38 }
39 b.finish();
40
41 assertEquals(paths.length, dc.getEntryCount());
42 for (int i = 0; i < ents.length; i++)
43 assertSame(ents[i], dc.getEntry(i));
44
45 {
46 final DirCacheEntry[] aContents = dc.getEntriesWithin("a");
47 assertNotNull(aContents);
48 assertEquals(aLast - aFirst + 1, aContents.length);
49 for (int i = aFirst, j = 0; i <= aLast; i++, j++)
50 assertSame(ents[i], aContents[j]);
51 }
52 {
53 final DirCacheEntry[] aContents = dc.getEntriesWithin("a/");
54 assertNotNull(aContents);
55 assertEquals(aLast - aFirst + 1, aContents.length);
56 for (int i = aFirst, j = 0; i <= aLast; i++, j++)
57 assertSame(ents[i], aContents[j]);
58 }
59 {
60 final DirCacheEntry[] aContents = dc.getEntriesWithin("");
61 assertNotNull(aContents);
62 assertEquals(ents.length, aContents.length);
63 for (int i = 0; i < ents.length; i++)
64 assertSame(ents[i], aContents[i]);
65 }
66
67 assertNotNull(dc.getEntriesWithin("a-"));
68 assertEquals(0, dc.getEntriesWithin("a-").length);
69
70 assertNotNull(dc.getEntriesWithin("a0b"));
71 assertEquals(0, dc.getEntriesWithin("a0b-").length);
72
73 assertNotNull(dc.getEntriesWithin("zoo"));
74 assertEquals(0, dc.getEntriesWithin("zoo-").length);
75 }
76 }