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.

PathSuffixFilterTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2009, 2021 Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.treewalk.filter;
  11. import static org.junit.Assert.assertEquals;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import org.eclipse.jgit.dircache.DirCache;
  17. import org.eclipse.jgit.dircache.DirCacheBuilder;
  18. import org.eclipse.jgit.dircache.DirCacheEntry;
  19. import org.eclipse.jgit.junit.RepositoryTestCase;
  20. import org.eclipse.jgit.lib.FileMode;
  21. import org.eclipse.jgit.lib.ObjectId;
  22. import org.eclipse.jgit.lib.ObjectInserter;
  23. import org.eclipse.jgit.treewalk.TreeWalk;
  24. import org.junit.Test;
  25. public class PathSuffixFilterTest extends RepositoryTestCase {
  26. @Test
  27. public void testNonRecursiveFiltering() throws IOException {
  28. ObjectId treeId = createTree("a.sth", "a.txt");
  29. List<String> paths = getMatchingPaths(".txt", treeId);
  30. List<String> expected = Arrays.asList("a.txt");
  31. assertEquals(expected, paths);
  32. }
  33. @Test
  34. public void testRecursiveFiltering() throws IOException {
  35. ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth", "sub/b.txt",
  36. "t.sth", "t.txt");
  37. List<String> paths = getMatchingPaths(".txt", treeId, true);
  38. List<String> expected = Arrays.asList("a.txt", "sub/b.txt", "t.txt");
  39. assertEquals(expected, paths);
  40. }
  41. @Test
  42. public void testEdgeCases() throws IOException {
  43. ObjectId treeId = createTree("abc", "abcd", "bcd", "c");
  44. assertEquals(new ArrayList<String>(), getMatchingPaths("xbcd", treeId));
  45. assertEquals(new ArrayList<String>(), getMatchingPaths("abcx", treeId));
  46. assertEquals(Arrays.asList("abcd"), getMatchingPaths("abcd", treeId));
  47. assertEquals(Arrays.asList("abcd", "bcd"), getMatchingPaths("bcd", treeId));
  48. assertEquals(Arrays.asList("abc", "c"), getMatchingPaths("c", treeId));
  49. }
  50. @Test
  51. public void testNegated() throws IOException {
  52. ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth",
  53. "sub/b.txt", "t.sth", "t.txt");
  54. List<String> paths = getMatchingPaths(".txt", treeId, true, true);
  55. List<String> expected = Arrays.asList("a.sth", "sub/b.sth", "t.sth");
  56. assertEquals(expected, paths);
  57. }
  58. private ObjectId createTree(String... paths) throws IOException {
  59. final ObjectInserter odi = db.newObjectInserter();
  60. final DirCache dc = db.readDirCache();
  61. final DirCacheBuilder builder = dc.builder();
  62. for (String path : paths) {
  63. DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE);
  64. builder.add(entry);
  65. }
  66. builder.finish();
  67. final ObjectId treeId = dc.writeTree(odi);
  68. odi.flush();
  69. return treeId;
  70. }
  71. private List<String> getMatchingPaths(String suffixFilter,
  72. final ObjectId treeId) throws IOException {
  73. return getMatchingPaths(suffixFilter, treeId, false);
  74. }
  75. private List<String> getMatchingPaths(String suffixFilter,
  76. final ObjectId treeId, boolean recursiveWalk) throws IOException {
  77. return getMatchingPaths(suffixFilter, treeId, recursiveWalk, false);
  78. }
  79. private List<String> getMatchingPaths(String suffixFilter,
  80. final ObjectId treeId, boolean recursiveWalk, boolean negated)
  81. throws IOException {
  82. try (TreeWalk tw = new TreeWalk(db)) {
  83. TreeFilter filter = PathSuffixFilter.create(suffixFilter);
  84. if (negated) {
  85. filter = filter.negate();
  86. }
  87. tw.setFilter(filter);
  88. tw.setRecursive(recursiveWalk);
  89. tw.addTree(treeId);
  90. List<String> paths = new ArrayList<>();
  91. while (tw.next())
  92. paths.add(tw.getPathString());
  93. return paths;
  94. }
  95. }
  96. }