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.assertTrue;
50 import static org.junit.Assert.fail;
51
52 import java.io.File;
53 import java.text.MessageFormat;
54
55 import org.eclipse.jgit.errors.CorruptObjectException;
56 import org.eclipse.jgit.internal.JGitText;
57 import org.eclipse.jgit.junit.MockSystemReader;
58 import org.eclipse.jgit.junit.RepositoryTestCase;
59 import org.eclipse.jgit.lib.Constants;
60 import org.eclipse.jgit.lib.FileMode;
61 import org.eclipse.jgit.lib.ObjectInserter;
62 import org.eclipse.jgit.util.SystemReader;
63 import org.junit.Test;
64
65 public class DirCacheBasicTest extends RepositoryTestCase {
66 @Test
67 public void testReadMissing_RealIndex() throws Exception {
68 final File idx = new File(db.getDirectory(), "index");
69 assertFalse(idx.exists());
70
71 final DirCache dc = db.readDirCache();
72 assertNotNull(dc);
73 assertEquals(0, dc.getEntryCount());
74 }
75
76 @Test
77 public void testReadMissing_TempIndex() throws Exception {
78 final File idx = new File(db.getDirectory(), "tmp_index");
79 assertFalse(idx.exists());
80
81 final DirCache dc = DirCache.read(idx, db.getFS());
82 assertNotNull(dc);
83 assertEquals(0, dc.getEntryCount());
84 }
85
86 @Test
87 public void testLockMissing_RealIndex() throws Exception {
88 final File idx = new File(db.getDirectory(), "index");
89 final File lck = new File(db.getDirectory(), "index.lock");
90 assertFalse(idx.exists());
91 assertFalse(lck.exists());
92
93 final DirCache dc = db.lockDirCache();
94 assertNotNull(dc);
95 assertFalse(idx.exists());
96 assertTrue(lck.exists());
97 assertEquals(0, dc.getEntryCount());
98
99 dc.unlock();
100 assertFalse(idx.exists());
101 assertFalse(lck.exists());
102 }
103
104 @Test
105 public void testLockMissing_TempIndex() throws Exception {
106 final File idx = new File(db.getDirectory(), "tmp_index");
107 final File lck = new File(db.getDirectory(), "tmp_index.lock");
108 assertFalse(idx.exists());
109 assertFalse(lck.exists());
110
111 final DirCache dc = DirCache.lock(idx, db.getFS());
112 assertNotNull(dc);
113 assertFalse(idx.exists());
114 assertTrue(lck.exists());
115 assertEquals(0, dc.getEntryCount());
116
117 dc.unlock();
118 assertFalse(idx.exists());
119 assertFalse(lck.exists());
120 }
121
122 @Test
123 public void testWriteEmptyUnlock_RealIndex() throws Exception {
124 final File idx = new File(db.getDirectory(), "index");
125 final File lck = new File(db.getDirectory(), "index.lock");
126 assertFalse(idx.exists());
127 assertFalse(lck.exists());
128
129 final DirCache dc = db.lockDirCache();
130 assertEquals(0, lck.length());
131 dc.write();
132 assertEquals(12 + 20, lck.length());
133
134 dc.unlock();
135 assertFalse(idx.exists());
136 assertFalse(lck.exists());
137 }
138
139 @Test
140 public void testWriteEmptyCommit_RealIndex() throws Exception {
141 final File idx = new File(db.getDirectory(), "index");
142 final File lck = new File(db.getDirectory(), "index.lock");
143 assertFalse(idx.exists());
144 assertFalse(lck.exists());
145
146 final DirCache dc = db.lockDirCache();
147 assertEquals(0, lck.length());
148 dc.write();
149 assertEquals(12 + 20, lck.length());
150
151 assertTrue(dc.commit());
152 assertTrue(idx.exists());
153 assertFalse(lck.exists());
154 assertEquals(12 + 20, idx.length());
155 }
156
157 @Test
158 public void testWriteEmptyReadEmpty_RealIndex() throws Exception {
159 final File idx = new File(db.getDirectory(), "index");
160 final File lck = new File(db.getDirectory(), "index.lock");
161 assertFalse(idx.exists());
162 assertFalse(lck.exists());
163 {
164 final DirCache dc = db.lockDirCache();
165 dc.write();
166 assertTrue(dc.commit());
167 assertTrue(idx.exists());
168 }
169 {
170 final DirCache dc = db.readDirCache();
171 assertEquals(0, dc.getEntryCount());
172 }
173 }
174
175 @Test
176 public void testWriteEmptyLockEmpty_RealIndex() throws Exception {
177 final File idx = new File(db.getDirectory(), "index");
178 final File lck = new File(db.getDirectory(), "index.lock");
179 assertFalse(idx.exists());
180 assertFalse(lck.exists());
181 {
182 final DirCache dc = db.lockDirCache();
183 dc.write();
184 assertTrue(dc.commit());
185 assertTrue(idx.exists());
186 }
187 {
188 final DirCache dc = db.lockDirCache();
189 assertEquals(0, dc.getEntryCount());
190 assertTrue(idx.exists());
191 assertTrue(lck.exists());
192 dc.unlock();
193 }
194 }
195
196 @Test
197 public void testBuildThenClear() throws Exception {
198 final DirCache dc = db.readDirCache();
199
200 final String[] paths = { "a-", "a.b", "a/b", "a0b" };
201 final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
202 for (int i = 0; i < paths.length; i++) {
203 ents[i] = new DirCacheEntry(paths[i]);
204 ents[i].setFileMode(FileMode.REGULAR_FILE);
205 }
206
207 final DirCacheBuilder b = dc.builder();
208 for (DirCacheEntry ent : ents) {
209 b.add(ent);
210 }
211 b.finish();
212 assertFalse(dc.hasUnmergedPaths());
213
214 assertEquals(paths.length, dc.getEntryCount());
215 dc.clear();
216 assertEquals(0, dc.getEntryCount());
217 assertFalse(dc.hasUnmergedPaths());
218 }
219
220 @Test
221 public void testDetectUnmergedPaths() throws Exception {
222 final DirCache dc = db.readDirCache();
223 final DirCacheEntry[] ents = new DirCacheEntry[3];
224
225 ents[0] = new DirCacheEntry("a", 1);
226 ents[0].setFileMode(FileMode.REGULAR_FILE);
227 ents[1] = new DirCacheEntry("a", 2);
228 ents[1].setFileMode(FileMode.REGULAR_FILE);
229 ents[2] = new DirCacheEntry("a", 3);
230 ents[2].setFileMode(FileMode.REGULAR_FILE);
231
232 final DirCacheBuilder b = dc.builder();
233 for (DirCacheEntry ent : ents) {
234 b.add(ent);
235 }
236 b.finish();
237 assertTrue(dc.hasUnmergedPaths());
238 }
239
240 @Test
241 public void testFindOnEmpty() throws Exception {
242 final DirCache dc = DirCache.newInCore();
243 final byte[] path = Constants.encode("a");
244 assertEquals(-1, dc.findEntry(path, path.length));
245 }
246
247 @Test
248 public void testRejectInvalidWindowsPaths() throws Exception {
249 SystemReader.setInstance(new MockSystemReader() {
250 {
251 setUnix();
252 }
253 });
254
255 String path = "src/con.txt";
256 DirCache dc = db.lockDirCache();
257 DirCacheBuilder b = dc.builder();
258 DirCacheEntry e = new DirCacheEntry(path);
259 e.setFileMode(FileMode.REGULAR_FILE);
260 try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
261 e.setObjectId(formatter.idFor(
262 Constants.OBJ_BLOB,
263 Constants.encode(path)));
264 }
265 b.add(e);
266 b.commit();
267 db.readDirCache();
268
269 SystemReader.setInstance(new MockSystemReader() {
270 {
271 setWindows();
272 }
273 });
274
275 try {
276 db.readDirCache();
277 fail("should have rejected " + path);
278 } catch (CorruptObjectException err) {
279 assertEquals(MessageFormat.format(JGitText.get().invalidPath, path),
280 err.getMessage());
281 assertNotNull(err.getCause());
282 assertEquals("invalid name 'CON'", err.getCause().getMessage());
283 }
284 }
285 }