View Javadoc
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  
11  package org.eclipse.jgit.treewalk.filter;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.io.IOException;
16  import java.util.ArrayList;
17  import java.util.Arrays;
18  import java.util.List;
19  
20  import org.eclipse.jgit.dircache.DirCache;
21  import org.eclipse.jgit.dircache.DirCacheBuilder;
22  import org.eclipse.jgit.dircache.DirCacheEntry;
23  import org.eclipse.jgit.junit.RepositoryTestCase;
24  import org.eclipse.jgit.lib.FileMode;
25  import org.eclipse.jgit.lib.ObjectId;
26  import org.eclipse.jgit.lib.ObjectInserter;
27  import org.eclipse.jgit.treewalk.TreeWalk;
28  import org.junit.Test;
29  
30  public class PathSuffixFilterTest extends RepositoryTestCase {
31  
32  	@Test
33  	public void testNonRecursiveFiltering() throws IOException {
34  		ObjectId treeId = createTree("a.sth", "a.txt");
35  
36  		List<String> paths = getMatchingPaths(".txt", treeId);
37  		List<String> expected = Arrays.asList("a.txt");
38  
39  		assertEquals(expected, paths);
40  	}
41  
42  	@Test
43  	public void testRecursiveFiltering() throws IOException {
44  		ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth", "sub/b.txt",
45  				"t.sth", "t.txt");
46  
47  		List<String> paths = getMatchingPaths(".txt", treeId, true);
48  		List<String> expected = Arrays.asList("a.txt", "sub/b.txt", "t.txt");
49  
50  		assertEquals(expected, paths);
51  	}
52  
53  	@Test
54  	public void testEdgeCases() throws IOException {
55  		ObjectId treeId = createTree("abc", "abcd", "bcd", "c");
56  		assertEquals(new ArrayList<String>(), getMatchingPaths("xbcd", treeId));
57  		assertEquals(new ArrayList<String>(), getMatchingPaths("abcx", treeId));
58  		assertEquals(Arrays.asList("abcd"), getMatchingPaths("abcd", treeId));
59  		assertEquals(Arrays.asList("abcd", "bcd"), getMatchingPaths("bcd", treeId));
60  		assertEquals(Arrays.asList("abc", "c"), getMatchingPaths("c", treeId));
61  	}
62  
63  	@Test
64  	public void testNegated() throws IOException {
65  		ObjectId treeId = createTree("a.sth", "a.txt", "sub/b.sth",
66  				"sub/b.txt", "t.sth", "t.txt");
67  
68  		List<String> paths = getMatchingPaths(".txt", treeId, true, true);
69  		List<String> expected = Arrays.asList("a.sth", "sub/b.sth", "t.sth");
70  
71  		assertEquals(expected, paths);
72  	}
73  
74  	private ObjectId createTree(String... paths) throws IOException {
75  		final ObjectInserter odi = db.newObjectInserter();
76  		final DirCache dc = db.readDirCache();
77  		final DirCacheBuilder builder = dc.builder();
78  		for (String path : paths) {
79  			DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE);
80  			builder.add(entry);
81  		}
82  		builder.finish();
83  		final ObjectId treeId = dc.writeTree(odi);
84  		odi.flush();
85  		return treeId;
86  	}
87  
88  	private List<String> getMatchingPaths(String suffixFilter,
89  			final ObjectId treeId) throws IOException {
90  		return getMatchingPaths(suffixFilter, treeId, false);
91  	}
92  
93  	private List<String> getMatchingPaths(String suffixFilter,
94  			final ObjectId treeId, boolean recursiveWalk) throws IOException {
95  		return getMatchingPaths(suffixFilter, treeId, recursiveWalk, false);
96  	}
97  
98  	private List<String> getMatchingPaths(String suffixFilter,
99  			final ObjectId treeId, boolean recursiveWalk, boolean negated)
100 			throws IOException {
101 		try (TreeWalk tw = new TreeWalk(db)) {
102 			TreeFilter filter = PathSuffixFilter.create(suffixFilter);
103 			if (negated) {
104 				filter = filter.negate();
105 			}
106 			tw.setFilter(filter);
107 			tw.setRecursive(recursiveWalk);
108 			tw.addTree(treeId);
109 
110 			List<String> paths = new ArrayList<>();
111 			while (tw.next())
112 				paths.add(tw.getPathString());
113 			return paths;
114 		}
115 	}
116 
117 }