1 19 20 package org.netbeans.modules.diff.builtin.provider; 21 22 import java.io.BufferedReader ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 import org.netbeans.api.diff.Difference; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.spi.diff.DiffProvider; 30 31 36 public class BuiltInDiffProviderTest extends NbTestCase { 37 38 private static final String [] SIMPLE1 = { 39 "Hi", 40 "there!", 41 " ", 42 "Oops,", 43 "end." 44 }; 45 46 47 public BuiltInDiffProviderTest(String name) { 48 super(name); 49 } 50 51 private static DiffProvider createDiffProvider() { 52 BuiltInDiffProvider provider = new BuiltInDiffProvider(); 53 provider.setTrimLines(false); 54 return provider; 55 } 58 59 public void testSimpleAdd() throws Exception { 61 DiffProvider bdp = createDiffProvider(); 62 String s1 = linesToString(SIMPLE1); 63 String [] simple2add = new String [SIMPLE1.length + 1]; 64 String 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 s2 = linesToString(simple2add); 76 Difference[] diff = bdp.computeDiff(new StringReader (s1), 77 new StringReader (s2)); 78 assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length); 79 String 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 public void testSimpleDelete() throws Exception { 86 DiffProvider bdp = createDiffProvider(); 87 String s1 = linesToString(SIMPLE1); 88 String [] simple2delete = new String [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 s2 = linesToString(simple2delete); 98 Difference[] diff = bdp.computeDiff(new StringReader (s1), 99 new StringReader (s2)); 100 assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length); 101 String 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 public void testSimpleChange() throws Exception { 108 DiffProvider bdp = createDiffProvider(); 109 String s1 = linesToString(SIMPLE1); 110 String [] simple2delete = new String [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 s2 = linesToString(simple2delete); 122 Difference[] diff = bdp.computeDiff(new StringReader (s1), 123 new StringReader (s2)); 124 assertEquals("WAS COMPARING:\n"+s1+"WITH:\n"+s2, 1, diff.length); 125 String 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 { 131 BufferedReader r1 = new BufferedReader (new InputStreamReader ( 134 BuiltInDiffProviderTest.class.getResourceAsStream( 135 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile1a.txt") 136 )); 137 BufferedReader r2 = new BufferedReader (new InputStreamReader ( 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 differences = new BufferedReader (new InputStreamReader ( 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 { 151 BufferedReader r1 = new BufferedReader (new InputStreamReader ( 154 BuiltInDiffProviderTest.class.getResourceAsStream( 155 "/org/netbeans/modules/diff/builtin/provider/DiffTestFile2a.txt") 156 )); 157 BufferedReader r2 = new BufferedReader (new InputStreamReader ( 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 differences = new BufferedReader (new InputStreamReader ( 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 linesToString(String [] lines) { 171 String newline = System.getProperty("line.separator"); 172 StringBuffer sb1 = new StringBuffer (); 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 differences) throws IOException { 181 int i = 0; 182 String diffLine; 183 184 int type = 0; 185 int from1 = 0; 186 int end1 = 0; 187 int from2 = 0; 188 int end2 = 0; 189 String text1 = ""; 190 String 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 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 i++; 269 return i == diffs.length; 270 } 271 } 272 | Popular Tags |