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.assertNotNull;
49 import static org.junit.Assert.assertNotSame;
50 import static org.junit.Assert.assertNull;
51 import static org.junit.Assert.assertSame;
52
53 import java.io.IOException;
54
55 import org.eclipse.jgit.errors.CorruptObjectException;
56 import org.eclipse.jgit.junit.RepositoryTestCase;
57 import org.eclipse.jgit.lib.FileMode;
58 import org.junit.Test;
59
60 public class DirCacheTreeTest extends RepositoryTestCase {
61 @Test
62 public void testEmptyCache_NoCacheTree() throws Exception {
63 final DirCache dc = db.readDirCache();
64 assertNull(dc.getCacheTree(false));
65 }
66
67 @Test
68 public void testEmptyCache_CreateEmptyCacheTree() throws Exception {
69 final DirCache dc = db.readDirCache();
70 final DirCacheTree tree = dc.getCacheTree(true);
71 assertNotNull(tree);
72 assertSame(tree, dc.getCacheTree(false));
73 assertSame(tree, dc.getCacheTree(true));
74 assertEquals("", tree.getNameString());
75 assertEquals("", tree.getPathString());
76 assertEquals(0, tree.getChildCount());
77 assertEquals(0, tree.getEntrySpan());
78 assertFalse(tree.isValid());
79 }
80
81 @Test
82 public void testEmptyCache_Clear_NoCacheTree() throws Exception {
83 final DirCache dc = db.readDirCache();
84 final DirCacheTree tree = dc.getCacheTree(true);
85 assertNotNull(tree);
86 dc.clear();
87 assertNull(dc.getCacheTree(false));
88 assertNotSame(tree, dc.getCacheTree(true));
89 }
90
91 @Test
92 public void testSingleSubtree() throws Exception {
93 final DirCache dc = db.readDirCache();
94
95 final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
96 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
97 for (int i = 0; i < paths.length; i++) {
98 ents[i] = new DirCacheEntry(paths[i]);
99 ents[i].setFileMode(FileMode.REGULAR_FILE);
100 }
101 final int aFirst = 1;
102 final int aLast = 3;
103
104 final DirCacheBuilder b = dc.builder();
105 for (int i = 0; i < ents.length; i++)
106 b.add(ents[i]);
107 b.finish();
108
109 assertNull(dc.getCacheTree(false));
110 final DirCacheTree root = dc.getCacheTree(true);
111 assertNotNull(root);
112 assertSame(root, dc.getCacheTree(true));
113 assertEquals("", root.getNameString());
114 assertEquals("", root.getPathString());
115 assertEquals(1, root.getChildCount());
116 assertEquals(dc.getEntryCount(), root.getEntrySpan());
117 assertFalse(root.isValid());
118
119 final DirCacheTree aTree = root.getChild(0);
120 assertNotNull(aTree);
121 assertSame(aTree, root.getChild(0));
122 assertEquals("a", aTree.getNameString());
123 assertEquals("a/", aTree.getPathString());
124 assertEquals(0, aTree.getChildCount());
125 assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
126 assertFalse(aTree.isValid());
127 }
128
129 @Test
130 public void testTwoLevelSubtree() throws Exception {
131 final DirCache dc = db.readDirCache();
132
133 final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
134 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
135 for (int i = 0; i < paths.length; i++) {
136 ents[i] = new DirCacheEntry(paths[i]);
137 ents[i].setFileMode(FileMode.REGULAR_FILE);
138 }
139 final int aFirst = 1;
140 final int aLast = 4;
141 final int acFirst = 2;
142 final int acLast = 3;
143
144 final DirCacheBuilder b = dc.builder();
145 for (int i = 0; i < ents.length; i++)
146 b.add(ents[i]);
147 b.finish();
148
149 assertNull(dc.getCacheTree(false));
150 final DirCacheTree root = dc.getCacheTree(true);
151 assertNotNull(root);
152 assertSame(root, dc.getCacheTree(true));
153 assertEquals("", root.getNameString());
154 assertEquals("", root.getPathString());
155 assertEquals(1, root.getChildCount());
156 assertEquals(dc.getEntryCount(), root.getEntrySpan());
157 assertFalse(root.isValid());
158
159 final DirCacheTree aTree = root.getChild(0);
160 assertNotNull(aTree);
161 assertSame(aTree, root.getChild(0));
162 assertEquals("a", aTree.getNameString());
163 assertEquals("a/", aTree.getPathString());
164 assertEquals(1, aTree.getChildCount());
165 assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
166 assertFalse(aTree.isValid());
167
168 final DirCacheTree acTree = aTree.getChild(0);
169 assertNotNull(acTree);
170 assertSame(acTree, aTree.getChild(0));
171 assertEquals("c", acTree.getNameString());
172 assertEquals("a/c/", acTree.getPathString());
173 assertEquals(0, acTree.getChildCount());
174 assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
175 assertFalse(acTree.isValid());
176 }
177
178
179
180
181
182
183
184
185
186
187 @Test
188 public void testWriteReadTree() throws CorruptObjectException, IOException {
189 final DirCache dc = db.lockDirCache();
190
191 final String A = String.format("a%2000s", "a");
192 final String B = String.format("b%2000s", "b");
193 final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
194 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
195 for (int i = 0; i < paths.length; i++) {
196 ents[i] = new DirCacheEntry(paths[i]);
197 ents[i].setFileMode(FileMode.REGULAR_FILE);
198 }
199
200 final DirCacheBuilder b = dc.builder();
201 for (int i = 0; i < ents.length; i++)
202 b.add(ents[i]);
203
204 b.commit();
205 DirCache read = db.readDirCache();
206
207 assertEquals(paths.length, read.getEntryCount());
208 assertEquals(1, read.getCacheTree(true).getChildCount());
209 }
210 }