View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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.assertNotNull;
47  import static org.junit.Assert.assertNull;
48  
49  import java.io.IOException;
50  import java.util.Date;
51  
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.revwalk.filter.AndRevFilter;
56  import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
57  import org.eclipse.jgit.revwalk.filter.NotRevFilter;
58  import org.eclipse.jgit.revwalk.filter.OrRevFilter;
59  import org.eclipse.jgit.revwalk.filter.RevFilter;
60  import org.junit.Test;
61  
62  public class RevWalkFilterTest extends RevWalkTestCase {
63  	private static final MyAll MY_ALL = new MyAll();
64  
65  	@Test
66  	public void testFilter_ALL() throws Exception {
67  		final RevCommit a = commit();
68  		final RevCommit b = commit(a);
69  		final RevCommit c = commit(b);
70  
71  		rw.setRevFilter(RevFilter.ALL);
72  		markStart(c);
73  		assertCommit(c, rw.next());
74  		assertCommit(b, rw.next());
75  		assertCommit(a, rw.next());
76  		assertNull(rw.next());
77  	}
78  
79  	@Test
80  	public void testFilter_Negate_ALL() throws Exception {
81  		final RevCommit a = commit();
82  		final RevCommit b = commit(a);
83  		final RevCommit c = commit(b);
84  
85  		rw.setRevFilter(RevFilter.ALL.negate());
86  		markStart(c);
87  		assertNull(rw.next());
88  	}
89  
90  	@Test
91  	public void testFilter_NOT_ALL() throws Exception {
92  		final RevCommit a = commit();
93  		final RevCommit b = commit(a);
94  		final RevCommit c = commit(b);
95  
96  		rw.setRevFilter(NotRevFilter.create(RevFilter.ALL));
97  		markStart(c);
98  		assertNull(rw.next());
99  	}
100 
101 	@Test
102 	public void testFilter_NONE() throws Exception {
103 		final RevCommit a = commit();
104 		final RevCommit b = commit(a);
105 		final RevCommit c = commit(b);
106 
107 		rw.setRevFilter(RevFilter.NONE);
108 		markStart(c);
109 		assertNull(rw.next());
110 	}
111 
112 	@Test
113 	public void testFilter_NOT_NONE() throws Exception {
114 		final RevCommit a = commit();
115 		final RevCommit b = commit(a);
116 		final RevCommit c = commit(b);
117 
118 		rw.setRevFilter(NotRevFilter.create(RevFilter.NONE));
119 		markStart(c);
120 		assertCommit(c, rw.next());
121 		assertCommit(b, rw.next());
122 		assertCommit(a, rw.next());
123 		assertNull(rw.next());
124 	}
125 
126 	@Test
127 	public void testFilter_ALL_And_NONE() throws Exception {
128 		final RevCommit a = commit();
129 		final RevCommit b = commit(a);
130 		final RevCommit c = commit(b);
131 
132 		rw.setRevFilter(AndRevFilter.create(RevFilter.ALL, RevFilter.NONE));
133 		markStart(c);
134 		assertNull(rw.next());
135 	}
136 
137 	@Test
138 	public void testFilter_NONE_And_ALL() throws Exception {
139 		final RevCommit a = commit();
140 		final RevCommit b = commit(a);
141 		final RevCommit c = commit(b);
142 
143 		rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, RevFilter.ALL));
144 		markStart(c);
145 		assertNull(rw.next());
146 	}
147 
148 	@Test
149 	public void testFilter_ALL_Or_NONE() throws Exception {
150 		final RevCommit a = commit();
151 		final RevCommit b = commit(a);
152 		final RevCommit c = commit(b);
153 
154 		rw.setRevFilter(OrRevFilter.create(RevFilter.ALL, RevFilter.NONE));
155 		markStart(c);
156 		assertCommit(c, rw.next());
157 		assertCommit(b, rw.next());
158 		assertCommit(a, rw.next());
159 		assertNull(rw.next());
160 	}
161 
162 	@Test
163 	public void testFilter_NONE_Or_ALL() throws Exception {
164 		final RevCommit a = commit();
165 		final RevCommit b = commit(a);
166 		final RevCommit c = commit(b);
167 
168 		rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, RevFilter.ALL));
169 		markStart(c);
170 		assertCommit(c, rw.next());
171 		assertCommit(b, rw.next());
172 		assertCommit(a, rw.next());
173 		assertNull(rw.next());
174 	}
175 
176 	@Test
177 	public void testFilter_MY_ALL_And_NONE() throws Exception {
178 		final RevCommit a = commit();
179 		final RevCommit b = commit(a);
180 		final RevCommit c = commit(b);
181 
182 		rw.setRevFilter(AndRevFilter.create(MY_ALL, RevFilter.NONE));
183 		markStart(c);
184 		assertNull(rw.next());
185 	}
186 
187 	@Test
188 	public void testFilter_NONE_And_MY_ALL() throws Exception {
189 		final RevCommit a = commit();
190 		final RevCommit b = commit(a);
191 		final RevCommit c = commit(b);
192 
193 		rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, MY_ALL));
194 		markStart(c);
195 		assertNull(rw.next());
196 	}
197 
198 	@Test
199 	public void testFilter_MY_ALL_Or_NONE() throws Exception {
200 		final RevCommit a = commit();
201 		final RevCommit b = commit(a);
202 		final RevCommit c = commit(b);
203 
204 		rw.setRevFilter(OrRevFilter.create(MY_ALL, RevFilter.NONE));
205 		markStart(c);
206 		assertCommit(c, rw.next());
207 		assertCommit(b, rw.next());
208 		assertCommit(a, rw.next());
209 		assertNull(rw.next());
210 	}
211 
212 	@Test
213 	public void testFilter_NONE_Or_MY_ALL() throws Exception {
214 		final RevCommit a = commit();
215 		final RevCommit b = commit(a);
216 		final RevCommit c = commit(b);
217 
218 		rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, MY_ALL));
219 		markStart(c);
220 		assertCommit(c, rw.next());
221 		assertCommit(b, rw.next());
222 		assertCommit(a, rw.next());
223 		assertNull(rw.next());
224 	}
225 
226 	@Test
227 	public void testFilter_NO_MERGES() throws Exception {
228 		final RevCommit a = commit();
229 		final RevCommit b = commit(a);
230 		final RevCommit c1 = commit(b);
231 		final RevCommit c2 = commit(b);
232 		final RevCommit d = commit(c1, c2);
233 		final RevCommit e = commit(d);
234 
235 		rw.setRevFilter(RevFilter.NO_MERGES);
236 		markStart(e);
237 		assertCommit(e, rw.next());
238 		assertCommit(c2, rw.next());
239 		assertCommit(c1, rw.next());
240 		assertCommit(b, rw.next());
241 		assertCommit(a, rw.next());
242 		assertNull(rw.next());
243 	}
244 
245 	@Test
246 	public void testCommitTimeRevFilter() throws Exception {
247 		final RevCommit a = commit();
248 		tick(100);
249 
250 		final RevCommit b = commit(a);
251 		tick(100);
252 
253 		Date since = getDate();
254 		final RevCommit c1 = commit(b);
255 		tick(100);
256 
257 		final RevCommit c2 = commit(b);
258 		tick(100);
259 
260 		Date until = getDate();
261 		final RevCommit d = commit(c1, c2);
262 		tick(100);
263 
264 		final RevCommit e = commit(d);
265 
266 		{
267 			RevFilter after = CommitTimeRevFilter.after(since);
268 			assertNotNull(after);
269 			rw.setRevFilter(after);
270 			markStart(e);
271 			assertCommit(e, rw.next());
272 			assertCommit(d, rw.next());
273 			assertCommit(c2, rw.next());
274 			assertCommit(c1, rw.next());
275 			assertNull(rw.next());
276 		}
277 
278 		{
279 			RevFilter before = CommitTimeRevFilter.before(until);
280 			assertNotNull(before);
281 			rw.reset();
282 			rw.setRevFilter(before);
283 			markStart(e);
284 			assertCommit(c2, rw.next());
285 			assertCommit(c1, rw.next());
286 			assertCommit(b, rw.next());
287 			assertCommit(a, rw.next());
288 			assertNull(rw.next());
289 		}
290 
291 		{
292 			RevFilter between = CommitTimeRevFilter.between(since, until);
293 			assertNotNull(between);
294 			rw.reset();
295 			rw.setRevFilter(between);
296 			markStart(e);
297 			assertCommit(c2, rw.next());
298 			assertCommit(c1, rw.next());
299 			assertNull(rw.next());
300 		}
301 	}
302 
303 	private static class MyAll extends RevFilter {
304 		@Override
305 		public RevFilter clone() {
306 			return this;
307 		}
308 
309 		@Override
310 		public boolean include(RevWalk walker, RevCommit cmit)
311 				throws StopWalkException, MissingObjectException,
312 				IncorrectObjectTypeException, IOException {
313 			return true;
314 		}
315 
316 		@Override
317 		public boolean requiresCommitBody() {
318 			return false;
319 		}
320 	}
321 }