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

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