View Javadoc
1   /*
2    * Copyright (C) 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.diff;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.UnsupportedEncodingException;
50  
51  import org.junit.Test;
52  
53  public abstract class AbstractDiffTestCase {
54  	@Test
55  	public void testEmptyInputs() {
56  		EditList r = diff(t(""), t(""));
57  		assertTrue("is empty", r.isEmpty());
58  	}
59  
60  	@Test
61  	public void testCreateFile() {
62  		EditList r = diff(t(""), t("AB"));
63  		assertEquals(1, r.size());
64  		assertEquals(new Edit(0, 0, 0, 2), r.get(0));
65  	}
66  
67  	@Test
68  	public void testDeleteFile() {
69  		EditList r = diff(t("AB"), t(""));
70  		assertEquals(1, r.size());
71  		assertEquals(new Edit(0, 2, 0, 0), r.get(0));
72  	}
73  
74  	@Test
75  	public void testDegenerate_InsertMiddle() {
76  		EditList r = diff(t("ac"), t("aBc"));
77  		assertEquals(1, r.size());
78  		assertEquals(new Edit(1, 1, 1, 2), r.get(0));
79  	}
80  
81  	@Test
82  	public void testDegenerate_DeleteMiddle() {
83  		EditList r = diff(t("aBc"), t("ac"));
84  		assertEquals(1, r.size());
85  		assertEquals(new Edit(1, 2, 1, 1), r.get(0));
86  	}
87  
88  	@Test
89  	public void testDegenerate_ReplaceMiddle() {
90  		EditList r = diff(t("bCd"), t("bEd"));
91  		assertEquals(1, r.size());
92  		assertEquals(new Edit(1, 2, 1, 2), r.get(0));
93  	}
94  
95  	@Test
96  	public void testDegenerate_InsertsIntoMidPosition() {
97  		EditList r = diff(t("aaaa"), t("aaXaa"));
98  		assertEquals(1, r.size());
99  		assertEquals(new Edit(2, 2, 2, 3), r.get(0));
100 	}
101 
102 	@Test
103 	public void testDegenerate_InsertStart() {
104 		EditList r = diff(t("bc"), t("Abc"));
105 		assertEquals(1, r.size());
106 		assertEquals(new Edit(0, 0, 0, 1), r.get(0));
107 	}
108 
109 	@Test
110 	public void testDegenerate_DeleteStart() {
111 		EditList r = diff(t("Abc"), t("bc"));
112 		assertEquals(1, r.size());
113 		assertEquals(new Edit(0, 1, 0, 0), r.get(0));
114 	}
115 
116 	@Test
117 	public void testDegenerate_InsertEnd() {
118 		EditList r = diff(t("bc"), t("bcD"));
119 		assertEquals(1, r.size());
120 		assertEquals(new Edit(2, 2, 2, 3), r.get(0));
121 	}
122 
123 	@Test
124 	public void testDegenerate_DeleteEnd() {
125 		EditList r = diff(t("bcD"), t("bc"));
126 		assertEquals(1, r.size());
127 		assertEquals(new Edit(2, 3, 2, 2), r.get(0));
128 	}
129 
130 	@Test
131 	public void testEdit_ReplaceCommonDelete() {
132 		EditList r = diff(t("RbC"), t("Sb"));
133 		assertEquals(2, r.size());
134 		assertEquals(new Edit(0, 1, 0, 1), r.get(0));
135 		assertEquals(new Edit(2, 3, 2, 2), r.get(1));
136 	}
137 
138 	@Test
139 	public void testEdit_CommonReplaceCommonDeleteCommon() {
140 		EditList r = diff(t("aRbCd"), t("aSbd"));
141 		assertEquals(2, r.size());
142 		assertEquals(new Edit(1, 2, 1, 2), r.get(0));
143 		assertEquals(new Edit(3, 4, 3, 3), r.get(1));
144 	}
145 
146 	@Test
147 	public void testEdit_MoveBlock() {
148 		EditList r = diff(t("aYYbcdz"), t("abcdYYz"));
149 		assertEquals(2, r.size());
150 		assertEquals(new Edit(1, 3, 1, 1), r.get(0));
151 		assertEquals(new Edit(6, 6, 4, 6), r.get(1));
152 	}
153 
154 	@Test
155 	public void testEdit_InvertBlocks() {
156 		EditList r = diff(t("aYYbcdXXz"), t("aXXbcdYYz"));
157 		assertEquals(2, r.size());
158 		assertEquals(new Edit(1, 3, 1, 3), r.get(0));
159 		assertEquals(new Edit(6, 8, 6, 8), r.get(1));
160 	}
161 
162 	@Test
163 	public void testEdit_UniqueCommonLargerThanMatchPoint() {
164 		// We are testing 3 unique common matches, but two of
165 		// them are consumed as part of the 1st's LCS region.
166 		EditList r = diff(t("AbdeZ"), t("PbdeQR"));
167 		assertEquals(2, r.size());
168 		assertEquals(new Edit(0, 1, 0, 1), r.get(0));
169 		assertEquals(new Edit(4, 5, 4, 6), r.get(1));
170 	}
171 
172 	@Test
173 	public void testEdit_CommonGrowsPrefixAndSuffix() {
174 		// Here there is only one common unique point, but we can grow it
175 		// in both directions to find the LCS in the middle.
176 		EditList r = diff(t("AaabccZ"), t("PaabccR"));
177 		assertEquals(2, r.size());
178 		assertEquals(new Edit(0, 1, 0, 1), r.get(0));
179 		assertEquals(new Edit(6, 7, 6, 7), r.get(1));
180 	}
181 
182 	@Test
183 	public void testEdit_DuplicateAButCommonUniqueInB() {
184 		EditList r = diff(t("AbbcR"), t("CbcS"));
185 		assertEquals(2, r.size());
186 		assertEquals(new Edit(0, 2, 0, 1), r.get(0));
187 		assertEquals(new Edit(4, 5, 3, 4), r.get(1));
188 	}
189 
190 	@Test
191 	public void testEdit_InsertNearCommonTail() {
192 		EditList r = diff(t("aq}nb"), t("aCq}nD}nb"));
193 		assertEquals(new Edit(1, 1, 1, 2), r.get(0));
194 		assertEquals(new Edit(4, 4, 5, 8), r.get(1));
195 		assertEquals(2, r.size());
196 	}
197 
198 	@Test
199 	public void testEdit_DeleteNearCommonTail() {
200 		EditList r = diff(t("aCq}nD}nb"), t("aq}nb"));
201 		assertEquals(new Edit(1, 2, 1, 1), r.get(0));
202 		assertEquals(new Edit(5, 8, 4, 4), r.get(1));
203 		assertEquals(2, r.size());
204 	}
205 
206 	@Test
207 	public void testEdit_DeleteNearCommonCenter() {
208 		EditList r = diff(t("abcd123123uvwxpq"), t("aBcd123uvwxPq"));
209 		assertEquals(new Edit(1, 2, 1, 2), r.get(0));
210 		assertEquals(new Edit(7, 10, 7, 7), r.get(1));
211 		assertEquals(new Edit(14, 15, 11, 12), r.get(2));
212 		assertEquals(3, r.size());
213 	}
214 
215 	@Test
216 	public void testEdit_InsertNearCommonCenter() {
217 		EditList r = diff(t("aBcd123uvwxPq"), t("abcd123123uvwxpq"));
218 		assertEquals(new Edit(1, 2, 1, 2), r.get(0));
219 		assertEquals(new Edit(7, 7, 7, 10), r.get(1));
220 		assertEquals(new Edit(11, 12, 14, 15), r.get(2));
221 		assertEquals(3, r.size());
222 	}
223 
224 	@Test
225 	public void testEdit_LinuxBug() {
226 		EditList r = diff(t("a{bcdE}z"), t("a{0bcdEE}z"));
227 		assertEquals(new Edit(2, 2, 2, 3), r.get(0));
228 		assertEquals(new Edit(6, 6, 7, 8), r.get(1));
229 		assertEquals(2, r.size());
230 	}
231 
232 	public EditList diff(RawText a, RawText b) {
233 		return algorithm().diff(RawTextComparator.DEFAULT, a, b);
234 	}
235 
236 	protected abstract DiffAlgorithm algorithm();
237 
238 	public static RawText t(String text) {
239 		StringBuilder r = new StringBuilder();
240 		for (int i = 0; i < text.length(); i++) {
241 			r.append(text.charAt(i));
242 			r.append('\n');
243 		}
244 		try {
245 			return new RawText(r.toString().getBytes("UTF-8"));
246 		} catch (UnsupportedEncodingException e) {
247 			throw new RuntimeException(e);
248 		}
249 	}
250 }