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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 java.io.File;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.eclipse.jgit.lib.FileMode;
  51. import org.eclipse.jgit.lib.RepositoryTestCase;
  52. import org.junit.Test;
  53. public class DirCacheBasicTest extends RepositoryTestCase {
  54. @Test
  55. public void testReadMissing_RealIndex() throws Exception {
  56. final File idx = new File(db.getDirectory(), "index");
  57. assertFalse(idx.exists());
  58. final DirCache dc = db.readDirCache();
  59. assertNotNull(dc);
  60. assertEquals(0, dc.getEntryCount());
  61. }
  62. @Test
  63. public void testReadMissing_TempIndex() throws Exception {
  64. final File idx = new File(db.getDirectory(), "tmp_index");
  65. assertFalse(idx.exists());
  66. final DirCache dc = DirCache.read(idx, db.getFS());
  67. assertNotNull(dc);
  68. assertEquals(0, dc.getEntryCount());
  69. }
  70. @Test
  71. public void testLockMissing_RealIndex() throws Exception {
  72. final File idx = new File(db.getDirectory(), "index");
  73. final File lck = new File(db.getDirectory(), "index.lock");
  74. assertFalse(idx.exists());
  75. assertFalse(lck.exists());
  76. final DirCache dc = db.lockDirCache();
  77. assertNotNull(dc);
  78. assertFalse(idx.exists());
  79. assertTrue(lck.exists());
  80. assertEquals(0, dc.getEntryCount());
  81. dc.unlock();
  82. assertFalse(idx.exists());
  83. assertFalse(lck.exists());
  84. }
  85. @Test
  86. public void testLockMissing_TempIndex() throws Exception {
  87. final File idx = new File(db.getDirectory(), "tmp_index");
  88. final File lck = new File(db.getDirectory(), "tmp_index.lock");
  89. assertFalse(idx.exists());
  90. assertFalse(lck.exists());
  91. final DirCache dc = DirCache.lock(idx, db.getFS());
  92. assertNotNull(dc);
  93. assertFalse(idx.exists());
  94. assertTrue(lck.exists());
  95. assertEquals(0, dc.getEntryCount());
  96. dc.unlock();
  97. assertFalse(idx.exists());
  98. assertFalse(lck.exists());
  99. }
  100. @Test
  101. public void testWriteEmptyUnlock_RealIndex() throws Exception {
  102. final File idx = new File(db.getDirectory(), "index");
  103. final File lck = new File(db.getDirectory(), "index.lock");
  104. assertFalse(idx.exists());
  105. assertFalse(lck.exists());
  106. final DirCache dc = db.lockDirCache();
  107. assertEquals(0, lck.length());
  108. dc.write();
  109. assertEquals(12 + 20, lck.length());
  110. dc.unlock();
  111. assertFalse(idx.exists());
  112. assertFalse(lck.exists());
  113. }
  114. @Test
  115. public void testWriteEmptyCommit_RealIndex() throws Exception {
  116. final File idx = new File(db.getDirectory(), "index");
  117. final File lck = new File(db.getDirectory(), "index.lock");
  118. assertFalse(idx.exists());
  119. assertFalse(lck.exists());
  120. final DirCache dc = db.lockDirCache();
  121. assertEquals(0, lck.length());
  122. dc.write();
  123. assertEquals(12 + 20, lck.length());
  124. assertTrue(dc.commit());
  125. assertTrue(idx.exists());
  126. assertFalse(lck.exists());
  127. assertEquals(12 + 20, idx.length());
  128. }
  129. @Test
  130. public void testWriteEmptyReadEmpty_RealIndex() throws Exception {
  131. final File idx = new File(db.getDirectory(), "index");
  132. final File lck = new File(db.getDirectory(), "index.lock");
  133. assertFalse(idx.exists());
  134. assertFalse(lck.exists());
  135. {
  136. final DirCache dc = db.lockDirCache();
  137. dc.write();
  138. assertTrue(dc.commit());
  139. assertTrue(idx.exists());
  140. }
  141. {
  142. final DirCache dc = db.readDirCache();
  143. assertEquals(0, dc.getEntryCount());
  144. }
  145. }
  146. @Test
  147. public void testWriteEmptyLockEmpty_RealIndex() throws Exception {
  148. final File idx = new File(db.getDirectory(), "index");
  149. final File lck = new File(db.getDirectory(), "index.lock");
  150. assertFalse(idx.exists());
  151. assertFalse(lck.exists());
  152. {
  153. final DirCache dc = db.lockDirCache();
  154. dc.write();
  155. assertTrue(dc.commit());
  156. assertTrue(idx.exists());
  157. }
  158. {
  159. final DirCache dc = db.lockDirCache();
  160. assertEquals(0, dc.getEntryCount());
  161. assertTrue(idx.exists());
  162. assertTrue(lck.exists());
  163. dc.unlock();
  164. }
  165. }
  166. @Test
  167. public void testBuildThenClear() throws Exception {
  168. final DirCache dc = db.readDirCache();
  169. final String[] paths = { "a.", "a.b", "a/b", "a0b" };
  170. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  171. for (int i = 0; i < paths.length; i++) {
  172. ents[i] = new DirCacheEntry(paths[i]);
  173. ents[i].setFileMode(FileMode.REGULAR_FILE);
  174. }
  175. final DirCacheBuilder b = dc.builder();
  176. for (int i = 0; i < ents.length; i++)
  177. b.add(ents[i]);
  178. b.finish();
  179. assertFalse(dc.hasUnmergedPaths());
  180. assertEquals(paths.length, dc.getEntryCount());
  181. dc.clear();
  182. assertEquals(0, dc.getEntryCount());
  183. assertFalse(dc.hasUnmergedPaths());
  184. }
  185. @Test
  186. public void testDetectUnmergedPaths() throws Exception {
  187. final DirCache dc = db.readDirCache();
  188. final DirCacheEntry[] ents = new DirCacheEntry[3];
  189. ents[0] = new DirCacheEntry("a", 1);
  190. ents[0].setFileMode(FileMode.REGULAR_FILE);
  191. ents[1] = new DirCacheEntry("a", 2);
  192. ents[1].setFileMode(FileMode.REGULAR_FILE);
  193. ents[2] = new DirCacheEntry("a", 3);
  194. ents[2].setFileMode(FileMode.REGULAR_FILE);
  195. final DirCacheBuilder b = dc.builder();
  196. for (int i = 0; i < ents.length; i++)
  197. b.add(ents[i]);
  198. b.finish();
  199. assertTrue(dc.hasUnmergedPaths());
  200. }
  201. @Test
  202. public void testFindOnEmpty() throws Exception {
  203. final DirCache dc = DirCache.newInCore();
  204. final byte[] path = Constants.encode("a");
  205. assertEquals(-1, dc.findEntry(path, path.length));
  206. }
  207. }