View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
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.merge;
45  
46  import static java.nio.charset.StandardCharsets.UTF_8;
47  import static org.junit.Assert.assertEquals;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.diff.RawText;
53  import org.eclipse.jgit.diff.RawTextComparator;
54  import org.eclipse.jgit.lib.Constants;
55  import org.junit.Assume;
56  import org.junit.Test;
57  import org.junit.experimental.theories.DataPoints;
58  import org.junit.experimental.theories.Theories;
59  import org.junit.runner.RunWith;
60  
61  @RunWith(Theories.class)
62  public class MergeAlgorithmTest {
63  	MergeFormatter fmt=new MergeFormatter();
64  
65  	private final boolean newlineAtEnd;
66  
67  	@DataPoints
68  	public static boolean[] newlineAtEndDataPoints = { false, true };
69  
70  	public MergeAlgorithmTest(boolean newlineAtEnd) {
71  		this.newlineAtEnd = newlineAtEnd;
72  	}
73  
74  	/**
75  	 * Check for a conflict where the second text was changed similar to the
76  	 * first one, but the second texts modification covers one more line.
77  	 *
78  	 * @throws IOException
79  	 */
80  	@Test
81  	public void testTwoConflictingModifications() throws IOException {
82  		assertEquals(t("a<b=Z>Zdefghij"),
83  				merge("abcdefghij", "abZdefghij", "aZZdefghij"));
84  	}
85  
86  	/**
87  	 * Test a case where we have three consecutive chunks. The first text
88  	 * modifies all three chunks. The second text modifies the first and the
89  	 * last chunk. This should be reported as one conflicting region.
90  	 *
91  	 * @throws IOException
92  	 */
93  	@Test
94  	public void testOneAgainstTwoConflictingModifications() throws IOException {
95  		assertEquals(t("aZ<Z=c>Zefghij"),
96  				merge("abcdefghij", "aZZZefghij", "aZcZefghij"));
97  	}
98  
99  	/**
100 	 * Test a merge where only the second text contains modifications. Expect as
101 	 * merge result the second text.
102 	 *
103 	 * @throws IOException
104 	 */
105 	@Test
106 	public void testNoAgainstOneModification() throws IOException {
107 		assertEquals(t("aZcZefghij"),
108 				merge("abcdefghij", "abcdefghij", "aZcZefghij"));
109 	}
110 
111 	/**
112 	 * Both texts contain modifications but not on the same chunks. Expect a
113 	 * non-conflict merge result.
114 	 *
115 	 * @throws IOException
116 	 */
117 	@Test
118 	public void testTwoNonConflictingModifications() throws IOException {
119 		assertEquals(t("YbZdefghij"),
120 				merge("abcdefghij", "abZdefghij", "Ybcdefghij"));
121 	}
122 
123 	/**
124 	 * Merge two complicated modifications. The merge algorithm has to extend
125 	 * and combine conflicting regions to get to the expected merge result.
126 	 *
127 	 * @throws IOException
128 	 */
129 	@Test
130 	public void testTwoComplicatedModifications() throws IOException {
131 		assertEquals(t("a<ZZZZfZhZj=bYdYYYYiY>"),
132 				merge("abcdefghij", "aZZZZfZhZj", "abYdYYYYiY"));
133 	}
134 
135 	/**
136 	 * Merge two modifications with a shared delete at the end. The underlying
137 	 * diff algorithm has to provide consistent edit results to get the expected
138 	 * merge result.
139 	 *
140 	 * @throws IOException
141 	 */
142 	@Test
143 	public void testTwoModificationsWithSharedDelete() throws IOException {
144 		assertEquals(t("Cb}n}"),
145 				merge("ab}n}n}", "ab}n}", "Cb}n}"));
146 	}
147 
148 	/**
149 	 * Merge modifications with a shared insert in the middle. The
150 	 * underlying diff algorithm has to provide consistent edit
151 	 * results to get the expected merge result.
152 	 *
153 	 * @throws IOException
154 	 */
155 	@Test
156 	public void testModificationsWithMiddleInsert() throws IOException {
157 		assertEquals(t("aBcd123123uvwxPq"),
158 				merge("abcd123uvwxpq", "aBcd123123uvwxPq", "abcd123123uvwxpq"));
159 	}
160 
161 	/**
162 	 * Merge modifications with a shared delete in the middle. The
163 	 * underlying diff algorithm has to provide consistent edit
164 	 * results to get the expected merge result.
165 	 *
166 	 * @throws IOException
167 	 */
168 	@Test
169 	public void testModificationsWithMiddleDelete() throws IOException {
170 		assertEquals(t("Abz}z123Q"),
171 				merge("abz}z}z123q", "Abz}z123Q", "abz}z123q"));
172 	}
173 
174 	/**
175 	 * Test a conflicting region at the very start of the text.
176 	 *
177 	 * @throws IOException
178 	 */
179 	@Test
180 	public void testConflictAtStart() throws IOException {
181 		assertEquals(t("<Z=Y>bcdefghij"),
182 				merge("abcdefghij", "Zbcdefghij", "Ybcdefghij"));
183 	}
184 
185 	/**
186 	 * Test a conflicting region at the very end of the text.
187 	 *
188 	 * @throws IOException
189 	 */
190 	@Test
191 	public void testConflictAtEnd() throws IOException {
192 		assertEquals(t("abcdefghi<Z=Y>"),
193 				merge("abcdefghij", "abcdefghiZ", "abcdefghiY"));
194 	}
195 
196 	/**
197 	 * Check for a conflict where the second text was changed similar to the
198 	 * first one, but the second texts modification covers one more line.
199 	 *
200 	 * @throws IOException
201 	 */
202 	@Test
203 	public void testSameModification() throws IOException {
204 		assertEquals(t("abZdefghij"),
205 				merge("abcdefghij", "abZdefghij", "abZdefghij"));
206 	}
207 
208 	/**
209 	 * Check that a deleted vs. a modified line shows up as conflict (see Bug
210 	 * 328551)
211 	 *
212 	 * @throws IOException
213 	 */
214 	@Test
215 	public void testDeleteVsModify() throws IOException {
216 		assertEquals(t("ab<=Z>defghij"),
217 				merge("abcdefghij", "abdefghij", "abZdefghij"));
218 	}
219 
220 	@Test
221 	public void testInsertVsModify() throws IOException {
222 		assertEquals(t("a<bZ=XY>"), merge("ab", "abZ", "aXY"));
223 	}
224 
225 	@Test
226 	public void testAdjacentModifications() throws IOException {
227 		assertEquals(t("a<Zc=bY>d"), merge("abcd", "aZcd", "abYd"));
228 	}
229 
230 	@Test
231 	public void testSeparateModifications() throws IOException {
232 		assertEquals(t("aZcYe"), merge("abcde", "aZcde", "abcYe"));
233 	}
234 
235 	@Test
236 	public void testBlankLines() throws IOException {
237 		assertEquals(t("aZc\nYe"), merge("abc\nde", "aZc\nde", "abc\nYe"));
238 	}
239 
240 	/**
241 	 * Test merging two contents which do one similar modification and one
242 	 * insertion is only done by one side, in the middle. Between modification
243 	 * and insertion is a block which is common between the two contents and the
244 	 * common base
245 	 *
246 	 * @throws IOException
247 	 */
248 	@Test
249 	public void testTwoSimilarModsAndOneInsert() throws IOException {
250 		assertEquals(t("aBcDde"), merge("abcde", "aBcde", "aBcDde"));
251 		assertEquals(t("IAAAJCAB"), merge("iACAB", "IACAB", "IAAAJCAB"));
252 		assertEquals(t("HIAAAJCAB"), merge("HiACAB", "HIACAB", "HIAAAJCAB"));
253 		assertEquals(t("AGADEFHIAAAJCAB"),
254 				merge("AGADEFHiACAB", "AGADEFHIACAB", "AGADEFHIAAAJCAB"));
255 	}
256 
257 	/**
258 	 * Test merging two contents which do one similar modification and one
259 	 * insertion is only done by one side, at the end. Between modification and
260 	 * insertion is a block which is common between the two contents and the
261 	 * common base
262 	 *
263 	 * @throws IOException
264 	 */
265 	@Test
266 	public void testTwoSimilarModsAndOneInsertAtEnd() throws IOException {
267 		Assume.assumeTrue(newlineAtEnd);
268 		assertEquals(t("IAAJ"), merge("iA", "IA", "IAAJ"));
269 		assertEquals(t("IAJ"), merge("iA", "IA", "IAJ"));
270 		assertEquals(t("IAAAJ"), merge("iA", "IA", "IAAAJ"));
271 	}
272 
273 	@Test
274 	public void testTwoSimilarModsAndOneInsertAtEndNoNewlineAtEnd()
275 			throws IOException {
276 		Assume.assumeFalse(newlineAtEnd);
277 		assertEquals(t("I<A=AAJ>"), merge("iA", "IA", "IAAJ"));
278 		assertEquals(t("I<A=AJ>"), merge("iA", "IA", "IAJ"));
279 		assertEquals(t("I<A=AAAJ>"), merge("iA", "IA", "IAAAJ"));
280 	}
281 
282 	/**
283 	 * Test situations where (at least) one input value is the empty text
284 	 *
285 	 * @throws IOException
286 	 */
287 	@Test
288 	public void testEmptyTexts() throws IOException {
289 		// test modification against deletion
290 		assertEquals(t("<AB=>"), merge("A", "AB", ""));
291 		assertEquals(t("<=AB>"), merge("A", "", "AB"));
292 
293 		// test unmodified against deletion
294 		assertEquals(t(""), merge("AB", "AB", ""));
295 		assertEquals(t(""), merge("AB", "", "AB"));
296 
297 		// test deletion against deletion
298 		assertEquals(t(""), merge("AB", "", ""));
299 	}
300 
301 	private String merge(String commonBase, String ours, String theirs) throws IOException {
302 		MergeResult r = new MergeAlgorithm().merge(RawTextComparator.DEFAULT,
303 				T(commonBase), T(ours), T(theirs));
304 		ByteArrayOutputStream bo=new ByteArrayOutputStream(50);
305 		fmt.formatMerge(bo, r, "B", "O", "T", UTF_8);
306 		return new String(bo.toByteArray(), UTF_8);
307 	}
308 
309 	public String t(String text) {
310 		StringBuilder r = new StringBuilder();
311 		for (int i = 0; i < text.length(); i++) {
312 			char c = text.charAt(i);
313 			switch (c) {
314 			case '<':
315 				r.append("<<<<<<< O\n");
316 				break;
317 			case '=':
318 				r.append("=======\n");
319 				break;
320 			case '>':
321 				r.append(">>>>>>> T\n");
322 				break;
323 			default:
324 				r.append(c);
325 				if (newlineAtEnd || i < text.length() - 1)
326 					r.append('\n');
327 			}
328 		}
329 		return r.toString();
330 	}
331 
332 	public RawText T(String text) {
333 		return new RawText(Constants.encode(t(text)));
334 	}
335 }