KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > builtin > provider > BuiltInDiffProviderTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.diff.builtin.provider;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.Reader JavaDoc;
26 import java.io.StringReader JavaDoc;
27 import org.netbeans.api.diff.Difference;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.spi.diff.DiffProvider;
30
31 /**
32  * The test of the built-in diff provider
33  *
34  * @author Martin Entlicher
35  */

36 public class BuiltInDiffProviderTest extends NbTestCase {
37
38     private static final String JavaDoc[] SIMPLE1 = {
39         "Hi",
40         "there!",
41         " ",
42         "Oops,",
43         "end."
44     };
45     
46     /** Creates a new instance of BuiltInDiffProviderTest */
47     public BuiltInDiffProviderTest(String JavaDoc name) {
48         super(name);
49     }
50     
51     private static DiffProvider createDiffProvider() {
52         BuiltInDiffProvider provider = new BuiltInDiffProvider();
53         provider.setTrimLines(false);
54         return provider;
55         // Use CmdlineDiffProvider as a reference to check the test is O.K.
56
//return org.netbeans.modules.diff.cmdline.CmdlineDiffProvider.createDefault();
57
}
58     
59     // A simple ADD difference
60
public void testSimpleAdd() throws Exception JavaDoc {
61         DiffProvider bdp = createDiffProvider();
62         String JavaDoc s1 = linesToString(SIMPLE1);
63         String JavaDoc[] simple2add = new String JavaDoc[SIMPLE1.length + 1];
64         String JavaDoc added = "Added Line";
65         for (int i = 0; i <= SIMPLE1.length; i++) {
66             for (int j = 0; j < simple2add.length; j++) {
67                 if (i == j) {
68                     simple2add[j] = added;
69                 } else if (j < i) {
70                     simple2add[j] = SIMPLE1[j];
71                 } else {
72                     simple2add[j] = SIMPLE1[j-1];
73                 }
74             }
75             String JavaDoc s2 = linesToString(simple2add);
76             Difference[] diff = bdp.computeDiff(new StringReader JavaDoc(s1),
77                                                 new StringReader JavaDoc(s2));
78             assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length);
79             String JavaDoc rightDiff = "Difference(ADD, "+i+", "+0+", "+(i+1)+", "+(i+1)+")";
80             assertEquals(diff[0].toString()+" != "+rightDiff+"\nWAS COMPARING:\n"+s1+"WITH:\n"+s2, rightDiff, diff[0].toString());
81         }
82     }
83     
84     // A simple DELETE difference
85
public void testSimpleDelete() throws Exception JavaDoc {
86         DiffProvider bdp = createDiffProvider();
87         String JavaDoc s1 = linesToString(SIMPLE1);
88         String JavaDoc[] simple2delete = new String JavaDoc[SIMPLE1.length - 1];
89         for (int i = 0; i < SIMPLE1.length; i++) {
90             for (int j = 0; j < simple2delete.length; j++) {
91                 if (j < i) {
92                     simple2delete[j] = SIMPLE1[j];
93                 } else {
94                     simple2delete[j] = SIMPLE1[j+1];
95                 }
96             }
97             String JavaDoc s2 = linesToString(simple2delete);
98             Difference[] diff = bdp.computeDiff(new StringReader JavaDoc(s1),
99                                                 new StringReader JavaDoc(s2));
100             assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length);
101             String JavaDoc rightDiff = "Difference(DELETE, "+(i+1)+", "+(i+1)+", "+i+", "+0+")";
102             assertEquals(diff[0].toString()+" != "+rightDiff+"\nWAS COMPARING:\n"+s1+"WITH:\n"+s2, rightDiff, diff[0].toString());
103         }
104     }
105     
106     // A simple CHANGE difference
107
public void testSimpleChange() throws Exception JavaDoc {
108         DiffProvider bdp = createDiffProvider();
109         String JavaDoc s1 = linesToString(SIMPLE1);
110         String JavaDoc[] simple2delete = new String JavaDoc[SIMPLE1.length];
111         for (int i = 0; i < SIMPLE1.length; i++) {
112             for (int j = 0; j < simple2delete.length; j++) {
113                 if (i == j) {
114                     simple2delete[j] = "Changed Line";
115                 } else if (j < i) {
116                     simple2delete[j] = SIMPLE1[j];
117                 } else {
118                     simple2delete[j] = SIMPLE1[j];
119                 }
120             }
121             String JavaDoc s2 = linesToString(simple2delete);
122             Difference[] diff = bdp.computeDiff(new StringReader JavaDoc(s1),
123                                                 new StringReader JavaDoc(s2));
124             assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length);
125             String JavaDoc rightDiff = "Difference(CHANGE, "+(i+1)+", "+(i+1)+", "+(i+1)+", "+(i+1)+")";
126             assertEquals(diff[0].toString()+" != "+rightDiff+"\nWAS COMPARING:\n"+s1+"WITH:\n"+s2, rightDiff, diff[0].toString());
127         }
128     }
129     
130     public void testFile1() throws Exception JavaDoc {
131         //System.out.println("Stream = "+BuiltInDiffProviderTest.class.getResourceAsStream(
132
// "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1a.txt"));
133
BufferedReader JavaDoc r1 = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
134             BuiltInDiffProviderTest.class.getResourceAsStream(
135                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1a.txt")
136         ));
137         BufferedReader JavaDoc r2 = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
138             BuiltInDiffProviderTest.class.getResourceAsStream(
139                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1b.txt")
140         ));
141         DiffProvider bdp = createDiffProvider();
142         Difference[] diff = bdp.computeDiff(r1, r2);
143         BufferedReader JavaDoc differences = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
144             BuiltInDiffProviderTest.class.getResourceAsStream(
145                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1d.txt")
146         ));
147         assertTrue(checkDifferences(diff, differences));
148     }
149     
150     public void testFile2() throws Exception JavaDoc {
151         //System.out.println("Stream = "+BuiltInDiffProviderTest.class.getResourceAsStream(
152
// "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2a.txt"));
153
BufferedReader JavaDoc r1 = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
154             BuiltInDiffProviderTest.class.getResourceAsStream(
155                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2a.txt")
156         ));
157         BufferedReader JavaDoc r2 = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
158             BuiltInDiffProviderTest.class.getResourceAsStream(
159                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2b.txt")
160         ));
161         DiffProvider bdp = createDiffProvider();
162         Difference[] diff = bdp.computeDiff(r1, r2);
163         BufferedReader JavaDoc differences = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
164             BuiltInDiffProviderTest.class.getResourceAsStream(
165                 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2d.txt")
166         ));
167         assertTrue(checkDifferences(diff, differences));
168     }
169     
170     private static String JavaDoc linesToString(String JavaDoc[] lines) {
171         String JavaDoc newline = System.getProperty("line.separator");
172         StringBuffer JavaDoc sb1 = new StringBuffer JavaDoc();
173         for (int i = 0; i < lines.length; i++) {
174             sb1.append(lines[i]);
175             sb1.append(newline);
176         }
177         return sb1.toString();
178     }
179     
180     private static boolean checkDifferences(Difference[] diffs, BufferedReader JavaDoc differences) throws IOException JavaDoc {
181         int i = 0;
182         String JavaDoc diffLine;
183         
184         int type = 0;
185         int from1 = 0;
186         int end1 = 0;
187         int from2 = 0;
188         int end2 = 0;
189         String JavaDoc text1 = "";
190         String JavaDoc text2 = "";
191         while ((diffLine = differences.readLine()) != null) {
192             if ("---".equals(diffLine)) continue;
193             if (diffLine.startsWith(">")) {
194                 text2 += diffLine.substring(2) + System.getProperty("line.separator");
195                 continue;
196             } else if (diffLine.startsWith("<")) {
197                 text1 += diffLine.substring(2) + System.getProperty("line.separator");
198                 continue;
199             } else {
200                 if (from1 != 0 || from2 != 0) {
201                     if (text1.length() == 0) text1 = null;
202                     if (text2.length() == 0) text2 = null;
203                     assertEquals("Type of difference "+i+" does not match.", type, diffs[i].getType());
204                     assertEquals("First start of difference "+i+" does not match.", from1, diffs[i].getFirstStart());
205                     assertEquals("First end of difference "+i+" does not match.", end1, diffs[i].getFirstEnd());
206                     assertEquals("Second start of difference "+i+" does not match.", from2, diffs[i].getSecondStart());
207                     assertEquals("Second end of difference "+i+" does not match.", end2, diffs[i].getSecondEnd());
208                     //assertEquals("First text of difference "+i+" does not match\nExpected:\n"+text1+"\nWas:\n"+diffs[i].getFirstText()+"\n", text1, diffs[i].getFirstText());
209
//assertEquals("Second text of difference "+i+" does not match\nExpected:\n"+text2+"\nWas:\n"+diffs[i].getSecondText()+"\n", text2, diffs[i].getSecondText());
210
i++;
211                     from1 = from2 = end1 = end2 = 0;
212                     text1 = text2 = "";
213                 }
214             }
215             char c = '\0';
216             int index = 0;
217             while (index < diffLine.length() && Character.isDigit(c = diffLine.charAt(index))) {
218                 index++;
219                 from1 = 10*from1 + Character.digit(c, 10);
220             }
221             if (c == ',') {
222                 index++;
223                 while (index < diffLine.length() && Character.isDigit(c = diffLine.charAt(index))) {
224                     index++;
225                     end1 = 10*end1 + Character.digit(c, 10);
226                 }
227             } else {
228                 end1 = from1;
229             }
230             if (c == 'a') {
231                 type = Difference.ADD;
232             } else if (c == 'd') {
233                 type = Difference.DELETE;
234             } else if (c == 'c') {
235                 type = Difference.CHANGE;
236             } else {
237                 fail("Unknown change '"+c+"' read at line '"+diffLine+"'");
238             }
239             index++;
240             while (index < diffLine.length() && Character.isDigit(c = diffLine.charAt(index))) {
241                 index++;
242                 from2 = 10*from2 + Character.digit(c, 10);
243             }
244             if (c == ',') {
245                 index++;
246                 while (index < diffLine.length() && Character.isDigit(c = diffLine.charAt(index))) {
247                     index++;
248                     end2 = 10*end2 + Character.digit(c, 10);
249                 }
250             } else {
251                 end2 = from2;
252             }
253             if (type == Difference.ADD) {
254                 end1 = 0;
255             } else if (type == Difference.DELETE) {
256                 end2 = 0;
257             }
258         }
259         if (text1.length() == 0) text1 = null;
260         if (text2.length() == 0) text2 = null;
261         assertEquals("Type of difference "+i+" does not match.", type, diffs[i].getType());
262         assertEquals("First start of difference "+i+" does not match.", from1, diffs[i].getFirstStart());
263         assertEquals("First end of difference "+i+" does not match.", end1, diffs[i].getFirstEnd());
264         assertEquals("Second start of difference "+i+" does not match.", from2, diffs[i].getSecondStart());
265         assertEquals("Second end of difference "+i+" does not match.", end2, diffs[i].getSecondEnd());
266         //assertEquals("First text of difference "+i+" does not match.\nExpected:\n"+text1+"\nWas:\n"+diffs[i].getFirstText()+"\n", text1, diffs[i].getFirstText());
267
//assertEquals("Second text of difference "+i+" does not match\nExpected:\n"+text2+"\nWas:\n"+diffs[i].getSecondText()+"\n", text2, diffs[i].getSecondText());
268
i++;
269         return i == diffs.length;
270     }
271 }
272
Popular Tags