001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.diff.builtin.provider;
043:
044: import java.io.BufferedReader;
045: import java.io.IOException;
046: import java.io.InputStreamReader;
047: import java.io.Reader;
048: import java.io.StringReader;
049: import org.netbeans.api.diff.Difference;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.spi.diff.DiffProvider;
052:
053: /**
054: * The test of the built-in diff provider
055: *
056: * @author Martin Entlicher
057: */
058: public class BuiltInDiffProviderTest extends NbTestCase {
059:
060: private static final String[] SIMPLE1 = { "Hi", "there!", " ",
061: "Oops,", "end." };
062:
063: /** Creates a new instance of BuiltInDiffProviderTest */
064: public BuiltInDiffProviderTest(String name) {
065: super (name);
066: }
067:
068: private static DiffProvider createDiffProvider() {
069: BuiltInDiffProvider provider = new BuiltInDiffProvider();
070: provider.setTrimLines(false);
071: return provider;
072: // Use CmdlineDiffProvider as a reference to check the test is O.K.
073: //return org.netbeans.modules.diff.cmdline.CmdlineDiffProvider.createDefault();
074: }
075:
076: // A simple ADD difference
077: public void testSimpleAdd() throws Exception {
078: DiffProvider bdp = createDiffProvider();
079: String s1 = linesToString(SIMPLE1);
080: String[] simple2add = new String[SIMPLE1.length + 1];
081: String added = "Added Line";
082: for (int i = 0; i <= SIMPLE1.length; i++) {
083: for (int j = 0; j < simple2add.length; j++) {
084: if (i == j) {
085: simple2add[j] = added;
086: } else if (j < i) {
087: simple2add[j] = SIMPLE1[j];
088: } else {
089: simple2add[j] = SIMPLE1[j - 1];
090: }
091: }
092: String s2 = linesToString(simple2add);
093: Difference[] diff = bdp.computeDiff(new StringReader(s1),
094: new StringReader(s2));
095: assertEquals("WAS COMPARING:\n" + s1 + "WITH:\n" + s2, 1,
096: diff.length);
097: String rightDiff = "Difference(ADD, " + i + ", " + 0 + ", "
098: + (i + 1) + ", " + (i + 1) + ")";
099: assertEquals(diff[0].toString() + " != " + rightDiff
100: + "\nWAS COMPARING:\n" + s1 + "WITH:\n" + s2,
101: rightDiff, diff[0].toString());
102: }
103: }
104:
105: // A simple DELETE difference
106: public void testSimpleDelete() throws Exception {
107: DiffProvider bdp = createDiffProvider();
108: String s1 = linesToString(SIMPLE1);
109: String[] simple2delete = new String[SIMPLE1.length - 1];
110: for (int i = 0; i < SIMPLE1.length; i++) {
111: for (int j = 0; j < simple2delete.length; j++) {
112: if (j < i) {
113: simple2delete[j] = SIMPLE1[j];
114: } else {
115: simple2delete[j] = SIMPLE1[j + 1];
116: }
117: }
118: String s2 = linesToString(simple2delete);
119: Difference[] diff = bdp.computeDiff(new StringReader(s1),
120: new StringReader(s2));
121: assertEquals("WAS COMPARING:\n" + s1 + "WITH:\n" + s2, 1,
122: diff.length);
123: String rightDiff = "Difference(DELETE, " + (i + 1) + ", "
124: + (i + 1) + ", " + i + ", " + 0 + ")";
125: assertEquals(diff[0].toString() + " != " + rightDiff
126: + "\nWAS COMPARING:\n" + s1 + "WITH:\n" + s2,
127: rightDiff, diff[0].toString());
128: }
129: }
130:
131: // A simple CHANGE difference
132: public void testSimpleChange() throws Exception {
133: DiffProvider bdp = createDiffProvider();
134: String s1 = linesToString(SIMPLE1);
135: String[] simple2delete = new String[SIMPLE1.length];
136: for (int i = 0; i < SIMPLE1.length; i++) {
137: for (int j = 0; j < simple2delete.length; j++) {
138: if (i == j) {
139: simple2delete[j] = "Changed Line";
140: } else if (j < i) {
141: simple2delete[j] = SIMPLE1[j];
142: } else {
143: simple2delete[j] = SIMPLE1[j];
144: }
145: }
146: String s2 = linesToString(simple2delete);
147: Difference[] diff = bdp.computeDiff(new StringReader(s1),
148: new StringReader(s2));
149: assertEquals("WAS COMPARING:\n" + s1 + "WITH:\n" + s2, 1,
150: diff.length);
151: String rightDiff = "Difference(CHANGE, " + (i + 1) + ", "
152: + (i + 1) + ", " + (i + 1) + ", " + (i + 1) + ")";
153: assertEquals(diff[0].toString() + " != " + rightDiff
154: + "\nWAS COMPARING:\n" + s1 + "WITH:\n" + s2,
155: rightDiff, diff[0].toString());
156: }
157: }
158:
159: public void testFile1() throws Exception {
160: //System.out.println("Stream = "+BuiltInDiffProviderTest.class.getResourceAsStream(
161: // "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1a.txt"));
162: BufferedReader r1 = new BufferedReader(
163: new InputStreamReader(
164: BuiltInDiffProviderTest.class
165: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile1a.txt")));
166: BufferedReader r2 = new BufferedReader(
167: new InputStreamReader(
168: BuiltInDiffProviderTest.class
169: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile1b.txt")));
170: DiffProvider bdp = createDiffProvider();
171: Difference[] diff = bdp.computeDiff(r1, r2);
172: BufferedReader differences = new BufferedReader(
173: new InputStreamReader(
174: BuiltInDiffProviderTest.class
175: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile1d.txt")));
176: assertTrue(checkDifferences(diff, differences));
177: }
178:
179: public void testFile2() throws Exception {
180: //System.out.println("Stream = "+BuiltInDiffProviderTest.class.getResourceAsStream(
181: // "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2a.txt"));
182: BufferedReader r1 = new BufferedReader(
183: new InputStreamReader(
184: BuiltInDiffProviderTest.class
185: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile2a.txt")));
186: BufferedReader r2 = new BufferedReader(
187: new InputStreamReader(
188: BuiltInDiffProviderTest.class
189: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile2b.txt")));
190: DiffProvider bdp = createDiffProvider();
191: Difference[] diff = bdp.computeDiff(r1, r2);
192: BufferedReader differences = new BufferedReader(
193: new InputStreamReader(
194: BuiltInDiffProviderTest.class
195: .getResourceAsStream("/org/netbeans/modules/diff/builtin/provider/DiffTestFile2d.txt")));
196: assertTrue(checkDifferences(diff, differences));
197: }
198:
199: private static String linesToString(String[] lines) {
200: String newline = System.getProperty("line.separator");
201: StringBuffer sb1 = new StringBuffer();
202: for (int i = 0; i < lines.length; i++) {
203: sb1.append(lines[i]);
204: sb1.append(newline);
205: }
206: return sb1.toString();
207: }
208:
209: private static boolean checkDifferences(Difference[] diffs,
210: BufferedReader differences) throws IOException {
211: int i = 0;
212: String diffLine;
213:
214: int type = 0;
215: int from1 = 0;
216: int end1 = 0;
217: int from2 = 0;
218: int end2 = 0;
219: String text1 = "";
220: String text2 = "";
221: while ((diffLine = differences.readLine()) != null) {
222: if ("---".equals(diffLine))
223: continue;
224: if (diffLine.startsWith(">")) {
225: text2 += diffLine.substring(2)
226: + System.getProperty("line.separator");
227: continue;
228: } else if (diffLine.startsWith("<")) {
229: text1 += diffLine.substring(2)
230: + System.getProperty("line.separator");
231: continue;
232: } else {
233: if (from1 != 0 || from2 != 0) {
234: if (text1.length() == 0)
235: text1 = null;
236: if (text2.length() == 0)
237: text2 = null;
238: assertEquals("Type of difference " + i
239: + " does not match.", type, diffs[i]
240: .getType());
241: assertEquals("First start of difference " + i
242: + " does not match.", from1, diffs[i]
243: .getFirstStart());
244: assertEquals("First end of difference " + i
245: + " does not match.", end1, diffs[i]
246: .getFirstEnd());
247: assertEquals("Second start of difference " + i
248: + " does not match.", from2, diffs[i]
249: .getSecondStart());
250: assertEquals("Second end of difference " + i
251: + " does not match.", end2, diffs[i]
252: .getSecondEnd());
253: //assertEquals("First text of difference "+i+" does not match\nExpected:\n"+text1+"\nWas:\n"+diffs[i].getFirstText()+"\n", text1, diffs[i].getFirstText());
254: //assertEquals("Second text of difference "+i+" does not match\nExpected:\n"+text2+"\nWas:\n"+diffs[i].getSecondText()+"\n", text2, diffs[i].getSecondText());
255: i++;
256: from1 = from2 = end1 = end2 = 0;
257: text1 = text2 = "";
258: }
259: }
260: char c = '\0';
261: int index = 0;
262: while (index < diffLine.length()
263: && Character.isDigit(c = diffLine.charAt(index))) {
264: index++;
265: from1 = 10 * from1 + Character.digit(c, 10);
266: }
267: if (c == ',') {
268: index++;
269: while (index < diffLine.length()
270: && Character
271: .isDigit(c = diffLine.charAt(index))) {
272: index++;
273: end1 = 10 * end1 + Character.digit(c, 10);
274: }
275: } else {
276: end1 = from1;
277: }
278: if (c == 'a') {
279: type = Difference.ADD;
280: } else if (c == 'd') {
281: type = Difference.DELETE;
282: } else if (c == 'c') {
283: type = Difference.CHANGE;
284: } else {
285: fail("Unknown change '" + c + "' read at line '"
286: + diffLine + "'");
287: }
288: index++;
289: while (index < diffLine.length()
290: && Character.isDigit(c = diffLine.charAt(index))) {
291: index++;
292: from2 = 10 * from2 + Character.digit(c, 10);
293: }
294: if (c == ',') {
295: index++;
296: while (index < diffLine.length()
297: && Character
298: .isDigit(c = diffLine.charAt(index))) {
299: index++;
300: end2 = 10 * end2 + Character.digit(c, 10);
301: }
302: } else {
303: end2 = from2;
304: }
305: if (type == Difference.ADD) {
306: end1 = 0;
307: } else if (type == Difference.DELETE) {
308: end2 = 0;
309: }
310: }
311: if (text1.length() == 0)
312: text1 = null;
313: if (text2.length() == 0)
314: text2 = null;
315: assertEquals("Type of difference " + i + " does not match.",
316: type, diffs[i].getType());
317: assertEquals("First start of difference " + i
318: + " does not match.", from1, diffs[i].getFirstStart());
319: assertEquals("First end of difference " + i
320: + " does not match.", end1, diffs[i].getFirstEnd());
321: assertEquals("Second start of difference " + i
322: + " does not match.", from2, diffs[i].getSecondStart());
323: assertEquals("Second end of difference " + i
324: + " does not match.", end2, diffs[i].getSecondEnd());
325: //assertEquals("First text of difference "+i+" does not match.\nExpected:\n"+text1+"\nWas:\n"+diffs[i].getFirstText()+"\n", text1, diffs[i].getFirstText());
326: //assertEquals("Second text of difference "+i+" does not match\nExpected:\n"+text2+"\nWas:\n"+diffs[i].getSecondText()+"\n", text2, diffs[i].getSecondText());
327: i++;
328: return i == diffs.length;
329: }
330: }
|