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