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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.assertFalse;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.fail;
  47. import java.io.IOException;
  48. import org.eclipse.jgit.dircache.DirCache;
  49. import org.eclipse.jgit.dircache.DirCacheEditor;
  50. import org.eclipse.jgit.dircache.DirCacheEntry;
  51. import org.eclipse.jgit.dircache.DirCacheIterator;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.errors.MissingObjectException;
  54. import org.eclipse.jgit.errors.StopWalkException;
  55. import org.eclipse.jgit.lib.FileMode;
  56. import org.eclipse.jgit.lib.ObjectReader;
  57. import org.eclipse.jgit.treewalk.TreeWalk;
  58. import org.junit.Before;
  59. import org.junit.Test;
  60. public class PathFilterGroupTest {
  61. private TreeFilter filter;
  62. @Before
  63. public void setup() {
  64. // @formatter:off
  65. String[] paths = new String[] {
  66. "/a", // never match
  67. "/a/b", // never match
  68. "a",
  69. "b/c",
  70. "c/d/e",
  71. "c/d/f",
  72. "d/e/f/g",
  73. "d/e/f/g.x"
  74. };
  75. // @formatter:on
  76. filter = PathFilterGroup.createFromStrings(paths);
  77. }
  78. @Test
  79. public void testExact() throws MissingObjectException,
  80. IncorrectObjectTypeException, IOException {
  81. assertTrue(filter.include(fakeWalk("a")));
  82. assertTrue(filter.include(fakeWalk("b/c")));
  83. assertTrue(filter.include(fakeWalk("c/d/e")));
  84. assertTrue(filter.include(fakeWalk("c/d/f")));
  85. assertTrue(filter.include(fakeWalk("d/e/f/g")));
  86. assertTrue(filter.include(fakeWalk("d/e/f/g.x")));
  87. }
  88. @Test
  89. public void testNoMatchButClose() throws MissingObjectException,
  90. IncorrectObjectTypeException, IOException {
  91. assertFalse(filter.include(fakeWalk("a+")));
  92. assertFalse(filter.include(fakeWalk("b+/c")));
  93. assertFalse(filter.include(fakeWalk("c+/d/e")));
  94. assertFalse(filter.include(fakeWalk("c+/d/f")));
  95. assertFalse(filter.include(fakeWalk("c/d.a")));
  96. assertFalse(filter.include(fakeWalk("d+/e/f/g")));
  97. }
  98. @Test
  99. public void testJustCommonPrefixIsNotMatch() throws MissingObjectException,
  100. IncorrectObjectTypeException, IOException {
  101. assertFalse(filter.include(fakeWalk("b/a")));
  102. assertFalse(filter.include(fakeWalk("b/d")));
  103. assertFalse(filter.include(fakeWalk("c/d/a")));
  104. assertFalse(filter.include(fakeWalk("d/e/e")));
  105. }
  106. @Test
  107. public void testKeyIsPrefixOfFilter() throws MissingObjectException,
  108. IncorrectObjectTypeException, IOException {
  109. assertTrue(filter.include(fakeWalk("b")));
  110. assertTrue(filter.include(fakeWalk("c/d")));
  111. assertTrue(filter.include(fakeWalk("c/d")));
  112. assertTrue(filter.include(fakeWalk("c")));
  113. assertTrue(filter.include(fakeWalk("d/e/f")));
  114. assertTrue(filter.include(fakeWalk("d/e")));
  115. assertTrue(filter.include(fakeWalk("d")));
  116. }
  117. @Test
  118. public void testFilterIsPrefixOfKey() throws MissingObjectException,
  119. IncorrectObjectTypeException, IOException {
  120. assertTrue(filter.include(fakeWalk("a/b")));
  121. assertTrue(filter.include(fakeWalk("b/c/d")));
  122. assertTrue(filter.include(fakeWalk("c/d/e/f")));
  123. assertTrue(filter.include(fakeWalk("c/d/f/g")));
  124. assertTrue(filter.include(fakeWalk("d/e/f/g/h")));
  125. assertTrue(filter.include(fakeWalk("d/e/f/g/y")));
  126. assertTrue(filter.include(fakeWalk("d/e/f/g.x/h")));
  127. // listed before g/y, so can't StopWalk here, but it's not included
  128. // either
  129. assertFalse(filter.include(fakeWalk("d/e/f/g.y")));
  130. }
  131. @Test
  132. public void testLongPaths() throws MissingObjectException,
  133. IncorrectObjectTypeException, IOException {
  134. TreeFilter longPathFilter = PathFilterGroup
  135. .createFromStrings(
  136. "tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest.java",
  137. "tst/org/eclipse/jgit/treewalk/filter/PathFilterGroupTest2.java");
  138. assertFalse(longPathFilter
  139. .include(fakeWalk("tst/org/eclipse/jgit/treewalk/FileTreeIteratorTest.java")));
  140. assertFalse(longPathFilter.include(fakeWalk("tst/a-other-in-same")));
  141. assertFalse(longPathFilter.include(fakeWalk("a-nothing-in-common")));
  142. }
  143. @Test
  144. public void testStopWalk() throws MissingObjectException,
  145. IncorrectObjectTypeException, IOException {
  146. // Obvious
  147. filter.include(fakeWalk("d/e/f/f"));
  148. // Obvious
  149. try {
  150. filter.include(fakeWalk("de"));
  151. fail("StopWalkException expected");
  152. } catch (StopWalkException e) {
  153. // good
  154. }
  155. // less obvious due to git sorting order
  156. filter.include(fakeWalk("d-"));
  157. // less obvious due to git sorting order
  158. try {
  159. filter.include(fakeWalk("d0"));
  160. fail("StopWalkException expected");
  161. } catch (StopWalkException e) {
  162. // good
  163. }
  164. // less obvious #2 due to git sorting order
  165. filter.include(fakeWalk("d/e/f/g/h.txt"));
  166. // non-ascii
  167. try {
  168. filter.include(fakeWalk("\u00C0"));
  169. fail("StopWalkException expected");
  170. } catch (StopWalkException e) {
  171. // good
  172. }
  173. }
  174. TreeWalk fakeWalk(final String path) throws IOException {
  175. DirCache dc = DirCache.newInCore();
  176. DirCacheEditor dce = dc.editor();
  177. dce.add(new DirCacheEditor.PathEdit(path) {
  178. public void apply(DirCacheEntry ent) {
  179. ent.setFileMode(FileMode.REGULAR_FILE);
  180. }
  181. });
  182. dce.finish();
  183. TreeWalk ret = new TreeWalk((ObjectReader) null);
  184. ret.reset();
  185. ret.setRecursive(true);
  186. ret.addTree(new DirCacheIterator(dc));
  187. ret.next();
  188. return ret;
  189. }
  190. }