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.assertEquals;
47  import static org.junit.Assert.assertNotNull;
48  
49  import java.lang.reflect.Field;
50  import java.util.Collections;
51  import java.util.HashMap;
52  
53  import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
54  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
55  import org.eclipse.jgit.treewalk.filter.TreeFilter;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  // Note: Much of this test case is broken as it depends upon
60  // the graph applying topological sorting *before* doing merge
61  // simplification.  It also depends upon a difference between
62  // full history and non-full history for a path, something we
63  // don't quite yet have a distiction for in JGit.
64  //
65  public class RevWalkPathFilter6012Test extends RevWalkTestCase {
66  	private static final String pA = "pA", pF = "pF", pE = "pE";
67  
68  	private RevCommit a, b, c, d, e, f, g, h, i;
69  
70  	private HashMap<RevCommit, String> byName;
71  
72  	@Override
73  	@Before
74  	public void setUp() throws Exception {
75  		super.setUp();
76  
77  		// Test graph was stolen from git-core t6012-rev-list-simplify
78  		// (by Junio C Hamano in 65347030590bcc251a9ff2ed96487a0f1b9e9fa8)
79  		//
80  		final RevBlob zF = blob("zF");
81  		final RevBlob zH = blob("zH");
82  		final RevBlob zI = blob("zI");
83  		final RevBlob zS = blob("zS");
84  		final RevBlob zY = blob("zY");
85  
86  		a = commit(tree(file(pF, zH)));
87  		b = commit(tree(file(pF, zI)), a);
88  		c = commit(tree(file(pF, zI)), a);
89  		d = commit(tree(file(pA, zS), file(pF, zI)), c);
90  		parseBody(d);
91  		e = commit(d.getTree(), d, b);
92  		f = commit(tree(file(pA, zS), file(pE, zY), file(pF, zI)), e);
93  		parseBody(f);
94  		g = commit(tree(file(pE, zY), file(pF, zI)), b);
95  		h = commit(f.getTree(), g, f);
96  		i = commit(tree(file(pA, zS), file(pE, zY), file(pF, zF)), h);
97  
98  		byName = new HashMap<>();
99  		for (Field z : RevWalkPathFilter6012Test.class.getDeclaredFields()) {
100 			if (z.getType() == RevCommit.class)
101 				byName.put((RevCommit) z.get(this), z.getName());
102 		}
103 	}
104 
105 	protected void check(RevCommit... order) throws Exception {
106 		markStart(i);
107 		final StringBuilder act = new StringBuilder();
108 		for (RevCommit z : rw) {
109 			final String name = byName.get(z);
110 			assertNotNull(name);
111 			act.append(name);
112 			act.append(' ');
113 		}
114 		final StringBuilder exp = new StringBuilder();
115 		for (RevCommit z : order) {
116 			final String name = byName.get(z);
117 			assertNotNull(name);
118 			exp.append(name);
119 			exp.append(' ');
120 		}
121 		assertEquals(exp.toString(), act.toString());
122 	}
123 
124 	protected void filter(String path) {
125 		rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup
126 				.createFromStrings(Collections.singleton(path)),
127 				TreeFilter.ANY_DIFF));
128 	}
129 
130 	@Test
131 	public void test1() throws Exception {
132 		// TODO --full-history
133 		check(i, h, g, f, e, d, c, b, a);
134 	}
135 
136 	@Test
137 	public void test2() throws Exception {
138 		// TODO --full-history
139 		filter(pF);
140 		// TODO fix broken test
141 		// check(i, h, e, c, b, a);
142 	}
143 
144 	@Test
145 	public void test3() throws Exception {
146 		// TODO --full-history
147 		rw.sort(RevSort.TOPO);
148 		filter(pF);
149 		// TODO fix broken test
150 		// check(i, h, e, c, b, a);
151 	}
152 
153 	@Test
154 	public void test4() throws Exception {
155 		// TODO --full-history
156 		rw.sort(RevSort.COMMIT_TIME_DESC);
157 		filter(pF);
158 		// TODO fix broken test
159 		// check(i, h, e, c, b, a);
160 	}
161 
162 	@Test
163 	public void test5() throws Exception {
164 		// TODO --simplify-merges
165 		filter(pF);
166 		// TODO fix broken test
167 		// check(i, e, c, b, a);
168 	}
169 
170 	@Test
171 	public void test6() throws Exception {
172 		filter(pF);
173 		check(i, b, a);
174 	}
175 
176 	@Test
177 	public void test7() throws Exception {
178 		rw.sort(RevSort.TOPO);
179 		filter(pF);
180 		check(i, b, a);
181 	}
182 }