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