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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.dircache;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertNotNull;
  48. import static org.junit.Assert.assertNotSame;
  49. import static org.junit.Assert.assertSame;
  50. import static org.junit.Assert.assertTrue;
  51. import static org.junit.Assert.fail;
  52. import java.io.File;
  53. import java.time.Instant;
  54. import org.eclipse.jgit.events.IndexChangedEvent;
  55. import org.eclipse.jgit.events.IndexChangedListener;
  56. import org.eclipse.jgit.events.ListenerList;
  57. import org.eclipse.jgit.junit.RepositoryTestCase;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.junit.Test;
  61. public class DirCacheBuilderTest extends RepositoryTestCase {
  62. @Test
  63. public void testBuildEmpty() throws Exception {
  64. {
  65. final DirCache dc = db.lockDirCache();
  66. final DirCacheBuilder b = dc.builder();
  67. assertNotNull(b);
  68. b.finish();
  69. dc.write();
  70. assertTrue(dc.commit());
  71. }
  72. {
  73. final DirCache dc = db.readDirCache();
  74. assertEquals(0, dc.getEntryCount());
  75. }
  76. }
  77. @Test
  78. public void testBuildRejectsUnsetFileMode() throws Exception {
  79. final DirCache dc = DirCache.newInCore();
  80. final DirCacheBuilder b = dc.builder();
  81. assertNotNull(b);
  82. final DirCacheEntry e = new DirCacheEntry("a");
  83. assertEquals(0, e.getRawMode());
  84. try {
  85. b.add(e);
  86. fail("did not reject unset file mode");
  87. } catch (IllegalArgumentException err) {
  88. assertEquals("FileMode not set for path a", err.getMessage());
  89. }
  90. }
  91. @Test
  92. public void testBuildOneFile_FinishWriteCommit() throws Exception {
  93. final String path = "a-file-path";
  94. final FileMode mode = FileMode.REGULAR_FILE;
  95. final Instant lastModified = Instant.ofEpochMilli(1218123387057L);
  96. final int length = 1342;
  97. final DirCacheEntry entOrig;
  98. {
  99. final DirCache dc = db.lockDirCache();
  100. final DirCacheBuilder b = dc.builder();
  101. assertNotNull(b);
  102. entOrig = new DirCacheEntry(path);
  103. entOrig.setFileMode(mode);
  104. entOrig.setLastModified(lastModified);
  105. entOrig.setLength(length);
  106. assertNotSame(path, entOrig.getPathString());
  107. assertEquals(path, entOrig.getPathString());
  108. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  109. assertEquals(mode.getBits(), entOrig.getRawMode());
  110. assertEquals(0, entOrig.getStage());
  111. assertEquals(lastModified, entOrig.getLastModifiedInstant());
  112. assertEquals(length, entOrig.getLength());
  113. assertFalse(entOrig.isAssumeValid());
  114. b.add(entOrig);
  115. b.finish();
  116. assertEquals(1, dc.getEntryCount());
  117. assertSame(entOrig, dc.getEntry(0));
  118. dc.write();
  119. assertTrue(dc.commit());
  120. }
  121. {
  122. final DirCache dc = db.readDirCache();
  123. assertEquals(1, dc.getEntryCount());
  124. final DirCacheEntry entRead = dc.getEntry(0);
  125. assertNotSame(entOrig, entRead);
  126. assertEquals(path, entRead.getPathString());
  127. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  128. assertEquals(mode.getBits(), entOrig.getRawMode());
  129. assertEquals(0, entOrig.getStage());
  130. assertEquals(lastModified, entOrig.getLastModifiedInstant());
  131. assertEquals(length, entOrig.getLength());
  132. assertFalse(entOrig.isAssumeValid());
  133. }
  134. }
  135. @Test
  136. public void testBuildOneFile_Commit() throws Exception {
  137. final String path = "a-file-path";
  138. final FileMode mode = FileMode.REGULAR_FILE;
  139. final Instant lastModified = Instant.ofEpochMilli(1218123387057L);
  140. final int length = 1342;
  141. final DirCacheEntry entOrig;
  142. {
  143. final DirCache dc = db.lockDirCache();
  144. final DirCacheBuilder b = dc.builder();
  145. assertNotNull(b);
  146. entOrig = new DirCacheEntry(path);
  147. entOrig.setFileMode(mode);
  148. entOrig.setLastModified(lastModified);
  149. entOrig.setLength(length);
  150. assertNotSame(path, entOrig.getPathString());
  151. assertEquals(path, entOrig.getPathString());
  152. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  153. assertEquals(mode.getBits(), entOrig.getRawMode());
  154. assertEquals(0, entOrig.getStage());
  155. assertEquals(lastModified, entOrig.getLastModifiedInstant());
  156. assertEquals(length, entOrig.getLength());
  157. assertFalse(entOrig.isAssumeValid());
  158. b.add(entOrig);
  159. assertTrue(b.commit());
  160. assertEquals(1, dc.getEntryCount());
  161. assertSame(entOrig, dc.getEntry(0));
  162. assertFalse(new File(db.getDirectory(), "index.lock").exists());
  163. }
  164. {
  165. final DirCache dc = db.readDirCache();
  166. assertEquals(1, dc.getEntryCount());
  167. final DirCacheEntry entRead = dc.getEntry(0);
  168. assertNotSame(entOrig, entRead);
  169. assertEquals(path, entRead.getPathString());
  170. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  171. assertEquals(mode.getBits(), entOrig.getRawMode());
  172. assertEquals(0, entOrig.getStage());
  173. assertEquals(lastModified, entOrig.getLastModifiedInstant());
  174. assertEquals(length, entOrig.getLength());
  175. assertFalse(entOrig.isAssumeValid());
  176. }
  177. }
  178. @Test
  179. public void testBuildOneFile_Commit_IndexChangedEvent()
  180. throws Exception {
  181. final class ReceivedEventMarkerException extends RuntimeException {
  182. private static final long serialVersionUID = 1L;
  183. // empty
  184. }
  185. final String path = "a-file-path";
  186. final FileMode mode = FileMode.REGULAR_FILE;
  187. // "old" date in 2008
  188. final Instant lastModified = Instant.ofEpochMilli(1218123387057L);
  189. final int length = 1342;
  190. DirCacheEntry entOrig;
  191. boolean receivedEvent = false;
  192. DirCache dc = db.lockDirCache();
  193. IndexChangedListener listener = new IndexChangedListener() {
  194. @Override
  195. public void onIndexChanged(IndexChangedEvent event) {
  196. throw new ReceivedEventMarkerException();
  197. }
  198. };
  199. ListenerList l = db.getListenerList();
  200. l.addIndexChangedListener(listener);
  201. DirCacheBuilder b = dc.builder();
  202. entOrig = new DirCacheEntry(path);
  203. entOrig.setFileMode(mode);
  204. entOrig.setLastModified(lastModified);
  205. entOrig.setLength(length);
  206. b.add(entOrig);
  207. try {
  208. b.commit();
  209. } catch (ReceivedEventMarkerException e) {
  210. receivedEvent = true;
  211. }
  212. if (!receivedEvent)
  213. fail("did not receive IndexChangedEvent");
  214. // do the same again, as this doesn't change index compared to first
  215. // round we should get no event this time
  216. dc = db.lockDirCache();
  217. listener = new IndexChangedListener() {
  218. @Override
  219. public void onIndexChanged(IndexChangedEvent event) {
  220. throw new ReceivedEventMarkerException();
  221. }
  222. };
  223. l = db.getListenerList();
  224. l.addIndexChangedListener(listener);
  225. b = dc.builder();
  226. entOrig = new DirCacheEntry(path);
  227. entOrig.setFileMode(mode);
  228. entOrig.setLastModified(lastModified);
  229. entOrig.setLength(length);
  230. b.add(entOrig);
  231. try {
  232. b.commit();
  233. } catch (ReceivedEventMarkerException e) {
  234. fail("unexpected IndexChangedEvent");
  235. }
  236. }
  237. @Test
  238. public void testFindSingleFile() throws Exception {
  239. final String path = "a-file-path";
  240. final DirCache dc = db.readDirCache();
  241. final DirCacheBuilder b = dc.builder();
  242. assertNotNull(b);
  243. final DirCacheEntry entOrig = new DirCacheEntry(path);
  244. entOrig.setFileMode(FileMode.REGULAR_FILE);
  245. assertNotSame(path, entOrig.getPathString());
  246. assertEquals(path, entOrig.getPathString());
  247. b.add(entOrig);
  248. b.finish();
  249. assertEquals(1, dc.getEntryCount());
  250. assertSame(entOrig, dc.getEntry(0));
  251. assertEquals(0, dc.findEntry(path));
  252. assertEquals(-1, dc.findEntry("@@-before"));
  253. assertEquals(0, real(dc.findEntry("@@-before")));
  254. assertEquals(-2, dc.findEntry("a-zoo"));
  255. assertEquals(1, real(dc.findEntry("a-zoo")));
  256. assertSame(entOrig, dc.getEntry(path));
  257. }
  258. @Test
  259. public void testAdd_InGitSortOrder() throws Exception {
  260. final DirCache dc = db.readDirCache();
  261. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  262. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  263. for (int i = 0; i < paths.length; i++) {
  264. ents[i] = new DirCacheEntry(paths[i]);
  265. ents[i].setFileMode(FileMode.REGULAR_FILE);
  266. }
  267. final DirCacheBuilder b = dc.builder();
  268. for (int i = 0; i < ents.length; i++)
  269. b.add(ents[i]);
  270. b.finish();
  271. assertEquals(paths.length, dc.getEntryCount());
  272. for (int i = 0; i < paths.length; i++) {
  273. assertSame(ents[i], dc.getEntry(i));
  274. assertEquals(paths[i], dc.getEntry(i).getPathString());
  275. assertEquals(i, dc.findEntry(paths[i]));
  276. assertSame(ents[i], dc.getEntry(paths[i]));
  277. }
  278. }
  279. @Test
  280. public void testAdd_ReverseGitSortOrder() throws Exception {
  281. final DirCache dc = db.readDirCache();
  282. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  283. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  284. for (int i = 0; i < paths.length; i++) {
  285. ents[i] = new DirCacheEntry(paths[i]);
  286. ents[i].setFileMode(FileMode.REGULAR_FILE);
  287. }
  288. final DirCacheBuilder b = dc.builder();
  289. for (int i = ents.length - 1; i >= 0; i--)
  290. b.add(ents[i]);
  291. b.finish();
  292. assertEquals(paths.length, dc.getEntryCount());
  293. for (int i = 0; i < paths.length; i++) {
  294. assertSame(ents[i], dc.getEntry(i));
  295. assertEquals(paths[i], dc.getEntry(i).getPathString());
  296. assertEquals(i, dc.findEntry(paths[i]));
  297. assertSame(ents[i], dc.getEntry(paths[i]));
  298. }
  299. }
  300. @Test
  301. public void testBuilderClear() throws Exception {
  302. final DirCache dc = db.readDirCache();
  303. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  304. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  305. for (int i = 0; i < paths.length; i++) {
  306. ents[i] = new DirCacheEntry(paths[i]);
  307. ents[i].setFileMode(FileMode.REGULAR_FILE);
  308. }
  309. {
  310. final DirCacheBuilder b = dc.builder();
  311. for (int i = 0; i < ents.length; i++)
  312. b.add(ents[i]);
  313. b.finish();
  314. }
  315. assertEquals(paths.length, dc.getEntryCount());
  316. {
  317. final DirCacheBuilder b = dc.builder();
  318. b.finish();
  319. }
  320. assertEquals(0, dc.getEntryCount());
  321. }
  322. private static int real(int eIdx) {
  323. if (eIdx < 0)
  324. eIdx = -(eIdx + 1);
  325. return eIdx;
  326. }
  327. }