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

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