1 21 22 package nu.xom.tests; 23 24 import junit.framework.ComparisonFailure; 25 import junit.framework.TestCase; 26 import nu.xom.Attribute; 27 import nu.xom.Comment; 28 import nu.xom.DocType; 29 import nu.xom.Document; 30 import nu.xom.Element; 31 import nu.xom.Node; 32 import nu.xom.ProcessingInstruction; 33 import nu.xom.Text; 34 35 36 46 public class XOMTestCase extends TestCase { 47 48 53 public XOMTestCase(String name) { 54 super(name); 55 } 56 57 71 public static void assertEquals(Text expected, Text actual) { 72 assertEquals(null, expected, actual); 73 } 74 75 76 92 public static void assertEquals( 93 String message, Text expected, Text actual) { 94 95 if (actual == expected) return; 96 nullCheck(message, expected, actual); 97 98 assertEquals(message, expected.getValue(), actual.getValue()); 99 } 100 101 102 private static void nullCheck(String message, Node expected, Node actual) { 103 104 if (expected == null) { 105 throw new ComparisonFailure(message, null, actual.toXML()); 106 } 107 else if (actual == null) { 108 throw new ComparisonFailure(message, expected.toXML(), null); 109 } 110 111 } 112 113 114 136 public static void assertEquals( 137 Attribute expected, Attribute actual) { 138 assertEquals(null, expected, actual); 139 } 140 141 142 166 public static void assertEquals( 167 String message, Attribute expected, Attribute actual) { 168 169 if (actual == expected) return; 170 nullCheck(message, expected, actual); 171 172 String value1 = expected.getValue(); 173 String value2 = actual.getValue(); 174 if ("xml:base".equals(expected.getQualifiedName())) { 175 if (value1.equals(value2)) return; 177 if (value1.startsWith("../")) { 178 assertTrue(message, value2.endsWith(value1.substring(2))); 179 } 180 else { 181 assertTrue(message, 182 value1.endsWith('/' + value2) || value2.endsWith('/' + value1)); 183 } 184 } 185 else { 186 assertEquals(message, value1, value2); 187 assertEquals(message, expected.getLocalName(), actual.getLocalName()); 188 assertEquals(message, 189 expected.getQualifiedName(), actual.getQualifiedName() 190 ); 191 assertEquals(message, 192 expected.getNamespaceURI(), actual.getNamespaceURI() 193 ); 194 } 195 196 } 197 198 199 215 public static void assertEquals(DocType expected, DocType actual) { 216 assertEquals(null, expected, actual); 217 } 218 219 220 239 public static void assertEquals( 240 String message, DocType expected, DocType actual) { 241 242 if (actual == expected) return; 243 nullCheck(message, expected, actual); 244 245 assertEquals(message, 246 expected.getPublicID(), 247 actual.getPublicID() 248 ); 249 assertEquals(message, 250 expected.getSystemID(), 251 actual.getSystemID() 252 ); 253 assertEquals(message, 254 expected.getRootElementName(), 255 actual.getRootElementName() 256 ); 257 } 258 259 260 276 public static void assertEquals( 277 Element expected, Element actual) { 278 assertEquals(null, expected, actual); 279 280 } 281 282 283 301 public static void assertEquals(String message, 302 Element expected, Element actual) { 303 304 if (actual == expected) return; 305 nullCheck(message, expected, actual); 306 307 assertEquals(message, 308 expected.getLocalName(), 309 actual.getLocalName() 310 ); 311 assertEquals(message, 312 expected.getQualifiedName(), 313 actual.getQualifiedName() 314 ); 315 assertEquals(message, 316 expected.getNamespaceURI(), 317 actual.getNamespaceURI() 318 ); 319 320 assertEquals(message, 321 expected.getAttributeCount(), 322 actual.getAttributeCount() 323 ); 324 325 for (int i = 0; i < expected.getAttributeCount(); i++ ) { 326 Attribute att1 = expected.getAttribute(i); 327 Attribute att2 328 = actual.getAttribute( 329 att1.getLocalName(), 330 att1.getNamespaceURI() 331 ); 332 assertNotNull(message, att2); 333 assertEquals(message, att1, att2); 334 } 335 336 for (int i = 0; 344 i < expected.getNamespaceDeclarationCount(); 345 i++ ) { 346 String prefix1 = expected.getNamespacePrefix(i); 347 String uri1 = expected.getNamespaceURI(prefix1); 348 assertNotNull(message, actual.getNamespaceURI(prefix1)); 349 assertEquals(message, 350 uri1, actual.getNamespaceURI(prefix1) 351 ); 352 } 353 for (int i = 0; 354 i < actual.getNamespaceDeclarationCount(); 355 i++ ) { 356 String prefix1 = actual.getNamespacePrefix(i); 357 String uri1 = actual.getNamespaceURI(prefix1); 358 assertNotNull(message, expected.getNamespaceURI(prefix1)); 359 assertEquals(message, 360 uri1, expected.getNamespaceURI(prefix1) 361 ); 362 } 363 364 if (expected.getChildCount() != actual.getChildCount()) { 365 combineTextNodes(expected); 369 combineTextNodes(actual); 370 } 371 assertEquals(message, 372 expected.getChildCount(), actual.getChildCount()); 373 for (int i = 0; i < expected.getChildCount(); i++) { 374 Node child1 = expected.getChild(i); 375 Node child2 = actual.getChild(i); 376 assertEquals(message, child1, child2); 377 } 378 379 } 380 381 382 private static void combineTextNodes(Element element) { 383 384 for (int i = 0; i < element.getChildCount()-1; i++) { 385 Node child = element.getChild(i); 386 if (child instanceof Text) { 387 Node followingSibling = element.getChild(i+1); 388 if (followingSibling instanceof Text) { 389 Text combined = new Text(child.getValue() 390 + followingSibling.getValue()); 391 element.replaceChild(child, combined); 392 element.removeChild(followingSibling); 393 i--; 394 } 395 } 396 } 397 398 } 399 400 401 414 public static void assertEquals( 415 Document expected, Document actual) { 416 assertEquals(null, expected, actual); 417 } 418 419 420 435 public static void assertEquals( 436 String message, Document expected, Document actual) { 437 438 if (actual == expected) return; 439 nullCheck(message, expected, actual); 440 441 assertEquals(message, 442 expected.getChildCount(), 443 actual.getChildCount() 444 ); 445 for (int i = 0; i < actual.getChildCount(); i++) { 446 Node child1 = expected.getChild(i); 447 Node child2 = actual.getChild(i); 448 assertEquals(message, child1, child2); 449 } 450 451 } 452 453 454 468 public static void assertEquals(Comment expected, Comment actual) { 469 assertEquals(null, expected, actual); 470 } 471 472 473 489 public static void assertEquals( 490 String message, Comment expected, Comment actual) { 491 492 if (actual == expected) return; 493 nullCheck(message, expected, actual); 494 assertEquals(message, expected.getValue(), actual.getValue()); 495 496 } 497 498 499 514 public static void assertEquals(ProcessingInstruction expected, 515 ProcessingInstruction actual) { 516 assertEquals(null, expected, actual); 517 } 518 519 520 539 public static void assertEquals(String message, 540 ProcessingInstruction expected, 541 ProcessingInstruction actual) { 542 543 if (actual == expected) return; 544 nullCheck(message, expected, actual); 545 546 assertEquals(message, expected.getValue(), actual.getValue()); 547 assertEquals(message, expected.getTarget(), actual.getTarget()); 548 549 } 550 551 552 568 public static void assertEquals(Node expected, Node actual) { 569 assertEquals(null, expected, actual); 570 } 571 572 573 590 public static void assertEquals( 591 String message, Node expected, Node actual) { 592 593 if (actual == expected) return; 594 nullCheck(message, expected, actual); 595 596 try { 597 if (expected instanceof Document) { 598 assertEquals(message, (Document) expected, (Document) actual); 599 } 600 else if (expected instanceof Element) { 601 assertEquals(message, (Element) expected, (Element) actual); 602 } 603 else if (expected instanceof Text) { 604 assertEquals(message, (Text) expected, (Text) actual); 605 } 606 else if (expected instanceof DocType) { 607 assertEquals(message, 608 (DocType) expected, 609 (DocType) actual 610 ); 611 } 612 else if (expected instanceof Comment) { 613 assertEquals(message, 614 (Comment) expected, 615 (Comment) actual 616 ); 617 } 618 else if (expected instanceof ProcessingInstruction) { 619 assertEquals(message, 620 (ProcessingInstruction) expected, 621 (ProcessingInstruction) actual 622 ); 623 } 624 else { 625 throw new IllegalArgumentException ( 626 "Unexpected node type " 627 + expected.getClass().getName() 628 ); 629 } 630 } 631 catch (ClassCastException ex) { 632 fail(message + "; Mismatched node types: " 633 + expected.getClass().getName() + " != " 634 + actual.getClass().getName()); 635 } 636 637 } 638 639 640 } 641 | Popular Tags |