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 (DirCacheEntry ent : ents) {
106 b.add(ent);
107 }
108 b.finish();
109
110 assertNull(dc.getCacheTree(false));
111 final DirCacheTree root = dc.getCacheTree(true);
112 assertNotNull(root);
113 assertSame(root, dc.getCacheTree(true));
114 assertEquals("", root.getNameString());
115 assertEquals("", root.getPathString());
116 assertEquals(1, root.getChildCount());
117 assertEquals(dc.getEntryCount(), root.getEntrySpan());
118 assertFalse(root.isValid());
119
120 final DirCacheTree aTree = root.getChild(0);
121 assertNotNull(aTree);
122 assertSame(aTree, root.getChild(0));
123 assertEquals("a", aTree.getNameString());
124 assertEquals("a/", aTree.getPathString());
125 assertEquals(0, aTree.getChildCount());
126 assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
127 assertFalse(aTree.isValid());
128 }
129
130 @Test
131 public void testTwoLevelSubtree() throws Exception {
132 final DirCache dc = db.readDirCache();
133
134 final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
135 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
136 for (int i = 0; i < paths.length; i++) {
137 ents[i] = new DirCacheEntry(paths[i]);
138 ents[i].setFileMode(FileMode.REGULAR_FILE);
139 }
140 final int aFirst = 1;
141 final int aLast = 4;
142 final int acFirst = 2;
143 final int acLast = 3;
144
145 final DirCacheBuilder b = dc.builder();
146 for (DirCacheEntry ent : ents) {
147 b.add(ent);
148 }
149 b.finish();
150
151 assertNull(dc.getCacheTree(false));
152 final DirCacheTree root = dc.getCacheTree(true);
153 assertNotNull(root);
154 assertSame(root, dc.getCacheTree(true));
155 assertEquals("", root.getNameString());
156 assertEquals("", root.getPathString());
157 assertEquals(1, root.getChildCount());
158 assertEquals(dc.getEntryCount(), root.getEntrySpan());
159 assertFalse(root.isValid());
160
161 final DirCacheTree aTree = root.getChild(0);
162 assertNotNull(aTree);
163 assertSame(aTree, root.getChild(0));
164 assertEquals("a", aTree.getNameString());
165 assertEquals("a/", aTree.getPathString());
166 assertEquals(1, aTree.getChildCount());
167 assertEquals(aLast - aFirst + 1, aTree.getEntrySpan());
168 assertFalse(aTree.isValid());
169
170 final DirCacheTree acTree = aTree.getChild(0);
171 assertNotNull(acTree);
172 assertSame(acTree, aTree.getChild(0));
173 assertEquals("c", acTree.getNameString());
174 assertEquals("a/c/", acTree.getPathString());
175 assertEquals(0, acTree.getChildCount());
176 assertEquals(acLast - acFirst + 1, acTree.getEntrySpan());
177 assertFalse(acTree.isValid());
178 }
179
180
181
182
183
184
185
186
187
188
189 @Test
190 public void testWriteReadTree() throws CorruptObjectException, IOException {
191 final DirCache dc = db.lockDirCache();
192
193 final String A = String.format("a%2000s", "a");
194 final String B = String.format("b%2000s", "b");
195 final String[] paths = { A + "-", A + "-" + B, A + "/" + B, A + "0" + B };
196 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
197 for (int i = 0; i < paths.length; i++) {
198 ents[i] = new DirCacheEntry(paths[i]);
199 ents[i].setFileMode(FileMode.REGULAR_FILE);
200 }
201
202 final DirCacheBuilder b = dc.builder();
203 for (DirCacheEntry ent : ents) {
204 b.add(ent);
205 }
206
207 b.commit();
208 DirCache read = db.readDirCache();
209
210 assertEquals(paths.length, read.getEntryCount());
211 assertEquals(1, read.getCacheTree(true).getChildCount());
212 }
213 }