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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.dircache;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import java.text.MessageFormat;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.junit.MockSystemReader;
  54. import org.eclipse.jgit.junit.RepositoryTestCase;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.FileMode;
  57. import org.eclipse.jgit.lib.ObjectInserter;
  58. import org.eclipse.jgit.util.SystemReader;
  59. import org.junit.Test;
  60. public class DirCacheBasicTest extends RepositoryTestCase {
  61. @Test
  62. public void testReadMissing_RealIndex() throws Exception {
  63. final File idx = new File(db.getDirectory(), "index");
  64. assertFalse(idx.exists());
  65. final DirCache dc = db.readDirCache();
  66. assertNotNull(dc);
  67. assertEquals(0, dc.getEntryCount());
  68. }
  69. @Test
  70. public void testReadMissing_TempIndex() throws Exception {
  71. final File idx = new File(db.getDirectory(), "tmp_index");
  72. assertFalse(idx.exists());
  73. final DirCache dc = DirCache.read(idx, db.getFS());
  74. assertNotNull(dc);
  75. assertEquals(0, dc.getEntryCount());
  76. }
  77. @Test
  78. public void testLockMissing_RealIndex() throws Exception {
  79. final File idx = new File(db.getDirectory(), "index");
  80. final File lck = new File(db.getDirectory(), "index.lock");
  81. assertFalse(idx.exists());
  82. assertFalse(lck.exists());
  83. final DirCache dc = db.lockDirCache();
  84. assertNotNull(dc);
  85. assertFalse(idx.exists());
  86. assertTrue(lck.exists());
  87. assertEquals(0, dc.getEntryCount());
  88. dc.unlock();
  89. assertFalse(idx.exists());
  90. assertFalse(lck.exists());
  91. }
  92. @Test
  93. public void testLockMissing_TempIndex() throws Exception {
  94. final File idx = new File(db.getDirectory(), "tmp_index");
  95. final File lck = new File(db.getDirectory(), "tmp_index.lock");
  96. assertFalse(idx.exists());
  97. assertFalse(lck.exists());
  98. final DirCache dc = DirCache.lock(idx, db.getFS());
  99. assertNotNull(dc);
  100. assertFalse(idx.exists());
  101. assertTrue(lck.exists());
  102. assertEquals(0, dc.getEntryCount());
  103. dc.unlock();
  104. assertFalse(idx.exists());
  105. assertFalse(lck.exists());
  106. }
  107. @Test
  108. public void testWriteEmptyUnlock_RealIndex() throws Exception {
  109. final File idx = new File(db.getDirectory(), "index");
  110. final File lck = new File(db.getDirectory(), "index.lock");
  111. assertFalse(idx.exists());
  112. assertFalse(lck.exists());
  113. final DirCache dc = db.lockDirCache();
  114. assertEquals(0, lck.length());
  115. dc.write();
  116. assertEquals(12 + 20, lck.length());
  117. dc.unlock();
  118. assertFalse(idx.exists());
  119. assertFalse(lck.exists());
  120. }
  121. @Test
  122. public void testWriteEmptyCommit_RealIndex() throws Exception {
  123. final File idx = new File(db.getDirectory(), "index");
  124. final File lck = new File(db.getDirectory(), "index.lock");
  125. assertFalse(idx.exists());
  126. assertFalse(lck.exists());
  127. final DirCache dc = db.lockDirCache();
  128. assertEquals(0, lck.length());
  129. dc.write();
  130. assertEquals(12 + 20, lck.length());
  131. assertTrue(dc.commit());
  132. assertTrue(idx.exists());
  133. assertFalse(lck.exists());
  134. assertEquals(12 + 20, idx.length());
  135. }
  136. @Test
  137. public void testWriteEmptyReadEmpty_RealIndex() throws Exception {
  138. final File idx = new File(db.getDirectory(), "index");
  139. final File lck = new File(db.getDirectory(), "index.lock");
  140. assertFalse(idx.exists());
  141. assertFalse(lck.exists());
  142. {
  143. final DirCache dc = db.lockDirCache();
  144. dc.write();
  145. assertTrue(dc.commit());
  146. assertTrue(idx.exists());
  147. }
  148. {
  149. final DirCache dc = db.readDirCache();
  150. assertEquals(0, dc.getEntryCount());
  151. }
  152. }
  153. @Test
  154. public void testWriteEmptyLockEmpty_RealIndex() throws Exception {
  155. final File idx = new File(db.getDirectory(), "index");
  156. final File lck = new File(db.getDirectory(), "index.lock");
  157. assertFalse(idx.exists());
  158. assertFalse(lck.exists());
  159. {
  160. final DirCache dc = db.lockDirCache();
  161. dc.write();
  162. assertTrue(dc.commit());
  163. assertTrue(idx.exists());
  164. }
  165. {
  166. final DirCache dc = db.lockDirCache();
  167. assertEquals(0, dc.getEntryCount());
  168. assertTrue(idx.exists());
  169. assertTrue(lck.exists());
  170. dc.unlock();
  171. }
  172. }
  173. @Test
  174. public void testBuildThenClear() throws Exception {
  175. final DirCache dc = db.readDirCache();
  176. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  177. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  178. for (int i = 0; i < paths.length; i++) {
  179. ents[i] = new DirCacheEntry(paths[i]);
  180. ents[i].setFileMode(FileMode.REGULAR_FILE);
  181. }
  182. final DirCacheBuilder b = dc.builder();
  183. for (DirCacheEntry ent : ents) {
  184. b.add(ent);
  185. }
  186. b.finish();
  187. assertFalse(dc.hasUnmergedPaths());
  188. assertEquals(paths.length, dc.getEntryCount());
  189. dc.clear();
  190. assertEquals(0, dc.getEntryCount());
  191. assertFalse(dc.hasUnmergedPaths());
  192. }
  193. @Test
  194. public void testDetectUnmergedPaths() throws Exception {
  195. final DirCache dc = db.readDirCache();
  196. final DirCacheEntry[] ents = new DirCacheEntry[3];
  197. ents[0] = new DirCacheEntry("a", 1);
  198. ents[0].setFileMode(FileMode.REGULAR_FILE);
  199. ents[1] = new DirCacheEntry("a", 2);
  200. ents[1].setFileMode(FileMode.REGULAR_FILE);
  201. ents[2] = new DirCacheEntry("a", 3);
  202. ents[2].setFileMode(FileMode.REGULAR_FILE);
  203. final DirCacheBuilder b = dc.builder();
  204. for (DirCacheEntry ent : ents) {
  205. b.add(ent);
  206. }
  207. b.finish();
  208. assertTrue(dc.hasUnmergedPaths());
  209. }
  210. @Test
  211. public void testFindOnEmpty() throws Exception {
  212. final DirCache dc = DirCache.newInCore();
  213. final byte[] path = Constants.encode("a");
  214. assertEquals(-1, dc.findEntry(path, path.length));
  215. }
  216. @Test
  217. public void testRejectInvalidWindowsPaths() throws Exception {
  218. SystemReader.setInstance(new MockSystemReader() {
  219. {
  220. setUnix();
  221. }
  222. });
  223. String path = "src/con.txt";
  224. DirCache dc = db.lockDirCache();
  225. DirCacheBuilder b = dc.builder();
  226. DirCacheEntry e = new DirCacheEntry(path);
  227. e.setFileMode(FileMode.REGULAR_FILE);
  228. try (ObjectInserter.Formatter formatter = new ObjectInserter.Formatter()) {
  229. e.setObjectId(formatter.idFor(
  230. Constants.OBJ_BLOB,
  231. Constants.encode(path)));
  232. }
  233. b.add(e);
  234. b.commit();
  235. db.readDirCache();
  236. SystemReader.setInstance(new MockSystemReader() {
  237. {
  238. setWindows();
  239. }
  240. });
  241. try {
  242. db.readDirCache();
  243. fail("should have rejected " + path);
  244. } catch (CorruptObjectException err) {
  245. assertEquals(MessageFormat.format(JGitText.get().invalidPath, path),
  246. err.getMessage());
  247. assertNotNull(err.getCause());
  248. assertEquals("invalid name 'CON'", err.getCause().getMessage());
  249. }
  250. }
  251. }