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.

DirCacheBuilderTest.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.FileMode;
  46. import org.eclipse.jgit.lib.ObjectId;
  47. import org.eclipse.jgit.lib.RepositoryTestCase;
  48. public class DirCacheBuilderTest extends RepositoryTestCase {
  49. public void testBuildEmpty() throws Exception {
  50. {
  51. final DirCache dc = DirCache.lock(db);
  52. final DirCacheBuilder b = dc.builder();
  53. assertNotNull(b);
  54. b.finish();
  55. dc.write();
  56. assertTrue(dc.commit());
  57. }
  58. {
  59. final DirCache dc = DirCache.read(db);
  60. assertEquals(0, dc.getEntryCount());
  61. }
  62. }
  63. public void testBuildRejectsUnsetFileMode() throws Exception {
  64. final DirCache dc = DirCache.newInCore();
  65. final DirCacheBuilder b = dc.builder();
  66. assertNotNull(b);
  67. final DirCacheEntry e = new DirCacheEntry("a");
  68. assertEquals(0, e.getRawMode());
  69. try {
  70. b.add(e);
  71. } catch (IllegalArgumentException err) {
  72. assertEquals("FileMode not set for path a", err.getMessage());
  73. }
  74. }
  75. public void testBuildOneFile_FinishWriteCommit() throws Exception {
  76. final String path = "a-file-path";
  77. final FileMode mode = FileMode.REGULAR_FILE;
  78. final long lastModified = 1218123387057L;
  79. final int length = 1342;
  80. final DirCacheEntry entOrig;
  81. {
  82. final DirCache dc = DirCache.lock(db);
  83. final DirCacheBuilder b = dc.builder();
  84. assertNotNull(b);
  85. entOrig = new DirCacheEntry(path);
  86. entOrig.setFileMode(mode);
  87. entOrig.setLastModified(lastModified);
  88. entOrig.setLength(length);
  89. assertNotSame(path, entOrig.getPathString());
  90. assertEquals(path, entOrig.getPathString());
  91. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  92. assertEquals(mode.getBits(), entOrig.getRawMode());
  93. assertEquals(0, entOrig.getStage());
  94. assertEquals(lastModified, entOrig.getLastModified());
  95. assertEquals(length, entOrig.getLength());
  96. assertFalse(entOrig.isAssumeValid());
  97. b.add(entOrig);
  98. b.finish();
  99. assertEquals(1, dc.getEntryCount());
  100. assertSame(entOrig, dc.getEntry(0));
  101. dc.write();
  102. assertTrue(dc.commit());
  103. }
  104. {
  105. final DirCache dc = DirCache.read(db);
  106. assertEquals(1, dc.getEntryCount());
  107. final DirCacheEntry entRead = dc.getEntry(0);
  108. assertNotSame(entOrig, entRead);
  109. assertEquals(path, entRead.getPathString());
  110. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  111. assertEquals(mode.getBits(), entOrig.getRawMode());
  112. assertEquals(0, entOrig.getStage());
  113. assertEquals(lastModified, entOrig.getLastModified());
  114. assertEquals(length, entOrig.getLength());
  115. assertFalse(entOrig.isAssumeValid());
  116. }
  117. }
  118. public void testBuildOneFile_Commit() throws Exception {
  119. final String path = "a-file-path";
  120. final FileMode mode = FileMode.REGULAR_FILE;
  121. final long lastModified = 1218123387057L;
  122. final int length = 1342;
  123. final DirCacheEntry entOrig;
  124. {
  125. final DirCache dc = DirCache.lock(db);
  126. final DirCacheBuilder b = dc.builder();
  127. assertNotNull(b);
  128. entOrig = new DirCacheEntry(path);
  129. entOrig.setFileMode(mode);
  130. entOrig.setLastModified(lastModified);
  131. entOrig.setLength(length);
  132. assertNotSame(path, entOrig.getPathString());
  133. assertEquals(path, entOrig.getPathString());
  134. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  135. assertEquals(mode.getBits(), entOrig.getRawMode());
  136. assertEquals(0, entOrig.getStage());
  137. assertEquals(lastModified, entOrig.getLastModified());
  138. assertEquals(length, entOrig.getLength());
  139. assertFalse(entOrig.isAssumeValid());
  140. b.add(entOrig);
  141. assertTrue(b.commit());
  142. assertEquals(1, dc.getEntryCount());
  143. assertSame(entOrig, dc.getEntry(0));
  144. assertFalse(new File(db.getDirectory(), "index.lock").exists());
  145. }
  146. {
  147. final DirCache dc = DirCache.read(db);
  148. assertEquals(1, dc.getEntryCount());
  149. final DirCacheEntry entRead = dc.getEntry(0);
  150. assertNotSame(entOrig, entRead);
  151. assertEquals(path, entRead.getPathString());
  152. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  153. assertEquals(mode.getBits(), entOrig.getRawMode());
  154. assertEquals(0, entOrig.getStage());
  155. assertEquals(lastModified, entOrig.getLastModified());
  156. assertEquals(length, entOrig.getLength());
  157. assertFalse(entOrig.isAssumeValid());
  158. }
  159. }
  160. public void testFindSingleFile() throws Exception {
  161. final String path = "a-file-path";
  162. final DirCache dc = DirCache.read(db);
  163. final DirCacheBuilder b = dc.builder();
  164. assertNotNull(b);
  165. final DirCacheEntry entOrig = new DirCacheEntry(path);
  166. entOrig.setFileMode(FileMode.REGULAR_FILE);
  167. assertNotSame(path, entOrig.getPathString());
  168. assertEquals(path, entOrig.getPathString());
  169. b.add(entOrig);
  170. b.finish();
  171. assertEquals(1, dc.getEntryCount());
  172. assertSame(entOrig, dc.getEntry(0));
  173. assertEquals(0, dc.findEntry(path));
  174. assertEquals(-1, dc.findEntry("@@-before"));
  175. assertEquals(0, real(dc.findEntry("@@-before")));
  176. assertEquals(-2, dc.findEntry("a-zoo"));
  177. assertEquals(1, real(dc.findEntry("a-zoo")));
  178. assertSame(entOrig, dc.getEntry(path));
  179. }
  180. public void testAdd_InGitSortOrder() throws Exception {
  181. final DirCache dc = DirCache.read(db);
  182. final String[] paths = { "a.", "a.b", "a/b", "a0b" };
  183. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  184. for (int i = 0; i < paths.length; i++) {
  185. ents[i] = new DirCacheEntry(paths[i]);
  186. ents[i].setFileMode(FileMode.REGULAR_FILE);
  187. }
  188. final DirCacheBuilder b = dc.builder();
  189. for (int i = 0; i < ents.length; i++)
  190. b.add(ents[i]);
  191. b.finish();
  192. assertEquals(paths.length, dc.getEntryCount());
  193. for (int i = 0; i < paths.length; i++) {
  194. assertSame(ents[i], dc.getEntry(i));
  195. assertEquals(paths[i], dc.getEntry(i).getPathString());
  196. assertEquals(i, dc.findEntry(paths[i]));
  197. assertSame(ents[i], dc.getEntry(paths[i]));
  198. }
  199. }
  200. public void testAdd_ReverseGitSortOrder() throws Exception {
  201. final DirCache dc = DirCache.read(db);
  202. final String[] paths = { "a.", "a.b", "a/b", "a0b" };
  203. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  204. for (int i = 0; i < paths.length; i++) {
  205. ents[i] = new DirCacheEntry(paths[i]);
  206. ents[i].setFileMode(FileMode.REGULAR_FILE);
  207. }
  208. final DirCacheBuilder b = dc.builder();
  209. for (int i = ents.length - 1; i >= 0; i--)
  210. b.add(ents[i]);
  211. b.finish();
  212. assertEquals(paths.length, dc.getEntryCount());
  213. for (int i = 0; i < paths.length; i++) {
  214. assertSame(ents[i], dc.getEntry(i));
  215. assertEquals(paths[i], dc.getEntry(i).getPathString());
  216. assertEquals(i, dc.findEntry(paths[i]));
  217. assertSame(ents[i], dc.getEntry(paths[i]));
  218. }
  219. }
  220. public void testBuilderClear() throws Exception {
  221. final DirCache dc = DirCache.read(db);
  222. final String[] paths = { "a.", "a.b", "a/b", "a0b" };
  223. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  224. for (int i = 0; i < paths.length; i++) {
  225. ents[i] = new DirCacheEntry(paths[i]);
  226. ents[i].setFileMode(FileMode.REGULAR_FILE);
  227. }
  228. {
  229. final DirCacheBuilder b = dc.builder();
  230. for (int i = 0; i < ents.length; i++)
  231. b.add(ents[i]);
  232. b.finish();
  233. }
  234. assertEquals(paths.length, dc.getEntryCount());
  235. {
  236. final DirCacheBuilder b = dc.builder();
  237. b.finish();
  238. }
  239. assertEquals(0, dc.getEntryCount());
  240. }
  241. private static int real(int eIdx) {
  242. if (eIdx < 0)
  243. eIdx = -(eIdx + 1);
  244. return eIdx;
  245. }
  246. }