You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DirCacheBasicTest.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.dircache;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.File;
  17. import java.text.MessageFormat;
  18. import org.eclipse.jgit.errors.CorruptObjectException;
  19. import org.eclipse.jgit.internal.JGitText;
  20. import org.eclipse.jgit.junit.MockSystemReader;
  21. import org.eclipse.jgit.junit.RepositoryTestCase;
  22. import org.eclipse.jgit.lib.Constants;
  23. import org.eclipse.jgit.lib.FileMode;
  24. import org.eclipse.jgit.lib.ObjectInserter;
  25. import org.eclipse.jgit.util.SystemReader;
  26. import org.junit.Test;
  27. public class DirCacheBasicTest extends RepositoryTestCase {
  28. @Test
  29. public void testReadMissing_RealIndex() throws Exception {
  30. final File idx = new File(db.getDirectory(), "index");
  31. assertFalse(idx.exists());
  32. final DirCache dc = db.readDirCache();
  33. assertNotNull(dc);
  34. assertEquals(0, dc.getEntryCount());
  35. }
  36. @Test
  37. public void testReadMissing_TempIndex() throws Exception {
  38. final File idx = new File(db.getDirectory(), "tmp_index");
  39. assertFalse(idx.exists());
  40. final DirCache dc = DirCache.read(idx, db.getFS());
  41. assertNotNull(dc);
  42. assertEquals(0, dc.getEntryCount());
  43. }
  44. @Test
  45. public void testLockMissing_RealIndex() throws Exception {
  46. final File idx = new File(db.getDirectory(), "index");
  47. final File lck = new File(db.getDirectory(), "index.lock");
  48. assertFalse(idx.exists());
  49. assertFalse(lck.exists());
  50. final DirCache dc = db.lockDirCache();
  51. assertNotNull(dc);
  52. assertFalse(idx.exists());
  53. assertTrue(lck.exists());
  54. assertEquals(0, dc.getEntryCount());
  55. dc.unlock();
  56. assertFalse(idx.exists());
  57. assertFalse(lck.exists());
  58. }
  59. @Test
  60. public void testLockMissing_TempIndex() throws Exception {
  61. final File idx = new File(db.getDirectory(), "tmp_index");
  62. final File lck = new File(db.getDirectory(), "tmp_index.lock");
  63. assertFalse(idx.exists());
  64. assertFalse(lck.exists());
  65. final DirCache dc = DirCache.lock(idx, db.getFS());
  66. assertNotNull(dc);
  67. assertFalse(idx.exists());
  68. assertTrue(lck.exists());
  69. assertEquals(0, dc.getEntryCount());
  70. dc.unlock();
  71. assertFalse(idx.exists());
  72. assertFalse(lck.exists());
  73. }
  74. @Test
  75. public void testWriteEmptyUnlock_RealIndex() throws Exception {
  76. final File idx = new File(db.getDirectory(), "index");
  77. final File lck = new File(db.getDirectory(), "index.lock");
  78. assertFalse(idx.exists());
  79. assertFalse(lck.exists());
  80. final DirCache dc = db.lockDirCache();
  81. assertEquals(0, lck.length());
  82. dc.write();
  83. assertEquals(12 + 20, lck.length());
  84. dc.unlock();
  85. assertFalse(idx.exists());
  86. assertFalse(lck.exists());
  87. }
  88. @Test
  89. public void testWriteEmptyCommit_RealIndex() throws Exception {
  90. final File idx = new File(db.getDirectory(), "index");
  91. final File lck = new File(db.getDirectory(), "index.lock");
  92. assertFalse(idx.exists());
  93. assertFalse(lck.exists());
  94. final DirCache dc = db.lockDirCache();
  95. assertEquals(0, lck.length());
  96. dc.write();
  97. assertEquals(12 + 20, lck.length());
  98. assertTrue(dc.commit());
  99. assertTrue(idx.exists());
  100. assertFalse(lck.exists());
  101. assertEquals(12 + 20, idx.length());
  102. }
  103. @Test
  104. public void testWriteEmptyReadEmpty_RealIndex() throws Exception {
  105. final File idx = new File(db.getDirectory(), "index");
  106. final File lck = new File(db.getDirectory(), "index.lock");
  107. assertFalse(idx.exists());
  108. assertFalse(lck.exists());
  109. {
  110. final DirCache dc = db.lockDirCache();
  111. dc.write();
  112. assertTrue(dc.commit());
  113. assertTrue(idx.exists());
  114. }
  115. {
  116. final DirCache dc = db.readDirCache();
  117. assertEquals(0, dc.getEntryCount());
  118. }
  119. }
  120. @Test
  121. public void testWriteEmptyLockEmpty_RealIndex() throws Exception {
  122. final File idx = new File(db.getDirectory(), "index");
  123. final File lck = new File(db.getDirectory(), "index.lock");
  124. assertFalse(idx.exists());
  125. assertFalse(lck.exists());
  126. {
  127. final DirCache dc = db.lockDirCache();
  128. dc.write();
  129. assertTrue(dc.commit());
  130. assertTrue(idx.exists());
  131. }
  132. {
  133. final DirCache dc = db.lockDirCache();
  134. assertEquals(0, dc.getEntryCount());
  135. assertTrue(idx.exists());
  136. assertTrue(lck.exists());
  137. dc.unlock();
  138. }
  139. }
  140. @Test
  141. public void testBuildThenClear() throws Exception {
  142. final DirCache dc = db.readDirCache();
  143. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  144. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  145. for (int i = 0; i < paths.length; i++) {
  146. ents[i] = new DirCacheEntry(paths[i]);
  147. ents[i].setFileMode(FileMode.REGULAR_FILE);
  148. }
  149. final DirCacheBuilder b = dc.builder();
  150. for (DirCacheEntry ent : ents) {
  151. b.add(ent);
  152. }
  153. b.finish();
  154. assertFalse(dc.hasUnmergedPaths());
  155. assertEquals(paths.length, dc.getEntryCount());
  156. dc.clear();
  157. assertEquals(0, dc.getEntryCount());
  158. assertFalse(dc.hasUnmergedPaths());
  159. }
  160. @Test
  161. public void testDetectUnmergedPaths() throws Exception {
  162. final DirCache dc = db.readDirCache();
  163. final DirCacheEntry[] ents = new DirCacheEntry[3];
  164. ents[0] = new DirCacheEntry("a", 1);
  165. ents[0].setFileMode(FileMode.REGULAR_FILE);
  166. ents[1] = new DirCacheEntry("a", 2);
  167. ents[1].setFileMode(FileMode.REGULAR_FILE);
  168. ents[2] = new DirCacheEntry("a", 3);
  169. ents[2].setFileMode(FileMode.REGULAR_FILE);
  170. final DirCacheBuilder b = dc.builder();
  171. for (DirCacheEntry ent : ents) {
  172. b.add(ent);
  173. }
  174. b.finish();
  175. assertTrue(dc.hasUnmergedPaths());
  176. }
  177. @Test
  178. public void testFindOnEmpty() throws Exception {
  179. final DirCache dc = DirCache.newInCore();
  180. final byte[] path = Constants.encode("a");
  181. assertEquals(-1, dc.findEntry(path, path.length));
  182. }
  183. @Test
  184. public void testRejectInvalidWindowsPaths() throws Exception {
  185. SystemReader.setInstance(new MockSystemReader() {
  186. {
  187. setUnix();
  188. }
  189. });
  190. String path = "src/con.txt";
  191. DirCache dc = db.lockDirCache();
  192. DirCacheBuilder b = dc.builder();
  193. DirCacheEntry e = new DirCacheEntry(path);
  194. e.setFileMode(FileMode.REGULAR_FILE);
  195. try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
  196. e.setObjectId(formatter.idFor(
  197. Constants.OBJ_BLOB,
  198. Constants.encode(path)));
  199. }
  200. b.add(e);
  201. b.commit();
  202. db.readDirCache();
  203. SystemReader.setInstance(new MockSystemReader() {
  204. {
  205. setWindows();
  206. }
  207. });
  208. try {
  209. db.readDirCache();
  210. fail("should have rejected " + path);
  211. } catch (CorruptObjectException err) {
  212. assertEquals(MessageFormat.format(JGitText.get().invalidPath, path),
  213. err.getMessage());
  214. assertNotNull(err.getCause());
  215. assertEquals("invalid name 'CON'", err.getCause().getMessage());
  216. }
  217. }
  218. }