View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub 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  package org.eclipse.jgit.api.blame;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import org.eclipse.jgit.api.Git;
50  import org.eclipse.jgit.blame.BlameGenerator;
51  import org.eclipse.jgit.blame.BlameResult;
52  import org.eclipse.jgit.junit.RepositoryTestCase;
53  import org.eclipse.jgit.lib.Constants;
54  import org.eclipse.jgit.revwalk.RevCommit;
55  import org.junit.Test;
56  
57  /** Unit tests of {@link BlameGenerator}. */
58  public class BlameGeneratorTest extends RepositoryTestCase {
59  	@Test
60  	public void testBoundLineDelete() throws Exception {
61  		try (Git git = new Git(db)) {
62  			String[] content1 = new String[] { "first", "second" };
63  			writeTrashFile("file.txt", join(content1));
64  			git.add().addFilepattern("file.txt").call();
65  			RevCommit c1 = git.commit().setMessage("create file").call();
66  
67  			String[] content2 = new String[] { "third", "first", "second" };
68  			writeTrashFile("file.txt", join(content2));
69  			git.add().addFilepattern("file.txt").call();
70  			RevCommit c2 = git.commit().setMessage("create file").call();
71  
72  			try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
73  				generator.push(null, db.resolve(Constants.HEAD));
74  				assertEquals(3, generator.getResultContents().size());
75  
76  				assertTrue(generator.next());
77  				assertEquals(c2, generator.getSourceCommit());
78  				assertEquals(1, generator.getRegionLength());
79  				assertEquals(0, generator.getResultStart());
80  				assertEquals(1, generator.getResultEnd());
81  				assertEquals(0, generator.getSourceStart());
82  				assertEquals(1, generator.getSourceEnd());
83  				assertEquals("file.txt", generator.getSourcePath());
84  
85  				assertTrue(generator.next());
86  				assertEquals(c1, generator.getSourceCommit());
87  				assertEquals(2, generator.getRegionLength());
88  				assertEquals(1, generator.getResultStart());
89  				assertEquals(3, generator.getResultEnd());
90  				assertEquals(0, generator.getSourceStart());
91  				assertEquals(2, generator.getSourceEnd());
92  				assertEquals("file.txt", generator.getSourcePath());
93  
94  				assertFalse(generator.next());
95  			}
96  		}
97  	}
98  
99  	@Test
100 	public void testRenamedBoundLineDelete() throws Exception {
101 		try (Git git = new Git(db)) {
102 			final String FILENAME_1 = "subdir/file1.txt";
103 			final String FILENAME_2 = "subdir/file2.txt";
104 
105 			String[] content1 = new String[] { "first", "second" };
106 			writeTrashFile(FILENAME_1, join(content1));
107 			git.add().addFilepattern(FILENAME_1).call();
108 			RevCommit c1 = git.commit().setMessage("create file1").call();
109 
110 			// rename it
111 			writeTrashFile(FILENAME_2, join(content1));
112 			git.add().addFilepattern(FILENAME_2).call();
113 			deleteTrashFile(FILENAME_1);
114 			git.rm().addFilepattern(FILENAME_1).call();
115 			git.commit().setMessage("rename file1.txt to file2.txt").call();
116 
117 			// and change the new file
118 			String[] content2 = new String[] { "third", "first", "second" };
119 			writeTrashFile(FILENAME_2, join(content2));
120 			git.add().addFilepattern(FILENAME_2).call();
121 			RevCommit c2 = git.commit().setMessage("change file2").call();
122 
123 			try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
124 				generator.push(null, db.resolve(Constants.HEAD));
125 				assertEquals(3, generator.getResultContents().size());
126 
127 				assertTrue(generator.next());
128 				assertEquals(c2, generator.getSourceCommit());
129 				assertEquals(1, generator.getRegionLength());
130 				assertEquals(0, generator.getResultStart());
131 				assertEquals(1, generator.getResultEnd());
132 				assertEquals(0, generator.getSourceStart());
133 				assertEquals(1, generator.getSourceEnd());
134 				assertEquals(FILENAME_2, generator.getSourcePath());
135 
136 				assertTrue(generator.next());
137 				assertEquals(c1, generator.getSourceCommit());
138 				assertEquals(2, generator.getRegionLength());
139 				assertEquals(1, generator.getResultStart());
140 				assertEquals(3, generator.getResultEnd());
141 				assertEquals(0, generator.getSourceStart());
142 				assertEquals(2, generator.getSourceEnd());
143 				assertEquals(FILENAME_1, generator.getSourcePath());
144 
145 				assertFalse(generator.next());
146 			}
147 
148 			// and test again with other BlameGenerator API:
149 			try (BlameGenerator generator = new BlameGenerator(db, FILENAME_2)) {
150 				generator.push(null, db.resolve(Constants.HEAD));
151 				BlameResult result = generator.computeBlameResult();
152 
153 				assertEquals(3, result.getResultContents().size());
154 
155 				assertEquals(c2, result.getSourceCommit(0));
156 				assertEquals(FILENAME_2, result.getSourcePath(0));
157 
158 				assertEquals(c1, result.getSourceCommit(1));
159 				assertEquals(FILENAME_1, result.getSourcePath(1));
160 
161 				assertEquals(c1, result.getSourceCommit(2));
162 				assertEquals(FILENAME_1, result.getSourcePath(2));
163 			}
164 		}
165 	}
166 
167 	@Test
168 	public void testLinesAllDeletedShortenedWalk() throws Exception {
169 		try (Git git = new Git(db)) {
170 			String[] content1 = new String[] { "first", "second", "third" };
171 
172 			writeTrashFile("file.txt", join(content1));
173 			git.add().addFilepattern("file.txt").call();
174 			git.commit().setMessage("create file").call();
175 
176 			String[] content2 = new String[] { "" };
177 
178 			writeTrashFile("file.txt", join(content2));
179 			git.add().addFilepattern("file.txt").call();
180 			git.commit().setMessage("create file").call();
181 
182 			writeTrashFile("file.txt", join(content1));
183 			git.add().addFilepattern("file.txt").call();
184 			RevCommit c3 = git.commit().setMessage("create file").call();
185 
186 			try (BlameGenerator generator = new BlameGenerator(db, "file.txt")) {
187 				generator.push(null, db.resolve(Constants.HEAD));
188 				assertEquals(3, generator.getResultContents().size());
189 
190 				assertTrue(generator.next());
191 				assertEquals(c3, generator.getSourceCommit());
192 				assertEquals(0, generator.getResultStart());
193 				assertEquals(3, generator.getResultEnd());
194 
195 				assertFalse(generator.next());
196 			}
197 		}
198 	}
199 
200 	private static String join(String... lines) {
201 		StringBuilder joined = new StringBuilder();
202 		for (String line : lines)
203 			joined.append(line).append('\n');
204 		return joined.toString();
205 	}
206 }