1 7 8 package org.netbeans.modules.xml.xdm.diff; 9 import java.io.BufferedInputStream ; 10 import java.io.FileNotFoundException ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.util.List ; 14 import javax.swing.text.Document ; 15 import junit.framework.*; 16 import org.netbeans.modules.xml.xdm.Util; 17 import org.netbeans.modules.xml.xdm.diff.Change.AttributeChange; 18 import org.netbeans.modules.xml.xdm.diff.Change.AttributeDiff; 19 import org.netbeans.modules.xml.xdm.nodes.Attribute; 20 import org.netbeans.modules.xml.xdm.nodes.Node; 21 22 26 public class XDMUtilTest extends TestCase { 27 28 public XDMUtilTest(String testName) { 29 super(testName); 30 } 31 32 public void testPrettyPrintXML() throws Exception { 33 XDMUtil util = new XDMUtil(); 34 String indent = " "; 35 String xml = readXMLString("diff/xdu/prettyprint1_1.xml"); 36 String expected = readXMLString("diff/xdu/prettyprint1_2.xml"); 37 String changed = util.prettyPrintXML(xml, indent); 38 assertEquals("pretty print", expected, changed); 39 } 40 41 public void testPrettyPrintXML2() throws Exception { 42 XDMUtil util = new XDMUtil(); 43 String indent = " "; 44 String xml = readXMLString("diff/xdu/prettyprint2_1.xml"); 45 String expected = readXMLString("diff/xdu/prettyprint2_2.xml"); 46 String changed = util.prettyPrintXML(xml, indent); 47 assertEquals("pretty print", expected, changed); 48 } 49 50 public void testPrettyPrintAddNamespace() throws Exception { 51 XDMUtil util = new XDMUtil(); 52 String xml1 = readXMLString("diff/xdu/pp-add-ns-decl-1.xml"); 53 String xml1_pretty = util.prettyPrintXML(xml1, " "); 54 String xml2 = readXMLString("diff/xdu/pp-add-ns-decl-2.xml"); 55 assertEquals(xml2, xml1_pretty); 56 } 57 58 public void testPrettyPrintXMLNegative() throws Exception { 59 XDMUtil util = new XDMUtil(); 60 String indent = " "; 61 String expected = XDMUtil.XML_PROLOG; 62 String xml = XDMUtil.XML_PROLOG; 63 String changed = util.prettyPrintXML(xml, indent); 64 assertEquals("pretty print", expected, changed); 65 } 66 67 public void testCompareXMLEquals() throws Exception { 68 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.EQUAL; 69 String xml1 = readXMLString("diff/xdu/equals1_1.xml"); 71 String xml2 = readXMLString("diff/xdu/equals1_2.xml"); 72 List <Difference> diffs = compareXML(xml1, xml2, criteria); 73 assertEquals("pretty print", 0, diffs.size()); 74 75 xml1 = readXMLString("diff/xdu/equals1_3.xml"); 77 xml2 = readXMLString("diff/xdu/equals1_4.xml"); 78 diffs = compareXML(xml1, xml2, criteria); 79 assertEquals("pretty print size", 1, diffs.size()); 80 81 xml1 = readXMLString("diff/xdu/equals1_5.xml"); 83 xml2 = readXMLString("diff/xdu/equals1_6.xml"); 84 diffs = compareXML(xml1, xml2, criteria); 85 assertEquals("pretty print size", 1, diffs.size()); 86 assertTrue("pretty print attribute change", ((Change)diffs.get(0)).isAttributeChanged()); 87 assertEquals("pretty print attribute change size", 1, 88 ((Change)diffs.get(0)).getAttrChanges().size()); 89 } 90 91 public void testCompareXMLIdentical() throws Exception { 92 XDMUtil util = new XDMUtil(); 93 String indent = " "; 94 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 95 String xml1 = readXMLString("diff/xdu/identical1_1.xml"); 97 String xml2 = readXMLString("diff/xdu/identical1_2.xml"); 98 List <Difference> diffs = compareXML(xml1, xml2, criteria); 99 assertEquals("pretty print", 2, diffs.size()); 100 101 xml1 = readXMLString("diff/xdu/identical1_3.xml"); 103 xml2 = readXMLString("diff/xdu/identical1_4.xml"); 104 diffs = compareXML(xml1, xml2, criteria); 105 assertEquals("pretty print size", 2, diffs.size()); assertTrue("pretty print attribute change", ((Change)diffs.get(0)).isAttributeChanged()); 107 assertTrue("pretty print element pos change", ((Change)diffs.get(1)).isPositionChanged()); 108 109 List <Change.AttributeDiff> attrDiffs = ((Change)diffs.get(0)).getAttrChanges(); 110 assertEquals("pretty print attribute change size", 2, attrDiffs.size()); 111 Change.AttributeChange change1 = (Change.AttributeChange) attrDiffs.get(0); 112 assertTrue("pretty print attribute pos & token change", change1.isPositionChanged()); 113 assertTrue("pretty print attribute pos & token change", change1.isTokenChanged()); 114 Change.AttributeChange change2 = (Change.AttributeChange) attrDiffs.get(1); 115 assertTrue("pretty print attribute pos only change", change2.isPositionChanged()); 116 assertFalse("pretty print attribute no token change", change2.isTokenChanged()); 117 118 xml1 = readXMLString("diff/xdu/identical1_5.xml"); 120 xml2 = readXMLString("diff/xdu/identical1_6.xml"); 121 diffs = compareXML(xml1, xml2, criteria); 122 assertEquals("pretty print size", 2, diffs.size()); assertTrue("pretty print attribute change", ((Change)diffs.get(0)).isAttributeChanged()); 124 assertTrue("pretty print element pos change", ((Change)diffs.get(1)).isPositionChanged()); 125 126 attrDiffs = ((Change)diffs.get(0)).getAttrChanges(); 127 assertEquals("pretty print attribute change size", 3, attrDiffs.size()); 128 change1 = (Change.AttributeChange) attrDiffs.get(0); 129 assertTrue("pretty print attribute pos & token change", change1.isPositionChanged()); 130 assertTrue("pretty print attribute pos & token change", change1.isTokenChanged()); 131 change2 = (Change.AttributeChange) attrDiffs.get(1); 132 assertTrue("pretty print attribute pos only change", change2.isPositionChanged()); 133 assertFalse("pretty print attribute no token change", change2.isTokenChanged()); 134 Change.AttributeAdd add = (Change.AttributeAdd) attrDiffs.get(2); 135 assertEquals("pretty print attribute pos only change", 2, add.getNewAttributePosition()); 136 137 xml1 = readXMLString("diff/xdu/identical1_7.xml"); 139 xml2 = readXMLString("diff/xdu/identical1_8.xml"); 140 diffs = compareXML(xml1, xml2, criteria); 141 assertEquals("pretty print size", 2, diffs.size()); assertTrue("pretty print attribute change", ((Change)diffs.get(0)).isAttributeChanged()); 143 assertTrue("pretty print element pos change", ((Change)diffs.get(1)).isPositionChanged()); 144 145 attrDiffs = ((Change)diffs.get(0)).getAttrChanges(); 146 assertEquals("pretty print attribute change size", 3, attrDiffs.size()); 147 change1 = (Change.AttributeChange) attrDiffs.get(0); 148 assertTrue("pretty print attribute pos & token change", change1.isPositionChanged()); 149 assertTrue("pretty print attribute pos & token change", change1.isTokenChanged()); 150 change2 = (Change.AttributeChange) attrDiffs.get(1); 151 assertTrue("pretty print attribute pos only change", change2.isPositionChanged()); 152 assertTrue("pretty print attribute no token change", change2.isTokenChanged()); 153 add = (Change.AttributeAdd) attrDiffs.get(2); 154 assertEquals("pretty print attribute pos only change", 2, add.getNewAttributePosition()); 155 } 156 157 public void testCompareXMLIdentical2() throws Exception { 158 XDMUtil util = new XDMUtil(); 159 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 160 String xml1 = readXMLString("diff/xdu/identical2_1.xml"); 162 String xml2 = readXMLString("diff/xdu/identical2_2.xml"); 163 List <Difference> diffs = compareXML(xml1, xml2, criteria); 164 assertEquals("compare identical XML", 0, diffs.size()); 165 } 166 167 170 public void testComparePrefix() throws Exception { 171 String xml1 = readXMLString("diff/xdu/pfx1_1.xml"); 172 String xml2 = readXMLString("diff/xdu/pfx1_2.xml"); 173 174 XDMUtil util = new XDMUtil(); 175 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 176 177 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 178 assertEquals("testComparePrefix is equal?", 0, diffs.size()); 179 } 180 181 182 185 public void testCompareExtraNamespace() throws Exception { 186 String xml1 = readXMLString("diff/xdu/extrans1_1.xml"); 187 String xml2 = readXMLString("diff/xdu/extrans1_2.xml"); 188 189 XDMUtil util = new XDMUtil(); 190 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 191 192 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 193 assertEquals("testCompareExtraNamespace is equal?", 0, diffs.size()); 194 } 195 196 197 199 public void testCompareWhitespaceOutofElement_SchemaLoc() throws Exception { 200 String xml1 = readXMLString("diff/xdu/wsschemaloc1_1.xml"); 201 String xml2 = readXMLString("diff/xdu/wsschemaloc1_2.xml"); 202 203 XDMUtil util = new XDMUtil(); 204 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 205 206 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 207 assertEquals("testCompareWhitespaceOutofElement_SchemaLoc?", 0, diffs.size()); 208 } 209 210 211 213 public void testCompareWhitespaceOutofElement_NoSchemaLoc() throws Exception { 214 String xml1 = readXMLString("diff/xdu/wsnoschemaloc1_1.xml"); 215 String xml2 = readXMLString("diff/xdu/wsnoschemaloc1_2.xml"); 216 217 XDMUtil util = new XDMUtil(); 218 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 219 220 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 221 assertEquals("testCompareWhitespaceOutofElement_NoSchemaLoc?", 0, diffs.size()); 222 } 223 224 227 public void testCompareExtraWhiteSpaceBetweenAttr() throws Exception { 228 String xml1 = readXMLString("diff/xdu/extrawsattr1_1.xml"); 229 String xml2 = readXMLString("diff/xdu/extrawsattr1_2.xml"); 230 231 XDMUtil util = new XDMUtil(); 232 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 233 234 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 235 assertEquals("testCompareExtraWhiteSpaceBetweenAttr is equal?", 0, diffs.size()); 236 } 237 238 241 public void testCompareXMLWSchemaLocation() throws Exception { 242 String xml1 = readXMLString("diff/xdu/schemalocation1_1.xml"); 243 String xml2 = readXMLString("diff/xdu/schemalocation1_2.xml"); 244 245 XDMUtil util = new XDMUtil(); 246 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 247 248 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 249 assertEquals("testCompareXMLWSchemaLocation is equal?", 0, diffs.size()); 250 } 251 252 255 public void testCompareXMLSamePrefixDifferentURL() throws Exception { 256 String xml1 = readXMLString("diff/xdu/samepfx1_1.xml"); 257 String xml2 = readXMLString("diff/xdu/samepfx1_2.xml"); 258 259 XDMUtil util = new XDMUtil(); 260 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 261 262 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 263 assertEquals("testComparePrefix is equal?", 2, diffs.size()); 264 } 265 266 269 public void testCompareXMLSamePrefixDifferentURL2() throws Exception { 270 String xml1 = readXMLString("diff/xdu/samepfx2_1.xml"); 271 String xml2 = readXMLString("diff/xdu/samepfx2_2.xml"); 272 273 XDMUtil util = new XDMUtil(); 274 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 275 276 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 277 assertEquals("testComparePrefix is equal?", 2, diffs.size()); 278 } 279 280 283 public void testCompareXMLDifferentPrefixSameURL() throws Exception { 284 String xml1 = readXMLString("diff/xdu/difpfx1_1.xml"); 285 String xml2 = readXMLString("diff/xdu/difpfx1_2.xml"); 286 287 XDMUtil util = new XDMUtil(); 288 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 289 290 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 291 assertEquals("testComparePrefix is equal?", 0, diffs.size()); 292 } 293 294 297 public void testCompareXMLWithWhitespace() throws Exception { 298 String xml1 = readXMLString("diff/xdu/textchange2_1.xml"); 299 String xml2 = readXMLString("diff/xdu/whitespace.xml"); 300 301 XDMUtil util = new XDMUtil(); 302 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 303 304 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 305 assertEquals("testCompareXMLWSchemaLocation is equal?", 0, diffs.size()); 306 } 307 308 311 public void testCompareXMLWithTextChange() throws Exception { 312 String xml1 = readXMLString("diff/xdu/textchange2_1.xml"); 313 String xml2 = readXMLString("diff/xdu/textchange2_2.xml"); 314 315 XDMUtil util = new XDMUtil(); 316 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 317 318 List <Difference> diffs = compareXML(xml1, xml2 , criteria); 319 assertEquals("testCompareXMLWSchemaLocation is equal?", 1, diffs.size()); 320 } 321 322 public void testFilterAttributeOrderChange() throws Exception { 323 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 324 String xml1 = readXMLString("diff/xdu/attrorder1_1.xml"); 326 String xml2 = readXMLString("diff/xdu/attrorder1_2.xml"); 327 List <Difference> diffs = compareXML(xml1, xml2, criteria); 328 assertEquals("attr order & token change", 1, diffs.size()); 329 assertEquals("attr order & token change", 2, 330 ((Change)diffs.get(0)).getAttrChanges().size()); XDMUtil.filterAttributeOrderChange(diffs); assertEquals("attr order & token change", 0, diffs.size()); 333 334 xml1 = readXMLString("diff/xdu/attrorder2_1.xml"); 336 xml2 = readXMLString("diff/xdu/attrorder2_2.xml"); 337 diffs = compareXML(xml1, xml2, criteria); 338 assertEquals("attr order & token change", 1, diffs.size()); 339 assertEquals("attr order & token change", 2, 340 ((Change)diffs.get(0)).getAttrChanges().size()); 341 XDMUtil.filterAttributeOrderChange(diffs); assertEquals("attr order & token change", 1, diffs.size()); 343 assertEquals("attr token change", 1, 344 ((Change)diffs.get(0)).getAttrChanges().size()); 345 } 346 347 public void testFindOffsets() throws Exception { 348 XDMUtil util = new XDMUtil(); 349 String indent = " "; 350 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 351 String xml1 = readXMLString("diff/xdu/findoffset1_1.xml"); 353 String xml2 = readXMLString("diff/xdu/findoffset1_2.xml"); 354 List <Difference> diffs = compareXML(xml1, xml2, criteria); 355 assertEquals("find Offsets", 2, diffs.size()); 356 357 Difference d = diffs.get(1); 358 assertTrue("diff: ", d instanceof Change); 359 NodeInfo oldInfo = d.getOldNodeInfo(); 360 Node oldNode = oldInfo.getNode(); 361 NodeInfo info = d.getNewNodeInfo(); 362 Node node = info.getNode(); 363 if(oldNode != null) 364 assertEquals("old change begin: ", 50, XDMUtil.findPosition(oldNode)); 365 if(node != null) 366 assertEquals("new change begin: ", 45, XDMUtil.findPosition(node)); 367 368 d = diffs.get(0); 369 assertTrue("diff: ", d instanceof Change); 370 oldInfo = d.getOldNodeInfo(); 371 oldNode = oldInfo.getNode(); 372 info = d.getNewNodeInfo(); 373 node = info.getNode(); 374 if(oldNode != null) 375 assertEquals("old change begin: ", 22, XDMUtil.findPosition(oldNode)); 376 if(node != null) 377 assertEquals("new change begin: ", 22, XDMUtil.findPosition(node)); 378 if(d instanceof Change) { 379 List <AttributeDiff> attrDiffs = ((Change)d).getAttrChanges(); 380 AttributeDiff ad = attrDiffs.get(0); 381 assertTrue("attr diff: ", ad instanceof AttributeChange); 382 Attribute oldAttr = ad.getOldAttribute(); 383 Attribute attr = ad.getNewAttribute(); 384 if(oldAttr != null) 385 assertEquals("old attr change begin: ", 28, XDMUtil.findPosition(oldAttr)); 386 if(attr != null) 387 assertEquals("new attr change begin: ", 35, XDMUtil.findPosition(attr)); 388 389 ad = attrDiffs.get(1); 390 assertTrue("attr diff: ", ad instanceof AttributeChange); 391 oldAttr = ad.getOldAttribute(); 392 attr = ad.getNewAttribute(); 393 if(oldAttr != null) 394 assertEquals("old attr change begin: ", 35, XDMUtil.findPosition(oldAttr)); 395 if(attr != null) 396 assertEquals("new attr change begin: ", 28, XDMUtil.findPosition(attr)); 397 } 398 } 399 400 public void testCompareXMLIdenticalExtraEmptyNS() throws Exception { 401 XDMUtil util = new XDMUtil(); 402 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 403 String xml1 = readXMLString("diff/xdu/extraemptyns1_1.xml"); 404 String xml2 = readXMLString("diff/xdu/extraemptyns1_2.xml"); 405 List <Difference> diffs = compareXML(xml1, xml2, criteria); 406 assertEquals("compare identical XML", 0, diffs.size()); 407 } 408 409 public void testCompareXMLIdenticalExtraEmptyNS2() throws Exception { 410 XDMUtil util = new XDMUtil(); 411 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 412 String xml1 = readXMLString("diff/xdu/extraemptyns2_1.xml"); 413 String xml2 = readXMLString("diff/xdu/extraemptyns2_2.xml"); 414 List <Difference> diffs = compareXML(xml1, xml2, criteria); 415 assertEquals("compare identical XML", 0, diffs.size()); 416 } 417 418 public void testCompareXMLIdenticalExtraEmptyNS3() throws Exception { 419 XDMUtil util = new XDMUtil(); 420 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 421 String xml1 = readXMLString("diff/xdu/extraemptyns3_1.xml"); 422 String xml2 = readXMLString("diff/xdu/extraemptyns3_2.xml"); 423 List <Difference> diffs = compareXML(xml1, xml2, criteria); 424 assertEquals("compare identical XML", 0, diffs.size()); 425 } 426 427 public void testCompareXMLIdenticalExtraEmptyNS4() throws Exception { 428 XDMUtil util = new XDMUtil(); 429 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 430 String xml1 = readXMLString("diff/xdu/extraemptyns4_1.xml"); 431 String xml2 = readXMLString("diff/xdu/extraemptyns4_2.xml"); 432 List <Difference> diffs = compareXML(xml1, xml2, criteria); 433 assertEquals("compare identical XML", 0, diffs.size()); 434 } 435 436 public void testCompareWithDiffInLeafNodes() throws Exception { 437 XDMUtil util = new XDMUtil(); 438 XDMUtil.ComparisonCriteria criteria = XDMUtil.ComparisonCriteria.IDENTICAL; 439 String xml1 = readXMLString("diff/xdu/textchange1_1.xml"); 440 String xml2 = readXMLString("diff/xdu/textchange1_2.xml"); 441 List <Difference> diffs = compareXML(xml1, xml2, criteria); 442 assertEquals("compare identical XML", 2, diffs.size()); 443 } 444 445 private String readXMLString(String path) throws Exception { 446 Document doc = Util.getResourceAsDocument(path); 447 return doc.getText(0, doc.getLength()); 448 } 449 450 public List <Difference> compareXML(String xml1, String xml2, 451 XDMUtil.ComparisonCriteria criteria) 452 throws Exception { 453 return new XDMUtil().compareXML(xml1, xml2, criteria); 454 } 455 } 456 | Popular Tags |