1 21 22 package nu.xom.tests; 23 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStreamWriter ; 30 import java.io.PrintStream ; 31 import java.io.Reader ; 32 import java.io.StringReader ; 33 import java.io.Writer ; 34 import java.net.URL ; 35 36 import junit.framework.AssertionFailedError; 37 38 import nu.xom.Attribute; 39 import nu.xom.Builder; 40 import nu.xom.Comment; 41 import nu.xom.DocType; 42 import nu.xom.Document; 43 import nu.xom.Element; 44 import nu.xom.Elements; 45 import nu.xom.Node; 46 import nu.xom.NodeFactory; 47 import nu.xom.Nodes; 48 import nu.xom.ParsingException; 49 import nu.xom.Serializer; 50 import nu.xom.Text; 51 52 import nu.xom.xinclude.BadEncodingAttributeException; 53 import nu.xom.xinclude.BadHTTPHeaderException; 54 import nu.xom.xinclude.BadHrefAttributeException; 55 import nu.xom.xinclude.BadParseAttributeException; 56 import nu.xom.xinclude.InclusionLoopException; 57 import nu.xom.xinclude.NoIncludeLocationException; 58 import nu.xom.xinclude.XIncludeException; 59 import nu.xom.xinclude.XIncluder; 60 61 70 public class XIncludeTest extends XOMTestCase { 71 72 private static boolean windows 73 = System.getProperty("os.name", "Unix").indexOf("Windows") >= 0; 74 75 76 public XIncludeTest(String name) { 77 super(name); 78 } 79 80 81 private Builder builder = new Builder(); 82 private File inputDir; 83 private File outputDir; 84 85 private PrintStream systemErr = System.err; 89 90 91 protected void setUp() { 92 93 System.setErr(new PrintStream (new ByteArrayOutputStream ())); 94 95 inputDir = new File ("data"); 96 inputDir = new File (inputDir, "xinclude"); 97 inputDir = new File (inputDir, "input"); 98 99 outputDir = new File ("data"); 100 outputDir = new File (outputDir, "xinclude"); 101 outputDir = new File (outputDir, "output"); 102 103 } 104 105 106 protected void tearDown() { 107 System.setErr(systemErr); 108 } 109 110 111 private void dumpResult(File original, Document result) 112 throws IOException { 113 114 String name = original.getName(); 115 File debug = new File ("data"); 116 debug = new File (debug, "xinclude"); 117 debug = new File (debug, "debug"); 118 File output = new File (debug, name); 119 FileOutputStream out = new FileOutputStream (output); 120 Serializer serializer = new Serializer(out); 121 serializer.write(result); 122 123 } 124 125 126 public void testXPointersResolvedAgainstAcquiredInfoset() 127 throws ParsingException, IOException , XIncludeException { 128 129 File input = new File (inputDir, "tobintop.xml"); 130 Document doc = builder.build(input); 131 Document result = XIncluder.resolve(doc); 132 Document expected = builder.build( 133 new File (outputDir, "tobintop.xml") 134 ); 135 assertEquals(expected, result); 136 137 } 138 139 140 public void testXMLBaseUsedToResolveHref() 141 throws ParsingException, IOException , XIncludeException { 142 143 File input = new File (inputDir, "xmlbasetest.xml"); 144 Document doc = builder.build(input); 145 Document result = XIncluder.resolve(doc); 146 Document expected = builder.build( 147 new File (outputDir, "xmlbasetest.xml") 148 ); 149 assertEquals(expected, result); 150 151 } 152 153 154 public void testResolveThroughXPointer() 158 throws ParsingException, IOException , XIncludeException { 159 160 File input = new File (inputDir, "resolvethruxpointer.xml"); 161 Document doc = builder.build(input); 162 Document result = XIncluder.resolve(doc); 163 Document expectedResult = builder.build( 164 new File (outputDir, "resolvethruxpointer.xml") 165 ); 166 assertEquals(expectedResult, result); 167 168 } 169 170 171 public void testXMLBaseOnIncludeElementUsedToResolveHref() 172 throws ParsingException, IOException , XIncludeException { 173 174 File input = new File (inputDir, "xmlbasetest2.xml"); 175 Document doc = builder.build(input); 176 Document result = XIncluder.resolve(doc); 177 Document expected = builder.build( 178 new File (outputDir, "xmlbasetest2.xml") 179 ); 180 assertEquals(expected, result); 181 182 } 183 184 185 public void testXMLBaseRetainedFromUnincludedElement() 186 throws ParsingException, IOException , XIncludeException { 187 188 File input = new File (inputDir, "xmlbasetest3.xml"); 189 Document doc = builder.build(input); 190 Document result = XIncluder.resolve(doc); 191 Document expected = builder.build( 192 new File (outputDir, "xmlbasetest3.xml") 193 ); 194 assertEquals(expected, result); 195 196 } 197 198 199 public void testMarsh() 200 throws ParsingException, IOException , XIncludeException { 201 202 File input = new File (inputDir, "marshtest.xml"); 203 Document doc = builder.build(input); 204 Document result = XIncluder.resolve(doc); 205 Document expectedResult = builder.build( 206 new File (outputDir, "marshtest.xml") 207 ); 208 assertEquals(expectedResult, result); 209 210 } 211 212 213 public void testIncludeDocumentThatUsesIntradocumentReferences() 214 throws ParsingException, IOException , XIncludeException { 215 216 File input = new File (inputDir, "includedocumentwithintradocumentreferences.xml"); 217 Document doc = builder.build(input); 218 Document result = XIncluder.resolve(doc); 219 Document expectedResult = builder.build( 220 new File (outputDir, "includedocumentwithintradocumentreferences.xml") 221 ); 222 assertEquals(expectedResult, result); 223 224 } 225 226 227 public void testXMLLangAttributes() 228 throws ParsingException, IOException , XIncludeException { 229 230 File input = new File (inputDir, "langtest1.xml"); 231 Document doc = builder.build(input); 232 Document result = XIncluder.resolve(doc); 233 Document expectedResult = builder.build( 234 new File (outputDir, "langtest1.xml") 235 ); 236 assertEquals(expectedResult, result); 237 238 } 239 240 241 public void testInheritedXMLLangAttributes() 242 throws ParsingException, IOException , XIncludeException { 243 244 File input = new File (inputDir, "langtest2.xml"); 245 Document doc = builder.build(input); 246 Document result = XIncluder.resolve(doc); 247 Document expectedResult = builder.build( 248 new File (outputDir, "langtest2.xml") 249 ); 250 assertEquals(expectedResult, result); 251 252 } 253 254 255 public void testNoLanguageSpecified() 256 throws ParsingException, IOException , XIncludeException { 257 258 File input = new File (inputDir, "langtest3.xml"); 259 Document doc = builder.build(input); 260 Document result = XIncluder.resolve(doc); 261 Document expectedResult = builder.build( 262 new File (outputDir, "langtest3.xml") 263 ); 264 assertEquals(expectedResult, result); 265 266 } 267 268 269 public void testXMLBaseNotUsedToResolveMissingHref() 272 throws ParsingException, IOException , XIncludeException { 273 274 File input = new File (inputDir, "marshtestwithxmlbase.xml"); 275 Document doc = builder.build(input); 276 Document result = XIncluder.resolve(doc); 277 Document expectedResult = builder.build( 278 new File (outputDir, "marshtestwithxmlbase.xml") 279 ); 280 assertEquals(expectedResult, result); 281 282 } 283 284 285 public void testEmptyHrefTreatedSameAsMissingHref() 286 throws ParsingException, IOException , XIncludeException { 287 288 File input = new File (inputDir, "marshtestwithxmlbaseandemptyhref.xml"); 289 Document doc = builder.build(input); 290 Document result = XIncluder.resolve(doc); 291 Document expectedResult = builder.build( 292 new File (outputDir, "marshtestwithxmlbase.xml") 293 ); 294 assertEquals(expectedResult, result); 295 296 } 297 298 299 public void testBaselessDocument() 300 throws IOException , ParsingException, XIncludeException { 301 302 Element root = new Element("root"); 303 Element child1 = new Element("xi:include", XIncluder.XINCLUDE_NS); 304 child1.addAttribute(new Attribute("xpointer", "p1")); 305 Element child2 = new Element("child2"); 306 root.appendChild(child1); 307 root.appendChild(child2); 308 child2.addAttribute(new Attribute("id", "p1", Attribute.Type.ID)); 309 Document in = new Document(root); 310 Document out = XIncluder.resolve(in); 311 String result = out.toXML(); 312 assertEquals("<?xml version=\"1.0\"?>\n" + 313 "<root><child2 id=\"p1\" /><child2 id=\"p1\" /></root>\n", result); 314 } 315 316 317 public void testIncludeTextWithCustomNodeFactory() 318 throws ParsingException, IOException , XIncludeException { 319 320 File input = new File (inputDir, "c2.xml"); 321 Builder builder = new Builder(new TextNodeFactory()); 322 Document doc = builder.build(input); 323 Document result = XIncluder.resolve(doc, builder); 324 Document expectedResult = builder.build( 325 new File (outputDir, "c2.xml") 326 ); 327 assertEquals(expectedResult, result); 328 Element root = result.getRootElement(); 329 for (int i = 0; i < root.getChildCount(); i++) { 330 Node node = root.getChild(i); 331 if (node instanceof Text) { 332 assertTrue(node instanceof TextSubclass); 333 } 334 } 335 336 } 337 338 339 public void testParseEqualsTextWithNodeFactoryThatRemovesAllTextNodes() 340 throws ParsingException, IOException , XIncludeException { 341 342 File input = new File (inputDir, "c2.xml"); 343 Document doc = builder.build(input); 344 Document result = XIncluder.resolve(doc, new Builder(new TextFilter())); 345 Document expectedResult = builder.build( 346 new File (outputDir, "c2a.xml") 347 ); 348 assertEquals(expectedResult, result); 349 350 } 351 352 353 private static class TextFilter extends NodeFactory { 354 355 public Nodes makeText(String data) { 356 return new Nodes(); 357 } 358 359 } 360 361 362 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithComments() 363 throws ParsingException, IOException , XIncludeException { 364 365 File input = new File (inputDir, "c2.xml"); 366 Document doc = builder.build(input); 367 Document result = XIncluder.resolve(doc, new Builder(new TextToComment())); 368 Document expectedResult = builder.build( 369 new File (outputDir, "c2b.xml") 370 ); 371 assertEquals(expectedResult, result); 372 373 } 374 375 376 private static class TextToComment extends NodeFactory { 377 378 public Nodes makeText(String data) { 379 return new Nodes(new Comment(data)); 380 } 381 382 } 383 384 385 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithAttributes() 386 throws ParsingException, IOException , XIncludeException { 387 388 File input = new File (inputDir, "c2.xml"); 389 Document doc = builder.build(input); 390 Document result = XIncluder.resolve(doc, new Builder(new TextToAttribute())); 391 Document expectedResult = builder.build( 392 new File (outputDir, "c2c.xml") 393 ); 394 assertEquals(expectedResult, result); 395 396 } 397 398 399 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithTwoElements() 400 throws ParsingException, IOException , XIncludeException { 401 402 File input = new File (inputDir, "c2.xml"); 403 Document doc = builder.build(input); 404 Document result = XIncluder.resolve(doc, new Builder(new TextToElements())); 405 Document expectedResult = builder.build( 406 new File (outputDir, "c2d.xml") 407 ); 408 assertEquals(expectedResult, result); 409 410 } 411 412 413 private static class TextToElements extends NodeFactory { 414 415 public Nodes makeText(String data) { 416 Nodes result = new Nodes(); 417 result.append(new Element("empty1")); 418 result.append(new Element("empty2")); 419 return result; 420 } 421 422 } 423 424 425 private static class TextToAttribute extends NodeFactory { 426 427 public Nodes makeText(String data) { 428 return new Nodes(new Attribute("name", data)); 429 } 430 431 } 432 433 434 public void testUnrecognizedXPointerScheme() 435 throws ParsingException, IOException { 436 437 File input = new File (inputDir, "unrecognizedscheme.xml"); 438 Document doc = builder.build(input); 439 try { 440 XIncluder.resolve(doc); 441 fail("Allowed unrecognized scheme"); 442 } 443 catch (XIncludeException success) { 444 assertNotNull(success.getMessage()); 445 } 446 447 } 448 449 450 public void testUnrecognizedXPointerSchemeWithFallback() 451 throws IOException , ParsingException, XIncludeException { 452 453 File input = new File (inputDir, "unrecognizedschemewithfallback.xml"); 454 File output = new File (outputDir, "unrecognizedschemewithfallback.xml"); 455 Document doc = builder.build(input); 456 Document actual = XIncluder.resolve(doc); 457 Document expected = builder.build(output); 458 assertEquals(expected, actual); 459 460 } 461 462 463 public void testIncludeTextWithCustomNodeFactoryThatChangesElementNames() 464 throws ParsingException, IOException , XIncludeException { 465 466 File input = new File (inputDir, "c1.xml"); 467 Document doc = builder.build(input); 468 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.CFactory())); 469 Document expectedResult = builder.build( 470 new File (outputDir, "c1a.xml") 471 ); 472 assertEquals(expectedResult, result); 473 474 } 475 476 477 public void testIncludeTextWithCustomNodeFactoryThatOnlyReturnsRoot() 478 throws ParsingException, IOException , XIncludeException { 479 480 File input = new File (inputDir, "c1.xml"); 481 Document doc = builder.build(input); 482 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.MinimizingFactory())); 483 Document expectedResult = builder.build( 484 new File (outputDir, "c1b.xml") 485 ); 486 assertEquals(expectedResult, result); 487 488 } 489 490 491 public void testIncludeTextWithCustomNodeFactoryThatFiltersElementsNamedB() 492 throws ParsingException, IOException , XIncludeException { 493 494 File input = new File (inputDir, "d1.xml"); 495 Document doc = builder.build(input); 496 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.BFilter())); 497 Document expectedResult = builder.build( 498 new File (outputDir, "d1.xml") 499 ); 500 assertEquals(expectedResult, result); 501 502 } 503 504 505 public void testIncludeTextWithCustomNodeFactoryThatReturnsEachNonRootElementThreeTimes() 506 throws ParsingException, IOException , XIncludeException { 507 508 File input = new File (inputDir, "c1.xml"); 509 Document doc = builder.build(input); 510 Document result = XIncluder.resolve(doc, 511 new Builder(new NodeFactoryTest.TripleElementFilter())); 512 Document expectedResult = builder.build( 513 new File (outputDir, "triple.xml") 514 ); 515 assertEquals(expectedResult, result); 516 517 } 518 519 520 public void test1() 521 throws ParsingException, IOException , XIncludeException { 522 523 File input = new File (inputDir, "test.xml"); 524 Document doc = builder.build(input); 525 Document result = XIncluder.resolve(doc); 526 Document expectedResult = builder.build( 527 new File (outputDir, "test.xml") 528 ); 529 assertEquals(expectedResult, result); 530 531 } 532 533 534 public void testBaseURIsPreservedInSameDocumentInclusion() 535 throws ParsingException, IOException , XIncludeException { 536 537 File input = new File (inputDir, "includefromsamedocumentwithbase.xml"); 538 Document doc = builder.build(input); 539 Document result = XIncluder.resolve(doc); 540 Document expectedResult = builder.build( 541 new File (outputDir, "includefromsamedocumentwithbase.xml") 542 ); 543 assertEquals(expectedResult, result); 544 545 } 546 547 548 560 561 562 public void testNullBaseURI() 563 throws ParsingException, IOException , XIncludeException { 564 565 File input = new File (inputDir, "disclaimer.xml"); 566 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>" 567 + "\n <p>120 Mz is adequate for an average home user.</p>" 568 + "\n <xi:include HREF='" + input.toURL() + "'/>\n</document>"; 569 Reader reader = new StringReader (data); 570 Document doc = builder.build(reader); 571 Document result = XIncluder.resolve(doc); 572 Document expectedResult = builder.build( 573 new File (outputDir, "c1.xml") 574 ); 575 assertEquals(expectedResult, result); 576 577 } 578 579 580 public void testBadIRIIsAFatalError() 581 throws IOException , ParsingException, XIncludeException { 582 583 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>" 584 + "<xi:include HREF='http://www.example.com/a%5.html'>" 585 + "<xi:fallback>Ooops!</xi:fallback></xi:include></document>"; 586 Reader reader = new StringReader (data); 587 Document doc = builder.build(reader); 588 try { 589 XIncluder.resolve(doc); 590 fail("Resolved fallback when encountering a syntactically incorrect URI"); 591 } 592 catch (BadHrefAttributeException success) { 593 assertNotNull(success.getMessage()); 594 } 595 596 } 597 598 599 public void testBadIRIWithUnrecognizedSchemeIsAFatalError() 600 throws IOException , ParsingException, XIncludeException { 601 602 String data = "<doc xmlns:xi='http://www.w3.org/2001/XInclude'>" 603 + "<xi:include HREF='scheme://www.example.com/a%5.html'>" 604 + "<xi:fallback>Ooops!</xi:fallback></xi:include></doc>"; 605 Reader reader = new StringReader (data); 606 Document doc = builder.build(reader); 607 try { 608 XIncluder.resolve(doc); 609 fail("Resolved fallback when encountering a syntactically incorrect URI"); 610 } 611 catch (BadHrefAttributeException success) { 612 assertNotNull(success.getMessage()); 613 } 614 615 } 616 617 618 public void testGoodIRIWithUnrecognizedSchemeIsAResourceError() 619 throws IOException , ParsingException, XIncludeException { 620 621 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>" 622 + "<xi:include HREF='scheme://www.example.com/a.html'>" 623 + "<xi:fallback>Correct!</xi:fallback></xi:include></document>"; 624 Reader reader = new StringReader (data); 625 Document doc = builder.build(reader); 626 Document result = XIncluder.resolve(doc); 627 assertEquals("<?xml version=\"1.0\"?>\n" 628 + "<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">Correct!</document>\n", 629 result.toXML()); 630 631 } 632 633 634 public void testBadAcceptAttribute() 635 throws ParsingException, IOException , XIncludeException { 636 637 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>" 638 + "\n <p>120 MHz is adequate for an average home user.</p>" 639 + "\n <xi:include HREF='http://www.example.com' " 640 + "accept='text/html
Something: bad'/>\n</document>"; 641 Reader reader = new StringReader (data); 642 Document doc = builder.build(reader); 643 try { 644 XIncluder.resolve(doc); 645 fail("Allowed accept header containing carriage return linefeed"); 646 } 647 catch (BadHTTPHeaderException success) { 648 assertNotNull(success.getMessage()); 649 } 650 651 } 652 653 654 public void testBadAcceptAttributeWithLatin1Character() 655 throws ParsingException, IOException , XIncludeException { 656 657 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>" 658 + "\n <p>120 MHz is adequate for an average home user.</p>" 659 + "\n <xi:include HREF='http://www.example.com' " 660 + "accept='text/html Something: bad'/>\n</document>"; 661 Reader reader = new StringReader (data); 662 Document doc = builder.build(reader); 663 try { 664 XIncluder.resolve(doc); 665 fail("Allowed accept header containing non-ASCII character"); 666 } 667 catch (BadHTTPHeaderException success) { 668 assertNotNull(success.getMessage()); 669 } 670 671 } 672 673 674 public void testUnrecognizedAttributesAreIgnored() 675 throws ParsingException, IOException , XIncludeException { 676 677 File input = new File (inputDir, "extraattributes.xml"); 678 Document doc = builder.build(input); 679 Document result = XIncluder.resolve(doc); 680 Document expectedResult = builder.build( 681 new File (outputDir, "c1.xml") 682 ); 683 assertEquals(expectedResult, result); 684 685 } 686 687 688 public void testEmptyFallback() 689 throws ParsingException, IOException , XIncludeException { 690 691 File input = new File (inputDir, "emptyfallback.xml"); 692 Document doc = builder.build(input); 693 Document result = XIncluder.resolve(doc); 694 Document expectedResult = builder.build( 695 new File (outputDir, "emptyfallback.xml") 696 ); 697 assertEquals(expectedResult, result); 698 699 } 700 701 702 public void testFallbackInIncludedDocument() 703 throws ParsingException, IOException , XIncludeException { 704 705 File input = new File (inputDir, "metafallbacktest.xml"); 706 Document doc = builder.build(input); 707 Document result = XIncluder.resolve(doc); 708 Document expectedResult = builder.build( 709 new File (outputDir, "metafallbacktest.xml") 710 ); 711 assertEquals(expectedResult, result); 712 713 } 714 715 716 public void testFallbackInIncludedDocumentUsesAnIntradocumentXPointer() 717 throws ParsingException, IOException , XIncludeException { 718 719 File input = new File (inputDir, "metafallbacktest6.xml"); 720 Document doc = builder.build(input); 721 Document result = XIncluder.resolve(doc); 722 Document expectedResult = builder.build( 723 new File (outputDir, "metafallbacktest6.xml") 724 ); 725 assertEquals(expectedResult, result); 726 727 } 728 729 730 public void testFallbackInIncludedDocumentIncludesADocumentWithParseEqualsText() 732 throws ParsingException, IOException , XIncludeException { 733 734 File input = new File (inputDir, "metafallbacktest2.xml"); 735 Document doc = builder.build(input); 736 Document result = XIncluder.resolve(doc); 737 Document expectedResult = builder.build( 738 new File (outputDir, "metafallbacktest2.xml") 739 ); 740 assertEquals(expectedResult, result); 741 742 } 743 744 745 public void testFallbackInIncludedDocumentWithBadParseAttribute() 746 throws ParsingException, IOException , XIncludeException { 747 748 File input = new File (inputDir, "metafallbacktest3.xml"); 749 Document doc = builder.build(input); 750 try { 751 XIncluder.resolve(doc); 752 fail("Allowed bad parse attribute"); 753 } 754 catch (BadParseAttributeException success) { 755 assertNotNull(success.getMessage()); 756 } 757 758 } 759 760 761 public void testFallbackInIncludedDocumentWithMissingHrefAndParseAttributes() 762 throws ParsingException, IOException , XIncludeException { 763 764 File input = new File (inputDir, "metafallbacktest4.xml"); 765 Document doc = builder.build(input); 766 try { 767 XIncluder.resolve(doc); 768 fail("Allowed bad parse attribute"); 769 } 770 catch (NoIncludeLocationException success) { 771 assertNotNull(success.getMessage()); 772 } 773 774 } 775 776 777 public void testFallbackInIncludedDocumentWithFragmentID() 778 throws ParsingException, IOException , XIncludeException { 779 780 File input = new File (inputDir, "metafallbacktestwithfragmentid.xml"); 781 Document doc = builder.build(input); 782 try { 783 XIncluder.resolve(doc); 784 fail("Allowed document with fragment ID in href attribute"); 785 } 786 catch (BadHrefAttributeException success) { 787 assertNotNull(success.getMessage()); 788 } 789 790 } 791 792 793 public void testXPointerIsNotResolvedAgainstTheSourceInfoset() 795 throws ParsingException, IOException , XIncludeException { 796 797 File input = new File (inputDir, "metafallbacktest5.xml"); 798 Document doc = builder.build(input); 799 try { 800 XIncluder.resolve(doc); 801 fail("Allowed XPointer that doesn't resolve against the acquired infoset but does resolve against the source infoset"); 802 } 803 catch (XIncludeException ex) { 804 assertNotNull(ex.getMessage()); 805 } 806 807 } 808 809 810 public void testFallbackInIncludedDocumentThatResolvesToNonElement() 811 throws ParsingException, IOException , XIncludeException { 812 813 File input = new File (inputDir, "metafallbacktotexttest.xml"); 814 Document doc = builder.build(input); 815 Document result = XIncluder.resolve(doc); 816 Document expectedResult = builder.build( 817 new File (outputDir, "metafallbacktotexttest.xml") 818 ); 819 assertEquals(expectedResult, result); 820 821 } 822 823 824 public void testFallbackInIncludedDocumentWithXPointer() 825 throws ParsingException, IOException , XIncludeException { 826 File input = new File (inputDir, "metafallbacktestwithxpointer.xml"); 828 Document doc = builder.build(input); 829 Document result = XIncluder.resolve(doc); 830 Document expectedResult = builder.build( 831 new File (outputDir, "metafallbacktestwithxpointer.xml") 832 ); 833 assertEquals(expectedResult, result); 834 835 } 836 837 838 public void testFallbackInIncludedDocumentWithXPointer2() 842 throws ParsingException, IOException , XIncludeException { 843 844 File input = new File (inputDir, "metafallbacktestwithxpointer2.xml"); 846 Document doc = builder.build(input); 847 Document result = XIncluder.resolve(doc); 848 Document expectedResult = builder.build( 849 new File (outputDir, "metafallbacktestwithxpointer2.xml") 850 ); 851 assertEquals(expectedResult, result); 852 853 } 854 855 856 public void testNoFallbackInIncludedDocumentWithXPointer() 857 throws ParsingException, IOException , XIncludeException { 858 859 File input = new File (inputDir, "metamissingfallbacktestwithxpointer.xml"); 861 Document doc = builder.build(input); 862 try { 863 XIncluder.resolve(doc); 864 fail("Should have thrown IOException"); 865 } 866 catch (IOException success) { 867 assertNotNull(success.getMessage()); 868 } 869 870 } 871 872 873 public void testFallbackInIncludedDocumentHasBadXPointer() 874 throws ParsingException, IOException , XIncludeException { 875 File input = new File (inputDir, "metafallbackwithbadxpointertest.xml"); 877 Document doc = builder.build(input); 878 try { 879 XIncluder.resolve(doc); 880 fail("Should have thrown XIncludeException"); 881 } 882 catch (XIncludeException success) { 883 assertNotNull(success.getMessage()); 884 assertNotNull(success.getCause()); 885 } 886 887 } 888 889 890 public void testC1() 892 throws ParsingException, IOException , XIncludeException { 893 894 File input = new File (inputDir, "c1.xml"); 895 Document doc = builder.build(input); 896 Document result = XIncluder.resolve(doc); 897 Document expectedResult = builder.build( 898 new File (outputDir, "c1.xml") 899 ); 900 assertEquals(expectedResult, result); 901 902 } 903 904 905 public void testParseEqualsXML() 907 throws ParsingException, IOException , XIncludeException { 908 909 File input = new File (inputDir, "parseequalxml.xml"); 910 Document doc = builder.build(input); 911 Document result = XIncluder.resolve(doc); 912 Document expectedResult = builder.build( 913 new File (outputDir, "c1.xml") 914 ); 915 assertEquals(expectedResult, result); 916 917 } 918 919 920 public void testAcceptableCirclePointer() 922 throws ParsingException, IOException , XIncludeException { 923 924 File input = new File (inputDir, "legalcircle.xml"); 925 Document doc = builder.build(input); 926 try { 927 XIncluder.resolve(doc); 928 fail("Allowed circular reference"); 929 } 930 catch (InclusionLoopException success) { 931 assertNotNull(success.getMessage()); 932 } 933 934 } 935 936 937 public void testC2() 939 throws ParsingException, IOException , XIncludeException { 940 941 File input = new File (inputDir, "c2.xml"); 942 Document doc = builder.build(input); 943 Document result = XIncluder.resolve(doc); 944 Document expectedResult = builder.build( 945 new File (outputDir, "c2.xml") 946 ); 947 assertEquals(expectedResult, result); 948 949 } 950 951 952 public void testC3() 954 throws ParsingException, IOException , XIncludeException { 955 956 File input = new File (inputDir, "c3.xml"); 957 Document doc = builder.build(input); 958 Document result = XIncluder.resolve(doc); 959 Document expectedResult = builder.build( 960 new File (outputDir, "c3.xml") 961 ); 962 assertEquals(expectedResult, result); 963 964 } 965 966 967 970 982 983 984 private static class TextNodeFactory extends NodeFactory { 985 986 public Nodes makeText(String data) { 987 return new Nodes(new TextSubclass(data)); 988 } 989 990 } 991 992 private static class TextSubclass extends Text { 993 994 TextSubclass(String data) { 995 super(data); 996 } 997 998 public Node copy() { 999 return new TextSubclass(this.getValue()); 1000 } 1001 1002 } 1003 1004 1005 public void testRecurseWithinSameDocument() 1006 throws ParsingException, IOException , XIncludeException { 1007 1008 File input = new File (inputDir, "recursewithinsamedocument.xml"); 1009 Document doc = builder.build(input); 1010 Document result = XIncluder.resolve(doc); 1011 Document expectedResult = builder.build( 1012 new File (outputDir, "recursewithinsamedocument.xml") 1013 ); 1014 assertEquals(expectedResult, result); 1015 1016 } 1017 1018 1019 public void testSiblingIncludes() 1020 throws ParsingException, IOException , XIncludeException { 1021 1022 File input = new File (inputDir, "paralleltest.xml"); 1023 Document doc = builder.build(input); 1024 Document result = XIncluder.resolve(doc); 1025 Document expectedResult = builder.build( 1026 new File (outputDir, "paralleltest.xml") 1027 ); 1028 assertEquals(expectedResult, result); 1029 1030 } 1031 1032 1033 public void testNamespaces() 1034 throws ParsingException, IOException , XIncludeException { 1035 1036 File input = new File (inputDir, "namespacetest.xml"); 1037 Document doc = builder.build(input); 1038 Document result = XIncluder.resolve(doc); 1039 Document expectedResult = builder.build( 1040 new File (outputDir, "namespacetest.xml") 1041 ); 1042 assertEquals(expectedResult, result); 1043 1044 } 1045 1046 1047 public void testIncludeReferencesItItself() 1048 throws ParsingException, IOException , XIncludeException { 1049 1050 File input = new File (inputDir, "internalcircular.xml"); 1051 Document doc = builder.build(input); 1052 try { 1053 XIncluder.resolve(doc); 1054 fail("Allowed include element to reference itself"); 1055 } 1056 catch (InclusionLoopException success) { 1057 assertNotNull(success.getMessage()); 1058 } 1059 1060 } 1061 1062 1063 public void testIncludeReferencesItsAncestor() 1064 throws ParsingException, IOException , XIncludeException { 1065 1066 File input = new File (inputDir, "internalcircularviaancestor.xml"); 1067 Document doc = builder.build(input); 1068 try { 1069 XIncluder.resolve(doc); 1070 fail("Allowed include element to reference its own ancestor"); 1071 } 1072 catch (InclusionLoopException success) { 1073 assertNotNull(success.getMessage()); 1074 } 1075 1076 } 1077 1078 1079 public void testNoInclusions() 1080 throws ParsingException, IOException , XIncludeException { 1081 1082 File input = new File (inputDir, "latin1.xml"); 1083 Document doc = builder.build(input); 1084 Document result = XIncluder.resolve(doc); 1085 Document expectedResult = builder.build( 1086 new File (outputDir, "latin1.xml") 1087 ); 1088 assertEquals(expectedResult, result); 1089 1090 } 1091 1092 1093 public void test2() 1094 throws ParsingException, IOException , XIncludeException { 1095 1096 File input = new File (inputDir, "simple.xml"); 1097 Document doc = builder.build(input); 1098 Document result = XIncluder.resolve(doc); 1099 Document expectedResult = builder.build( 1100 new File (outputDir, "simple.xml") 1101 ); 1102 assertEquals(expectedResult, result); 1103 1104 } 1105 1106 1107 public void testReplaceRoot() 1108 throws ParsingException, IOException , XIncludeException { 1109 1110 File input = new File (inputDir, "roottest.xml"); 1111 Document doc = builder.build(input); 1112 Document result = XIncluder.resolve(doc); 1113 Document expectedResult = builder.build( 1114 new File (outputDir, "roottest.xml") 1115 ); 1116 assertEquals(expectedResult, result); 1117 1118 } 1119 1120 1121 public void testReplaceRoot2() 1123 throws ParsingException, IOException , XIncludeException { 1124 1125 File input = new File (inputDir, "roottest2.xml"); 1126 Document doc = builder.build(input); 1127 Document result = XIncluder.resolve(doc); 1128 Document expectedResult = builder.build( 1129 new File (outputDir, "roottest2.xml") 1130 ); 1131 assertEquals(expectedResult, result); 1132 1133 } 1134 1135 1136 public void testIncludeElementsCannotHaveIncludeChildren() 1137 throws ParsingException, IOException , XIncludeException { 1138 File input = new File (inputDir, "nestedxinclude.xml"); 1139 Document doc = builder.build(input); 1140 try { 1141 XIncluder.resolve(doc); 1142 fail("allowed include element to contain another include element"); 1143 } 1144 catch (XIncludeException success) { 1145 assertNotNull(success.getMessage()); 1146 } 1147 } 1148 1149 1150 public void testIncludeElementsCannotHaveChildrenFromXIncludeNamespace() 1151 throws ParsingException, IOException , XIncludeException { 1152 File input = new File (inputDir, "nestedxincludenamespace.xml"); 1153 Document doc = builder.build(input); 1154 try { 1155 XIncluder.resolve(doc); 1156 fail("allowed include element to contain another include element"); 1157 } 1158 catch (XIncludeException success) { 1159 assertNotNull(success.getMessage()); 1160 } 1161 } 1162 1163 1164 public void testFallbackIsNotChildOfIncludeElement() 1165 throws ParsingException, IOException , XIncludeException { 1166 File input = new File (inputDir, "nakedfallback.xml"); 1167 Document doc = builder.build(input); 1168 try { 1169 XIncluder.resolve(doc); 1170 fail("allowed fallback that was not child of an include element"); 1171 } 1172 catch (XIncludeException success) { 1173 assertNotNull(success.getMessage()); 1174 } 1175 } 1176 1177 1178 public void testFallbackCantContainFallbackElement() 1179 throws ParsingException, IOException , XIncludeException { 1180 File input = new File (inputDir, "fallbackcontainsfallback.xml"); 1181 Document doc = builder.build(input); 1182 try { 1183 XIncluder.resolve(doc); 1184 fail("allowed fallback inside another fallback element"); 1185 } 1186 catch (XIncludeException success) { 1187 assertNotNull(success.getMessage()); 1188 } 1189 } 1190 1191 1192 public void testMultipleFallbacks() 1194 throws ParsingException, IOException , XIncludeException { 1195 File input = new File (inputDir, "multiplefallbacks.xml"); 1196 Document doc = builder.build(input); 1197 try { 1198 XIncluder.resolve(doc); 1199 fail("allowed multiple fallback elements"); 1200 } 1201 catch (XIncludeException success) { 1202 assertNotNull(success.getMessage()); 1203 } 1204 } 1205 1206 1207 public void testMultipleFallbacks2() 1209 throws ParsingException, IOException , XIncludeException { 1210 File input = new File (inputDir, "multiplefallbacks2.xml"); 1211 Document doc = builder.build(input); 1212 try { 1213 XIncluder.resolve(doc); 1214 fail("allowed multiple fallback elements"); 1215 } 1216 catch (XIncludeException success) { 1217 assertNotNull(success.getMessage()); 1218 } 1219 } 1220 1221 1222 public void testDocumentIncludesItself() 1223 throws ParsingException, IOException , XIncludeException { 1224 File input = new File (inputDir, "circle1.xml"); 1225 Document doc = builder.build(input); 1226 try { 1227 XIncluder.resolve(doc); 1228 fail("allowed parsed include of self"); 1229 } 1230 catch (InclusionLoopException success) { 1231 assertNotNull(success.getMessage()); 1232 assertEquals(input.toURL().toExternalForm(), success.getURI()); 1233 } 1234 } 1235 1236 1237 public void testInclusionLoopWithLength2Cycle() 1238 throws ParsingException, IOException , XIncludeException { 1239 1240 File input = new File (inputDir, "circle2a.xml"); 1241 File errorFile = new File (inputDir, "circle2b.xml"); 1242 Document doc = builder.build(input); 1243 try { 1244 XIncluder.resolve(doc); 1245 fail("allowed circular include, cycle length 1"); 1246 } 1247 catch (InclusionLoopException success) { 1248 assertTrue(success.getMessage().indexOf(errorFile.toURL().toExternalForm()) > 1); 1249 assertTrue(success.getMessage().indexOf(input.toURL().toExternalForm()) > 1); 1250 assertEquals(errorFile.toURL().toExternalForm(), success.getURI()); 1251 } 1252 1253 } 1254 1255 1256 public void testMissingHref() 1257 throws ParsingException, IOException , XIncludeException { 1258 1259 File input = new File (inputDir, "missinghref.xml"); 1260 Document doc = builder.build(input); 1261 try { 1262 XIncluder.resolve(doc); 1263 fail("allowed missing href"); 1264 } 1265 catch (NoIncludeLocationException success) { 1266 assertNotNull(success.getMessage()); 1267 assertEquals(doc.getBaseURI(), success.getURI()); 1268 } 1269 1270 } 1271 1272 1273 public void testBadParseAttribute() 1274 throws ParsingException, IOException , XIncludeException { 1275 1276 File input = new File (inputDir, "badparseattribute.xml"); 1277 Document doc = builder.build(input); 1278 try { 1279 XIncluder.resolve(doc); 1280 fail("allowed bad parse attribute"); 1281 } 1282 catch (BadParseAttributeException success) { 1283 assertNotNull(success.getMessage()); 1284 URL u1 = input.toURL(); 1285 URL u2 = new URL (success.getURI()); 1286 assertEquals(u1, u2); 1287 } 1288 1289 } 1290 1291 1292 public void testUnavailableResource() 1293 throws ParsingException, IOException , XIncludeException { 1294 File input = new File (inputDir, "missingfile.xml"); 1295 Document doc = builder.build(input); 1296 try { 1297 XIncluder.resolve(doc); 1298 fail("allowed unresolvable resource"); 1299 } 1300 catch (IOException success) { 1301 assertNotNull(success.getMessage()); 1302 } 1303 1304 } 1305 1306 1307 public void testFallback() 1308 throws ParsingException, IOException , XIncludeException { 1309 1310 File input = new File (inputDir, "fallbacktest.xml"); 1311 Document doc = builder.build(input); 1312 Document result = XIncluder.resolve(doc); 1313 Document expectedResult = builder.build( 1314 new File (outputDir, "fallbacktest.xml") 1315 ); 1316 assertEquals(expectedResult, result); 1317 1318 } 1319 1320 1321 public void testFallbackWithRecursiveInclude() 1322 throws ParsingException, IOException , XIncludeException { 1323 1324 File input = new File (inputDir, "fallbacktest2.xml"); 1325 Document doc = builder.build(input); 1326 Document result = XIncluder.resolve(doc); 1327 Document expectedResult = builder.build( 1328 new File (outputDir, "fallbacktest2.xml") 1329 ); 1330 assertEquals(expectedResult, result); 1331 1332 } 1333 1334 1335 public void testEncodingAttribute() 1336 throws ParsingException, IOException , XIncludeException { 1337 1338 File input = new File (inputDir, "utf16.xml"); 1339 Document doc = builder.build(input); 1340 Document result = XIncluder.resolve(doc); 1341 Document expectedResult = builder.build( 1342 new File (outputDir, "utf16.xml") 1343 ); 1344 assertEquals(expectedResult, result); 1345 1346 } 1347 1348 1349 public void testXPointerBareNameID() 1350 throws ParsingException, IOException , XIncludeException { 1351 1352 File input = new File (inputDir, "xptridtest.xml"); 1353 Document doc = builder.build(input); 1354 Document result = XIncluder.resolve(doc); 1355 Document expectedResult = builder.build( 1356 new File (outputDir, "xptridtest.xml") 1357 ); 1358 assertEquals(expectedResult, result); 1359 1360 } 1361 1362 1363 public void testShorthandXPointerMatchesNothing() 1364 throws ParsingException, IOException { 1365 1366 File input = new File (inputDir, "xptridtest2.xml"); 1367 Document doc = builder.build(input); 1368 try { 1369 XIncluder.resolve(doc); 1370 fail("Resolved a document with an XPointer " + 1371 "that selects no subresource"); 1372 } 1373 catch (XIncludeException success) { 1374 assertNotNull(success.getMessage()); 1375 URL u1 = input.toURL(); 1378 URL u2 = new URL (success.getURI()); 1379 assertEquals(u1, u2); 1380 } 1381 1382 1383 1391 1392 } 1393 1394 1395 public void testXPointerPureTumbler() 1396 throws ParsingException, IOException , XIncludeException { 1397 1398 File input = new File (inputDir, "xptrtumblertest.xml"); 1399 Document doc = builder.build(input); 1400 Document result = XIncluder.resolve(doc); 1401 Document expectedResult = builder.build( 1402 new File (outputDir, "xptrtumblertest.xml") 1403 ); 1404 assertEquals(expectedResult, result); 1405 1406 } 1407 1408 1409 public void testUnrecognizedColonizedSchemeNameBackedUpByTumbler() 1410 throws ParsingException, IOException , XIncludeException { 1411 1412 File input = new File (inputDir, "colonizedschemename.xml"); 1413 Document doc = builder.build(input); 1414 Document result = XIncluder.resolve(doc); 1415 Document expectedResult = builder.build( 1416 new File (outputDir, "xptrtumblertest.xml") 1417 ); 1418 assertEquals(expectedResult, result); 1419 1420 } 1421 1422 1423 public void testXPointerSyntaxErrorInSecondPart() 1424 throws ParsingException, IOException { 1425 1426 File input = new File (inputDir, "laterfailure.xml"); 1427 Document doc = builder.build(input); 1428 try { 1429 XIncluder.resolve(doc); 1430 fail("Didn't find syntax error in 2nd XPointer part" + 1431 " when the first part resolved successfully"); 1432 } 1433 catch (XIncludeException success) { 1434 assertNotNull(success.getMessage()); 1435 } 1436 1437 } 1438 1439 1440 public void testBadElementSchemeDataIsNotAnError() 1441 throws ParsingException, IOException , XIncludeException { 1442 1443 File input = new File (inputDir, "badelementschemedata.xml"); 1444 Document doc = builder.build(input); 1445 Document result = XIncluder.resolve(doc); 1446 Document expectedResult = builder.build( 1447 new File (outputDir, "badelementschemedata.xml") 1448 ); 1449 1451 assertEquals(expectedResult, result); 1452 1453 } 1454 1455 1456 public void testXPointerSyntaxErrorMissingFinalParenthesis() 1457 throws ParsingException, IOException { 1458 1459 File input = new File (inputDir, "laterfailure2.xml"); 1460 Document doc = builder.build(input); 1461 try { 1462 XIncluder.resolve(doc); 1463 fail("Didn't find syntax error in 2nd XPointer part" + 1464 " when the first part resolved successfully"); 1465 } 1466 catch (XIncludeException success) { 1467 assertNotNull(success.getMessage()); 1468 } 1469 1470 } 1471 1472 1473 public void testOnlyXPointer() 1476 throws ParsingException, IOException , XIncludeException { 1477 1478 File input = new File (inputDir, "onlyxpointer.xml"); 1479 Document doc = builder.build(input); 1480 Document result = XIncluder.resolve(doc); 1481 Document expectedResult = builder.build( 1482 new File (outputDir, "onlyxpointer.xml") 1483 ); 1484 1485 assertEquals(expectedResult, result); 1486 1487 } 1488 1489 1490 public void testXPointerTripleTumbler() 1494 throws ParsingException, IOException , XIncludeException { 1495 1496 File input = new File (inputDir, "xptr2tumblertest.xml"); 1497 Document doc = builder.build(input); 1498 Document result = XIncluder.resolve(doc); 1499 Document expectedResult = builder.build( 1500 new File (outputDir, "xptrtumblertest.xml") 1501 ); 1502 assertEquals(expectedResult, result); 1503 1504 } 1505 1506 1507 public void testXPointerDoubleTumbler() 1512 throws ParsingException, IOException , XIncludeException { 1513 1514 File input = new File (inputDir, "xptrdoubletumblertest.xml"); 1515 Document doc = builder.build(input); 1516 Document result = XIncluder.resolve(doc); 1517 Document expectedResult = builder.build( 1518 new File (outputDir, "xptrtumblertest.xml") 1519 ); 1520 assertEquals(expectedResult, result); 1521 1522 } 1523 1524 1525 public void testXPointerDoubleElementByID() 1530 throws ParsingException, IOException , XIncludeException { 1531 1532 File input = new File (inputDir, "xptrdoubleelementtest.xml"); 1533 Document doc = builder.build(input); 1534 Document result = XIncluder.resolve(doc); 1535 Document expectedResult = builder.build( 1536 new File (outputDir, "xptrdoubleelementtest.xml") 1537 ); 1538 assertEquals(expectedResult, result); 1539 1540 } 1541 1542 1543 public void testXPointerDoubleElementByChildSequence() 1548 throws ParsingException, IOException , XIncludeException { 1549 1550 File input = new File (inputDir, "xptrdoublechildsequence.xml"); 1551 Document doc = builder.build(input); 1552 Document result = XIncluder.resolve(doc); 1553 Document expectedResult = builder.build( 1554 new File (outputDir, "xptrdoubleelementtest.xml") 1555 ); 1556 assertEquals(expectedResult, result); 1557 1558 } 1559 1560 1561 public void testXPointerFailureIsAResourceError() 1564 throws ParsingException, IOException , XIncludeException { 1565 1566 File input = new File ( 1567 "data/xinclude/input/xptrtumblerfailsbutfallback.xml" 1568 ); 1569 Document doc = builder.build(input); 1570 Document result = XIncluder.resolve(doc); 1571 Document expectedResult = builder.build( 1574 new File (outputDir, "xptrtumblertest.xml") 1575 ); 1576 assertEquals(expectedResult, result); 1577 1578 } 1579 1580 1581 1591 public void testXPointerSyntaxErrorIsAResourceError() 1592 throws ParsingException, IOException , XIncludeException { 1593 1594 File input = new File ( 1595 "data/xinclude/input/xptrsyntaxerrorbutfallback.xml" 1596 ); 1597 Document doc = builder.build(input); 1598 Document result = XIncluder.resolve(doc); 1599 Document expectedResult = builder.build( 1600 new File (outputDir, "xptrtumblertest.xml") 1601 ); 1602 assertEquals(expectedResult, result); 1603 1604 } 1605 1606 1607 public void testXPointerTumblerWithWhiteSpace() 1612 throws ParsingException, IOException , XIncludeException { 1613 1614 File input 1615 = new File (inputDir, "xptrtumblertest3.xml"); 1616 Document doc = builder.build(input); 1617 Document result = XIncluder.resolve(doc); 1618 Document expectedResult = builder.build( 1619 new File (outputDir, "xptrtumblertest.xml") 1620 ); 1621 assertEquals(expectedResult, result); 1622 1623 } 1624 1625 1626 public void testXPointerTumblerMatchesNothing() 1627 throws ParsingException, IOException { 1628 1629 File input = new File ( 1630 "data/xinclude/input/xptrtumblertest2.xml" 1631 ); 1632 Document doc = builder.build(input); 1633 try { 1634 XIncluder.resolve(doc); 1635 fail("Did not error on XPointer matching nothing"); 1636 } 1637 catch (XIncludeException success) { 1638 assertNotNull(success.getMessage()); 1639 URL u1 = input.toURL(); 1640 URL u2 = new URL (success.getURI()); 1641 assertEquals(u1, u2); 1642 } 1643 1644 } 1645 1646 1647 public void testMalformedXPointer() 1648 throws ParsingException, IOException { 1649 1650 File input = new File (inputDir, "badxptr.xml"); 1651 Document doc = builder.build(input); 1652 try { 1653 XIncluder.resolve(doc); 1654 fail("Allowed malformed XPointer"); 1655 } 1656 catch (XIncludeException success) { 1657 assertNotNull(success.getMessage()); 1658 URL u1 = input.toURL(); 1659 URL u2 = new URL (success.getURI()); 1660 assertEquals(u1, u2); 1661 } 1662 1663 } 1664 1665 1666 public void testAnotherMalformedXPointer() 1667 throws ParsingException, IOException { 1668 1669 File input = new File (inputDir, "badxptr2.xml"); 1671 Document doc = builder.build(input); 1672 try { 1673 XIncluder.resolve(doc); 1674 fail("Allowed another malformed XPointer"); 1675 } 1676 catch (XIncludeException success) { 1677 assertNotNull(success.getMessage()); 1678 URL u1 = input.toURL(); 1679 URL u2 = new URL (success.getURI()); 1680 assertEquals(u1, u2); 1681 } 1682 1683 } 1684 1685 1686 public void testMalformedXPointerWithFallback() 1687 throws ParsingException, IOException , XIncludeException { 1688 1689 File input = new File (inputDir, "xptrfallback.xml"); 1690 Document doc = builder.build(input); 1691 Document result = XIncluder.resolve(doc); 1692 Document expectedResult = builder.build(new File ( 1693 "data/xinclude/output/xptrfallback.xml") 1694 ); 1695 assertEquals(expectedResult, result); 1696 1697 } 1698 1699 1700 public void testIDAndTumbler() 1701 throws ParsingException, IOException , XIncludeException { 1702 1703 File input = new File ( 1704 "data/xinclude/input/xptridandtumblertest.xml" 1705 ); 1706 Document doc = builder.build(input); 1707 Document result = XIncluder.resolve(doc); 1708 Document expectedResult = builder.build(new File ( 1709 "data/xinclude/output/xptridandtumblertest.xml") 1710 ); 1711 assertEquals(expectedResult, result); 1712 1713 } 1714 1715 1716 public void testAutoDetectUTF16BigEndianWithByteOrderMark() 1717 throws ParsingException, IOException , XIncludeException { 1718 1719 File input = new File ( 1720 "data/xinclude/input/UTF16BigEndianWithByteOrderMark.xml" 1721 ); 1722 Document doc = builder.build(input); 1723 Document result = XIncluder.resolve(doc); 1724 Document expectedResult = builder.build(new File ( 1725 "data/xinclude/output/UTF16BigEndianWithByteOrderMark.xml") 1726 ); 1727 assertEquals(expectedResult, result); 1728 1729 } 1730 1731 1732 public void testAutoDetectUTF16LittleEndianWithByteOrderMark() 1733 throws ParsingException, IOException , XIncludeException { 1734 1735 File input = new File ( 1736 "data/xinclude/input/UTF16LittleEndianWithByteOrderMark.xml" 1737 ); 1738 Document doc = builder.build(input); 1739 Document result = XIncluder.resolve(doc); 1740 Document expectedResult = builder.build(new File ( 1741 "data/xinclude/output/UTF16LittleEndianWithByteOrderMark.xml" 1742 )); 1743 assertEquals(expectedResult, result); 1744 1745 } 1746 1747 1748 public void testAutoDetectUTF8WithByteOrderMark() 1749 throws ParsingException, IOException , XIncludeException { 1750 1751 File input = new File ( 1752 "data/xinclude/input/UTF8WithByteOrderMark.xml" 1753 ); 1754 Document doc = builder.build(input); 1755 Document result = XIncluder.resolve(doc); 1756 Document expectedResult = builder.build( 1757 new File (outputDir, "UTF8WithByteOrderMark.xml") 1758 ); 1759 assertEquals(expectedResult, result); 1760 1761 } 1762 1763 1764 public void testAutoDetectUnicodeBigUnmarked() 1765 throws ParsingException, IOException , XIncludeException { 1766 1767 File input = new File ( 1768 "data/xinclude/input/UnicodeBigUnmarked.xml" 1769 ); 1770 Document doc = builder.build(input); 1771 Document result = XIncluder.resolve(doc); 1772 Document expectedResult = builder.build( 1773 new File (outputDir, "UnicodeBigUnmarked.xml") 1774 ); 1775 assertEquals(expectedResult, result); 1776 1777 } 1778 1779 1780 public void testUnicodeLittleUnmarked() 1781 throws ParsingException, IOException , XIncludeException { 1782 1783 File input = new File ( 1784 "data/xinclude/input/UnicodeLittleUnmarked.xml" 1785 ); 1786 Document doc = builder.build(input); 1787 Document result = XIncluder.resolve(doc); 1788 Document expectedResult = builder.build( 1789 new File (outputDir, "UnicodeLittleUnmarked.xml") 1790 ); 1791 assertEquals(expectedResult, result); 1792 1793 } 1794 1795 1796 1827 1828 1829 public void testEBCDIC() 1830 throws ParsingException, IOException , XIncludeException { 1831 1832 File input = new File (inputDir, "EBCDIC.xml"); 1833 Document doc = builder.build(input); 1834 Document result = XIncluder.resolve(doc); 1835 Document expected = builder.build(new File (outputDir, "EBCDIC.xml")); 1836 assertEquals(expected, result); 1837 1838 } 1839 1840 1841 public void testXIncludeTestSuite() 1851 throws ParsingException, IOException , XIncludeException { 1852 1853 File testDescription = new File ("data"); 1854 testDescription = new File (testDescription, "XInclude-Test-Suite"); 1855 testDescription = new File (testDescription, "testdescr.xml"); 1856 URL baseURL = testDescription.toURL(); 1857 if (!testDescription.exists()) { 1858 baseURL = new URL ( 1859 "http://dev.w3.org/cvsweb/~checkout~/2001/" + 1860 "XInclude-Test-Suite/testdescr.xml?content-type=text/" + 1861 "plain&only_with_tag=HEAD" 1862 ); 1863 } 1864 Document master = builder.build(baseURL.toExternalForm()); 1865 Element testsuite = master.getRootElement(); 1866 Elements testcases = testsuite.getChildElements("testcases"); 1867 for (int i = 0; i < testcases.size(); i++) { 1868 Element group = testcases.get(i); 1869 String basedir = group.getAttributeValue("basedir"); 1870 if (basedir.startsWith("Harold")) { 1871 continue; 1876 } 1877 Elements cases = group.getChildElements("testcase"); 1878 for (int j = 0; j < cases.size(); j++) { 1879 Element testcase = cases.get(j); 1880 String id = testcase.getAttributeValue("id"); 1881 String features = testcase.getAttributeValue("features"); 1882 if (features != null) { 1883 if (features.indexOf("unexpanded-entities") >= 0) continue; 1884 if (features.indexOf("unparsed-entities") >= 0) continue; 1885 if (features.indexOf("xpointer-scheme") >= 0) continue; 1886 } 1887 String description 1888 = testcase.getFirstChildElement("description").getValue(); 1889 if (!basedir.endsWith("/")) basedir += '/'; 1890 URL input = new URL (baseURL, basedir); 1891 input = new URL (input, testcase.getAttributeValue("href")); 1892 Element output = testcase.getFirstChildElement("output"); 1893 if (output == null) { try { 1895 Document doc = builder.build(input.toExternalForm()); 1896 XIncluder.resolveInPlace(doc); 1897 fail("Failed test " + id + ": " + description); 1898 } 1899 catch (XIncludeException success) { 1900 assertNotNull(success.getMessage()); 1901 } 1902 catch (IOException success) { 1903 if (baseURL.getProtocol().equals("file")) { 1904 assertNotNull("Problem processing " + input, success.getMessage()); 1905 } 1906 } 1907 catch (ParsingException success) { 1908 assertNotNull(success.getMessage()); 1909 } 1910 } 1911 else { 1912 URL result = new URL (baseURL, basedir); 1913 result = new URL (result, output.getValue()); 1914 Document expected = builder.build(result.toExternalForm()); 1915 Document doc = builder.build(input.toExternalForm()); 1916 XIncluder.resolveInPlace(doc); 1917 try { 1918 assertEquals("Error when processing " 1919 + result, expected, doc); 1920 } 1921 catch (AssertionFailedError t) { 1922 DocType doctype = expected.getDocType(); 1926 DocType actualDoctype = doc.getDocType(); 1927 if (doctype != null) { 1928 expected.removeChild(doctype); 1929 assertEquals("Error when processing " 1930 + input, expected, doc); 1931 } 1932 else if (actualDoctype != null) { 1933 doc.removeChild(actualDoctype); 1934 assertEquals("Error when processing " 1935 + input, expected, doc); 1936 } 1937 else { 1938 fail(); 1939 } 1940 } 1941 } 1942 } 1943 } 1944 1945 } 1946 1947 1948 private void compare(File expected, File input) 1949 throws IOException , ParsingException, XIncludeException { 1950 1951 Document expectedDoc = builder.build(expected); 1952 Document doc = builder.build(input); 1953 XIncluder.resolveInPlace(doc); 1954 assertEquals("Error when processing " 1955 + input.getName(), expectedDoc, doc); 1956 1957 } 1958 1959 1960 1986 1987 1988 public void testChildDocumentSetsErrorURI() 1992 throws ParsingException, IOException , XIncludeException { 1993 1994 File input = new File (inputDir, "toplevel.xml"); 1995 File error = new File (inputDir, "onedown.xml"); 1996 Document doc = builder.build(input); 1997 try { 1998 XIncluder.resolve(doc); 1999 fail("Missing HREF not detected"); 2000 } 2001 catch (NoIncludeLocationException success) { 2002 assertNotNull(success.getMessage()); 2003 URL u1 = error.toURL(); 2004 URL u2 = new URL (success.getURI()); 2005 assertEquals(u1, u2); 2006 } 2007 2008 } 2009 2010 2011 public void testColonizedNameForIdValueInElementScheme() 2012 throws ParsingException, IOException { 2013 2014 File input = new File (inputDir, "badxptr3.xml"); 2015 Document doc = builder.build(input); 2016 try { 2017 XIncluder.resolve(doc); 2018 fail("Bad ID in element not detected"); 2019 } 2020 catch (XIncludeException success) { 2021 assertNotNull(success.getMessage()); 2022 } 2023 2024 } 2025 2026 2027 public void testBadIdValueInElementScheme() 2028 throws ParsingException, IOException { 2029 2030 File input = new File (inputDir, "badxptr4.xml"); 2031 Document doc = builder.build(input); 2032 try { 2033 XIncluder.resolve(doc); 2034 fail("Bad ID in element not detected"); 2035 } 2036 catch (XIncludeException success) { 2037 assertNotNull(success.getMessage()); 2038 } 2039 2040 } 2041 2042 2043 public void testCirclePointer() 2044 throws ParsingException, IOException , XIncludeException { 2045 2046 File input = new File (inputDir, "circlepointer1.xml"); 2047 Document doc = builder.build(input); 2048 try { 2049 XIncluder.resolve(doc); 2050 fail("Allowed circular reference via XPointer"); 2051 } 2052 catch (InclusionLoopException success) { 2053 assertNotNull(success.getMessage()); 2054 } 2055 2056 } 2057 2058 2059 public void testXPointerOverridesFragmentID() 2060 throws ParsingException, IOException , XIncludeException { 2061 2062 File input = new File (inputDir, "xpointeroverridesfragmentid.xml" 2063 ); 2064 Document doc = builder.build(input); 2065 try { 2066 XIncluder.resolve(doc); 2067 fail("Allowed href attribute with fragment ID"); 2068 } 2069 catch (XIncludeException success) { 2070 assertNotNull(success.getMessage()); 2071 } 2072 2073 } 2074 2075 2076 public void testFailsOnFragmentID() 2077 throws ParsingException, IOException , XIncludeException { 2078 2079 File input = new File (inputDir, "ignoresfragmentid.xml"); 2080 Document doc = builder.build(input); 2081 try { 2082 XIncluder.resolve(doc); 2083 fail("Allowed href attribute with fragment ID"); 2084 } 2085 catch (XIncludeException success) { 2086 assertNotNull(success.getMessage()); 2087 } 2088 2089 } 2090 2091 2092 public void testFragmentIDsAreRemovedFromElementBaseURIsAfterInclusion() 2095 throws ParsingException, IOException , XIncludeException { 2096 2097 File input = new File (inputDir, "basewithfragmentid.xml"); 2098 Document doc = builder.build(input); 2099 Document result = XIncluder.resolve(doc); 2100 Document expectedResult = builder.build( 2101 new File (outputDir, "basewithfragmentid.xml") 2102 ); 2103 assertEquals(expectedResult, result); 2104 2105 } 2106 2107 2108 public void testIncludeLowerCaseFileNames() 2109 throws ParsingException, IOException , XIncludeException { 2110 2111 File input = new File (inputDir, "lowercasealphabet.xml"); 2112 Document doc = builder.build(input); 2113 Document result = XIncluder.resolve(doc); 2114 Document expectedResult = builder.build( 2115 new File (outputDir, "lowercasealphabet.xml") 2116 ); 2117 assertEquals(expectedResult, result); 2118 2119 } 2120 2121 2122 public void testIncludeUpperCaseFileNames() 2123 throws ParsingException, IOException , XIncludeException { 2124 2125 File input = new File (inputDir, "uppercasealphabet.xml"); 2126 Document doc = builder.build(input); 2127 Document result = XIncluder.resolve(doc); 2128 Document expectedResult = builder.build( 2129 new File (outputDir, "uppercasealphabet.xml") 2130 ); 2131 assertEquals(expectedResult, result); 2132 2133 } 2134 2135 2136 public void testIncludeDigitFileNames() 2137 throws ParsingException, IOException , XIncludeException { 2138 2139 File input = new File (inputDir, "numeric.xml"); 2140 Document doc = builder.build(input); 2141 Document result = XIncluder.resolve(doc); 2142 Document expectedResult = builder.build( 2143 new File (outputDir, "numeric.xml") 2144 ); 2145 assertEquals(expectedResult, result); 2146 2147 } 2148 2149 2150 public void testIncludeHighPunctuationFileNames() 2151 throws ParsingException, IOException , XIncludeException { 2152 2153 try { 2157 File f = new File (inputDir, "{|}.txt"); 2158 Writer out = new OutputStreamWriter ( 2159 new FileOutputStream (f), "UTF8"); 2160 out.write("{|}"); 2161 out.flush(); 2162 out.close(); 2163 2164 File input = new File (inputDir, "punctuation.xml"); 2165 Document doc = builder.build(input); 2166 Document result = XIncluder.resolve(doc); 2167 Document expectedResult = builder.build( 2168 new File (outputDir, "punctuation.xml") 2169 ); 2170 f.delete(); 2171 assertEquals(expectedResult, result); 2172 } 2173 catch (FileNotFoundException ex) { 2174 if (!windows) throw ex; 2177 } 2178 2179 } 2180 2181 2182 public void testMiddlePunctuationError() 2183 throws ParsingException, IOException , XIncludeException { 2184 2185 File input = new File (inputDir, "middlepunctuationerror.xml"); 2186 Document doc = builder.build(input); 2187 try { 2188 XIncluder.resolve(doc); 2189 fail("Allowed illegal IRI with right square bracket ]"); 2190 } 2191 catch (BadHrefAttributeException success) { 2192 assertNotNull(success.getMessage()); 2193 } 2194 2195 } 2196 2197 2198 public void testIncludeLowerPunctuationFileNames() 2199 throws ParsingException, IOException , XIncludeException { 2200 2201 try { 2202 File f = new File (inputDir, "!\"$&'+,.txt"); 2203 Writer out = new OutputStreamWriter ( 2204 new FileOutputStream (f), "UTF8"); 2205 out.write("!\"$&'+,"); 2206 out.flush(); 2207 out.close(); 2208 2209 File input = new File (inputDir, "lowerpunctuation.xml"); 2210 Document doc = builder.build(input); 2211 Document result = XIncluder.resolve(doc); 2212 Document expectedResult = builder.build( 2213 new File (outputDir, "lowerpunctuation.xml") 2214 ); 2215 f.delete(); 2216 assertEquals(expectedResult, result); 2217 } 2218 catch (FileNotFoundException ex) { 2219 if (!windows) throw ex; 2222 } 2223 2224 } 2225 2226 2227 public void testLineEnds() 2228 throws ParsingException, IOException , XIncludeException { 2229 2230 File input = new File (inputDir, "lineends.xml"); 2231 Document doc = builder.build(input); 2232 Document result = XIncluder.resolve(doc); 2233 Document expected = builder.build( 2234 new File (outputDir, "lineends.xml") 2235 ); 2236 assertEquals(expected, result); 2237 2238 } 2239 2240 2241 public void testBadXPointerInFragmentIDIsFatalError() 2244 throws ParsingException, IOException , XIncludeException { 2245 2246 File input = new File ( 2247 "data/xinclude/input/meaninglessfragmentid.xml"); 2248 Document doc = builder.build(input); 2249 try { 2250 XIncluder.resolve(doc); 2251 fail("Allowed href attribute with fragment ID"); 2252 } 2253 catch (XIncludeException success) { 2254 assertNotNull(success.getMessage()); 2255 } 2256 2257 } 2258 2259 2260 public void testAcceptLanguageFrench() 2264 throws ParsingException, IOException , XIncludeException { 2265 2266 File input = new File (inputDir, "acceptfrench.xml"); 2267 Document doc = builder.build(input); 2268 Document result = XIncluder.resolve(doc); 2269 Document expectedResult = builder.build( 2270 new File (outputDir, "acceptfrench.xml") 2271 ); 2272 assertEquals(expectedResult, result); 2273 2274 } 2275 2276 2277 public void testAcceptLanguageEnglish() 2278 throws ParsingException, IOException , XIncludeException { 2279 2280 File input = new File (inputDir, "acceptenglish.xml"); 2281 Document doc = builder.build(input); 2282 Document result = XIncluder.resolve(doc); 2283 Document expectedResult = builder.build( 2284 new File (outputDir, "acceptenglish.xml") 2285 ); 2286 assertEquals(expectedResult, result); 2287 2288 } 2289 2290 2291 public void testAcceptPlainText() 2292 throws ParsingException, IOException , XIncludeException { 2293 2294 File input = new File (inputDir, "acceptplaintext.xml"); 2295 Document doc = builder.build(input); 2296 Document result = XIncluder.resolve(doc); 2297 Document expectedResult = builder.build( 2298 new File (outputDir, "acceptplaintext.xml") 2299 ); 2300 assertEquals(expectedResult, result); 2301 2302 } 2303 2304 2305 public void testAcceptHTML() 2306 throws ParsingException, IOException , XIncludeException { 2307 2308 File input = new File (inputDir, "accepthtml.xml"); 2309 Document doc = builder.build(input); 2310 Document result = XIncluder.resolve(doc); 2311 Document expectedResult = builder.build( 2312 new File (outputDir, "accepthtml.xml") 2313 ); 2314 assertEquals(expectedResult, result); 2315 2316 } 2317 2318 2319 public void testBadHTTPHeaderExceptionConstructor() { 2320 2321 String message = "test"; 2322 XIncludeException ex = new BadHTTPHeaderException( 2323 message, "http://www.example.com/"); 2324 assertEquals(message, ex.getMessage()); 2325 assertEquals("http://www.example.com/", ex.getURI()); 2326 2327 } 2328 2329 2330 public void testBadHrefAttributerExceptionConstructor() { 2331 2332 String message = "test"; 2333 Exception ex = new BadHrefAttributeException(message); 2334 assertEquals(message, ex.getMessage()); 2335 2336 } 2337 2338 2339 public void testPercentEscapesAreNotAllowedInXPointerAttributes() 2340 throws ParsingException, IOException , XIncludeException { 2341 2342 File input = new File (inputDir, "xpointerwithpercentescape.xml"); 2343 Document doc = builder.build(input); 2344 try { 2345 XIncluder.resolve(doc); 2346 fail("Allowed xpointer attribute with percent escape"); 2347 } 2348 catch (XIncludeException success) { 2349 assertNotNull(success.getMessage()); 2350 Exception cause = (Exception ) success.getCause(); 2351 assertNotNull(cause); 2352 } 2353 2354 } 2355 2356 2357 public void testMalformedEncodingAttribute() 2363 throws IOException , ParsingException, XIncludeException { 2364 2365 File input = new File (inputDir, "badencoding.xml"); 2366 Document doc = builder.build(input); 2367 try { 2368 XIncluder.resolve(doc); 2369 fail("Allowed encoding attribute with white space"); 2370 } 2371 catch (BadEncodingAttributeException success) { 2372 assertNotNull(success.getMessage()); 2373 assertTrue(success.getURI().endsWith(input.getName())); 2374 } 2375 2376 } 2377 2378 2379 public void testHiddenError() 2382 throws ParsingException, IOException , XIncludeException { 2383 2384 File input = new File (inputDir, "hiddenerror.xml"); 2385 Document doc = builder.build(input); 2386 XIncluder.resolve(doc); 2387 2388 } 2389 2390 2391 public void testHiddenError2() 2395 throws ParsingException, IOException , XIncludeException { 2396 2397 File input = new File (inputDir, "hiddenerror2.xml"); 2398 Document doc = builder.build(input); 2399 XIncluder.resolve(doc); 2400 2401 } 2402 2403 2404 public void testHiddenError3() 2408 throws ParsingException, IOException , XIncludeException { 2409 2410 File input = new File (inputDir, "hiddenerror3.xml"); 2411 Document doc = builder.build(input); 2412 XIncluder.resolve(doc); 2413 2414 } 2415 2416 2417 public void testXpointerAttributeContainsPercentEscapeInUnactivatedFallback() 2422 throws ParsingException, IOException , XIncludeException { 2423 2424 File input = new File (inputDir, "hiddenerror3.xml"); 2425 Document doc = builder.build(input); 2426 XIncluder.resolve(doc); 2427 2428 } 2429 2430 2431} | Popular Tags |