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