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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. } catch (IllegalArgumentException err) {
  86. assertEquals("FileMode not set for path a", err.getMessage());
  87. }
  88. }
  89. @Test
  90. public void testBuildOneFile_FinishWriteCommit() throws Exception {
  91. final String path = "a-file-path";
  92. final FileMode mode = FileMode.REGULAR_FILE;
  93. final long lastModified = 1218123387057L;
  94. final int length = 1342;
  95. final DirCacheEntry entOrig;
  96. {
  97. final DirCache dc = db.lockDirCache();
  98. final DirCacheBuilder b = dc.builder();
  99. assertNotNull(b);
  100. entOrig = new DirCacheEntry(path);
  101. entOrig.setFileMode(mode);
  102. entOrig.setLastModified(lastModified);
  103. entOrig.setLength(length);
  104. assertNotSame(path, entOrig.getPathString());
  105. assertEquals(path, entOrig.getPathString());
  106. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  107. assertEquals(mode.getBits(), entOrig.getRawMode());
  108. assertEquals(0, entOrig.getStage());
  109. assertEquals(lastModified, entOrig.getLastModified());
  110. assertEquals(length, entOrig.getLength());
  111. assertFalse(entOrig.isAssumeValid());
  112. b.add(entOrig);
  113. b.finish();
  114. assertEquals(1, dc.getEntryCount());
  115. assertSame(entOrig, dc.getEntry(0));
  116. dc.write();
  117. assertTrue(dc.commit());
  118. }
  119. {
  120. final DirCache dc = db.readDirCache();
  121. assertEquals(1, dc.getEntryCount());
  122. final DirCacheEntry entRead = dc.getEntry(0);
  123. assertNotSame(entOrig, entRead);
  124. assertEquals(path, entRead.getPathString());
  125. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  126. assertEquals(mode.getBits(), entOrig.getRawMode());
  127. assertEquals(0, entOrig.getStage());
  128. assertEquals(lastModified, entOrig.getLastModified());
  129. assertEquals(length, entOrig.getLength());
  130. assertFalse(entOrig.isAssumeValid());
  131. }
  132. }
  133. @Test
  134. public void testBuildOneFile_Commit() throws Exception {
  135. final String path = "a-file-path";
  136. final FileMode mode = FileMode.REGULAR_FILE;
  137. final long lastModified = 1218123387057L;
  138. final int length = 1342;
  139. final DirCacheEntry entOrig;
  140. {
  141. final DirCache dc = db.lockDirCache();
  142. final DirCacheBuilder b = dc.builder();
  143. assertNotNull(b);
  144. entOrig = new DirCacheEntry(path);
  145. entOrig.setFileMode(mode);
  146. entOrig.setLastModified(lastModified);
  147. entOrig.setLength(length);
  148. assertNotSame(path, entOrig.getPathString());
  149. assertEquals(path, entOrig.getPathString());
  150. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  151. assertEquals(mode.getBits(), entOrig.getRawMode());
  152. assertEquals(0, entOrig.getStage());
  153. assertEquals(lastModified, entOrig.getLastModified());
  154. assertEquals(length, entOrig.getLength());
  155. assertFalse(entOrig.isAssumeValid());
  156. b.add(entOrig);
  157. assertTrue(b.commit());
  158. assertEquals(1, dc.getEntryCount());
  159. assertSame(entOrig, dc.getEntry(0));
  160. assertFalse(new File(db.getDirectory(), "index.lock").exists());
  161. }
  162. {
  163. final DirCache dc = db.readDirCache();
  164. assertEquals(1, dc.getEntryCount());
  165. final DirCacheEntry entRead = dc.getEntry(0);
  166. assertNotSame(entOrig, entRead);
  167. assertEquals(path, entRead.getPathString());
  168. assertEquals(ObjectId.zeroId(), entOrig.getObjectId());
  169. assertEquals(mode.getBits(), entOrig.getRawMode());
  170. assertEquals(0, entOrig.getStage());
  171. assertEquals(lastModified, entOrig.getLastModified());
  172. assertEquals(length, entOrig.getLength());
  173. assertFalse(entOrig.isAssumeValid());
  174. }
  175. }
  176. @Test
  177. public void testBuildOneFile_Commit_IndexChangedEvent()
  178. throws Exception {
  179. final class ReceivedEventMarkerException extends RuntimeException {
  180. private static final long serialVersionUID = 1L;
  181. // empty
  182. }
  183. final String path = "a-file-path";
  184. final FileMode mode = FileMode.REGULAR_FILE;
  185. // "old" date in 2008
  186. final long lastModified = 1218123387057L;
  187. final int length = 1342;
  188. DirCacheEntry entOrig;
  189. boolean receivedEvent = false;
  190. DirCache dc = db.lockDirCache();
  191. IndexChangedListener listener = new IndexChangedListener() {
  192. public void onIndexChanged(IndexChangedEvent event) {
  193. throw new ReceivedEventMarkerException();
  194. }
  195. };
  196. ListenerList l = db.getListenerList();
  197. l.addIndexChangedListener(listener);
  198. DirCacheBuilder b = dc.builder();
  199. entOrig = new DirCacheEntry(path);
  200. entOrig.setFileMode(mode);
  201. entOrig.setLastModified(lastModified);
  202. entOrig.setLength(length);
  203. b.add(entOrig);
  204. try {
  205. b.commit();
  206. } catch (ReceivedEventMarkerException e) {
  207. receivedEvent = true;
  208. }
  209. if (!receivedEvent)
  210. fail("did not receive IndexChangedEvent");
  211. // do the same again, as this doesn't change index compared to first
  212. // round we should get no event this time
  213. dc = db.lockDirCache();
  214. listener = new IndexChangedListener() {
  215. public void onIndexChanged(IndexChangedEvent event) {
  216. throw new ReceivedEventMarkerException();
  217. }
  218. };
  219. l = db.getListenerList();
  220. l.addIndexChangedListener(listener);
  221. b = dc.builder();
  222. entOrig = new DirCacheEntry(path);
  223. entOrig.setFileMode(mode);
  224. entOrig.setLastModified(lastModified);
  225. entOrig.setLength(length);
  226. b.add(entOrig);
  227. try {
  228. b.commit();
  229. } catch (ReceivedEventMarkerException e) {
  230. fail("unexpected IndexChangedEvent");
  231. }
  232. }
  233. @Test
  234. public void testFindSingleFile() throws Exception {
  235. final String path = "a-file-path";
  236. final DirCache dc = db.readDirCache();
  237. final DirCacheBuilder b = dc.builder();
  238. assertNotNull(b);
  239. final DirCacheEntry entOrig = new DirCacheEntry(path);
  240. entOrig.setFileMode(FileMode.REGULAR_FILE);
  241. assertNotSame(path, entOrig.getPathString());
  242. assertEquals(path, entOrig.getPathString());
  243. b.add(entOrig);
  244. b.finish();
  245. assertEquals(1, dc.getEntryCount());
  246. assertSame(entOrig, dc.getEntry(0));
  247. assertEquals(0, dc.findEntry(path));
  248. assertEquals(-1, dc.findEntry("@@-before"));
  249. assertEquals(0, real(dc.findEntry("@@-before")));
  250. assertEquals(-2, dc.findEntry("a-zoo"));
  251. assertEquals(1, real(dc.findEntry("a-zoo")));
  252. assertSame(entOrig, dc.getEntry(path));
  253. }
  254. @Test
  255. public void testAdd_InGitSortOrder() throws Exception {
  256. final DirCache dc = db.readDirCache();
  257. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  258. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  259. for (int i = 0; i < paths.length; i++) {
  260. ents[i] = new DirCacheEntry(paths[i]);
  261. ents[i].setFileMode(FileMode.REGULAR_FILE);
  262. }
  263. final DirCacheBuilder b = dc.builder();
  264. for (int i = 0; i < ents.length; i++)
  265. b.add(ents[i]);
  266. b.finish();
  267. assertEquals(paths.length, dc.getEntryCount());
  268. for (int i = 0; i < paths.length; i++) {
  269. assertSame(ents[i], dc.getEntry(i));
  270. assertEquals(paths[i], dc.getEntry(i).getPathString());
  271. assertEquals(i, dc.findEntry(paths[i]));
  272. assertSame(ents[i], dc.getEntry(paths[i]));
  273. }
  274. }
  275. @Test
  276. public void testAdd_ReverseGitSortOrder() throws Exception {
  277. final DirCache dc = db.readDirCache();
  278. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  279. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  280. for (int i = 0; i < paths.length; i++) {
  281. ents[i] = new DirCacheEntry(paths[i]);
  282. ents[i].setFileMode(FileMode.REGULAR_FILE);
  283. }
  284. final DirCacheBuilder b = dc.builder();
  285. for (int i = ents.length - 1; i >= 0; i--)
  286. b.add(ents[i]);
  287. b.finish();
  288. assertEquals(paths.length, dc.getEntryCount());
  289. for (int i = 0; i < paths.length; i++) {
  290. assertSame(ents[i], dc.getEntry(i));
  291. assertEquals(paths[i], dc.getEntry(i).getPathString());
  292. assertEquals(i, dc.findEntry(paths[i]));
  293. assertSame(ents[i], dc.getEntry(paths[i]));
  294. }
  295. }
  296. @Test
  297. public void testBuilderClear() throws Exception {
  298. final DirCache dc = db.readDirCache();
  299. final String[] paths = { "a-", "a.b", "a/b", "a0b" };
  300. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  301. for (int i = 0; i < paths.length; i++) {
  302. ents[i] = new DirCacheEntry(paths[i]);
  303. ents[i].setFileMode(FileMode.REGULAR_FILE);
  304. }
  305. {
  306. final DirCacheBuilder b = dc.builder();
  307. for (int i = 0; i < ents.length; i++)
  308. b.add(ents[i]);
  309. b.finish();
  310. }
  311. assertEquals(paths.length, dc.getEntryCount());
  312. {
  313. final DirCacheBuilder b = dc.builder();
  314. b.finish();
  315. }
  316. assertEquals(0, dc.getEntryCount());
  317. }
  318. private static int real(int eIdx) {
  319. if (eIdx < 0)
  320. eIdx = -(eIdx + 1);
  321. return eIdx;
  322. }
  323. }