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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 org.eclipse.jgit.events.IndexChangedEvent;
  54. import org.eclipse.jgit.events.IndexChangedListener;
  55. import org.eclipse.jgit.events.ListenerList;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.lib.FileMode;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.junit.Test;
  60. public class DirCacheBuilderTest extends RepositoryTestCase {
  61. @Test
  62. public void testBuildEmpty() throws Exception {
  63. {
  64. final DirCache dc = db.lockDirCache();
  65. final DirCacheBuilder b = dc.builder();
  66. assertNotNull(b);
  67. b.finish();
  68. dc.write();
  69. assertTrue(dc.commit());
  70. }
  71. {
  72. final DirCache dc = db.readDirCache();
  73. assertEquals(0, dc.getEntryCount());
  74. }
  75. }
  76. @Test
  77. public void testBuildRejectsUnsetFileMode() throws Exception {
  78. final DirCache dc = DirCache.newInCore();
  79. final DirCacheBuilder b = dc.builder();
  80. assertNotNull(b);
  81. final DirCacheEntry e = new DirCacheEntry("a");
  82. assertEquals(0, e.getRawMode());
  83. try {
  84. b.add(e);
  85. fail("did not reject unset file mode");
  86. } catch (IllegalArgumentException err) {
  87. assertEquals("FileMode not set for path a", err.getMessage());
  88. }
  89. }
  90. @Test
  91. public void testBuildOneFile_FinishWriteCommit() throws Exception {
  92. final String path = "a-file-path";
  93. final FileMode mode = FileMode.REGULAR_FILE;
  94. final long lastModified = 1218123387057L;
  95. final int length = 1342;
  96. final DirCacheEntry entOrig;
  97. {
  98. final DirCache dc = db.lockDirCache();
  99. final DirCacheBuilder b = dc.builder();
  100. assertNotNull(b);
  101. entOrig = new DirCacheEntry(path);
  102. entOrig.setFileMode(mode);
  103. entOrig.setLastModified(lastModified);
  104. entOrig.setLength(length);
  105. assertNotSame(path, entOrig.getPathString());
  106. assertEquals(path, entOrig.getPathString());
  107. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  108. assertEquals(mode.getBits(), entOrig.getRawMode());
  109. assertEquals(0, entOrig.getStage());
  110. assertEquals(lastModified, entOrig.getLastModified());
  111. assertEquals(length, entOrig.getLength());
  112. assertFalse(entOrig.isAssumeValid());
  113. b.add(entOrig);
  114. b.finish();
  115. assertEquals(1, dc.getEntryCount());
  116. assertSame(entOrig, dc.getEntry(0));
  117. dc.write();
  118. assertTrue(dc.commit());
  119. }
  120. {
  121. final DirCache dc = db.readDirCache();
  122. assertEquals(1, dc.getEntryCount());
  123. final DirCacheEntry entRead = dc.getEntry(0);
  124. assertNotSame(entOrig, entRead);
  125. assertEquals(path, entRead.getPathString());
  126. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  127. assertEquals(mode.getBits(), entOrig.getRawMode());
  128. assertEquals(0, entOrig.getStage());
  129. assertEquals(lastModified, entOrig.getLastModified());
  130. assertEquals(length, entOrig.getLength());
  131. assertFalse(entOrig.isAssumeValid());
  132. }
  133. }
  134. @Test
  135. public void testBuildOneFile_Commit() throws Exception {
  136. final String path = "a-file-path";
  137. final FileMode mode = FileMode.REGULAR_FILE;
  138. final long lastModified = 1218123387057L;
  139. final int length = 1342;
  140. final DirCacheEntry entOrig;
  141. {
  142. final DirCache dc = db.lockDirCache();
  143. final DirCacheBuilder b = dc.builder();
  144. assertNotNull(b);
  145. entOrig = new DirCacheEntry(path);
  146. entOrig.setFileMode(mode);
  147. entOrig.setLastModified(lastModified);
  148. entOrig.setLength(length);
  149. assertNotSame(path, entOrig.getPathString());
  150. assertEquals(path, entOrig.getPathString());
  151. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  152. assertEquals(mode.getBits(), entOrig.getRawMode());
  153. assertEquals(0, entOrig.getStage());
  154. assertEquals(lastModified, entOrig.getLastModified());
  155. assertEquals(length, entOrig.getLength());
  156. assertFalse(entOrig.isAssumeValid());
  157. b.add(entOrig);
  158. assertTrue(b.commit());
  159. assertEquals(1, dc.getEntryCount());
  160. assertSame(entOrig, dc.getEntry(0));
  161. assertFalse(new File(db.getDirectory(), "index.lock").exists());
  162. }
  163. {
  164. final DirCache dc = db.readDirCache();
  165. assertEquals(1, dc.getEntryCount());
  166. final DirCacheEntry entRead = dc.getEntry(0);
  167. assertNotSame(entOrig, entRead);
  168. assertEquals(path, entRead.getPathString());
  169. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  170. assertEquals(mode.getBits(), entOrig.getRawMode());
  171. assertEquals(0, entOrig.getStage());
  172. assertEquals(lastModified, entOrig.getLastModified());
  173. assertEquals(length, entOrig.getLength());
  174. assertFalse(entOrig.isAssumeValid());
  175. }
  176. }
  177. @Test
  178. public void testBuildOneFile_Commit_IndexChangedEvent()
  179. throws Exception {
  180. final class ReceivedEventMarkerException extends RuntimeException {
  181. private static final long serialVersionUID = 1L;
  182. // empty
  183. }
  184. final String path = "a-file-path";
  185. final FileMode mode = FileMode.REGULAR_FILE;
  186. // "old" date in 2008
  187. final long lastModified = 1218123387057L;
  188. final int length = 1342;
  189. DirCacheEntry entOrig;
  190. boolean receivedEvent = false;
  191. DirCache dc = db.lockDirCache();
  192. IndexChangedListener listener = new IndexChangedListener() {
  193. @Override
  194. public void onIndexChanged(IndexChangedEvent event) {
  195. throw new ReceivedEventMarkerException();
  196. }
  197. };
  198. ListenerList l = db.getListenerList();
  199. l.addIndexChangedListener(listener);
  200. DirCacheBuilder b = dc.builder();
  201. entOrig = new DirCacheEntry(path);
  202. entOrig.setFileMode(mode);
  203. entOrig.setLastModified(lastModified);
  204. entOrig.setLength(length);
  205. b.add(entOrig);
  206. try {
  207. b.commit();
  208. } catch (ReceivedEventMarkerException e) {
  209. receivedEvent = true;
  210. }
  211. if (!receivedEvent)
  212. fail("did not receive IndexChangedEvent");
  213. // do the same again, as this doesn't change index compared to first
  214. // round we should get no event this time
  215. dc = db.lockDirCache();
  216. listener = new IndexChangedListener() {
  217. @Override
  218. public void onIndexChanged(IndexChangedEvent event) {
  219. throw new ReceivedEventMarkerException();
  220. }
  221. };
  222. l = db.getListenerList();
  223. l.addIndexChangedListener(listener);
  224. b = dc.builder();
  225. entOrig = new DirCacheEntry(path);
  226. entOrig.setFileMode(mode);
  227. entOrig.setLastModified(lastModified);
  228. entOrig.setLength(length);
  229. b.add(entOrig);
  230. try {
  231. b.commit();
  232. } catch (ReceivedEventMarkerException e) {
  233. fail("unexpected IndexChangedEvent");
  234. }
  235. }
  236. @Test
  237. public void testFindSingleFile() throws Exception {
  238. final String path = "a-file-path";
  239. final DirCache dc = db.readDirCache();
  240. final DirCacheBuilder b = dc.builder();
  241. assertNotNull(b);
  242. final DirCacheEntry entOrig = new DirCacheEntry(path);
  243. entOrig.setFileMode(FileMode.REGULAR_FILE);
  244. assertNotSame(path, entOrig.getPathString());
  245. assertEquals(path, entOrig.getPathString());
  246. b.add(entOrig);
  247. b.finish();
  248. assertEquals(1, dc.getEntryCount());
  249. assertSame(entOrig, dc.getEntry(0));
  250. assertEquals(0, dc.findEntry(path));
  251. assertEquals(-1, dc.findEntry("@@-before"));
  252. assertEquals(0, real(dc.findEntry("@@-before")));
  253. assertEquals(-2, dc.findEntry("a-zoo"));
  254. assertEquals(1, real(dc.findEntry("a-zoo")));
  255. assertSame(entOrig, dc.getEntry(path));
  256. }
  257. @Test
  258. public void testAdd_InGitSortOrder() throws Exception {
  259. final DirCache dc = db.readDirCache();
  260. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  261. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  262. for (int i = 0; i < paths.length; i++) {
  263. ents[i] = new DirCacheEntry(paths[i]);
  264. ents[i].setFileMode(FileMode.REGULAR_FILE);
  265. }
  266. final DirCacheBuilder b = dc.builder();
  267. for (DirCacheEntry ent : ents) {
  268. b.add(ent);
  269. }
  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 (DirCacheEntry ent : ents) {
  312. b.add(ent);
  313. }
  314. b.finish();
  315. }
  316. assertEquals(paths.length, dc.getEntryCount());
  317. {
  318. final DirCacheBuilder b = dc.builder();
  319. b.finish();
  320. }
  321. assertEquals(0, dc.getEntryCount());
  322. }
  323. private static int real(int eIdx) {
  324. if (eIdx < 0)
  325. eIdx = -(eIdx + 1);
  326. return eIdx;
  327. }
  328. }