Clover coverage report - Clover results for XOM 1.2d1
Coverage timestamp: Wed Feb 8 2006 08:31:33 EST
file stats: LOC: 2,601   Methods: 149
NCLOC: 1,766   Classes: 7
 
 Source file Conditionals Statements Methods TOTAL
XIncludeTest.java 76.5% 92.2% 98.7% 92.6%
coverage coverage
 1    /* Copyright 2002-2005 Elliotte Rusty Harold
 2   
 3    This library is free software; you can redistribute it and/or modify
 4    it under the terms of version 2.1 of the GNU Lesser General Public
 5    License as published by the Free Software Foundation.
 6   
 7    This library is distributed in the hope that it will be useful,
 8    but WITHOUT ANY WARRANTY; without even the implied warranty of
 9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 10    GNU Lesser General Public License for more details.
 11   
 12    You should have received a copy of the GNU Lesser General Public
 13    License along with this library; if not, write to the
 14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 15    Boston, MA 02111-1307 USA
 16   
 17    You can contact Elliotte Rusty Harold by sending e-mail to
 18    elharo@metalab.unc.edu. Please include the word "XOM" in the
 19    subject line. The XOM home page is located at http://www.xom.nu/
 20    */
 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.Namespace;
 46    import nu.xom.Node;
 47    import nu.xom.NodeFactory;
 48    import nu.xom.Nodes;
 49    import nu.xom.ParsingException;
 50    import nu.xom.Serializer;
 51    import nu.xom.Text;
 52   
 53    import nu.xom.xinclude.BadEncodingAttributeException;
 54    import nu.xom.xinclude.BadHTTPHeaderException;
 55    import nu.xom.xinclude.BadHrefAttributeException;
 56    import nu.xom.xinclude.BadParseAttributeException;
 57    import nu.xom.xinclude.InclusionLoopException;
 58    import nu.xom.xinclude.NoIncludeLocationException;
 59    import nu.xom.xinclude.XIncludeException;
 60    import nu.xom.xinclude.XIncluder;
 61   
 62    /**
 63    * <p>
 64    * Unit tests for the XInclude and XPointer engines.
 65    * </p>
 66    *
 67    * @author Elliotte Rusty Harold
 68    * @version 1.1b3
 69    *
 70    */
 71    public class XIncludeTest extends XOMTestCase {
 72   
 73    private static boolean windows
 74    = System.getProperty("os.name", "Unix").indexOf("Windows") >= 0;
 75   
 76   
 77  137 public XIncludeTest(String name) {
 78  137 super(name);
 79    }
 80   
 81   
 82    private Builder builder = new Builder();
 83    private File inputDir;
 84    private File outputDir;
 85   
 86    // This class tests error conditions, which Xerces
 87    // annoyingly logs to System.err. This hides System.err
 88    // before each test and restores it after each test.
 89    private PrintStream systemErr = System.err;
 90   
 91   
 92  137 protected void setUp() {
 93   
 94  137 System.setErr(new PrintStream(new ByteArrayOutputStream()));
 95   
 96  137 inputDir = new File("data");
 97  137 inputDir = new File(inputDir, "xinclude");
 98  137 inputDir = new File(inputDir, "input");
 99   
 100  137 outputDir = new File("data");
 101  137 outputDir = new File(outputDir, "xinclude");
 102  137 outputDir = new File(outputDir, "output");
 103   
 104    }
 105   
 106   
 107  137 protected void tearDown() {
 108  137 System.setErr(systemErr);
 109    }
 110   
 111   
 112  0 private void dumpResult(File original, Document result)
 113    throws IOException {
 114   
 115  0 String name = original.getName();
 116  0 File debug = new File("data");
 117  0 debug = new File(debug, "xinclude");
 118  0 debug = new File(debug, "debug");
 119  0 File output = new File(debug, name);
 120  0 FileOutputStream out = new FileOutputStream(output);
 121  0 Serializer serializer = new Serializer(out);
 122  0 serializer.write(result);
 123   
 124    }
 125   
 126   
 127  1 public void testXPointersResolvedAgainstAcquiredInfoset()
 128    throws ParsingException, IOException, XIncludeException {
 129   
 130  1 File input = new File(inputDir, "tobintop.xml");
 131  1 Document doc = builder.build(input);
 132  1 Document result = XIncluder.resolve(doc);
 133  1 Document expected = builder.build(
 134    new File(outputDir, "tobintop.xml")
 135    );
 136  1 assertEquals(expected, result);
 137   
 138    }
 139   
 140   
 141  1 public void testXMLBaseUsedToResolveHref()
 142    throws ParsingException, IOException, XIncludeException {
 143   
 144  1 File input = new File(inputDir, "xmlbasetest.xml");
 145  1 Document doc = builder.build(input);
 146  1 Document result = XIncluder.resolve(doc);
 147  1 Document expected = builder.build(
 148    new File(outputDir, "xmlbasetest.xml")
 149    );
 150  1 assertEquals(expected, result);
 151   
 152    }
 153   
 154   
 155    // Tests that use XPointer to
 156    // grab a part of the document that contains an include element
 157    // and make sure that's fully resolved too
 158  1 public void testResolveThroughXPointer()
 159    throws ParsingException, IOException, XIncludeException {
 160   
 161  1 File input = new File(inputDir, "resolvethruxpointer.xml");
 162  1 Document doc = builder.build(input);
 163  1 Document result = XIncluder.resolve(doc);
 164  1 Document expectedResult = builder.build(
 165    new File(outputDir, "resolvethruxpointer.xml")
 166    );
 167  1 assertEquals(expectedResult, result);
 168   
 169    }
 170   
 171   
 172  1 public void testXMLBaseOnIncludeElementUsedToResolveHref()
 173    throws ParsingException, IOException, XIncludeException {
 174   
 175  1 File input = new File(inputDir, "xmlbasetest2.xml");
 176  1 Document doc = builder.build(input);
 177  1 Document result = XIncluder.resolve(doc);
 178  1 Document expected = builder.build(
 179    new File(outputDir, "xmlbasetest2.xml")
 180    );
 181  1 assertEquals(expected, result);
 182   
 183    }
 184   
 185   
 186  1 public void testXMLBaseRetainedFromUnincludedElement()
 187    throws ParsingException, IOException, XIncludeException {
 188   
 189  1 File input = new File(inputDir, "xmlbasetest3.xml");
 190  1 Document doc = builder.build(input);
 191  1 Document result = XIncluder.resolve(doc);
 192  1 Document expected = builder.build(
 193    new File(outputDir, "xmlbasetest3.xml")
 194    );
 195  1 assertEquals(expected, result);
 196   
 197    }
 198   
 199   
 200  1 public void testMarsh()
 201    throws ParsingException, IOException, XIncludeException {
 202   
 203  1 File input = new File(inputDir, "marshtest.xml");
 204  1 Document doc = builder.build(input);
 205  1 Document result = XIncluder.resolve(doc);
 206  1 Document expectedResult = builder.build(
 207    new File(outputDir, "marshtest.xml")
 208    );
 209  1 assertEquals(expectedResult, result);
 210   
 211    }
 212   
 213   
 214  1 public void testIncludeDocumentThatUsesIntradocumentReferences()
 215    throws ParsingException, IOException, XIncludeException {
 216   
 217  1 File input = new File(inputDir, "includedocumentwithintradocumentreferences.xml");
 218  1 Document doc = builder.build(input);
 219  1 Document result = XIncluder.resolve(doc);
 220  1 Document expectedResult = builder.build(
 221    new File(outputDir, "includedocumentwithintradocumentreferences.xml")
 222    );
 223  1 assertEquals(expectedResult, result);
 224   
 225    }
 226   
 227   
 228  1 public void testXMLLangAttributes()
 229    throws ParsingException, IOException, XIncludeException {
 230   
 231  1 File input = new File(inputDir, "langtest1.xml");
 232  1 Document doc = builder.build(input);
 233  1 Document result = XIncluder.resolve(doc);
 234  1 Document expectedResult = builder.build(
 235    new File(outputDir, "langtest1.xml")
 236    );
 237  1 assertEquals(expectedResult, result);
 238   
 239    }
 240   
 241   
 242  1 public void testInheritedXMLLangAttributes()
 243    throws ParsingException, IOException, XIncludeException {
 244   
 245  1 File input = new File(inputDir, "langtest2.xml");
 246  1 Document doc = builder.build(input);
 247  1 Document result = XIncluder.resolve(doc);
 248  1 Document expectedResult = builder.build(
 249    new File(outputDir, "langtest2.xml")
 250    );
 251  1 assertEquals(expectedResult, result);
 252   
 253    }
 254   
 255   
 256  1 public void testNoLanguageSpecified()
 257    throws ParsingException, IOException, XIncludeException {
 258   
 259  1 File input = new File(inputDir, "langtest3.xml");
 260  1 Document doc = builder.build(input);
 261  1 Document result = XIncluder.resolve(doc);
 262  1 Document expectedResult = builder.build(
 263    new File(outputDir, "langtest3.xml")
 264    );
 265  1 assertEquals(expectedResult, result);
 266   
 267    }
 268   
 269   
 270    // According to RFC 2396 empty string URI always refers to the
 271    // current document irrespective of base URI
 272  1 public void testXMLBaseNotUsedToResolveMissingHref()
 273    throws ParsingException, IOException, XIncludeException {
 274   
 275  1 File input = new File(inputDir, "marshtestwithxmlbase.xml");
 276  1 Document doc = builder.build(input);
 277  1 Document result = XIncluder.resolve(doc);
 278  1 Document expectedResult = builder.build(
 279    new File(outputDir, "marshtestwithxmlbase.xml")
 280    );
 281  1 assertEquals(expectedResult, result);
 282   
 283    }
 284   
 285   
 286  1 public void testEmptyHrefTreatedSameAsMissingHref()
 287    throws ParsingException, IOException, XIncludeException {
 288   
 289  1 File input = new File(inputDir, "marshtestwithxmlbaseandemptyhref.xml");
 290  1 Document doc = builder.build(input);
 291  1 Document result = XIncluder.resolve(doc);
 292  1 Document expectedResult = builder.build(
 293    new File(outputDir, "marshtestwithxmlbase.xml")
 294    );
 295  1 assertEquals(expectedResult, result);
 296   
 297    }
 298   
 299   
 300  1 public void testBaselessDocument()
 301    throws IOException, ParsingException, XIncludeException {
 302   
 303  1 Element root = new Element("root");
 304  1 Element child1 = new Element("xi:include", XIncluder.XINCLUDE_NS);
 305  1 child1.addAttribute(new Attribute("xpointer", "p1"));
 306  1 Element child2 = new Element("child2");
 307  1 root.appendChild(child1);
 308  1 root.appendChild(child2);
 309  1 child2.addAttribute(new Attribute("id", "p1", Attribute.Type.ID));
 310  1 Document in = new Document(root);
 311  1 Document out = XIncluder.resolve(in);
 312  1 String result = out.toXML();
 313  1 assertEquals("<?xml version=\"1.0\"?>\n" +
 314    "<root><child2 id=\"p1\" /><child2 id=\"p1\" /></root>\n", result);
 315   
 316    }
 317   
 318   
 319  1 public void testRelativeURLInBaselessDocument()
 320    throws IOException, ParsingException, XIncludeException {
 321   
 322  1 Element root = new Element("root");
 323  1 Element child1 = new Element("xi:include", XIncluder.XINCLUDE_NS);
 324  1 child1.addAttribute(new Attribute("href", "test.xml"));
 325  1 Element child2 = new Element("child2");
 326  1 root.appendChild(child1);
 327  1 root.appendChild(child2);
 328  1 Document in = new Document(root);
 329  1 try {
 330  1 XIncluder.resolve(in);
 331  0 fail("Resolved relative URI in baseless document");
 332    }
 333    catch (BadHrefAttributeException success) {
 334  1 assertEquals(
 335    "Could not resolve relative URI test.xml because the "
 336    + "xi:include element does not have a base URI.",
 337    success.getMessage());
 338    }
 339   
 340    }
 341   
 342   
 343  1 public void testIncludeTextWithCustomNodeFactory()
 344    throws ParsingException, IOException, XIncludeException {
 345   
 346  1 File input = new File(inputDir, "c2.xml");
 347  1 Builder builder = new Builder(new TextNodeFactory());
 348  1 Document doc = builder.build(input);
 349  1 Document result = XIncluder.resolve(doc, builder);
 350  1 Document expectedResult = builder.build(
 351    new File(outputDir, "c2.xml")
 352    );
 353  1 assertEquals(expectedResult, result);
 354  1 Element root = result.getRootElement();
 355  1 for (int i = 0; i < root.getChildCount(); i++) {
 356  3 Node node = root.getChild(i);
 357  3 if (node instanceof Text) {
 358  2 assertTrue(node instanceof TextSubclass);
 359    }
 360    }
 361   
 362    }
 363   
 364   
 365  1 public void testParseEqualsTextWithNodeFactoryThatRemovesAllTextNodes()
 366    throws ParsingException, IOException, XIncludeException {
 367   
 368  1 File input = new File(inputDir, "c2.xml");
 369  1 Document doc = builder.build(input);
 370  1 Document result = XIncluder.resolve(doc, new Builder(new TextFilter()));
 371  1 Document expectedResult = builder.build(
 372    new File(outputDir, "c2a.xml")
 373    );
 374  1 assertEquals(expectedResult, result);
 375   
 376    }
 377   
 378   
 379    private static class TextFilter extends NodeFactory {
 380   
 381  1 public Nodes makeText(String data) {
 382  1 return new Nodes();
 383    }
 384   
 385    }
 386   
 387   
 388  1 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithComments()
 389    throws ParsingException, IOException, XIncludeException {
 390   
 391  1 File input = new File(inputDir, "c2.xml");
 392  1 Document doc = builder.build(input);
 393  1 Document result = XIncluder.resolve(doc, new Builder(new TextToComment()));
 394  1 Document expectedResult = builder.build(
 395    new File(outputDir, "c2b.xml")
 396    );
 397  1 assertEquals(expectedResult, result);
 398   
 399    }
 400   
 401   
 402    private static class TextToComment extends NodeFactory {
 403   
 404  1 public Nodes makeText(String data) {
 405  1 return new Nodes(new Comment(data));
 406    }
 407   
 408    }
 409   
 410   
 411  1 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithAttributes()
 412    throws ParsingException, IOException, XIncludeException {
 413   
 414  1 File input = new File(inputDir, "c2.xml");
 415  1 Document doc = builder.build(input);
 416  1 Document result = XIncluder.resolve(doc, new Builder(new TextToAttribute()));
 417  1 Document expectedResult = builder.build(
 418    new File(outputDir, "c2c.xml")
 419    );
 420  1 assertEquals(expectedResult, result);
 421   
 422    }
 423   
 424   
 425  1 public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithTwoElements()
 426    throws ParsingException, IOException, XIncludeException {
 427   
 428  1 File input = new File(inputDir, "c2.xml");
 429  1 Document doc = builder.build(input);
 430  1 Document result = XIncluder.resolve(doc, new Builder(new TextToElements()));
 431  1 Document expectedResult = builder.build(
 432    new File(outputDir, "c2d.xml")
 433    );
 434  1 assertEquals(expectedResult, result);
 435   
 436    }
 437   
 438   
 439    private static class TextToElements extends NodeFactory {
 440   
 441  1 public Nodes makeText(String data) {
 442  1 Nodes result = new Nodes();
 443  1 result.append(new Element("empty1"));
 444  1 result.append(new Element("empty2"));
 445  1 return result;
 446    }
 447   
 448    }
 449   
 450   
 451    private static class TextToAttribute extends NodeFactory {
 452   
 453  1 public Nodes makeText(String data) {
 454  1 return new Nodes(new Attribute("name", data));
 455    }
 456   
 457    }
 458   
 459   
 460  1 public void testUnrecognizedXPointerScheme()
 461    throws ParsingException, IOException {
 462   
 463  1 File input = new File(inputDir, "unrecognizedscheme.xml");
 464  1 Document doc = builder.build(input);
 465  1 try {
 466  1 XIncluder.resolve(doc);
 467  0 fail("Allowed unrecognized scheme");
 468    }
 469    catch (XIncludeException success) {
 470  1 assertNotNull(success.getMessage());
 471    }
 472   
 473    }
 474   
 475   
 476  1 public void testUnrecognizedXPointerSchemeWithFallback()
 477    throws IOException, ParsingException, XIncludeException {
 478   
 479  1 File input = new File(inputDir, "unrecognizedschemewithfallback.xml");
 480  1 File output = new File(outputDir, "unrecognizedschemewithfallback.xml");
 481  1 Document doc = builder.build(input);
 482  1 Document actual = XIncluder.resolve(doc);
 483  1 Document expected = builder.build(output);
 484  1 assertEquals(expected, actual);
 485   
 486    }
 487   
 488   
 489  1 public void testIncludeTextWithCustomNodeFactoryThatChangesElementNames()
 490    throws ParsingException, IOException, XIncludeException {
 491   
 492  1 File input = new File(inputDir, "c1.xml");
 493  1 Document doc = builder.build(input);
 494  1 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.CFactory()));
 495  1 Document expectedResult = builder.build(
 496    new File(outputDir, "c1a.xml")
 497    );
 498  1 assertEquals(expectedResult, result);
 499   
 500    }
 501   
 502   
 503  1 public void testIncludeTextWithCustomNodeFactoryThatOnlyReturnsRoot()
 504    throws ParsingException, IOException, XIncludeException {
 505   
 506  1 File input = new File(inputDir, "c1.xml");
 507  1 Document doc = builder.build(input);
 508  1 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.MinimizingFactory()));
 509  1 Document expectedResult = builder.build(
 510    new File(outputDir, "c1b.xml")
 511    );
 512  1 assertEquals(expectedResult, result);
 513   
 514    }
 515   
 516   
 517  1 public void testIncludeTextWithCustomNodeFactoryThatFiltersElementsNamedB()
 518    throws ParsingException, IOException, XIncludeException {
 519   
 520  1 File input = new File(inputDir, "d1.xml");
 521  1 Document doc = builder.build(input);
 522  1 Document result = XIncluder.resolve(doc, new Builder(new NodeFactoryTest.BFilter()));
 523  1 Document expectedResult = builder.build(
 524    new File(outputDir, "d1.xml")
 525    );
 526  1 assertEquals(expectedResult, result);
 527   
 528    }
 529   
 530   
 531  1 public void testIncludeTextWithCustomNodeFactoryThatReturnsEachNonRootElementThreeTimes()
 532    throws ParsingException, IOException, XIncludeException {
 533   
 534  1 File input = new File(inputDir, "c1.xml");
 535  1 Document doc = builder.build(input);
 536  1 Document result = XIncluder.resolve(doc,
 537    new Builder(new NodeFactoryTest.TripleElementFilter()));
 538  1 Document expectedResult = builder.build(
 539    new File(outputDir, "triple.xml")
 540    );
 541  1 assertEquals(expectedResult, result);
 542   
 543    }
 544   
 545   
 546  1 public void test1()
 547    throws ParsingException, IOException, XIncludeException {
 548   
 549  1 File input = new File(inputDir, "test.xml");
 550  1 Document doc = builder.build(input);
 551  1 Document result = XIncluder.resolve(doc);
 552  1 Document expectedResult = builder.build(
 553    new File(outputDir, "test.xml")
 554    );
 555  1 assertEquals(expectedResult, result);
 556   
 557    }
 558   
 559   
 560  1 public void testBaseURIsPreservedInSameDocumentInclusion()
 561    throws ParsingException, IOException, XIncludeException {
 562   
 563  1 File input = new File(inputDir, "includefromsamedocumentwithbase.xml");
 564  1 Document doc = builder.build(input);
 565  1 Document result = XIncluder.resolve(doc);
 566  1 Document expectedResult = builder.build(
 567    new File(outputDir, "includefromsamedocumentwithbase.xml")
 568    );
 569  1 assertEquals(expectedResult, result);
 570   
 571    }
 572   
 573   
 574  1 public void testBaseURIsPreservedInResultDocument()
 575    throws ParsingException, IOException, XIncludeException {
 576   
 577  1 Document doc = new Document(new Element("root"));
 578  1 doc.setBaseURI("http://www.example.org/");
 579  1 Document result = XIncluder.resolve(doc);
 580  1 assertEquals("http://www.example.org/", result.getBaseURI());
 581   
 582    }
 583   
 584   
 585    /* public void testResolveNodes()
 586    throws IOException, ParsingException, XIncludeException {
 587    File dir = new File(inputDir, "");
 588    Element include = new Element("xi:include", XIncluder.XINCLUDE_NS);
 589    include.setBaseURI(dir.toURL().toExternalForm());
 590    include.addAttribute(new Attribute("href", "disclaimer.xml"));
 591    Nodes in = new Nodes(include);
 592    Nodes out = XIncluder.resolve(in);
 593    assertEquals(1, out.size());
 594    Element result = (Element) out.get(0);
 595    assertEquals("disclaimer",result.getQualifiedName());
 596    } */
 597   
 598   
 599  1 public void testNullBaseURI()
 600    throws ParsingException, IOException, XIncludeException {
 601   
 602  1 File input = new File(inputDir, "disclaimer.xml");
 603  1 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
 604    + "\n <p>120 Mz is adequate for an average home user.</p>"
 605    + "\n <xi:include href='" + input.toURL() + "'/>\n</document>";
 606  1 Reader reader = new StringReader(data);
 607  1 Document doc = builder.build(reader);
 608  1 Document result = XIncluder.resolve(doc);
 609  1 Document expectedResult = builder.build(
 610    new File(outputDir, "c1.xml")
 611    );
 612  1 assertEquals(expectedResult, result);
 613   
 614    }
 615   
 616   
 617  1 public void testBadIRIIsAFatalError()
 618    throws IOException, ParsingException, XIncludeException {
 619   
 620  1 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
 621    + "<xi:include href='http://www.example.com/a%5.html'>"
 622    + "<xi:fallback>Ooops!</xi:fallback></xi:include></document>";
 623  1 Reader reader = new StringReader(data);
 624  1 Document doc = builder.build(reader);
 625  1 try {
 626  1 XIncluder.resolve(doc);
 627  0 fail("Resolved fallback when encountering a syntactically incorrect URI");
 628    }
 629    catch (BadHrefAttributeException success) {
 630  1 assertNotNull(success.getMessage());
 631    }
 632   
 633    }
 634   
 635   
 636  1 public void testBadIRIWithUnrecognizedSchemeIsAFatalError()
 637    throws IOException, ParsingException, XIncludeException {
 638   
 639  1 String data = "<doc xmlns:xi='http://www.w3.org/2001/XInclude'>"
 640    + "<xi:include href='scheme://www.example.com/a%5.html'>"
 641    + "<xi:fallback>Ooops!</xi:fallback></xi:include></doc>";
 642  1 Reader reader = new StringReader(data);
 643  1 Document doc = builder.build(reader);
 644  1 try {
 645  1 XIncluder.resolve(doc);
 646  0 fail("Resolved fallback when encountering a syntactically incorrect URI");
 647    }
 648    catch (BadHrefAttributeException success) {
 649  1 assertNotNull(success.getMessage());
 650    }
 651   
 652    }
 653   
 654   
 655  1 public void testGoodIRIWithUnrecognizedSchemeIsAResourceError()
 656    throws IOException, ParsingException, XIncludeException {
 657   
 658  1 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
 659    + "<xi:include href='scheme://www.example.com/a.html'>"
 660    + "<xi:fallback>Correct!</xi:fallback></xi:include></document>";
 661  1 Reader reader = new StringReader(data);
 662  1 Document doc = builder.build(reader);
 663  1 Document result = XIncluder.resolve(doc);
 664  1 assertEquals("<?xml version=\"1.0\"?>\n"
 665    + "<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">Correct!</document>\n",
 666    result.toXML());
 667   
 668    }
 669   
 670   
 671  1 public void testBadAcceptAttribute()
 672    throws ParsingException, IOException, XIncludeException {
 673   
 674  1 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
 675    + "\n <p>120 MHz is adequate for an average home user.</p>"
 676    + "\n <xi:include href='http://www.example.com' "
 677    + "accept='text/html&#x0D;&#x0A;Something: bad'/>\n</document>";
 678  1 Reader reader = new StringReader(data);
 679  1 Document doc = builder.build(reader);
 680  1 try {
 681  1 XIncluder.resolve(doc);
 682  0 fail("Allowed accept header containing carriage return linefeed");
 683    }
 684    catch (BadHTTPHeaderException success) {
 685  1 assertNotNull(success.getMessage());
 686    }
 687   
 688    }
 689   
 690   
 691  1 public void testBadAcceptAttributeWithLatin1Character()
 692    throws ParsingException, IOException, XIncludeException {
 693   
 694  1 String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
 695    + "\n <p>120 MHz is adequate for an average home user.</p>"
 696    + "\n <xi:include href='http://www.example.com' "
 697    + "accept='text/html&#xA0;Something: bad'/>\n</document>";
 698  1 Reader reader = new StringReader(data);
 699  1 Document doc = builder.build(reader);
 700  1 try {
 701  1 XIncluder.resolve(doc);
 702  0 fail("Allowed accept header containing non-ASCII character");
 703    }
 704    catch (BadHTTPHeaderException success) {
 705  1 assertNotNull(success.getMessage());
 706    }
 707   
 708    }
 709   
 710   
 711  1 public void testUnrecognizedAttributesAreIgnored()
 712    throws ParsingException, IOException, XIncludeException {
 713   
 714  1 File input = new File(inputDir, "extraattributes.xml");
 715  1 Document doc = builder.build(input);
 716  1 Document result = XIncluder.resolve(doc);
 717  1 Document expectedResult = builder.build(
 718    new File(outputDir, "c1.xml")
 719    );
 720  1 assertEquals(expectedResult, result);
 721   
 722    }
 723   
 724   
 725  1 public void testEmptyFallback()
 726    throws ParsingException, IOException, XIncludeException {
 727   
 728  1 File input = new File(inputDir, "emptyfallback.xml");
 729  1 Document doc = builder.build(input);
 730  1 Document result = XIncluder.resolve(doc);
 731  1 Document expectedResult = builder.build(
 732    new File(outputDir, "emptyfallback.xml")
 733    );
 734  1 assertEquals(expectedResult, result);
 735   
 736    }
 737   
 738   
 739  1 public void testFallbackInIncludedDocument()
 740    throws ParsingException, IOException, XIncludeException {
 741   
 742  1 File input = new File(inputDir, "metafallbacktest.xml");
 743  1 Document doc = builder.build(input);
 744  1 Document result = XIncluder.resolve(doc);
 745  1 Document expectedResult = builder.build(
 746    new File(outputDir, "metafallbacktest.xml")
 747    );
 748  1 assertEquals(expectedResult, result);
 749   
 750    }
 751   
 752   
 753  1 public void testFallbackInIncludedDocumentUsesAnIntradocumentXPointer()
 754    throws ParsingException, IOException, XIncludeException {
 755   
 756  1 File input = new File(inputDir, "metafallbacktest6.xml");
 757  1 Document doc = builder.build(input);
 758  1 Document result = XIncluder.resolve(doc);
 759  1 Document expectedResult = builder.build(
 760    new File(outputDir, "metafallbacktest6.xml")
 761    );
 762  1 assertEquals(expectedResult, result);
 763   
 764    }
 765   
 766   
 767    // changed for b5
 768  1 public void testFallbackInIncludedDocumentIncludesADocumentWithParseEqualsText()
 769    throws ParsingException, IOException, XIncludeException {
 770   
 771  1 File input = new File(inputDir, "metafallbacktest2.xml");
 772  1 Document doc = builder.build(input);
 773  1 Document result = XIncluder.resolve(doc);
 774  1 Document expectedResult = builder.build(
 775    new File(outputDir, "metafallbacktest2.xml")
 776    );
 777  1 assertEquals(expectedResult, result);
 778   
 779    }
 780   
 781   
 782  1 public void testFallbackInIncludedDocumentWithBadParseAttribute()
 783    throws ParsingException, IOException, XIncludeException {
 784   
 785  1 File input = new File(inputDir, "metafallbacktest3.xml");
 786  1 Document doc = builder.build(input);
 787  1 try {
 788  1 XIncluder.resolve(doc);
 789  0 fail("Allowed bad parse attribute");
 790    }
 791    catch (BadParseAttributeException success) {
 792  1 assertNotNull(success.getMessage());
 793    }
 794   
 795    }
 796   
 797   
 798  1 public void testFallbackInIncludedDocumentWithMissingHrefAndParseAttributes()
 799    throws ParsingException, IOException, XIncludeException {
 800   
 801  1 File input = new File(inputDir, "metafallbacktest4.xml");
 802  1 Document doc = builder.build(input);
 803  1 try {
 804  1 XIncluder.resolve(doc);
 805  0 fail("Allowed bad parse attribute");
 806    }
 807    catch (NoIncludeLocationException success) {
 808  1 assertNotNull(success.getMessage());
 809    }
 810   
 811    }
 812   
 813   
 814  1 public void testFallbackInIncludedDocumentWithFragmentID()
 815    throws ParsingException, IOException, XIncludeException {
 816   
 817  1 File input = new File(inputDir, "metafallbacktestwithfragmentid.xml");
 818  1 Document doc = builder.build(input);
 819  1 try {
 820  1 XIncluder.resolve(doc);
 821  0 fail("Allowed document with fragment ID in href attribute");
 822    }
 823    catch (BadHrefAttributeException success) {
 824  1 assertNotNull(success.getMessage());
 825    }
 826   
 827    }
 828   
 829   
 830    // changed in b5
 831  1 public void testXPointerIsNotResolvedAgainstTheSourceInfoset()
 832    throws ParsingException, IOException, XIncludeException {
 833   
 834  1 File input = new File(inputDir, "metafallbacktest5.xml");
 835  1 Document doc = builder.build(input);
 836  1 try {
 837  1 XIncluder.resolve(doc);
 838  0 fail("Allowed XPointer that doesn't resolve against the acquired infoset but does resolve against the source infoset");
 839    }
 840    catch (XIncludeException ex) {
 841  1 assertNotNull(ex.getMessage());
 842    }
 843   
 844    }
 845   
 846   
 847  1 public void testFallbackInIncludedDocumentThatResolvesToNonElement()
 848    throws ParsingException, IOException, XIncludeException {
 849   
 850  1 File input = new File(inputDir, "metafallbacktotexttest.xml");
 851  1 Document doc = builder.build(input);
 852  1 Document result = XIncluder.resolve(doc);
 853  1 Document expectedResult = builder.build(
 854    new File(outputDir, "metafallbacktotexttest.xml")
 855    );
 856  1 assertEquals(expectedResult, result);
 857   
 858    }
 859   
 860   
 861  1 public void testFallbackInIncludedDocumentWithXPointer()
 862    throws ParsingException, IOException, XIncludeException {
 863    // This test case activates processFallbackSilently
 864  1 File input = new File(inputDir, "metafallbacktestwithxpointer.xml");
 865  1 Document doc = builder.build(input);
 866  1 Document result = XIncluder.resolve(doc);
 867  1 Document expectedResult = builder.build(
 868    new File(outputDir, "metafallbacktestwithxpointer.xml")
 869    );
 870  1 assertEquals(expectedResult, result);
 871   
 872    }
 873   
 874   
 875    // changed in b5
 876    // test case where fallback falls back to text and comments rather than
 877    // an element
 878  1 public void testFallbackInIncludedDocumentWithXPointer2()
 879    throws ParsingException, IOException, XIncludeException {
 880   
 881    // This test case activates processFallbackSilently
 882  1 File input = new File(inputDir, "metafallbacktestwithxpointer2.xml");
 883  1 Document doc = builder.build(input);
 884  1 Document result = XIncluder.resolve(doc);
 885  1 Document expectedResult = builder.build(
 886    new File(outputDir, "metafallbacktestwithxpointer2.xml")
 887    );
 888  1 assertEquals(expectedResult, result);
 889   
 890    }
 891   
 892   
 893  1 public void testNoFallbackInIncludedDocumentWithXPointer()
 894    throws ParsingException, IOException, XIncludeException {
 895   
 896    // This test case activates processFallbackSilently
 897  1 File input = new File(inputDir, "metamissingfallbacktestwithxpointer.xml");
 898  1 Document doc = builder.build(input);
 899  1 try {
 900  1 XIncluder.resolve(doc);
 901  0 fail("Should have thrown IOException");
 902    }
 903    catch (IOException success) {
 904  1 assertNotNull(success.getMessage());
 905    }
 906   
 907    }
 908   
 909   
 910  1 public void testFallbackInIncludedDocumentHasBadXPointer()
 911    throws ParsingException, IOException, XIncludeException {
 912    // This test case activates processFallbackSilently
 913  1 File input = new File(inputDir, "metafallbackwithbadxpointertest.xml");
 914  1 Document doc = builder.build(input);
 915  1 try {
 916  1 XIncluder.resolve(doc);
 917  0 fail("Should have thrown XIncludeException");
 918    }
 919    catch (XIncludeException success) {
 920  1 assertNotNull(success.getMessage());
 921  1 assertNotNull(success.getCause());
 922    }
 923   
 924    }
 925   
 926   
 927    // from the XInclude CR
 928  1 public void testC1()
 929    throws ParsingException, IOException, XIncludeException {
 930   
 931  1 File input = new File(inputDir, "c1.xml");
 932  1 Document doc = builder.build(input);
 933  1 Document result = XIncluder.resolve(doc);
 934  1 Document expectedResult = builder.build(
 935    new File(outputDir, "c1.xml")
 936    );
 937  1 assertEquals(expectedResult, result);
 938   
 939    }
 940   
 941   
 942  1 public void testRelativeURLBaseURIFixup()
 943    throws ParsingException, IOException, XIncludeException {
 944   
 945  1 File input = new File(inputDir, "relative.xml");
 946  1 Document doc = builder.build(input);
 947  1 Document result = XIncluder.resolve(doc);
 948  1 assertEquals(doc.getBaseURI(), result.getBaseURI());
 949  1 Document expectedResult = builder.build(
 950    new File(outputDir, "relative.xml")
 951    );
 952  1 assertEquals(expectedResult, result);
 953  1 Element root = result.getRootElement();
 954  1 Element red = root.getFirstChildElement("red");
 955  1 String base = red.getAttributeValue("base", Namespace.XML_NAMESPACE);
 956  1 assertEquals("basedata/red.xml", base);
 957   
 958    }
 959   
 960   
 961    // same test with explicit parse="xml"
 962  1 public void testParseEqualsXML()
 963    throws ParsingException, IOException, XIncludeException {
 964   
 965  1 File input = new File(inputDir, "parseequalxml.xml");
 966  1 Document doc = builder.build(input);
 967  1 Document result = XIncluder.resolve(doc);
 968  1 Document expectedResult = builder.build(
 969    new File(outputDir, "c1.xml")
 970    );
 971  1 assertEquals(expectedResult, result);
 972   
 973    }
 974   
 975   
 976    // changed in b5
 977  1 public void testAcceptableCirclePointer()
 978    throws ParsingException, IOException, XIncludeException {
 979   
 980  1 File input = new File(inputDir, "legalcircle.xml");
 981  1 Document doc = builder.build(input);
 982  1 try {
 983  1 XIncluder.resolve(doc);
 984  0 fail("Allowed circular reference");
 985    }
 986    catch (InclusionLoopException success) {
 987  1 assertNotNull(success.getMessage());
 988    }
 989   
 990    }
 991   
 992   
 993    // from the XInclude CR
 994  1 public void testC2()
 995    throws ParsingException, IOException, XIncludeException {
 996   
 997  1 File input = new File(inputDir, "c2.xml");
 998  1 Document doc = builder.build(input);
 999  1 Document result = XIncluder.resolve(doc);
 1000  1 Document expectedResult = builder.build(
 1001    new File(outputDir, "c2.xml")
 1002    );
 1003  1 assertEquals(expectedResult, result);
 1004   
 1005    }
 1006   
 1007   
 1008    // from the XInclude CR
 1009  1 public void testC3()
 1010    throws ParsingException, IOException, XIncludeException {
 1011   
 1012  1 File input = new File(inputDir, "c3.xml");
 1013  1 Document doc = builder.build(input);
 1014  1 Document result = XIncluder.resolve(doc);
 1015  1 Document expectedResult = builder.build(
 1016    new File(outputDir, "c3.xml")
 1017    );
 1018  1 assertEquals(expectedResult, result);
 1019   
 1020    }
 1021   
 1022   
 1023    // C4 skipped for the moment because it uses XPointers
 1024    // that XOM doesn't yet support
 1025   
 1026    // from the XInclude CR
 1027    // Don't use this one yet, because there appear to be
 1028    // mistakes in the spec examples
 1029    /*public void testC5() throws ParsingException, IOException, XIncludeException {
 1030   
 1031    File input = new File(inputDir, "c5.xml");
 1032    Document doc = builder.build(input);
 1033    Document result = XIncluder.resolve(doc);
 1034    Document expectedResult = builder.build(new File(outputDir, "c5.xml"));
 1035    XMLAssert.assertEquals(expectedResult, result);
 1036   
 1037    } */
 1038   
 1039   
 1040    private static class TextNodeFactory extends NodeFactory {
 1041   
 1042  8 public Nodes makeText(String data) {
 1043  8 return new Nodes(new TextSubclass(data));
 1044    }
 1045   
 1046    }
 1047   
 1048    private static class TextSubclass extends Text {
 1049   
 1050  12 TextSubclass(String data) {
 1051  12 super(data);
 1052    }
 1053   
 1054  4 public Node copy() {
 1055  4 return new TextSubclass(this.getValue());
 1056    }
 1057   
 1058    }
 1059   
 1060   
 1061  1 public void testRecurseWithinSameDocument()
 1062    throws ParsingException, IOException, XIncludeException {
 1063   
 1064  1 File input = new File(inputDir, "recursewithinsamedocument.xml");
 1065  1 Document doc = builder.build(input);
 1066  1 Document result = XIncluder.resolve(doc);
 1067  1 Document expectedResult = builder.build(
 1068    new File(outputDir, "recursewithinsamedocument.xml")
 1069    );
 1070  1 assertEquals(expectedResult, result);
 1071   
 1072    }
 1073   
 1074   
 1075  1 public void testSiblingIncludes()
 1076    throws ParsingException, IOException, XIncludeException {
 1077   
 1078  1 File input = new File(inputDir, "paralleltest.xml");
 1079  1 Document doc = builder.build(input);
 1080  1 Document result = XIncluder.resolve(doc);
 1081  1 Document expectedResult = builder.build(
 1082    new File(outputDir, "paralleltest.xml")
 1083    );
 1084  1 assertEquals(expectedResult, result);
 1085   
 1086    }
 1087   
 1088   
 1089  1 public void testNamespaces()
 1090    throws ParsingException, IOException, XIncludeException {
 1091   
 1092  1 File input = new File(inputDir, "namespacetest.xml");
 1093  1 Document doc = builder.build(input);
 1094  1 Document result = XIncluder.resolve(doc);
 1095  1 Document expectedResult = builder.build(
 1096    new File(outputDir, "namespacetest.xml")
 1097    );
 1098  1 assertEquals(expectedResult, result);
 1099   
 1100    }
 1101   
 1102   
 1103  1 public void testIncludeReferencesItItself()
 1104    throws ParsingException, IOException, XIncludeException {
 1105   
 1106  1 File input = new File(inputDir, "internalcircular.xml");
 1107  1 Document doc = builder.build(input);
 1108  1 try {
 1109  1 XIncluder.resolve(doc);
 1110  0 fail("Allowed include element to reference itself");
 1111    }
 1112    catch (InclusionLoopException success) {
 1113  1 assertNotNull(success.getMessage());
 1114    }
 1115   
 1116    }
 1117   
 1118   
 1119  1 public void testIncludeReferencesItsAncestor()
 1120    throws ParsingException, IOException, XIncludeException {
 1121   
 1122  1 File input = new File(inputDir, "internalcircularviaancestor.xml");
 1123  1 Document doc = builder.build(input);
 1124  1 try {
 1125  1 XIncluder.resolve(doc);
 1126  0 fail("Allowed include element to reference its own ancestor");
 1127    }
 1128    catch (InclusionLoopException success) {
 1129  1 assertNotNull(success.getMessage());
 1130    }
 1131   
 1132    }
 1133   
 1134   
 1135  1 public void testNoInclusions()
 1136    throws ParsingException, IOException, XIncludeException {
 1137   
 1138  1 File input = new File(inputDir, "latin1.xml");
 1139  1 Document doc = builder.build(input);
 1140  1 Document result = XIncluder.resolve(doc);
 1141  1 Document expectedResult = builder.build(
 1142    new File(outputDir, "latin1.xml")
 1143    );
 1144  1 assertEquals(expectedResult, result);
 1145   
 1146    }
 1147   
 1148   
 1149  1 public void test2()
 1150    throws ParsingException, IOException, XIncludeException {
 1151   
 1152  1 File input = new File(inputDir, "simple.xml");
 1153  1 Document doc = builder.build(input);
 1154  1 Document result = XIncluder.resolve(doc);
 1155  1 Document expectedResult = builder.build(
 1156    new File(outputDir, "simple.xml")
 1157    );
 1158  1 assertEquals(expectedResult, result);
 1159   
 1160    }
 1161   
 1162   
 1163  1 public void testReplaceRoot()
 1164    throws ParsingException, IOException, XIncludeException {
 1165   
 1166  1 File input = new File(inputDir, "roottest.xml");
 1167  1 Document doc = builder.build(input);
 1168  1 Document result = XIncluder.resolve(doc);
 1169  1 Document expectedResult = builder.build(
 1170    new File(outputDir, "roottest.xml")
 1171    );
 1172  1 assertEquals(expectedResult, result);
 1173   
 1174    }
 1175   
 1176   
 1177    // In this test the included document has a prolog and an epilog
 1178  1 public void testReplaceRoot2()
 1179    throws ParsingException, IOException, XIncludeException {
 1180   
 1181  1 File input = new File(inputDir, "roottest2.xml");
 1182  1 Document doc = builder.build(input);
 1183  1 Document result = XIncluder.resolve(doc);
 1184  1 Document expectedResult = builder.build(
 1185    new File(outputDir, "roottest2.xml")
 1186    );
 1187  1 assertEquals(expectedResult, result);
 1188   
 1189    }
 1190   
 1191   
 1192  1 public void testIncludeElementsCannotHaveIncludeChildren()
 1193    throws ParsingException, IOException, XIncludeException {
 1194  1 File input = new File(inputDir, "nestedxinclude.xml");
 1195  1 Document doc = builder.build(input);
 1196  1 try {
 1197  1 XIncluder.resolve(doc);
 1198  0 fail("allowed include element to contain another include element");
 1199    }
 1200    catch (XIncludeException success) {
 1201  1 assertNotNull(success.getMessage());
 1202    }
 1203    }
 1204   
 1205   
 1206  1 public void testIncludeElementsCannotHaveChildrenFromXIncludeNamespace()
 1207    throws ParsingException, IOException, XIncludeException {
 1208  1 File input = new File(inputDir, "nestedxincludenamespace.xml");
 1209  1 Document doc = builder.build(input);
 1210  1 try {
 1211  1 XIncluder.resolve(doc);
 1212  0 fail("allowed include element to contain another include element");
 1213    }
 1214    catch (XIncludeException success) {
 1215  1 assertNotNull(success.getMessage());
 1216    }
 1217    }
 1218   
 1219   
 1220  1 public void testFallbackIsNotChildOfIncludeElement()
 1221    throws ParsingException, IOException, XIncludeException {
 1222  1 File input = new File(inputDir, "nakedfallback.xml");
 1223  1 Document doc = builder.build(input);
 1224  1 try {
 1225  1 XIncluder.resolve(doc);
 1226  0 fail("allowed fallback that was not child of an include element");
 1227    }
 1228    catch (XIncludeException success) {
 1229  1 assertNotNull(success.getMessage());
 1230    }
 1231    }
 1232   
 1233   
 1234  1 public void testFallbackCantContainFallbackElement()
 1235    throws ParsingException, IOException, XIncludeException {
 1236  1 File input = new File(inputDir, "fallbackcontainsfallback.xml");
 1237  1 Document doc = builder.build(input);
 1238  1 try {
 1239  1 XIncluder.resolve(doc);
 1240  0 fail("allowed fallback inside another fallback element");
 1241    }
 1242    catch (XIncludeException success) {
 1243  1 assertNotNull(success.getMessage());
 1244    }
 1245    }
 1246   
 1247   
 1248    // In this test the fallback is activated.
 1249  1 public void testMultipleFallbacks()
 1250    throws ParsingException, IOException, XIncludeException {
 1251  1 File input = new File(inputDir, "multiplefallbacks.xml");
 1252  1 Document doc = builder.build(input);
 1253  1 try {
 1254  1 XIncluder.resolve(doc);
 1255  0 fail("allowed multiple fallback elements");
 1256    }
 1257    catch (XIncludeException success) {
 1258  1 assertNotNull(success.getMessage());
 1259    }
 1260    }
 1261   
 1262   
 1263    // In this test the fallback is not needed.
 1264  1 public void testMultipleFallbacks2()
 1265    throws ParsingException, IOException, XIncludeException {
 1266  1 File input = new File(inputDir, "multiplefallbacks2.xml");
 1267  1 Document doc = builder.build(input);
 1268  1 try {
 1269  1 XIncluder.resolve(doc);
 1270  0 fail("allowed multiple fallback elements");
 1271    }
 1272    catch (XIncludeException success) {
 1273  1 assertNotNull(success.getMessage());
 1274    }
 1275    }
 1276   
 1277   
 1278  1 public void testDocumentIncludesItself()
 1279    throws ParsingException, IOException, XIncludeException {
 1280  1 File input = new File(inputDir, "circle1.xml");
 1281  1 Document doc = builder.build(input);
 1282  1 try {
 1283  1 XIncluder.resolve(doc);
 1284  0 fail("allowed parsed include of self");
 1285    }
 1286    catch (InclusionLoopException success) {
 1287  1 assertNotNull(success.getMessage());
 1288  1 assertEquals(input.toURL().toExternalForm(), success.getURI());
 1289    }
 1290    }
 1291   
 1292   
 1293  1 public void testInclusionLoopWithLength2Cycle()
 1294    throws ParsingException, IOException, XIncludeException {
 1295   
 1296  1 File input = new File(inputDir, "circle2a.xml");
 1297  1 File errorFile = new File(inputDir, "circle2b.xml");
 1298  1 Document doc = builder.build(input);
 1299  1 try {
 1300  1 XIncluder.resolve(doc);
 1301  0 fail("allowed circular include, cycle length 1");
 1302    }
 1303    catch (InclusionLoopException success) {
 1304  1 assertTrue(success.getMessage().indexOf(errorFile.toURL().toExternalForm()) > 1);
 1305  1 assertTrue(success.getMessage().indexOf(input.toURL().toExternalForm()) > 1);
 1306  1 assertEquals(errorFile.toURL().toExternalForm(), success.getURI());
 1307    }
 1308   
 1309    }
 1310   
 1311   
 1312  1 public void testMissingHref()
 1313    throws ParsingException, IOException, XIncludeException {
 1314   
 1315  1 File input = new File(inputDir, "missinghref.xml");
 1316  1 Document doc = builder.build(input);
 1317  1 try {
 1318  1 XIncluder.resolve(doc);
 1319  0 fail("allowed missing href");
 1320    }
 1321    catch (NoIncludeLocationException success) {
 1322  1 assertNotNull(success.getMessage());
 1323  1 assertEquals(doc.getBaseURI(), success.getURI());
 1324    }
 1325   
 1326    }
 1327   
 1328   
 1329  1 public void testBadParseAttribute()
 1330    throws ParsingException, IOException, XIncludeException {
 1331   
 1332  1 File input = new File(inputDir, "badparseattribute.xml");
 1333  1 Document doc = builder.build(input);
 1334  1 try {
 1335  1 XIncluder.resolve(doc);
 1336  0 fail("allowed bad parse attribute");
 1337    }
 1338    catch (BadParseAttributeException success) {
 1339  1 assertNotNull(success.getMessage());
 1340  1 URL u1 = input.toURL();
 1341  1 URL u2 = new URL(success.getURI());
 1342  1 assertEquals(u1, u2);
 1343    }
 1344   
 1345    }
 1346   
 1347   
 1348  1 public void testUnavailableResource()
 1349    throws ParsingException, IOException, XIncludeException {
 1350  1 File input = new File(inputDir, "missingfile.xml");
 1351  1 Document doc = builder.build(input);
 1352  1 try {
 1353  1 XIncluder.resolve(doc);
 1354  0 fail("allowed unresolvable resource");
 1355    }
 1356    catch (IOException success) {
 1357  1 assertNotNull(success.getMessage());
 1358    }
 1359   
 1360    }
 1361   
 1362   
 1363  1 public void testFallback()
 1364    throws ParsingException, IOException, XIncludeException {
 1365   
 1366  1 File input = new File(inputDir, "fallbacktest.xml");
 1367  1 Document doc = builder.build(input);
 1368  1 Document result = XIncluder.resolve(doc);
 1369  1 Document expectedResult = builder.build(
 1370    new File(outputDir, "fallbacktest.xml")
 1371    );
 1372  1 assertEquals(expectedResult, result);
 1373   
 1374    }
 1375   
 1376   
 1377  1 public void testFallbackWithRecursiveInclude()
 1378    throws ParsingException, IOException, XIncludeException {
 1379   
 1380  1 File input = new File(inputDir, "fallbacktest2.xml");
 1381  1 Document doc = builder.build(input);
 1382  1 Document result = XIncluder.resolve(doc);
 1383  1 Document expectedResult = builder.build(
 1384    new File(outputDir, "fallbacktest2.xml")
 1385    );
 1386  1 assertEquals(expectedResult, result);
 1387   
 1388    }
 1389   
 1390   
 1391  1 public void testEncodingAttribute()
 1392    throws ParsingException, IOException, XIncludeException {
 1393   
 1394  1 File input = new File(inputDir, "utf16.xml");
 1395  1 Document doc = builder.build(input);
 1396  1 Document result = XIncluder.resolve(doc);
 1397  1 Document expectedResult = builder.build(
 1398    new File(outputDir, "utf16.xml")
 1399    );
 1400  1 assertEquals(expectedResult, result);
 1401   
 1402    }
 1403   
 1404   
 1405  1 public void testXPointerBareNameID()
 1406    throws ParsingException, IOException, XIncludeException {
 1407   
 1408  1 File input = new File(inputDir, "xptridtest.xml");
 1409  1 Document doc = builder.build(input);
 1410  1 Document result = XIncluder.resolve(doc);
 1411  1 Document expectedResult = builder.build(
 1412    new File(outputDir, "xptridtest.xml")
 1413    );
 1414  1 assertEquals(expectedResult, result);
 1415   
 1416    }
 1417   
 1418   
 1419  1 public void testXPointerXMLID()
 1420    throws ParsingException, IOException, XIncludeException {
 1421   
 1422  1 File input = new File(inputDir, "xmlidtest.xml");
 1423  1 Document doc = builder.build(input);
 1424  1 Document result = XIncluder.resolve(doc);
 1425  1 Document expectedResult = builder.build(
 1426    new File(outputDir, "xmlidtest.xml")
 1427    );
 1428  1 assertEquals(expectedResult, result);
 1429   
 1430    }
 1431   
 1432   
 1433  1 public void testShorthandXPointerMatchesNothing()
 1434    throws ParsingException, IOException {
 1435   
 1436  1 File input = new File(inputDir, "xptridtest2.xml");
 1437  1 Document doc = builder.build(input);
 1438  1 try {
 1439  1 XIncluder.resolve(doc);
 1440  0 fail("Resolved a document with an XPointer " +
 1441    "that selects no subresource");
 1442    }
 1443    catch (XIncludeException success) {
 1444  1 assertNotNull(success.getMessage());
 1445    // Must compare URLs instead of strings here to avoid
 1446    // issues of whether a file URL begins file:/ or file:///
 1447  1 URL u1 = input.toURL();
 1448  1 URL u2 = new URL(success.getURI());
 1449  1 assertEquals(u1, u2);
 1450    }
 1451   
 1452   
 1453    /* I used to think this case included nothing.
 1454    Now I think an XPointer that matches no
 1455    subresource, and does not have a fallback is in error.
 1456    Document expectedResult = builder.build(
 1457    new File(outputDir, "xptridtest2.xml")
 1458    );
 1459    assertEquals(expectedResult, result);
 1460    */
 1461   
 1462    }
 1463   
 1464   
 1465  1 public void testXPointerPureTumbler()
 1466    throws ParsingException, IOException, XIncludeException {
 1467   
 1468  1 File input = new File(inputDir, "xptrtumblertest.xml");
 1469  1 Document doc = builder.build(input);
 1470  1 Document result = XIncluder.resolve(doc);
 1471  1 Document expectedResult = builder.build(
 1472    new File(outputDir, "xptrtumblertest.xml")
 1473    );
 1474  1 assertEquals(expectedResult, result);
 1475   
 1476    }
 1477   
 1478   
 1479  1 public void testUnrecognizedColonizedSchemeNameBackedUpByTumbler()
 1480    throws ParsingException, IOException, XIncludeException {
 1481   
 1482  1 File input = new File(inputDir, "colonizedschemename.xml");
 1483  1 Document doc = builder.build(input);
 1484  1 Document result = XIncluder.resolve(doc);
 1485  1 Document expectedResult = builder.build(
 1486    new File(outputDir, "xptrtumblertest.xml")
 1487    );
 1488  1 assertEquals(expectedResult, result);
 1489   
 1490    }
 1491   
 1492   
 1493  1 public void testXPointerSyntaxErrorInSecondPart()
 1494    throws ParsingException, IOException {
 1495   
 1496  1 File input = new File(inputDir, "laterfailure.xml");
 1497  1 Document doc = builder.build(input);
 1498  1 try {
 1499  1 XIncluder.resolve(doc);
 1500  0 fail("Didn't find syntax error in 2nd XPointer part" +
 1501    " when the first part resolved successfully");
 1502    }
 1503    catch (XIncludeException success) {
 1504  1 assertNotNull(success.getMessage());
 1505    }
 1506   
 1507    }
 1508   
 1509   
 1510  1 public void testBadElementSchemeDataIsNotAnError()
 1511    throws ParsingException, IOException, XIncludeException {
 1512   
 1513  1 File input = new File(inputDir, "badelementschemedata.xml");
 1514  1 Document doc = builder.build(input);
 1515  1 Document result = XIncluder.resolve(doc);
 1516  1 Document expectedResult = builder.build(
 1517    new File(outputDir, "badelementschemedata.xml")
 1518    );
 1519    // dumpResult(input, result);
 1520   
 1521  1 assertEquals(expectedResult, result);
 1522   
 1523    }
 1524   
 1525   
 1526  1 public void testXPointerSyntaxErrorMissingFinalParenthesis()
 1527    throws ParsingException, IOException {
 1528   
 1529  1 File input = new File(inputDir, "laterfailure2.xml");
 1530  1 Document doc = builder.build(input);
 1531  1 try {
 1532  1 XIncluder.resolve(doc);
 1533  0 fail("Didn't find syntax error in 2nd XPointer part" +
 1534    " when the first part resolved successfully");
 1535    }
 1536    catch (XIncludeException success) {
 1537  1 assertNotNull(success.getMessage());
 1538    }
 1539   
 1540    }
 1541   
 1542   
 1543    // Test we can include from same document using only
 1544    // an xpointer attribute
 1545  1 public void testOnlyXPointer()
 1546    throws ParsingException, IOException, XIncludeException {
 1547   
 1548  1 File input = new File(inputDir, "onlyxpointer.xml");
 1549  1 Document doc = builder.build(input);
 1550  1 Document result = XIncluder.resolve(doc);
 1551  1 Document expectedResult = builder.build(
 1552    new File(outputDir, "onlyxpointer.xml")
 1553    );
 1554   
 1555  1 assertEquals(expectedResult, result);
 1556   
 1557    }
 1558   
 1559   
 1560    // Test with 3 element schemes in the XPointer.
 1561    // The first and second one point to nothing. The third one
 1562    // selects something.
 1563  1 public void testXPointerTripleTumbler()
 1564    throws ParsingException, IOException, XIncludeException {
 1565   
 1566  1 File input = new File(inputDir, "xptr2tumblertest.xml");
 1567  1 Document doc = builder.build(input);
 1568  1 Document result = XIncluder.resolve(doc);
 1569  1 Document expectedResult = builder.build(
 1570    new File(outputDir, "xptrtumblertest.xml")
 1571    );
 1572  1 assertEquals(expectedResult, result);
 1573   
 1574    }
 1575   
 1576   
 1577    // Test with 2 element schemes in the XPointer.
 1578    // The first one uses an ID that doesn't exist
 1579    // and points to nothing. The second one
 1580    // selects something.
 1581  1 public void testXPointerDoubleTumbler()
 1582    throws ParsingException, IOException, XIncludeException {
 1583   
 1584  1 File input = new File(inputDir, "xptrdoubletumblertest.xml");
 1585  1 Document doc = builder.build(input);
 1586  1 Document result = XIncluder.resolve(doc);
 1587  1 Document expectedResult = builder.build(
 1588    new File(outputDir, "xptrtumblertest.xml")
 1589    );
 1590  1 assertEquals(expectedResult, result);
 1591   
 1592    }
 1593   
 1594   
 1595    // Test with 2 element schemes in the XPointer.
 1596    // The first one uses an ID that points to something.
 1597    // The second one points to something too. Both element schemes
 1598    // use IDs exclusively, no child sequences.
 1599  1 public void testXPointerDoubleElementByID()
 1600    throws ParsingException, IOException, XIncludeException {
 1601   
 1602  1 File input = new File(inputDir, "xptrdoubleelementtest.xml");
 1603  1 Document doc = builder.build(input);
 1604  1 Document result = XIncluder.resolve(doc);
 1605  1 Document expectedResult = builder.build(
 1606    new File(outputDir, "xptrdoubleelementtest.xml")
 1607    );
 1608  1 assertEquals(expectedResult, result);
 1609   
 1610    }
 1611   
 1612   
 1613    // Test with 2 element schemes in the XPointer.
 1614    // The first one uses a child sequence that points to something.
 1615    // The second one points to something too. Both element schemes
 1616    // use child sequences exclusively, no IDs.
 1617  1 public void testXPointerDoubleElementByChildSequence()
 1618    throws ParsingException, IOException, XIncludeException {
 1619   
 1620  1 File input = new File(inputDir, "xptrdoublechildsequence.xml");
 1621  1 Document doc = builder.build(input);
 1622  1 Document result = XIncluder.resolve(doc);
 1623  1 Document expectedResult = builder.build(
 1624    new File(outputDir, "xptrdoubleelementtest.xml")
 1625    );
 1626  1 assertEquals(expectedResult, result);
 1627   
 1628    }
 1629   
 1630   
 1631    // Make sure XPointer failures are treated as a resource error,
 1632    // not a fatal error.
 1633  1 public void testXPointerFailureIsAResourceError()
 1634    throws ParsingException, IOException, XIncludeException {
 1635   
 1636  1 File input = new File(
 1637    "data/xinclude/input/xptrtumblerfailsbutfallback.xml"
 1638    );
 1639  1 Document doc = builder.build(input);
 1640  1 Document result = XIncluder.resolve(doc);
 1641    // For debugging
 1642    // dumpResult(input, result);
 1643  1 Document expectedResult = builder.build(
 1644    new File(outputDir, "xptrtumblertest.xml")
 1645    );
 1646  1 assertEquals(expectedResult, result);
 1647   
 1648    }
 1649   
 1650   
 1651    // Make sure XPointer syntax errors are treated as a resource
 1652    // error, not a fatal error per section 4.2 of XInclude CR
 1653    /* Resources that are unavailable for any reason
 1654    (for example the resource doesn't exist, connection
 1655    difficulties or security restrictions prevent it from being
 1656    fetched, the URI scheme isn't a fetchable one, the resource
 1657    is in an unsuppored encoding, the resource is determined
 1658    through implementation-specific mechanisms not to be XML, or a
 1659    syntax error in an [XPointer Framework]) result in a resource
 1660    error. */
 1661  1 public void testXPointerSyntaxErrorIsAResourceError()
 1662    throws ParsingException, IOException, XIncludeException {
 1663   
 1664  1 File input = new File(
 1665    "data/xinclude/input/xptrsyntaxerrorbutfallback.xml"
 1666    );
 1667  1 Document doc = builder.build(input);
 1668  1 Document result = XIncluder.resolve(doc);
 1669  1 Document expectedResult = builder.build(
 1670    new File(outputDir, "xptrtumblertest.xml")
 1671    );
 1672  1 assertEquals(expectedResult, result);
 1673   
 1674    }
 1675   
 1676   
 1677    // Test with 3 element schemes in the XPointer,
 1678    // separated by white space.
 1679    // The first one points to nothing. The third one
 1680    // selects something.
 1681  1 public void testXPointerTumblerWithWhiteSpace()
 1682    throws ParsingException, IOException, XIncludeException {
 1683   
 1684  1 File input
 1685    = new File(inputDir, "xptrtumblertest3.xml");
 1686  1 Document doc = builder.build(input);
 1687  1 Document result = XIncluder.resolve(doc);
 1688  1 Document expectedResult = builder.build(
 1689    new File(outputDir, "xptrtumblertest.xml")
 1690    );
 1691  1 assertEquals(expectedResult, result);
 1692   
 1693    }
 1694   
 1695   
 1696  1 public void testXPointerTumblerMatchesNothing()
 1697    throws ParsingException, IOException {
 1698   
 1699  1 File input = new File(
 1700    "data/xinclude/input/xptrtumblertest2.xml"
 1701    );
 1702  1 Document doc = builder.build(input);
 1703  1 try {
 1704  1 XIncluder.resolve(doc);
 1705  0 fail("Did not error on XPointer matching nothing");
 1706    }
 1707    catch (XIncludeException success) {
 1708  1 assertNotNull(success.getMessage());
 1709  1 URL u1 = input.toURL();
 1710  1 URL u2 = new URL(success.getURI());
 1711  1 assertEquals(u1, u2);
 1712    }
 1713   
 1714    }
 1715   
 1716   
 1717  1 public void testMalformedXPointer()
 1718    throws ParsingException, IOException {
 1719   
 1720  1 File input = new File(inputDir, "badxptr.xml");
 1721  1 Document doc = builder.build(input);
 1722  1 try {
 1723  1 XIncluder.resolve(doc);
 1724  0 fail("Allowed malformed XPointer");
 1725    }
 1726    catch (XIncludeException success) {
 1727  1 assertNotNull(success.getMessage());
 1728  1 URL u1 = input.toURL();
 1729  1 URL u2 = new URL(success.getURI());
 1730  1 assertEquals(u1, u2);
 1731    }
 1732   
 1733    }
 1734   
 1735   
 1736  1 public void testXPointerExceptionSelfCausation()
 1737    throws ParsingException, IOException {
 1738   
 1739  1 File input = new File(inputDir, "badxptr.xml");
 1740  1 Document doc = builder.build(input);
 1741  1 try {
 1742  1 XIncluder.resolve(doc);
 1743  0 fail("Allowed malformed XPointer");
 1744    }
 1745    catch (XIncludeException success) {
 1746  1 Exception cause = (Exception) success.getCause();
 1747  1 assertNotNull(cause.getMessage());
 1748  1 try {
 1749  1 cause.initCause(cause);
 1750  0 fail("Self causation");
 1751    }
 1752    catch (IllegalArgumentException ex) {
 1753  1 assertNotNull(ex.getMessage());
 1754    }
 1755    }
 1756   
 1757    }
 1758   
 1759   
 1760  1 public void testXPointerExceptionGetCause()
 1761    throws ParsingException, IOException {
 1762   
 1763  1 File input = new File(inputDir, "badxptr.xml");
 1764  1 Document doc = builder.build(input);
 1765  1 try {
 1766  1 XIncluder.resolve(doc);
 1767  0 fail("Allowed malformed XPointer");
 1768    }
 1769    catch (XIncludeException success) {
 1770  1 Exception cause = (Exception) success.getCause();
 1771  1 Exception ex = new Exception();
 1772  1 cause.initCause(ex);
 1773  1 assertEquals(ex, cause.getCause());
 1774    }
 1775   
 1776    }
 1777   
 1778   
 1779  1 public void testAnotherMalformedXPointer()
 1780    throws ParsingException, IOException {
 1781   
 1782    // testing use of non NCNAME as ID
 1783  1 File input = new File(inputDir, "badxptr2.xml");
 1784  1 Document doc = builder.build(input);
 1785  1 try {
 1786  1 XIncluder.resolve(doc);
 1787  0 fail("Allowed another malformed XPointer");
 1788    }
 1789    catch (XIncludeException success) {
 1790  1 assertNotNull(success.getMessage());
 1791  1 URL u1 = input.toURL();
 1792  1 URL u2 = new URL(success.getURI());
 1793  1 assertEquals(u1, u2);
 1794    }
 1795   
 1796    }
 1797   
 1798   
 1799  1 public void testMalformedXPointerWithFallback()
 1800    throws ParsingException, IOException, XIncludeException {
 1801   
 1802  1 File input = new File(inputDir, "xptrfallback.xml");
 1803  1 Document doc = builder.build(input);
 1804  1 Document result = XIncluder.resolve(doc);
 1805  1 Document expectedResult = builder.build(new File(
 1806    "data/xinclude/output/xptrfallback.xml")
 1807    );
 1808  1 assertEquals(expectedResult, result);
 1809   
 1810    }
 1811   
 1812   
 1813  1 public void testIDAndTumbler()
 1814    throws ParsingException, IOException, XIncludeException {
 1815   
 1816  1 File input = new File(
 1817    "data/xinclude/input/xptridandtumblertest.xml"
 1818    );
 1819  1 Document doc = builder.build(input);
 1820  1 Document result = XIncluder.resolve(doc);
 1821  1 Document expectedResult = builder.build(new File(
 1822    "data/xinclude/output/xptridandtumblertest.xml")
 1823    );
 1824  1 assertEquals(expectedResult, result);
 1825   
 1826    }
 1827   
 1828   
 1829  1 public void testAutoDetectUTF16BigEndianWithByteOrderMark()
 1830    throws ParsingException, IOException, XIncludeException {
 1831   
 1832  1 File input = new File(
 1833    "data/xinclude/input/UTF16BigEndianWithByteOrderMark.xml"
 1834    );
 1835  1 Document doc = builder.build(input);
 1836  1 Document result = XIncluder.resolve(doc);
 1837  1 Document expectedResult = builder.build(new File(
 1838    "data/xinclude/output/UTF16BigEndianWithByteOrderMark.xml")
 1839    );
 1840  1 assertEquals(expectedResult, result);
 1841   
 1842    }
 1843   
 1844   
 1845  1 public void testAutoDetectUTF16LittleEndianWithByteOrderMark()
 1846    throws ParsingException, IOException, XIncludeException {
 1847   
 1848  1 File input = new File(
 1849    "data/xinclude/input/UTF16LittleEndianWithByteOrderMark.xml"
 1850    );
 1851  1 Document doc = builder.build(input);
 1852  1 Document result = XIncluder.resolve(doc);
 1853  1 Document expectedResult = builder.build(new File(
 1854    "data/xinclude/output/UTF16LittleEndianWithByteOrderMark.xml"
 1855    ));
 1856  1 assertEquals(expectedResult, result);
 1857   
 1858    }
 1859   
 1860   
 1861  1 public void testAutoDetectUTF8WithByteOrderMark()
 1862    throws ParsingException, IOException, XIncludeException {
 1863   
 1864  1 File input = new File(
 1865    "data/xinclude/input/UTF8WithByteOrderMark.xml"
 1866    );
 1867  1 Document doc = builder.build(input);
 1868  1 Document result = XIncluder.resolve(doc);
 1869  1 Document expectedResult = builder.build(
 1870    new File(outputDir, "UTF8WithByteOrderMark.xml")
 1871    );
 1872  1 assertEquals(expectedResult, result);
 1873   
 1874    }
 1875   
 1876   
 1877  1 public void testAutoDetectUnicodeBigUnmarked()
 1878    throws ParsingException, IOException, XIncludeException {
 1879   
 1880  1 File input = new File(
 1881    "data/xinclude/input/UnicodeBigUnmarked.xml"
 1882    );
 1883  1 Document doc = builder.build(input);
 1884  1 Document result = XIncluder.resolve(doc);
 1885  1 Document expectedResult = builder.build(
 1886    new File(outputDir, "UnicodeBigUnmarked.xml")
 1887    );
 1888  1 assertEquals(expectedResult, result);
 1889   
 1890    }
 1891   
 1892   
 1893  1 public void testUnicodeLittleUnmarked()
 1894    throws ParsingException, IOException, XIncludeException {
 1895   
 1896  1 File input = new File(
 1897    "data/xinclude/input/UnicodeLittleUnmarked.xml"
 1898    );
 1899  1 Document doc = builder.build(input);
 1900  1 Document result = XIncluder.resolve(doc);
 1901  1 Document expectedResult = builder.build(
 1902    new File(outputDir, "UnicodeLittleUnmarked.xml")
 1903    );
 1904  1 assertEquals(expectedResult, result);
 1905   
 1906    }
 1907   
 1908   
 1909    /*// Java doesn't yet support the UTF-32BE and UTF32LE encodings
 1910    public void testUTF32BE()
 1911    throws ParsingException, IOException, XIncludeException {
 1912   
 1913    File input = new File(
 1914    "data/xinclude/input/UTF32BE.xml"
 1915    );
 1916    Document doc = builder.build(input);
 1917    Document result = XIncluder.resolve(doc);
 1918    Document expectedResult = builder.build(
 1919    new File(outputDir, "UTF32BE.xml")
 1920    );
 1921    assertEquals(expectedResult, result);
 1922   
 1923    }
 1924   
 1925    public void testUTF32LE()
 1926    throws ParsingException, IOException, XIncludeException {
 1927   
 1928    File input = new File(
 1929    "data/xinclude/input/UTF32LE.xml"
 1930    );
 1931    Document doc = builder.build(input);
 1932    Document result = XIncluder.resolve(doc);
 1933    Document expectedResult = builder.build(
 1934    new File(outputDir, "UTF32LE.xml")
 1935    );
 1936    assertEquals(expectedResult, result);
 1937   
 1938    }
 1939    */
 1940   
 1941   
 1942  1 public void testEBCDIC()
 1943    throws ParsingException, IOException, XIncludeException {
 1944   
 1945  1 File input = new File(inputDir, "EBCDIC.xml");
 1946  1 Document doc = builder.build(input);
 1947  1 Document result = XIncluder.resolve(doc);
 1948  1 Document expected = builder.build(new File(outputDir, "EBCDIC.xml"));
 1949  1 assertEquals(expected, result);
 1950   
 1951    }
 1952   
 1953   
 1954    // This test requires files that I have not received permission
 1955    // to distribute so for the moment you won't be able to run it.
 1956    // For my own use it checks to see if the files are present
 1957    // and runs if it does find them. You can't just install the
 1958    // XInclude-Test-Suite data as distributed by the W3C here.
 1959    // Some of those tests rely on optional features XOM does not
 1960    // support such as the xpointer() scheme and notations.
 1961    // Plus some of those tests have mistakes. You need my patched
 1962    // version of the tests.
 1963  1 public void testXIncludeTestSuite()
 1964    throws ParsingException, IOException, XIncludeException {
 1965   
 1966  1 File testDescription = new File("data");
 1967  1 testDescription = new File(testDescription, "XInclude-Test-Suite");
 1968  1 testDescription = new File(testDescription, "testdescr.xml");
 1969  1 URL baseURL = testDescription.toURL();
 1970  1 if (!testDescription.exists()) {
 1971  1 baseURL = new URL(
 1972    "http://dev.w3.org/cvsweb/~checkout~/2001/" +
 1973    "XInclude-Test-Suite/testdescr.xml?content-type=text/" +
 1974    "plain&only_with_tag=HEAD"
 1975    );
 1976    }
 1977  1 Document master = builder.build(baseURL.toExternalForm());
 1978  1 Element testsuite = master.getRootElement();
 1979  1 Elements testcases = testsuite.getChildElements("testcases");
 1980  1 for (int i = 0; i < testcases.size(); i++) {
 1981  5 Element group = testcases.get(i);
 1982  5 String basedir = group.getAttributeValue("basedir");
 1983  5 if (basedir.startsWith("Harold")) {
 1984    // These tests are listed in the catalog but haven't
 1985    // yet been checked into CVS. besides, these are all
 1986    // based on individual tests in this class anyway, so
 1987    // running these is duplicated effort.
 1988  1 continue;
 1989    }
 1990  4 Elements cases = group.getChildElements("testcase");
 1991  4 for (int j = 0; j < cases.size(); j++) {
 1992  72 Element testcase = cases.get(j);
 1993  72 String id = testcase.getAttributeValue("id");
 1994  72 String features = testcase.getAttributeValue("features");
 1995  72 if (features != null) {
 1996  2 if (features.indexOf("unexpanded-entities") >= 0) continue;
 1997  2 if (features.indexOf("unparsed-entities") >= 0) continue;
 1998  15 if (features.indexOf("xpointer-scheme") >= 0) continue;
 1999    }
 2000  53 String description
 2001    = testcase.getFirstChildElement("description").getValue();
 2002  4 if (!basedir.endsWith("/")) basedir += '/';
 2003  53 URL input = new URL(baseURL, basedir);
 2004  53 input = new URL(input, testcase.getAttributeValue("href"));
 2005  53 Element output = testcase.getFirstChildElement("output");
 2006  53 if (output == null) { // test failure
 2007  15 try {
 2008  15 Document doc = builder.build(input.toExternalForm());
 2009  15 XIncluder.resolveInPlace(doc);
 2010  0 fail("Failed test " + id + ": " + description);
 2011    }
 2012    catch (XIncludeException success) {
 2013  11 assertNotNull(success.getMessage());
 2014    }
 2015    catch (IOException success) {
 2016  1 if (baseURL.getProtocol().equals("file")) {
 2017  0 assertNotNull("Problem processing " + input, success.getMessage());
 2018    }
 2019    }
 2020    catch (ParsingException success) {
 2021  3 assertNotNull(success.getMessage());
 2022    }
 2023    }
 2024    else {
 2025  38 URL result = new URL(baseURL, basedir);
 2026  38 result = new URL(result, output.getValue());
 2027  38 Document expected = builder.build(result.toExternalForm());
 2028  38 Document doc = builder.build(input.toExternalForm());
 2029  38 XIncluder.resolveInPlace(doc);
 2030  38 try {
 2031  38 assertEquals("Error when processing "
 2032    + result, expected, doc);
 2033    }
 2034    catch (AssertionFailedError t) {
 2035    // If it fails, try it without a doctype in result.
 2036    // A lot of the test cases have incorrect DOCTYPE
 2037    // declarations.
 2038  2 DocType doctype = expected.getDocType();
 2039  2 DocType actualDoctype = doc.getDocType();
 2040  2 if (doctype != null) {
 2041  0 expected.removeChild(doctype);
 2042  0 assertEquals("Error when processing "
 2043    + input, expected, doc);
 2044    }
 2045  2 else if (actualDoctype != null) {
 2046  2 doc.removeChild(actualDoctype);
 2047  2 assertEquals("Error when processing "
 2048    + input, expected, doc);
 2049    }
 2050    else {
 2051  0 fail();
 2052    }
 2053    }
 2054    }
 2055    }
 2056    }
 2057   
 2058    }
 2059   
 2060   
 2061  0 private void compare(File expected, File input)
 2062    throws IOException, ParsingException, XIncludeException {
 2063   
 2064  0 Document expectedDoc = builder.build(expected);
 2065  0 Document doc = builder.build(input);
 2066  0 XIncluder.resolveInPlace(doc);
 2067  0 assertEquals("Error when processing "
 2068    + input.getName(), expectedDoc, doc);
 2069   
 2070    }
 2071   
 2072   
 2073    // Turn off these tests because Java doesn't support UCS4 yet
 2074    /* public void testAutoDetectUCS4BE()
 2075    throws ParsingException, IOException, XIncludeException {
 2076   
 2077    File input = new File(inputDir, "UCS4BE.xml");
 2078    Document doc = builder.build(input);
 2079    Document result = XIncluder.resolve(doc);
 2080    Document expectedResult = builder.build(
 2081    new File(outputDir, "UTF8WithByteOrderMark.xml")
 2082    );
 2083    assertEquals(expectedResult, result);
 2084   
 2085    }
 2086   
 2087    public void testAutoDetectUCS4LE()
 2088    throws ParsingException, IOException, XIncludeException {
 2089   
 2090    File input = new File(inputDir, "UCS4LE.xml");
 2091    Document doc = builder.build(input);
 2092    Document result = XIncluder.resolve(doc);
 2093    Document expectedResult = builder.build(
 2094    new File(outputDir, "UTF8WithByteOrderMark.xml")
 2095    );
 2096    assertEquals(expectedResult, result);
 2097   
 2098    } */
 2099   
 2100   
 2101    // Need a test case where A includes B, B includes C
 2102    // and B encounters the error (e.g. a missing href)
 2103    // to make sure B's URL is in the error message, not A's
 2104  1 public void testChildDocumentSetsErrorURI()
 2105    throws ParsingException, IOException, XIncludeException {
 2106   
 2107  1 File input = new File(inputDir, "toplevel.xml");
 2108  1 File error = new File(inputDir, "onedown.xml");
 2109  1 Document doc = builder.build(input);
 2110  1 try {
 2111  1 XIncluder.resolve(doc);
 2112  0 fail("Missing HREF not detected");
 2113    }
 2114    catch (NoIncludeLocationException success) {
 2115  1 assertNotNull(success.getMessage());
 2116  1 URL u1 = error.toURL();
 2117  1 URL u2 = new URL(success.getURI());
 2118  1 assertEquals(u1, u2);
 2119    }
 2120   
 2121    }
 2122   
 2123   
 2124  1 public void testColonizedNameForIdValueInElementScheme()
 2125    throws ParsingException, IOException {
 2126   
 2127  1 File input = new File(inputDir, "badxptr3.xml");
 2128  1 Document doc = builder.build(input);
 2129  1 try {
 2130  1 XIncluder.resolve(doc);
 2131  0 fail("Bad ID in element not detected");
 2132    }
 2133    catch (XIncludeException success) {
 2134  1 assertNotNull(success.getMessage());
 2135    }
 2136   
 2137    }
 2138   
 2139   
 2140  1 public void testBadIdValueInElementScheme()
 2141    throws ParsingException, IOException {
 2142   
 2143  1 File input = new File(inputDir, "badxptr4.xml");
 2144  1 Document doc = builder.build(input);
 2145  1 try {
 2146  1 XIncluder.resolve(doc);
 2147  0 fail("Bad ID in element not detected");
 2148    }
 2149    catch (XIncludeException success) {
 2150  1 assertNotNull(success.getMessage());
 2151    }
 2152   
 2153    }
 2154   
 2155   
 2156  1 public void testCirclePointer()
 2157    throws ParsingException, IOException, XIncludeException {
 2158   
 2159  1 File input = new File(inputDir, "circlepointer1.xml");
 2160  1 Document doc = builder.build(input);
 2161  1 try {
 2162  1 XIncluder.resolve(doc);
 2163  0 fail("Allowed circular reference via XPointer");
 2164    }
 2165    catch (InclusionLoopException success) {
 2166  1 assertNotNull(success.getMessage());
 2167    }
 2168   
 2169    }
 2170   
 2171   
 2172  1 public void testXPointerOverridesFragmentID()
 2173    throws ParsingException, IOException, XIncludeException {
 2174   
 2175  1 File input = new File(inputDir, "xpointeroverridesfragmentid.xml"
 2176    );
 2177  1 Document doc = builder.build(input);
 2178  1 try {
 2179  1 XIncluder.resolve(doc);
 2180  0 fail("Allowed href attribute with fragment ID");
 2181    }
 2182    catch (XIncludeException success) {
 2183  1 assertNotNull(success.getMessage());
 2184    }
 2185   
 2186    }
 2187   
 2188   
 2189  1 public void testFailsOnFragmentID()
 2190    throws ParsingException, IOException, XIncludeException {
 2191   
 2192  1 File input = new File(inputDir, "ignoresfragmentid.xml");
 2193  1 Document doc = builder.build(input);
 2194  1 try {
 2195  1 XIncluder.resolve(doc);
 2196  0 fail("Allowed href attribute with fragment ID");
 2197    }
 2198    catch (XIncludeException success) {
 2199  1 assertNotNull(success.getMessage());
 2200    }
 2201   
 2202    }
 2203   
 2204   
 2205    // This also tests that the base URI applied to an element is as set by the xml:base
 2206    // attribute, not the document.
 2207  1 public void testFragmentIDsAreRemovedFromElementBaseURIsAfterInclusion()
 2208    throws ParsingException, IOException, XIncludeException {
 2209   
 2210  1 File input = new File(inputDir, "basewithfragmentid.xml");
 2211  1 Document doc = builder.build(input);
 2212  1 Document result = XIncluder.resolve(doc);
 2213  1 Document expectedResult = builder.build(
 2214    new File(outputDir, "basewithfragmentid.xml")
 2215    );
 2216  1 assertEquals(expectedResult, result);
 2217   
 2218    }
 2219   
 2220   
 2221  1 public void testIncludeLowerCaseFileNames()
 2222    throws ParsingException, IOException, XIncludeException {
 2223   
 2224  1 File input = new File(inputDir, "lowercasealphabet.xml");
 2225  1 Document doc = builder.build(input);
 2226  1 Document result = XIncluder.resolve(doc);
 2227  1 Document expectedResult = builder.build(
 2228    new File(outputDir, "lowercasealphabet.xml")
 2229    );
 2230  1 assertEquals(expectedResult, result);
 2231   
 2232    }
 2233   
 2234   
 2235  1 public void testIncludeUpperCaseFileNames()
 2236    throws ParsingException, IOException, XIncludeException {
 2237   
 2238  1 File input = new File(inputDir, "uppercasealphabet.xml");
 2239  1 Document doc = builder.build(input);
 2240  1 Document result = XIncluder.resolve(doc);
 2241  1 Document expectedResult = builder.build(
 2242    new File(outputDir, "uppercasealphabet.xml")
 2243    );
 2244  1 assertEquals(expectedResult, result);
 2245   
 2246    }
 2247   
 2248   
 2249  1 public void testIncludeDigitFileNames()
 2250    throws ParsingException, IOException, XIncludeException {
 2251   
 2252  1 File input = new File(inputDir, "numeric.xml");
 2253  1 Document doc = builder.build(input);
 2254  1 Document result = XIncluder.resolve(doc);
 2255  1 Document expectedResult = builder.build(
 2256    new File(outputDir, "numeric.xml")
 2257    );
 2258  1 assertEquals(expectedResult, result);
 2259   
 2260    }
 2261   
 2262   
 2263  1 public void testIncludeHighPunctuationFileNames()
 2264    throws ParsingException, IOException, XIncludeException {
 2265   
 2266    // Windows has a problem with some of these file names so
 2267    // first we have to generate the file, just to avoid storing
 2268    // it in the zip archive
 2269  1 try {
 2270  1 File f = new File(inputDir, "{|}.txt");
 2271  1 Writer out = new OutputStreamWriter(
 2272    new FileOutputStream(f), "UTF8");
 2273  1 out.write("{|}");
 2274  1 out.flush();
 2275  1 out.close();
 2276   
 2277  1 File input = new File(inputDir, "punctuation.xml");
 2278  1 Document doc = builder.build(input);
 2279  1 Document result = XIncluder.resolve(doc);
 2280  1 Document expectedResult = builder.build(
 2281    new File(outputDir, "punctuation.xml")
 2282    );
 2283  1 f.delete();
 2284  1 assertEquals(expectedResult, result);
 2285    }
 2286    catch (FileNotFoundException ex) {
 2287    // This file can't even exist on Windows.
 2288    // We can only test this on Unix.
 2289  0 if (!windows) throw ex;
 2290    }
 2291   
 2292    }
 2293   
 2294   
 2295  1 public void testMiddlePunctuationError()
 2296    throws ParsingException, IOException, XIncludeException {
 2297   
 2298  1 File input = new File(inputDir, "middlepunctuationerror.xml");
 2299  1 Document doc = builder.build(input);
 2300  1 try {
 2301  1 XIncluder.resolve(doc);
 2302  0 fail("Allowed illegal IRI with right square bracket ]");
 2303    }
 2304    catch (BadHrefAttributeException success) {
 2305  1 assertNotNull(success.getMessage());
 2306    }
 2307   
 2308    }
 2309   
 2310   
 2311  1 public void testIncludeLowerPunctuationFileNames()
 2312    throws ParsingException, IOException, XIncludeException {
 2313   
 2314  1 try {
 2315  1 File f = new File(inputDir, "!\"$&'+,.txt");
 2316  1 Writer out = new OutputStreamWriter(
 2317    new FileOutputStream(f), "UTF8");
 2318  1 out.write("!\"$&'+,");
 2319  1 out.flush();
 2320  1 out.close();
 2321   
 2322  1 File input = new File(inputDir, "lowerpunctuation.xml");
 2323  1 Document doc = builder.build(input);
 2324  1 Document result = XIncluder.resolve(doc);
 2325  1 Document expectedResult = builder.build(
 2326    new File(outputDir, "lowerpunctuation.xml")
 2327    );
 2328  1 f.delete();
 2329  1 assertEquals(expectedResult, result);
 2330    }
 2331    catch (FileNotFoundException ex) {
 2332    // This file can't even exist on Windows.
 2333    // We can only test this on Unix.
 2334  0 if (!windows) throw ex;
 2335    }
 2336   
 2337    }
 2338   
 2339   
 2340  1 public void testLineEnds()
 2341    throws ParsingException, IOException, XIncludeException {
 2342   
 2343  1 File input = new File(inputDir, "lineends.xml");
 2344  1 Document doc = builder.build(input);
 2345  1 Document result = XIncluder.resolve(doc);
 2346  1 Document expected = builder.build(
 2347    new File(outputDir, "lineends.xml")
 2348    );
 2349  1 assertEquals(expected, result);
 2350   
 2351    }
 2352   
 2353   
 2354    // This is semantically bad; but still meets the
 2355    // syntax of fragment IDs from RFC 2396
 2356  1 public void testBadXPointerInFragmentIDIsFatalError()
 2357    throws ParsingException, IOException, XIncludeException {
 2358   
 2359  1 File input = new File(
 2360    "data/xinclude/input/meaninglessfragmentid.xml");
 2361  1 Document doc = builder.build(input);
 2362  1 try {
 2363  1 XIncluder.resolve(doc);
 2364  0 fail("Allowed href attribute with fragment ID");
 2365    }
 2366    catch (XIncludeException success) {
 2367  1 assertNotNull(success.getMessage());
 2368    }
 2369   
 2370    }
 2371   
 2372   
 2373    // These tests actually connect to IBiblio to load the included
 2374    // data. This is necessary because file URLs don't support
 2375    // content negotiation
 2376  1 public void testAcceptLanguageFrench()
 2377    throws ParsingException, IOException, XIncludeException {
 2378   
 2379  1 File input = new File(inputDir, "acceptfrench.xml");
 2380  1 Document doc = builder.build(input);
 2381  1 Document result = XIncluder.resolve(doc);
 2382  1 Document expectedResult = builder.build(
 2383    new File(outputDir, "acceptfrench.xml")
 2384    );
 2385  1 assertEquals(expectedResult, result);
 2386   
 2387    }
 2388   
 2389   
 2390  1 public void testAcceptLanguageEnglish()
 2391    throws ParsingException, IOException, XIncludeException {
 2392   
 2393  1 File input = new File(inputDir, "acceptenglish.xml");
 2394  1 Document doc = builder.build(input);
 2395  1 Document result = XIncluder.resolve(doc);
 2396  1 Document expectedResult = builder.build(
 2397    new File(outputDir, "acceptenglish.xml")
 2398    );
 2399  1 assertEquals(expectedResult, result);
 2400   
 2401    }
 2402   
 2403   
 2404  1 public void testAcceptPlainText()
 2405    throws ParsingException, IOException, XIncludeException {
 2406   
 2407  1 File input = new File(inputDir, "acceptplaintext.xml");
 2408  1 Document doc = builder.build(input);
 2409  1 Document result = XIncluder.resolve(doc);
 2410  1 Document expectedResult = builder.build(
 2411    new File(outputDir, "acceptplaintext.xml")
 2412    );
 2413  1 assertEquals(expectedResult, result);
 2414   
 2415    }
 2416   
 2417   
 2418  1 public void testAcceptHTML()
 2419    throws ParsingException, IOException, XIncludeException {
 2420   
 2421  1 File input = new File(inputDir, "accepthtml.xml");
 2422  1 Document doc = builder.build(input);
 2423  1 Document result = XIncluder.resolve(doc);
 2424  1 Document expectedResult = builder.build(
 2425    new File(outputDir, "accepthtml.xml")
 2426    );
 2427  1 assertEquals(expectedResult, result);
 2428   
 2429    }
 2430   
 2431   
 2432  1 public void testBadHTTPHeaderExceptionConstructor() {
 2433   
 2434  1 String message = "test";
 2435  1 XIncludeException ex = new BadHTTPHeaderException(
 2436    message, "http://www.example.com/");
 2437  1 assertEquals(message, ex.getMessage());
 2438  1 assertEquals("http://www.example.com/", ex.getURI());
 2439   
 2440    }
 2441   
 2442   
 2443  1 public void testBadHrefAttributerExceptionConstructor() {
 2444   
 2445  1 String message = "test";
 2446  1 Exception ex = new BadHrefAttributeException(message);
 2447  1 assertEquals(message, ex.getMessage());
 2448   
 2449    }
 2450   
 2451   
 2452  1 public void testPercentEscapesAreNotAllowedInXPointerAttributes()
 2453    throws ParsingException, IOException, XIncludeException {
 2454   
 2455  1 File input = new File(inputDir, "xpointerwithpercentescape.xml");
 2456  1 Document doc = builder.build(input);
 2457  1 try {
 2458  1 XIncluder.resolve(doc);
 2459  0 fail("Allowed xpointer attribute with percent escape");
 2460    }
 2461    catch (XIncludeException success) {
 2462  1 assertNotNull(success.getMessage());
 2463  1 Exception cause = (Exception) success.getCause();
 2464  1 assertNotNull(cause);
 2465    }
 2466   
 2467    }
 2468   
 2469  1 public void testXPointerExceptionReinitializeCause()
 2470    throws ParsingException, IOException, XIncludeException {
 2471   
 2472  1 File input = new File(inputDir, "xpointerwithpercentescape.xml");
 2473  1 Document doc = builder.build(input);
 2474  1 try {
 2475  1 XIncluder.resolve(doc);
 2476  0 fail("Allowed xpointer attribute with percent escape");
 2477    }
 2478    catch (XIncludeException success) {
 2479  1 Exception cause = (Exception) success.getCause();
 2480  1 try {
 2481  1 cause.initCause(new Exception());
 2482  0 fail("Reinitialized cause");
 2483    }
 2484    catch (IllegalStateException ex) {
 2485  1 assertNotNull(ex.getMessage());
 2486    }
 2487    }
 2488   
 2489    }
 2490   
 2491   
 2492   
 2493    // WARNING: this test is one interpretation of the XInclude
 2494    // proposed recommendation. It asserts that encoding attributes
 2495    // that do not contain legal encoding names are fatal errors.
 2496    // This is far from certain. It is also possible the working group
 2497    // will choose to interpret these as resource errors.
 2498  1 public void testMalformedEncodingAttribute()
 2499    throws IOException, ParsingException, XIncludeException {
 2500   
 2501  1 File input = new File(inputDir, "badencoding.xml");
 2502  1 Document doc = builder.build(input);
 2503  1 try {
 2504  1 XIncluder.resolve(doc);
 2505  0 fail("Allowed encoding attribute with white space");
 2506    }
 2507    catch (BadEncodingAttributeException success) {
 2508  1 assertNotNull(success.getMessage());
 2509  1 assertTrue(success.getURI().endsWith(input.getName()));
 2510    }
 2511   
 2512    }
 2513   
 2514   
 2515  1 public void testEmptyEncodingAttribute()
 2516    throws IOException, ParsingException, XIncludeException {
 2517   
 2518  1 File input = new File(inputDir, "emptyencoding.xml");
 2519  1 Document doc = builder.build(input);
 2520  1 try {
 2521  1 XIncluder.resolve(doc);
 2522  0 fail("Allowed encoding attribute with no value");
 2523    }
 2524    catch (BadEncodingAttributeException success) {
 2525  1 assertNotNull(success.getMessage());
 2526  1 assertTrue(success.getURI().endsWith(input.getName()));
 2527    }
 2528   
 2529    }
 2530   
 2531   
 2532  1 public void testEncodingAttributeStartsWithDigit()
 2533    throws IOException, ParsingException, XIncludeException {
 2534   
 2535  1 File input = new File(inputDir, "digitencoding.xml");
 2536  1 Document doc = builder.build(input);
 2537  1 try {
 2538  1 XIncluder.resolve(doc);
 2539  0 fail("Allowed encoding attribute starting with digit");
 2540    }
 2541    catch (BadEncodingAttributeException success) {
 2542  1 assertNotNull(success.getMessage());
 2543  1 assertTrue(success.getURI().endsWith(input.getName()));
 2544    }
 2545   
 2546    }
 2547   
 2548   
 2549    // Test that a malformed parse attribute is not thrown when the
 2550    // fallback element containing it is not activated.
 2551  1 public void testHiddenError()
 2552    throws ParsingException, IOException, XIncludeException {
 2553   
 2554  1 File input = new File(inputDir, "hiddenerror.xml");
 2555  1 Document doc = builder.build(input);
 2556  1 XIncluder.resolve(doc);
 2557   
 2558    }
 2559   
 2560   
 2561    // Test that an href attribute that has a fragment identifier
 2562    // is not a fatal error when the fallback element containing
 2563    // it is not activated.
 2564  1 public void testHiddenError2()
 2565    throws ParsingException, IOException, XIncludeException {
 2566   
 2567  1 File input = new File(inputDir, "hiddenerror2.xml");
 2568  1 Document doc = builder.build(input);
 2569  1 XIncluder.resolve(doc);
 2570   
 2571    }
 2572   
 2573   
 2574    // Test that a fallback element with a non-include parent is not a
 2575    // fatal error when the ancestor fallback element containing it is
 2576    // not activated.
 2577  1 public void testHiddenError3()
 2578    throws ParsingException, IOException, XIncludeException {
 2579   
 2580  1 File input = new File(inputDir, "hiddenerror3.xml");
 2581  1 Document doc = builder.build(input);
 2582  1 XIncluder.resolve(doc);
 2583   
 2584    }
 2585   
 2586   
 2587    // Test that an xpointer attribute that uses percent escapes
 2588    // is a not a fatal error when the
 2589    // fallback element containing it is not activated. See
 2590    // http://lists.w3.org/Archives/Public/www-xml-xinclude-comments/2004Oct/0008.html
 2591  1 public void testXpointerAttributeContainsPercentEscapeInUnactivatedFallback()
 2592    throws ParsingException, IOException, XIncludeException {
 2593   
 2594  1 File input = new File(inputDir, "hiddenerror3.xml");
 2595  1 Document doc = builder.build(input);
 2596  1 XIncluder.resolve(doc);
 2597   
 2598    }
 2599   
 2600   
 2601    }