View Javadoc
1   /*
2    * Copyright (C) 2015, 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  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.IOException;
50  import java.util.Set;
51  
52  import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
53  import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
54  import org.eclipse.jgit.junit.TestRepository;
55  import org.eclipse.jgit.lib.AnyObjectId;
56  import org.eclipse.jgit.lib.Sets;
57  import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
58  import org.eclipse.jgit.revwalk.filter.NotRevFilter;
59  import org.eclipse.jgit.revwalk.filter.ObjectFilter;
60  import org.junit.After;
61  import org.junit.Before;
62  import org.junit.Test;
63  
64  public class ObjectWalkFilterTest {
65  	private TestRepository<InMemoryRepository> tr;
66  	private ObjectWalk rw;
67  
68  	// 3 commits, 2 top-level trees, 4 subtrees, 3 blobs
69  	private static final int OBJECT_COUNT = 12;
70  
71  	@Before
72  	public void setUp() throws Exception {
73  		tr = new TestRepository<>(new InMemoryRepository(
74  				new DfsRepositoryDescription("test")));
75  		rw = new ObjectWalk(tr.getRepository());
76  
77  		rw.markStart(tr.branch("master").commit()
78  			.add("a/a", "1")
79  			.add("b/b", "2")
80  			.add("c/c", "3")
81  			.message("initial commit")
82  
83  			.child()
84  			.rm("a/a")
85  			.add("a/A", "1")
86  			.message("capitalize a/a")
87  
88  			.child()
89  			.rm("a/A")
90  			.add("a/a", "1")
91  			.message("make a/A lowercase again")
92  			.create());
93  	}
94  
95  	@After
96  	public void tearDown() {
97  		rw.close();
98  		tr.getRepository().close();
99  	}
100 
101 	private static class BlacklistObjectFilter extends ObjectFilter {
102 		final Set<AnyObjectId> badObjects;
103 
104 		BlacklistObjectFilter(Set<AnyObjectId> badObjects) {
105 			this.badObjects = badObjects;
106 		}
107 
108 		@Override
109 		public boolean include(ObjectWalk walker, AnyObjectId o) {
110 			return !badObjects.contains(o);
111 		}
112 	}
113 
114 	private AnyObjectId resolve(String revstr) throws Exception {
115 		return tr.getRepository().resolve(revstr);
116 	}
117 
118 	private int countObjects() throws IOException {
119 		int n = 0;
120 		while (rw.next() != null) {
121 			n++;
122 		}
123 		while (rw.nextObject() != null) {
124 			n++;
125 		}
126 		return n;
127 	}
128 
129 	@Test
130 	public void testDefaultFilter() throws Exception {
131 		assertTrue("filter is ALL",
132 				rw.getObjectFilter() == ObjectFilter.ALL);
133 		assertEquals(OBJECT_COUNT, countObjects());
134 	}
135 
136 	@Test
137 	public void testObjectFilterCanFilterOutBlob() throws Exception {
138 		AnyObjectId one = rw.parseAny(resolve("master:a/a"));
139 		AnyObjectId two = rw.parseAny(resolve("master:b/b"));
140 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
141 
142 		// 2 blobs filtered out
143 		assertEquals(OBJECT_COUNT - 2, countObjects());
144 	}
145 
146 	@Test
147 	public void testFilteringCommitsHasNoEffect() throws Exception {
148 		AnyObjectId initial = rw.parseCommit(resolve("master^^"));
149 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(initial)));
150 		assertEquals(OBJECT_COUNT, countObjects());
151 	}
152 
153 	@Test
154 	public void testRevFilterAndObjectFilterCanCombine() throws Exception {
155 		AnyObjectId one = rw.parseAny(resolve("master:a/a"));
156 		AnyObjectId two = rw.parseAny(resolve("master:b/b"));
157 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(one, two)));
158 		rw.setRevFilter(NotRevFilter.create(
159 				MessageRevFilter.create("capitalize")));
160 
161 		// 2 blobs, one commit, two trees filtered out
162 		assertEquals(OBJECT_COUNT - 5, countObjects());
163 	}
164 
165 	@Test
166 	public void testFilteringTreeFiltersSubtrees() throws Exception {
167 		AnyObjectId capitalizeTree = rw.parseAny(resolve("master^:"));
168 		rw.setObjectFilter(new BlacklistObjectFilter(
169 				Sets.of(capitalizeTree)));
170 
171 		// trees "master^:" and "master^:a" filtered out
172 		assertEquals(OBJECT_COUNT - 2, countObjects());
173 	}
174 
175 	@Test
176 	public void testFilteringTreeFiltersReferencedBlobs() throws Exception {
177 		AnyObjectId a1 = rw.parseAny(resolve("master:a"));
178 		AnyObjectId a2 = rw.parseAny(resolve("master^:a"));
179 		rw.setObjectFilter(new BlacklistObjectFilter(Sets.of(a1, a2)));
180 
181 		// 2 trees, one blob filtered out
182 		assertEquals(OBJECT_COUNT - 3, countObjects());
183 	}
184 }