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.

PathFilterGroupTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2013, Robin Rosenberg
  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.treewalk.filter;
  44. import static org.junit.Assert.assertArrayEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.IOException;
  49. import java.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.HashMap;
  52. import java.util.List;
  53. import java.util.Map;
  54. import java.util.Set;
  55. import org.eclipse.jgit.dircache.DirCache;
  56. import org.eclipse.jgit.dircache.DirCacheEditor;
  57. import org.eclipse.jgit.dircache.DirCacheEntry;
  58. import org.eclipse.jgit.dircache.DirCacheIterator;
  59. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  60. import org.eclipse.jgit.errors.MissingObjectException;
  61. import org.eclipse.jgit.errors.StopWalkException;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.ObjectReader;
  64. import org.eclipse.jgit.lib.Sets;
  65. import org.eclipse.jgit.treewalk.TreeWalk;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. public class PathFilterGroupTest {
  69. private TreeFilter filter;
  70. private Map<String, TreeFilter> singles;
  71. @Before
  72. public void setup() {
  73. // @formatter:off
  74. String[] paths = new String[] {
  75. "/a", // never match
  76. "/a/b", // never match
  77. "a",
  78. "b/c",
  79. "c/d/e",
  80. "c/d/f",
  81. "d/e/f/g",
  82. "d/e/f/g.x"
  83. };
  84. // @formatter:on
  85. filter = PathFilterGroup.createFromStrings(paths);
  86. singles = new HashMap<>();
  87. for (String path : paths) {
  88. singles.put(path, PathFilterGroup.createFromStrings(path));
  89. }
  90. }
  91. @Test
  92. public void testExact() throws MissingObjectException,
  93. IncorrectObjectTypeException, IOException {
  94. assertMatches(Sets.of("a"), fakeWalk("a"));
  95. assertMatches(Sets.of("b/c"), fakeWalk("b/c"));
  96. assertMatches(Sets.of("c/d/e"), fakeWalk("c/d/e"));
  97. assertMatches(Sets.of("c/d/f"), fakeWalk("c/d/f"));
  98. assertMatches(Sets.of("d/e/f/g"), fakeWalk("d/e/f/g"));
  99. assertMatches(Sets.of("d/e/f/g.x"), fakeWalk("d/e/f/g.x"));
  100. }
  101. @Test
  102. public void testNoMatchButClose() throws MissingObjectException,
  103. IncorrectObjectTypeException, IOException {
  104. assertNoMatches(fakeWalk("a+"));
  105. assertNoMatches(fakeWalk("b+/c"));
  106. assertNoMatches(fakeWalk("c+/d/e"));
  107. assertNoMatches(fakeWalk("c+/d/f"));
  108. assertNoMatches(fakeWalk("c/d.a"));
  109. assertNoMatches(fakeWalk("d+/e/f/g"));
  110. }
  111. @Test
  112. public void testJustCommonPrefixIsNotMatch() throws MissingObjectException,
  113. IncorrectObjectTypeException, IOException {
  114. assertNoMatches(fakeWalk("b/a"));
  115. assertNoMatches(fakeWalk("b/d"));
  116. assertNoMatches(fakeWalk("c/d/a"));
  117. assertNoMatches(fakeWalk("d/e/e"));
  118. assertNoMatches(fakeWalk("d/e/f/g.y"));
  119. }
  120. @Test
  121. public void testKeyIsPrefixOfFilter() throws MissingObjectException,
  122. IncorrectObjectTypeException, IOException {
  123. assertMatches(Sets.of("b/c"), fakeWalkAtSubtree("b"));
  124. assertMatches(Sets.of("c/d/e", "c/d/f"), fakeWalkAtSubtree("c/d"));
  125. assertMatches(Sets.of("c/d/e", "c/d/f"), fakeWalkAtSubtree("c"));
  126. assertMatches(Sets.of("d/e/f/g", "d/e/f/g.x"),
  127. fakeWalkAtSubtree("d/e/f"));
  128. assertMatches(Sets.of("d/e/f/g", "d/e/f/g.x"),
  129. fakeWalkAtSubtree("d/e"));
  130. assertMatches(Sets.of("d/e/f/g", "d/e/f/g.x"), fakeWalkAtSubtree("d"));
  131. assertNoMatches(fakeWalk("b"));
  132. assertNoMatches(fakeWalk("c/d"));
  133. assertNoMatches(fakeWalk("c"));
  134. assertNoMatches(fakeWalk("d/e/f"));
  135. assertNoMatches(fakeWalk("d/e"));
  136. assertNoMatches(fakeWalk("d"));
  137. }
  138. @Test
  139. public void testFilterIsPrefixOfKey() throws MissingObjectException,
  140. IncorrectObjectTypeException, IOException {
  141. assertMatches(Sets.of("a"), fakeWalk("a/b"));
  142. assertMatches(Sets.of("b/c"), fakeWalk("b/c/d"));
  143. assertMatches(Sets.of("c/d/e"), fakeWalk("c/d/e/f"));
  144. assertMatches(Sets.of("c/d/f"), fakeWalk("c/d/f/g"));
  145. assertMatches(Sets.of("d/e/f/g"), fakeWalk("d/e/f/g/h"));
  146. assertMatches(Sets.of("d/e/f/g"), fakeWalk("d/e/f/g/y"));
  147. assertMatches(Sets.of("d/e/f/g.x"), fakeWalk("d/e/f/g.x/h"));
  148. }
  149. @Test
  150. public void testLongPaths() throws MissingObjectException,
  151. IncorrectObjectTypeException, IOException {
  152. TreeFilter longPathFilter = PathFilterGroup
  153. .createFromStrings(
  154. "tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java",
  155. "tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest2.java");
  156. assertFalse(longPathFilter
  157. .include(fakeWalk("tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java")));
  158. assertFalse(longPathFilter.include(fakeWalk("tst/a-other-in-same")));
  159. assertFalse(longPathFilter.include(fakeWalk("a-nothing-in-common")));
  160. }
  161. @Test
  162. public void testStopWalk() throws MissingObjectException,
  163. IncorrectObjectTypeException, IOException {
  164. // Obvious
  165. filter.include(fakeWalk("d/e/f/f"));
  166. // Obvious
  167. try {
  168. filter.include(fakeWalk("de"));
  169. fail("StopWalkException expected");
  170. } catch (StopWalkException e) {
  171. // good
  172. }
  173. // less obvious due to git sorting order
  174. filter.include(fakeWalk("d-"));
  175. // less obvious due to git sorting order
  176. try {
  177. filter.include(fakeWalk("d0"));
  178. fail("StopWalkException expected");
  179. } catch (StopWalkException e) {
  180. // good
  181. }
  182. // less obvious #2 due to git sorting order
  183. filter.include(fakeWalk("d/e/f/g/h.txt"));
  184. // listed before g/y, so can't StopWalk here
  185. filter.include(fakeWalk("d/e/f/g.y"));
  186. singles.get("d/e/f/g").include(fakeWalk("d/e/f/g.y"));
  187. // non-ascii
  188. try {
  189. filter.include(fakeWalk("\u00C0"));
  190. fail("StopWalkException expected");
  191. } catch (StopWalkException e) {
  192. // good
  193. }
  194. }
  195. private void assertNoMatches(TreeWalk tw) throws MissingObjectException,
  196. IncorrectObjectTypeException, IOException {
  197. assertMatches(Sets.<String> of(), tw);
  198. }
  199. private void assertMatches(Set<String> expect, TreeWalk tw)
  200. throws MissingObjectException, IncorrectObjectTypeException,
  201. IOException {
  202. List<String> actual = new ArrayList<>();
  203. for (String path : singles.keySet()) {
  204. if (includes(singles.get(path), tw)) {
  205. actual.add(path);
  206. }
  207. }
  208. String[] e = expect.toArray(new String[expect.size()]);
  209. String[] a = actual.toArray(new String[actual.size()]);
  210. Arrays.sort(e);
  211. Arrays.sort(a);
  212. assertArrayEquals(e, a);
  213. if (expect.isEmpty()) {
  214. assertFalse(includes(filter, tw));
  215. } else {
  216. assertTrue(includes(filter, tw));
  217. }
  218. }
  219. private static boolean includes(TreeFilter f, TreeWalk tw)
  220. throws MissingObjectException, IncorrectObjectTypeException,
  221. IOException {
  222. try {
  223. return f.include(tw);
  224. } catch (StopWalkException e) {
  225. return false;
  226. }
  227. }
  228. TreeWalk fakeWalk(final String path) throws IOException {
  229. DirCache dc = DirCache.newInCore();
  230. DirCacheEditor dce = dc.editor();
  231. dce.add(new DirCacheEditor.PathEdit(path) {
  232. public void apply(DirCacheEntry ent) {
  233. ent.setFileMode(FileMode.REGULAR_FILE);
  234. }
  235. });
  236. dce.finish();
  237. TreeWalk ret = new TreeWalk((ObjectReader) null);
  238. ret.reset();
  239. ret.setRecursive(true);
  240. ret.addTree(new DirCacheIterator(dc));
  241. ret.next();
  242. return ret;
  243. }
  244. TreeWalk fakeWalkAtSubtree(final String path) throws IOException {
  245. DirCache dc = DirCache.newInCore();
  246. DirCacheEditor dce = dc.editor();
  247. dce.add(new DirCacheEditor.PathEdit(path + "/README") {
  248. public void apply(DirCacheEntry ent) {
  249. ent.setFileMode(FileMode.REGULAR_FILE);
  250. }
  251. });
  252. dce.finish();
  253. TreeWalk ret = new TreeWalk((ObjectReader) null);
  254. ret.addTree(new DirCacheIterator(dc));
  255. ret.next();
  256. while (!path.equals(ret.getPathString())) {
  257. if (ret.isSubtree()) {
  258. ret.enterSubtree();
  259. }
  260. ret.next();
  261. }
  262. return ret;
  263. }
  264. }