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.

DirCacheIteratorTest.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertSame;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.util.Collections;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.RepositoryTestCase;
  54. import org.eclipse.jgit.treewalk.TreeWalk;
  55. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  56. import org.eclipse.jgit.util.FS;
  57. import org.junit.Test;
  58. public class DirCacheIteratorTest extends RepositoryTestCase {
  59. @Test
  60. public void testEmptyTree_NoTreeWalk() throws Exception {
  61. final DirCache dc = DirCache.newInCore();
  62. assertEquals(0, dc.getEntryCount());
  63. final DirCacheIterator i = new DirCacheIterator(dc);
  64. assertTrue(i.eof());
  65. }
  66. @Test
  67. public void testEmptyTree_WithTreeWalk() throws Exception {
  68. final DirCache dc = DirCache.newInCore();
  69. assertEquals(0, dc.getEntryCount());
  70. final TreeWalk tw = new TreeWalk(db);
  71. tw.addTree(new DirCacheIterator(dc));
  72. assertFalse(tw.next());
  73. }
  74. @Test
  75. public void testNoSubtree_NoTreeWalk() throws Exception {
  76. final DirCache dc = DirCache.newInCore();
  77. final String[] paths = { "a.", "a0b" };
  78. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  79. for (int i = 0; i < paths.length; i++) {
  80. ents[i] = new DirCacheEntry(paths[i]);
  81. ents[i].setFileMode(FileMode.REGULAR_FILE);
  82. }
  83. final DirCacheBuilder b = dc.builder();
  84. for (int i = 0; i < ents.length; i++)
  85. b.add(ents[i]);
  86. b.finish();
  87. final DirCacheIterator i = new DirCacheIterator(dc);
  88. int pathIdx = 0;
  89. for (; !i.eof(); i.next(1)) {
  90. assertEquals(pathIdx, i.ptr);
  91. assertSame(ents[pathIdx], i.getDirCacheEntry());
  92. pathIdx++;
  93. }
  94. assertEquals(paths.length, pathIdx);
  95. }
  96. @Test
  97. public void testNoSubtree_WithTreeWalk() throws Exception {
  98. final DirCache dc = DirCache.newInCore();
  99. final String[] paths = { "a.", "a0b" };
  100. final FileMode[] modes = { FileMode.EXECUTABLE_FILE, FileMode.GITLINK };
  101. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  102. for (int i = 0; i < paths.length; i++) {
  103. ents[i] = new DirCacheEntry(paths[i]);
  104. ents[i].setFileMode(modes[i]);
  105. }
  106. final DirCacheBuilder b = dc.builder();
  107. for (int i = 0; i < ents.length; i++)
  108. b.add(ents[i]);
  109. b.finish();
  110. final DirCacheIterator i = new DirCacheIterator(dc);
  111. final TreeWalk tw = new TreeWalk(db);
  112. tw.addTree(i);
  113. int pathIdx = 0;
  114. while (tw.next()) {
  115. assertSame(i, tw.getTree(0, DirCacheIterator.class));
  116. assertEquals(pathIdx, i.ptr);
  117. assertSame(ents[pathIdx], i.getDirCacheEntry());
  118. assertEquals(paths[pathIdx], tw.getPathString());
  119. assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0));
  120. assertSame(modes[pathIdx], tw.getFileMode(0));
  121. pathIdx++;
  122. }
  123. assertEquals(paths.length, pathIdx);
  124. }
  125. @Test
  126. public void testSingleSubtree_NoRecursion() throws Exception {
  127. final DirCache dc = DirCache.newInCore();
  128. final String[] paths = { "a.", "a/b", "a/c", "a/d", "a0b" };
  129. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  130. for (int i = 0; i < paths.length; i++) {
  131. ents[i] = new DirCacheEntry(paths[i]);
  132. ents[i].setFileMode(FileMode.REGULAR_FILE);
  133. }
  134. final DirCacheBuilder b = dc.builder();
  135. for (int i = 0; i < ents.length; i++)
  136. b.add(ents[i]);
  137. b.finish();
  138. final String[] expPaths = { "a.", "a", "a0b" };
  139. final FileMode[] expModes = { FileMode.REGULAR_FILE, FileMode.TREE,
  140. FileMode.REGULAR_FILE };
  141. final int expPos[] = { 0, -1, 4 };
  142. final DirCacheIterator i = new DirCacheIterator(dc);
  143. final TreeWalk tw = new TreeWalk(db);
  144. tw.addTree(i);
  145. tw.setRecursive(false);
  146. int pathIdx = 0;
  147. while (tw.next()) {
  148. assertSame(i, tw.getTree(0, DirCacheIterator.class));
  149. assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0));
  150. assertSame(expModes[pathIdx], tw.getFileMode(0));
  151. assertEquals(expPaths[pathIdx], tw.getPathString());
  152. if (expPos[pathIdx] >= 0) {
  153. assertEquals(expPos[pathIdx], i.ptr);
  154. assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry());
  155. } else {
  156. assertSame(FileMode.TREE, tw.getFileMode(0));
  157. }
  158. pathIdx++;
  159. }
  160. assertEquals(expPaths.length, pathIdx);
  161. }
  162. @Test
  163. public void testSingleSubtree_Recursive() throws Exception {
  164. final DirCache dc = DirCache.newInCore();
  165. final FileMode mode = FileMode.REGULAR_FILE;
  166. final String[] paths = { "a.", "a/b", "a/c", "a/d", "a0b" };
  167. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  168. for (int i = 0; i < paths.length; i++) {
  169. ents[i] = new DirCacheEntry(paths[i]);
  170. ents[i].setFileMode(mode);
  171. }
  172. final DirCacheBuilder b = dc.builder();
  173. for (int i = 0; i < ents.length; i++)
  174. b.add(ents[i]);
  175. b.finish();
  176. final DirCacheIterator i = new DirCacheIterator(dc);
  177. final TreeWalk tw = new TreeWalk(db);
  178. tw.addTree(i);
  179. tw.setRecursive(true);
  180. int pathIdx = 0;
  181. while (tw.next()) {
  182. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  183. assertNotNull(c);
  184. assertEquals(pathIdx, c.ptr);
  185. assertSame(ents[pathIdx], c.getDirCacheEntry());
  186. assertEquals(paths[pathIdx], tw.getPathString());
  187. assertEquals(mode.getBits(), tw.getRawMode(0));
  188. assertSame(mode, tw.getFileMode(0));
  189. pathIdx++;
  190. }
  191. assertEquals(paths.length, pathIdx);
  192. }
  193. @Test
  194. public void testTwoLevelSubtree_Recursive() throws Exception {
  195. final DirCache dc = DirCache.newInCore();
  196. final FileMode mode = FileMode.REGULAR_FILE;
  197. final String[] paths = { "a.", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  198. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  199. for (int i = 0; i < paths.length; i++) {
  200. ents[i] = new DirCacheEntry(paths[i]);
  201. ents[i].setFileMode(mode);
  202. }
  203. final DirCacheBuilder b = dc.builder();
  204. for (int i = 0; i < ents.length; i++)
  205. b.add(ents[i]);
  206. b.finish();
  207. final TreeWalk tw = new TreeWalk(db);
  208. tw.addTree(new DirCacheIterator(dc));
  209. tw.setRecursive(true);
  210. int pathIdx = 0;
  211. while (tw.next()) {
  212. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  213. assertNotNull(c);
  214. assertEquals(pathIdx, c.ptr);
  215. assertSame(ents[pathIdx], c.getDirCacheEntry());
  216. assertEquals(paths[pathIdx], tw.getPathString());
  217. assertEquals(mode.getBits(), tw.getRawMode(0));
  218. assertSame(mode, tw.getFileMode(0));
  219. pathIdx++;
  220. }
  221. assertEquals(paths.length, pathIdx);
  222. }
  223. @Test
  224. public void testTwoLevelSubtree_FilterPath() throws Exception {
  225. final DirCache dc = DirCache.newInCore();
  226. final FileMode mode = FileMode.REGULAR_FILE;
  227. final String[] paths = { "a.", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  228. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  229. for (int i = 0; i < paths.length; i++) {
  230. ents[i] = new DirCacheEntry(paths[i]);
  231. ents[i].setFileMode(mode);
  232. }
  233. final DirCacheBuilder b = dc.builder();
  234. for (int i = 0; i < ents.length; i++)
  235. b.add(ents[i]);
  236. b.finish();
  237. final TreeWalk tw = new TreeWalk(db);
  238. for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) {
  239. tw.reset();
  240. tw.addTree(new DirCacheIterator(dc));
  241. tw.setFilter(PathFilterGroup.createFromStrings(Collections
  242. .singleton(paths[victimIdx])));
  243. tw.setRecursive(tw.getFilter().shouldBeRecursive());
  244. assertTrue(tw.next());
  245. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  246. assertNotNull(c);
  247. assertEquals(victimIdx, c.ptr);
  248. assertSame(ents[victimIdx], c.getDirCacheEntry());
  249. assertEquals(paths[victimIdx], tw.getPathString());
  250. assertEquals(mode.getBits(), tw.getRawMode(0));
  251. assertSame(mode, tw.getFileMode(0));
  252. assertFalse(tw.next());
  253. }
  254. }
  255. @Test
  256. public void testRemovedSubtree() throws Exception {
  257. final File path = JGitTestUtil
  258. .getTestResourceFile("dircache.testRemovedSubtree");
  259. final DirCache dc = DirCache.read(path, FS.DETECTED);
  260. assertEquals(2, dc.getEntryCount());
  261. final TreeWalk tw = new TreeWalk(db);
  262. tw.setRecursive(true);
  263. tw.addTree(new DirCacheIterator(dc));
  264. assertTrue(tw.next());
  265. assertEquals("a/a", tw.getPathString());
  266. assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
  267. assertTrue(tw.next());
  268. assertEquals("q", tw.getPathString());
  269. assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
  270. assertFalse(tw.next());
  271. }
  272. }