View Javadoc
1   /*
2    * Copyright (C) 2011, GEBIT Solutions
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  package org.eclipse.jgit.revwalk;
44  
45  import static org.junit.Assert.assertNull;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  import org.eclipse.jgit.diff.DiffConfig;
51  import org.eclipse.jgit.diff.DiffEntry;
52  import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
53  import org.eclipse.jgit.lib.Config;
54  import org.junit.Assert;
55  import org.junit.Before;
56  import org.junit.Test;
57  
58  public class RevWalkFollowFilterTest extends RevWalkTestCase {
59  
60  	private static class DiffCollector extends RenameCallback {
61  		List<DiffEntry> diffs = new ArrayList<>();
62  
63  		@Override
64  		public void renamed(DiffEntry diff) {
65  			diffs.add(diff);
66  		}
67  	}
68  
69  	private DiffCollector diffCollector;
70  
71  	@Before
72  	@Override
73  	public void setUp() throws Exception {
74  		super.setUp();
75  		diffCollector = new DiffCollector();
76  	}
77  
78  	protected FollowFilter follow(String followPath) {
79  		FollowFilter followFilter =
80  			FollowFilter.create(followPath, new Config().get(DiffConfig.KEY));
81  		followFilter.setRenameCallback(diffCollector);
82  		rw.setTreeFilter(followFilter);
83  		return followFilter;
84  	}
85  
86  	@Test
87  	public void testNoRename() throws Exception {
88  		final RevCommit a = commit(tree(file("0", blob("0"))));
89  		follow("0");
90  		markStart(a);
91  		assertCommit(a, rw.next());
92  		assertNull(rw.next());
93  
94  		assertNoRenames();
95  	}
96  
97  	@Test
98  	public void testSingleRename() throws Exception {
99  		final RevCommit a = commit(tree(file("a", blob("A"))));
100 
101 		// rename a to b
102 		CommitBuilder commitBuilder = commitBuilder().parent(a)
103 				.add("b", blob("A")).rm("a");
104 		RevCommit renameCommit = commitBuilder.create();
105 
106 		follow("b");
107 		markStart(renameCommit);
108 		assertCommit(renameCommit, rw.next());
109 		assertCommit(a, rw.next());
110 		assertNull(rw.next());
111 
112 		assertRenames("a->b");
113 	}
114 
115 	@Test
116 	public void testMultiRename() throws Exception {
117 		final String contents = "A";
118 		final RevCommit a = commit(tree(file("a", blob(contents))));
119 
120 		// rename a to b
121 		CommitBuilder commitBuilder = commitBuilder().parent(a)
122 				.add("b", blob(contents)).rm("a");
123 		RevCommit renameCommit1 = commitBuilder.create();
124 
125 		// rename b to c
126 		commitBuilder = commitBuilder().parent(renameCommit1)
127 				.add("c", blob(contents)).rm("b");
128 		RevCommit renameCommit2 = commitBuilder.create();
129 
130 		// rename c to a
131 		commitBuilder = commitBuilder().parent(renameCommit2)
132 				.add("a", blob(contents)).rm("c");
133 		RevCommit renameCommit3 = commitBuilder.create();
134 
135 		follow("a");
136 		markStart(renameCommit3);
137 		assertCommit(renameCommit3, rw.next());
138 		assertCommit(renameCommit2, rw.next());
139 		assertCommit(renameCommit1, rw.next());
140 		assertCommit(a, rw.next());
141 		assertNull(rw.next());
142 
143 		assertRenames("c->a", "b->c", "a->b");
144 	}
145 
146 	/**
147 	 * Assert which renames should have happened, in traversal order.
148 	 *
149 	 * @param expectedRenames
150 	 *            the rename specs, each one in the form "srcPath-&gt;destPath"
151 	 */
152 	protected void assertRenames(String... expectedRenames) {
153 		Assert.assertEquals("Unexpected number of renames. Expected: " +
154 				expectedRenames.length + ", actual: " + diffCollector.diffs.size(),
155 				expectedRenames.length, diffCollector.diffs.size());
156 
157 		for (int i = 0; i < expectedRenames.length; i++) {
158 			DiffEntry diff = diffCollector.diffs.get(i);
159 			Assert.assertNotNull(diff);
160 			String[] split = expectedRenames[i].split("->");
161 
162 			Assert.assertNotNull(split);
163 			Assert.assertEquals(2, split.length);
164 			String src = split[0];
165 			String target = split[1];
166 
167 			Assert.assertEquals(src, diff.getOldPath());
168 			Assert.assertEquals(target, diff.getNewPath());
169 		}
170 	}
171 
172 	protected void assertNoRenames() {
173 		Assert.assertEquals("Found unexpected rename/copy diff", 0,
174 				diffCollector.diffs.size());
175 	}
176 
177 }