KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > XIncludeTest


1 /* Copyright 2002-2004 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 JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.PrintStream JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.StringReader JavaDoc;
33 import java.io.Writer JavaDoc;
34 import java.net.URL JavaDoc;
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 /**
62  * <p>
63  * Unit tests for the XInclude and XPointer engines.
64  * </p>
65  *
66  * @author Elliotte Rusty Harold
67  * @version 1.0
68  *
69  */

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 JavaDoc name) {
77         super(name);
78     }
79
80     
81     private Builder builder = new Builder();
82     private File JavaDoc inputDir;
83     private File JavaDoc outputDir;
84     
85     // This class tests error conditions, which Xerces
86
// annoyingly logs to System.err. This hides System.err
87
// before each test and restores it after each test.
88
private PrintStream JavaDoc systemErr = System.err;
89     
90     
91     protected void setUp() {
92         
93         System.setErr(new PrintStream JavaDoc(new ByteArrayOutputStream JavaDoc()));
94         
95         inputDir = new File JavaDoc("data");
96         inputDir = new File JavaDoc(inputDir, "xinclude");
97         inputDir = new File JavaDoc(inputDir, "input");
98         
99         outputDir = new File JavaDoc("data");
100         outputDir = new File JavaDoc(outputDir, "xinclude");
101         outputDir = new File JavaDoc(outputDir, "output");
102         
103     }
104     
105     
106     protected void tearDown() {
107         System.setErr(systemErr);
108     }
109     
110     
111     private void dumpResult(File JavaDoc original, Document result)
112       throws IOException JavaDoc {
113         
114         String JavaDoc name = original.getName();
115         File JavaDoc debug = new File JavaDoc("data");
116         debug = new File JavaDoc(debug, "xinclude");
117         debug = new File JavaDoc(debug, "debug");
118         File JavaDoc output = new File JavaDoc(debug, name);
119         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(output);
120         Serializer serializer = new Serializer(out);
121         serializer.write(result);
122         
123     }
124     
125     
126     public void testXPointersResolvedAgainstAcquiredInfoset()
127       throws ParsingException, IOException JavaDoc, XIncludeException {
128       
129         File JavaDoc input = new File JavaDoc(inputDir, "tobintop.xml");
130         Document doc = builder.build(input);
131         Document result = XIncluder.resolve(doc);
132         Document expected = builder.build(
133           new File JavaDoc(outputDir, "tobintop.xml")
134         );
135         assertEquals(expected, result);
136                 
137     }
138
139     
140     public void testXMLBaseUsedToResolveHref()
141       throws ParsingException, IOException JavaDoc, XIncludeException {
142       
143         File JavaDoc input = new File JavaDoc(inputDir, "xmlbasetest.xml");
144         Document doc = builder.build(input);
145         Document result = XIncluder.resolve(doc);
146         Document expected = builder.build(
147           new File JavaDoc(outputDir, "xmlbasetest.xml")
148         );
149         assertEquals(expected, result);
150                 
151     }
152
153     
154     // Tests that use XPointer to
155
// grab a part of the document that contains an include element
156
// and make sure that's fully resolved too
157
public void testResolveThroughXPointer()
158       throws ParsingException, IOException JavaDoc, XIncludeException {
159       
160         File JavaDoc input = new File JavaDoc(inputDir, "resolvethruxpointer.xml");
161         Document doc = builder.build(input);
162         Document result = XIncluder.resolve(doc);
163         Document expectedResult = builder.build(
164           new File JavaDoc(outputDir, "resolvethruxpointer.xml")
165         );
166         assertEquals(expectedResult, result);
167         
168     }
169  
170     
171     public void testXMLBaseOnIncludeElementUsedToResolveHref()
172       throws ParsingException, IOException JavaDoc, XIncludeException {
173       
174         File JavaDoc input = new File JavaDoc(inputDir, "xmlbasetest2.xml");
175         Document doc = builder.build(input);
176         Document result = XIncluder.resolve(doc);
177         Document expected = builder.build(
178           new File JavaDoc(outputDir, "xmlbasetest2.xml")
179         );
180         assertEquals(expected, result);
181                 
182     }
183     
184
185     public void testXMLBaseRetainedFromUnincludedElement()
186       throws ParsingException, IOException JavaDoc, XIncludeException {
187       
188         File JavaDoc input = new File JavaDoc(inputDir, "xmlbasetest3.xml");
189         Document doc = builder.build(input);
190         Document result = XIncluder.resolve(doc);
191         Document expected = builder.build(
192           new File JavaDoc(outputDir, "xmlbasetest3.xml")
193         );
194         assertEquals(expected, result);
195                 
196     }
197     
198  
199     public void testMarsh()
200       throws ParsingException, IOException JavaDoc, XIncludeException {
201       
202         File JavaDoc input = new File JavaDoc(inputDir, "marshtest.xml");
203         Document doc = builder.build(input);
204         Document result = XIncluder.resolve(doc);
205         Document expectedResult = builder.build(
206           new File JavaDoc(outputDir, "marshtest.xml")
207         );
208         assertEquals(expectedResult, result);
209         
210     }
211
212     
213     public void testIncludeDocumentThatUsesIntradocumentReferences()
214       throws ParsingException, IOException JavaDoc, XIncludeException {
215       
216         File JavaDoc input = new File JavaDoc(inputDir, "includedocumentwithintradocumentreferences.xml");
217         Document doc = builder.build(input);
218         Document result = XIncluder.resolve(doc);
219         Document expectedResult = builder.build(
220           new File JavaDoc(outputDir, "includedocumentwithintradocumentreferences.xml")
221         );
222         assertEquals(expectedResult, result);
223         
224     }
225     
226     
227     public void testXMLLangAttributes()
228       throws ParsingException, IOException JavaDoc, XIncludeException {
229       
230         File JavaDoc input = new File JavaDoc(inputDir, "langtest1.xml");
231         Document doc = builder.build(input);
232         Document result = XIncluder.resolve(doc);
233         Document expectedResult = builder.build(
234           new File JavaDoc(outputDir, "langtest1.xml")
235         );
236         assertEquals(expectedResult, result);
237         
238     }
239     
240     
241     public void testInheritedXMLLangAttributes()
242       throws ParsingException, IOException JavaDoc, XIncludeException {
243       
244         File JavaDoc input = new File JavaDoc(inputDir, "langtest2.xml");
245         Document doc = builder.build(input);
246         Document result = XIncluder.resolve(doc);
247         Document expectedResult = builder.build(
248           new File JavaDoc(outputDir, "langtest2.xml")
249         );
250         assertEquals(expectedResult, result);
251         
252     }
253     
254     
255     public void testNoLanguageSpecified()
256       throws ParsingException, IOException JavaDoc, XIncludeException {
257       
258         File JavaDoc input = new File JavaDoc(inputDir, "langtest3.xml");
259         Document doc = builder.build(input);
260         Document result = XIncluder.resolve(doc);
261         Document expectedResult = builder.build(
262           new File JavaDoc(outputDir, "langtest3.xml")
263         );
264         assertEquals(expectedResult, result);
265         
266     }
267     
268     
269     // According to RFC 2396 empty string URI always refers to the
270
// current document irrespective of base URI
271
public void testXMLBaseNotUsedToResolveMissingHref()
272       throws ParsingException, IOException JavaDoc, XIncludeException {
273       
274         File JavaDoc input = new File JavaDoc(inputDir, "marshtestwithxmlbase.xml");
275         Document doc = builder.build(input);
276         Document result = XIncluder.resolve(doc);
277         Document expectedResult = builder.build(
278           new File JavaDoc(outputDir, "marshtestwithxmlbase.xml")
279         );
280         assertEquals(expectedResult, result);
281         
282     }
283     
284     
285     public void testEmptyHrefTreatedSameAsMissingHref()
286       throws ParsingException, IOException JavaDoc, XIncludeException {
287       
288         File JavaDoc input = new File JavaDoc(inputDir, "marshtestwithxmlbaseandemptyhref.xml");
289         Document doc = builder.build(input);
290         Document result = XIncluder.resolve(doc);
291         Document expectedResult = builder.build(
292           new File JavaDoc(outputDir, "marshtestwithxmlbase.xml")
293         );
294         assertEquals(expectedResult, result);
295         
296     }
297     
298     
299     public void testBaselessDocument()
300       throws IOException JavaDoc, 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 JavaDoc 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 JavaDoc, XIncludeException {
319       
320         File JavaDoc input = new File JavaDoc(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 JavaDoc(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 JavaDoc, XIncludeException {
341       
342         File JavaDoc input = new File JavaDoc(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 JavaDoc(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 JavaDoc data) {
356             return new Nodes();
357         }
358         
359     }
360
361     
362     public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithComments()
363       throws ParsingException, IOException JavaDoc, XIncludeException {
364       
365         File JavaDoc input = new File JavaDoc(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 JavaDoc(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 JavaDoc data) {
379             return new Nodes(new Comment(data));
380         }
381         
382     }
383
384     
385     public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithAttributes()
386       throws ParsingException, IOException JavaDoc, XIncludeException {
387       
388         File JavaDoc input = new File JavaDoc(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 JavaDoc(outputDir, "c2c.xml")
393         );
394         assertEquals(expectedResult, result);
395         
396     }
397     
398     
399     public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithTwoElements()
400       throws ParsingException, IOException JavaDoc, XIncludeException {
401       
402         File JavaDoc input = new File JavaDoc(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 JavaDoc(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 JavaDoc 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 JavaDoc data) {
428             return new Nodes(new Attribute("name", data));
429         }
430         
431     }
432
433  
434     public void testUnrecognizedXPointerScheme()
435       throws ParsingException, IOException JavaDoc {
436       
437         File JavaDoc input = new File JavaDoc(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 JavaDoc, ParsingException, XIncludeException {
452       
453         File JavaDoc input = new File JavaDoc(inputDir, "unrecognizedschemewithfallback.xml");
454         File JavaDoc output = new File JavaDoc(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 JavaDoc, XIncludeException {
465       
466         File JavaDoc input = new File JavaDoc(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 JavaDoc(outputDir, "c1a.xml")
471         );
472         assertEquals(expectedResult, result);
473         
474     }
475     
476     
477     public void testIncludeTextWithCustomNodeFactoryThatOnlyReturnsRoot()
478       throws ParsingException, IOException JavaDoc, XIncludeException {
479       
480         File JavaDoc input = new File JavaDoc(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 JavaDoc(outputDir, "c1b.xml")
485         );
486         assertEquals(expectedResult, result);
487         
488     }
489     
490     
491     public void testIncludeTextWithCustomNodeFactoryThatFiltersElementsNamedB()
492       throws ParsingException, IOException JavaDoc, XIncludeException {
493       
494         File JavaDoc input = new File JavaDoc(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 JavaDoc(outputDir, "d1.xml")
499         );
500         assertEquals(expectedResult, result);
501         
502     }
503     
504     
505     public void testIncludeTextWithCustomNodeFactoryThatReturnsEachNonRootElementThreeTimes()
506       throws ParsingException, IOException JavaDoc, XIncludeException {
507       
508         File JavaDoc input = new File JavaDoc(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 JavaDoc(outputDir, "triple.xml")
514         );
515         assertEquals(expectedResult, result);
516         
517     }
518     
519     
520     public void test1()
521       throws ParsingException, IOException JavaDoc, XIncludeException {
522       
523         File JavaDoc input = new File JavaDoc(inputDir, "test.xml");
524         Document doc = builder.build(input);
525         Document result = XIncluder.resolve(doc);
526         Document expectedResult = builder.build(
527           new File JavaDoc(outputDir, "test.xml")
528         );
529         assertEquals(expectedResult, result);
530         
531     }
532     
533     
534     public void testBaseURIsPreservedInSameDocumentInclusion()
535       throws ParsingException, IOException JavaDoc, XIncludeException {
536       
537         File JavaDoc input = new File JavaDoc(inputDir, "includefromsamedocumentwithbase.xml");
538         Document doc = builder.build(input);
539         Document result = XIncluder.resolve(doc);
540         Document expectedResult = builder.build(
541           new File JavaDoc(outputDir, "includefromsamedocumentwithbase.xml")
542         );
543         assertEquals(expectedResult, result);
544         
545     }
546     
547     
548     /* public void testResolveNodes()
549       throws IOException, ParsingException, XIncludeException {
550         File dir = new File(inputDir, "");
551         Element include = new Element("xi:include", XIncluder.XINCLUDE_NS);
552         include.setBaseURI(dir.toURL().toExternalForm());
553         include.addAttribute(new Attribute("href", "disclaimer.xml"));
554         Nodes in = new Nodes(include);
555         Nodes out = XIncluder.resolve(in);
556         assertEquals(1, out.size());
557         Element result = (Element) out.get(0);
558         assertEquals("disclaimer",result.getQualifiedName());
559     } */

560
561     
562     public void testNullBaseURI()
563       throws ParsingException, IOException JavaDoc, XIncludeException {
564       
565         File JavaDoc input = new File JavaDoc(inputDir, "disclaimer.xml");
566         String JavaDoc 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 JavaDoc reader = new StringReader JavaDoc(data);
570         Document doc = builder.build(reader);
571         Document result = XIncluder.resolve(doc);
572         Document expectedResult = builder.build(
573           new File JavaDoc(outputDir, "c1.xml")
574         );
575         assertEquals(expectedResult, result);
576
577     }
578     
579     
580     public void testBadIRIIsAFatalError()
581       throws IOException JavaDoc, ParsingException, XIncludeException {
582      
583         String JavaDoc 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 JavaDoc reader = new StringReader JavaDoc(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 JavaDoc, ParsingException, XIncludeException {
601      
602         String JavaDoc 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 JavaDoc reader = new StringReader JavaDoc(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 JavaDoc, ParsingException, XIncludeException {
620      
621         String JavaDoc 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 JavaDoc reader = new StringReader JavaDoc(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 JavaDoc, XIncludeException {
636       
637         String JavaDoc 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&#x0D;&#x0A;Something: bad'/>\n</document>";
641         Reader JavaDoc reader = new StringReader JavaDoc(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 JavaDoc, XIncludeException {
656       
657         String JavaDoc 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&#xA0;Something: bad'/>\n</document>";
661         Reader JavaDoc reader = new StringReader JavaDoc(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 JavaDoc, XIncludeException {
676       
677         File JavaDoc input = new File JavaDoc(inputDir, "extraattributes.xml");
678         Document doc = builder.build(input);
679         Document result = XIncluder.resolve(doc);
680         Document expectedResult = builder.build(
681           new File JavaDoc(outputDir, "c1.xml")
682         );
683         assertEquals(expectedResult, result);
684
685     }
686     
687     
688     public void testEmptyFallback()
689       throws ParsingException, IOException JavaDoc, XIncludeException {
690       
691         File JavaDoc input = new File JavaDoc(inputDir, "emptyfallback.xml");
692         Document doc = builder.build(input);
693         Document result = XIncluder.resolve(doc);
694         Document expectedResult = builder.build(
695           new File JavaDoc(outputDir, "emptyfallback.xml")
696         );
697         assertEquals(expectedResult, result);
698
699     }
700     
701     
702     public void testFallbackInIncludedDocument()
703       throws ParsingException, IOException JavaDoc, XIncludeException {
704       
705         File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktest.xml");
706         Document doc = builder.build(input);
707         Document result = XIncluder.resolve(doc);
708         Document expectedResult = builder.build(
709           new File JavaDoc(outputDir, "metafallbacktest.xml")
710         );
711         assertEquals(expectedResult, result);
712
713     }
714     
715     
716     public void testFallbackInIncludedDocumentUsesAnIntradocumentXPointer()
717       throws ParsingException, IOException JavaDoc, XIncludeException {
718       
719         File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktest6.xml");
720         Document doc = builder.build(input);
721         Document result = XIncluder.resolve(doc);
722         Document expectedResult = builder.build(
723           new File JavaDoc(outputDir, "metafallbacktest6.xml")
724         );
725         assertEquals(expectedResult, result);
726
727     }
728     
729     
730     // changed for b5
731
public void testFallbackInIncludedDocumentIncludesADocumentWithParseEqualsText()
732       throws ParsingException, IOException JavaDoc, XIncludeException {
733       
734         File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktest2.xml");
735         Document doc = builder.build(input);
736         Document result = XIncluder.resolve(doc);
737         Document expectedResult = builder.build(
738           new File JavaDoc(outputDir, "metafallbacktest2.xml")
739         );
740         assertEquals(expectedResult, result);
741
742     }
743         
744     
745     public void testFallbackInIncludedDocumentWithBadParseAttribute()
746       throws ParsingException, IOException JavaDoc, XIncludeException {
747       
748         File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
763       
764         File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
779       
780         File JavaDoc input = new File JavaDoc(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     // changed in b5
794
public void testXPointerIsNotResolvedAgainstTheSourceInfoset()
795       throws ParsingException, IOException JavaDoc, XIncludeException {
796       
797         File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
812       
813         File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktotexttest.xml");
814         Document doc = builder.build(input);
815         Document result = XIncluder.resolve(doc);
816         Document expectedResult = builder.build(
817           new File JavaDoc(outputDir, "metafallbacktotexttest.xml")
818         );
819         assertEquals(expectedResult, result);
820
821     }
822     
823     
824     public void testFallbackInIncludedDocumentWithXPointer()
825       throws ParsingException, IOException JavaDoc, XIncludeException {
826         // This test case activates processFallbackSilently
827
File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktestwithxpointer.xml");
828         Document doc = builder.build(input);
829         Document result = XIncluder.resolve(doc);
830         Document expectedResult = builder.build(
831           new File JavaDoc(outputDir, "metafallbacktestwithxpointer.xml")
832         );
833         assertEquals(expectedResult, result);
834
835     }
836     
837     
838     // changed in b5
839
// test case where fallback falls back to text and comments rather than
840
// an element
841
public void testFallbackInIncludedDocumentWithXPointer2()
842       throws ParsingException, IOException JavaDoc, XIncludeException {
843         
844         // This test case activates processFallbackSilently
845
File JavaDoc input = new File JavaDoc(inputDir, "metafallbacktestwithxpointer2.xml");
846         Document doc = builder.build(input);
847         Document result = XIncluder.resolve(doc);
848         Document expectedResult = builder.build(
849           new File JavaDoc(outputDir, "metafallbacktestwithxpointer2.xml")
850         );
851         assertEquals(expectedResult, result);
852
853     }
854     
855
856     public void testNoFallbackInIncludedDocumentWithXPointer()
857       throws ParsingException, IOException JavaDoc, XIncludeException {
858         
859         // This test case activates processFallbackSilently
860
File JavaDoc input = new File JavaDoc(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 JavaDoc success) {
867             assertNotNull(success.getMessage());
868         }
869
870     }
871     
872     
873     public void testFallbackInIncludedDocumentHasBadXPointer()
874       throws ParsingException, IOException JavaDoc, XIncludeException {
875         // This test case activates processFallbackSilently
876
File JavaDoc input = new File JavaDoc(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     // from the XInclude CR
891
public void testC1()
892       throws ParsingException, IOException JavaDoc, XIncludeException {
893       
894         File JavaDoc input = new File JavaDoc(inputDir, "c1.xml");
895         Document doc = builder.build(input);
896         Document result = XIncluder.resolve(doc);
897         Document expectedResult = builder.build(
898           new File JavaDoc(outputDir, "c1.xml")
899         );
900         assertEquals(expectedResult, result);
901
902     }
903
904     
905     // same test with explicit parse="xml"
906
public void testParseEqualsXML()
907       throws ParsingException, IOException JavaDoc, XIncludeException {
908       
909         File JavaDoc input = new File JavaDoc(inputDir, "parseequalxml.xml");
910         Document doc = builder.build(input);
911         Document result = XIncluder.resolve(doc);
912         Document expectedResult = builder.build(
913           new File JavaDoc(outputDir, "c1.xml")
914         );
915         assertEquals(expectedResult, result);
916
917     }
918
919     
920     // changed in b5
921
public void testAcceptableCirclePointer()
922       throws ParsingException, IOException JavaDoc, XIncludeException {
923       
924         File JavaDoc input = new File JavaDoc(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     // from the XInclude CR
938
public void testC2()
939       throws ParsingException, IOException JavaDoc, XIncludeException {
940       
941         File JavaDoc input = new File JavaDoc(inputDir, "c2.xml");
942         Document doc = builder.build(input);
943         Document result = XIncluder.resolve(doc);
944         Document expectedResult = builder.build(
945           new File JavaDoc(outputDir, "c2.xml")
946         );
947         assertEquals(expectedResult, result);
948         
949     }
950     
951     
952     // from the XInclude CR
953
public void testC3()
954       throws ParsingException, IOException JavaDoc, XIncludeException {
955       
956         File JavaDoc input = new File JavaDoc(inputDir, "c3.xml");
957         Document doc = builder.build(input);
958         Document result = XIncluder.resolve(doc);
959         Document expectedResult = builder.build(
960           new File JavaDoc(outputDir, "c3.xml")
961         );
962         assertEquals(expectedResult, result);
963         
964     }
965     
966     
967     // C4 skipped for the moment because it uses XPointers
968
// that XOM doesn't yet support
969

970     // from the XInclude CR
971
// Don't use this one yet, because there appear to be
972
// mistakes in the spec examples
973
/*public void testC5() throws ParsingException, IOException, XIncludeException {
974       
975         File input = new File(inputDir, "c5.xml");
976         Document doc = builder.build(input);
977         Document result = XIncluder.resolve(doc);
978         Document expectedResult = builder.build(new File(outputDir, "c5.xml"));
979         XMLAssert.assertEquals(expectedResult, result);
980         
981     } */

982     
983     
984     private static class TextNodeFactory extends NodeFactory {
985         
986         public Nodes makeText(String JavaDoc data) {
987             return new Nodes(new TextSubclass(data));
988         }
989         
990     }
991     
992     private static class TextSubclass extends Text {
993         
994         TextSubclass(String JavaDoc 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 JavaDoc, XIncludeException {
1007      
1008        File JavaDoc input = new File JavaDoc(inputDir, "recursewithinsamedocument.xml");
1009        Document doc = builder.build(input);
1010        Document result = XIncluder.resolve(doc);
1011        Document expectedResult = builder.build(
1012          new File JavaDoc(outputDir, "recursewithinsamedocument.xml")
1013        );
1014        assertEquals(expectedResult, result);
1015        
1016    }
1017    
1018    
1019    public void testSiblingIncludes()
1020      throws ParsingException, IOException JavaDoc, XIncludeException {
1021      
1022        File JavaDoc input = new File JavaDoc(inputDir, "paralleltest.xml");
1023        Document doc = builder.build(input);
1024        Document result = XIncluder.resolve(doc);
1025        Document expectedResult = builder.build(
1026          new File JavaDoc(outputDir, "paralleltest.xml")
1027        );
1028        assertEquals(expectedResult, result);
1029        
1030    }
1031    
1032    
1033    public void testNamespaces()
1034      throws ParsingException, IOException JavaDoc, XIncludeException {
1035      
1036        File JavaDoc input = new File JavaDoc(inputDir, "namespacetest.xml");
1037        Document doc = builder.build(input);
1038        Document result = XIncluder.resolve(doc);
1039        Document expectedResult = builder.build(
1040          new File JavaDoc(outputDir, "namespacetest.xml")
1041        );
1042        assertEquals(expectedResult, result);
1043        
1044    }
1045    
1046    
1047    public void testIncludeReferencesItItself()
1048      throws ParsingException, IOException JavaDoc, XIncludeException {
1049      
1050        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1065      
1066        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1081      
1082        File JavaDoc input = new File JavaDoc(inputDir, "latin1.xml");
1083        Document doc = builder.build(input);
1084        Document result = XIncluder.resolve(doc);
1085        Document expectedResult = builder.build(
1086          new File JavaDoc(outputDir, "latin1.xml")
1087        );
1088        assertEquals(expectedResult, result);
1089        
1090    }
1091    
1092    
1093    public void test2()
1094      throws ParsingException, IOException JavaDoc, XIncludeException {
1095      
1096        File JavaDoc input = new File JavaDoc(inputDir, "simple.xml");
1097        Document doc = builder.build(input);
1098        Document result = XIncluder.resolve(doc);
1099        Document expectedResult = builder.build(
1100          new File JavaDoc(outputDir, "simple.xml")
1101        );
1102        assertEquals(expectedResult, result);
1103        
1104    }
1105    
1106    
1107    public void testReplaceRoot()
1108      throws ParsingException, IOException JavaDoc, XIncludeException {
1109      
1110        File JavaDoc input = new File JavaDoc(inputDir, "roottest.xml");
1111        Document doc = builder.build(input);
1112        Document result = XIncluder.resolve(doc);
1113        Document expectedResult = builder.build(
1114          new File JavaDoc(outputDir, "roottest.xml")
1115        );
1116        assertEquals(expectedResult, result);
1117        
1118    }
1119
1120
1121    // In this test the included document has a prolog and an epilog
1122
public void testReplaceRoot2()
1123      throws ParsingException, IOException JavaDoc, XIncludeException {
1124      
1125        File JavaDoc input = new File JavaDoc(inputDir, "roottest2.xml");
1126        Document doc = builder.build(input);
1127        Document result = XIncluder.resolve(doc);
1128        Document expectedResult = builder.build(
1129          new File JavaDoc(outputDir, "roottest2.xml")
1130        );
1131        assertEquals(expectedResult, result);
1132        
1133    }
1134
1135    
1136    public void testIncludeElementsCannotHaveIncludeChildren()
1137      throws ParsingException, IOException JavaDoc, XIncludeException {
1138        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1152        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1166        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1180        File JavaDoc input = new File JavaDoc(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    // In this test the fallback is activated.
1193
public void testMultipleFallbacks()
1194      throws ParsingException, IOException JavaDoc, XIncludeException {
1195        File JavaDoc input = new File JavaDoc(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    // In this test the fallback is not needed.
1208
public void testMultipleFallbacks2()
1209      throws ParsingException, IOException JavaDoc, XIncludeException {
1210        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1224        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1239        
1240        File JavaDoc input = new File JavaDoc(inputDir, "circle2a.xml");
1241        File JavaDoc errorFile = new File JavaDoc(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 JavaDoc, XIncludeException {
1258        
1259        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1275        
1276        File JavaDoc input = new File JavaDoc(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 JavaDoc u1 = input.toURL();
1285            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
1286            assertEquals(u1, u2);
1287        }
1288        
1289    }
1290    
1291    
1292    public void testUnavailableResource()
1293      throws ParsingException, IOException JavaDoc, XIncludeException {
1294        File JavaDoc input = new File JavaDoc(inputDir, "missingfile.xml");
1295        Document doc = builder.build(input);
1296        try {
1297            XIncluder.resolve(doc);
1298            fail("allowed unresolvable resource");
1299        }
1300        catch (IOException JavaDoc success) {
1301            assertNotNull(success.getMessage());
1302        }
1303        
1304    }
1305    
1306    
1307    public void testFallback()
1308      throws ParsingException, IOException JavaDoc, XIncludeException {
1309        
1310        File JavaDoc input = new File JavaDoc(inputDir, "fallbacktest.xml");
1311        Document doc = builder.build(input);
1312        Document result = XIncluder.resolve(doc);
1313        Document expectedResult = builder.build(
1314          new File JavaDoc(outputDir, "fallbacktest.xml")
1315        );
1316        assertEquals(expectedResult, result);
1317        
1318    }
1319    
1320    
1321    public void testFallbackWithRecursiveInclude()
1322      throws ParsingException, IOException JavaDoc, XIncludeException {
1323        
1324        File JavaDoc input = new File JavaDoc(inputDir, "fallbacktest2.xml");
1325        Document doc = builder.build(input);
1326        Document result = XIncluder.resolve(doc);
1327        Document expectedResult = builder.build(
1328          new File JavaDoc(outputDir, "fallbacktest2.xml")
1329        );
1330        assertEquals(expectedResult, result);
1331        
1332    }
1333
1334    
1335    public void testEncodingAttribute()
1336      throws ParsingException, IOException JavaDoc, XIncludeException {
1337      
1338        File JavaDoc input = new File JavaDoc(inputDir, "utf16.xml");
1339        Document doc = builder.build(input);
1340        Document result = XIncluder.resolve(doc);
1341        Document expectedResult = builder.build(
1342          new File JavaDoc(outputDir, "utf16.xml")
1343        );
1344        assertEquals(expectedResult, result);
1345        
1346    }
1347    
1348    
1349    public void testXPointerBareNameID()
1350      throws ParsingException, IOException JavaDoc, XIncludeException {
1351      
1352        File JavaDoc input = new File JavaDoc(inputDir, "xptridtest.xml");
1353        Document doc = builder.build(input);
1354        Document result = XIncluder.resolve(doc);
1355        Document expectedResult = builder.build(
1356          new File JavaDoc(outputDir, "xptridtest.xml")
1357        );
1358        assertEquals(expectedResult, result);
1359        
1360    }
1361    
1362    
1363    public void testShorthandXPointerMatchesNothing()
1364      throws ParsingException, IOException JavaDoc {
1365      
1366        File JavaDoc input = new File JavaDoc(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            // Must compare URLs instead of strings here to avoid
1376
// issues of whether a file URL begins file:/ or file:///
1377
URL JavaDoc u1 = input.toURL();
1378            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
1379            assertEquals(u1, u2);
1380        }
1381        
1382        
1383        /* I used to think this case included nothing.
1384           Now I think an XPointer that matches no
1385           subresource, and does not have a fallback is in error.
1386        Document expectedResult = builder.build(
1387          new File(outputDir, "xptridtest2.xml")
1388        );
1389        assertEquals(expectedResult, result);
1390        */

1391        
1392    }
1393    
1394    
1395    public void testXPointerPureTumbler()
1396      throws ParsingException, IOException JavaDoc, XIncludeException {
1397      
1398        File JavaDoc input = new File JavaDoc(inputDir, "xptrtumblertest.xml");
1399        Document doc = builder.build(input);
1400        Document result = XIncluder.resolve(doc);
1401        Document expectedResult = builder.build(
1402          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1403        );
1404        assertEquals(expectedResult, result);
1405        
1406    }
1407
1408    
1409    public void testUnrecognizedColonizedSchemeNameBackedUpByTumbler()
1410      throws ParsingException, IOException JavaDoc, XIncludeException {
1411      
1412        File JavaDoc input = new File JavaDoc(inputDir, "colonizedschemename.xml");
1413        Document doc = builder.build(input);
1414        Document result = XIncluder.resolve(doc);
1415        Document expectedResult = builder.build(
1416          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1417        );
1418        assertEquals(expectedResult, result);
1419        
1420    }
1421
1422    
1423    public void testXPointerSyntaxErrorInSecondPart()
1424      throws ParsingException, IOException JavaDoc {
1425      
1426        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
1442      
1443        File JavaDoc input = new File JavaDoc(inputDir, "badelementschemedata.xml");
1444        Document doc = builder.build(input);
1445        Document result = XIncluder.resolve(doc);
1446        Document expectedResult = builder.build(
1447          new File JavaDoc(outputDir, "badelementschemedata.xml")
1448        );
1449        // dumpResult(input, result);
1450

1451        assertEquals(expectedResult, result);
1452        
1453    }
1454
1455    
1456    public void testXPointerSyntaxErrorMissingFinalParenthesis()
1457      throws ParsingException, IOException JavaDoc {
1458      
1459        File JavaDoc input = new File JavaDoc(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    // Test we can include from same document using only
1474
// an xpointer attribute
1475
public void testOnlyXPointer()
1476      throws ParsingException, IOException JavaDoc, XIncludeException {
1477      
1478        File JavaDoc input = new File JavaDoc(inputDir, "onlyxpointer.xml");
1479        Document doc = builder.build(input);
1480        Document result = XIncluder.resolve(doc);
1481        Document expectedResult = builder.build(
1482          new File JavaDoc(outputDir, "onlyxpointer.xml")
1483        );
1484        
1485        assertEquals(expectedResult, result);
1486        
1487    }
1488
1489
1490    // Test with 3 element schemes in the XPointer.
1491
// The first and second one point to nothing. The third one
1492
// selects something.
1493
public void testXPointerTripleTumbler()
1494      throws ParsingException, IOException JavaDoc, XIncludeException {
1495      
1496        File JavaDoc input = new File JavaDoc(inputDir, "xptr2tumblertest.xml");
1497        Document doc = builder.build(input);
1498        Document result = XIncluder.resolve(doc);
1499        Document expectedResult = builder.build(
1500          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1501        );
1502        assertEquals(expectedResult, result);
1503        
1504    }
1505
1506    
1507    // Test with 2 element schemes in the XPointer.
1508
// The first one uses an ID that doesn't exist
1509
// and points to nothing. The second one
1510
// selects something.
1511
public void testXPointerDoubleTumbler()
1512      throws ParsingException, IOException JavaDoc, XIncludeException {
1513      
1514        File JavaDoc input = new File JavaDoc(inputDir, "xptrdoubletumblertest.xml");
1515        Document doc = builder.build(input);
1516        Document result = XIncluder.resolve(doc);
1517        Document expectedResult = builder.build(
1518          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1519        );
1520        assertEquals(expectedResult, result);
1521        
1522    }
1523
1524
1525    // Test with 2 element schemes in the XPointer.
1526
// The first one uses an ID that points to something.
1527
// The second one points to something too. Both element schemes
1528
// use IDs exclusively, no child sequences.
1529
public void testXPointerDoubleElementByID()
1530      throws ParsingException, IOException JavaDoc, XIncludeException {
1531      
1532        File JavaDoc input = new File JavaDoc(inputDir, "xptrdoubleelementtest.xml");
1533        Document doc = builder.build(input);
1534        Document result = XIncluder.resolve(doc);
1535        Document expectedResult = builder.build(
1536          new File JavaDoc(outputDir, "xptrdoubleelementtest.xml")
1537        );
1538        assertEquals(expectedResult, result);
1539        
1540    }
1541
1542
1543    // Test with 2 element schemes in the XPointer.
1544
// The first one uses a child sequence that points to something.
1545
// The second one points to something too. Both element schemes
1546
// use child sequences exclusively, no IDs.
1547
public void testXPointerDoubleElementByChildSequence()
1548      throws ParsingException, IOException JavaDoc, XIncludeException {
1549      
1550        File JavaDoc input = new File JavaDoc(inputDir, "xptrdoublechildsequence.xml");
1551        Document doc = builder.build(input);
1552        Document result = XIncluder.resolve(doc);
1553        Document expectedResult = builder.build(
1554          new File JavaDoc(outputDir, "xptrdoubleelementtest.xml")
1555        );
1556        assertEquals(expectedResult, result);
1557        
1558    }
1559
1560
1561    // Make sure XPointer failures are treated as a resource error,
1562
// not a fatal error.
1563
public void testXPointerFailureIsAResourceError()
1564      throws ParsingException, IOException JavaDoc, XIncludeException {
1565      
1566        File JavaDoc input = new File JavaDoc(
1567          "data/xinclude/input/xptrtumblerfailsbutfallback.xml"
1568        );
1569        Document doc = builder.build(input);
1570        Document result = XIncluder.resolve(doc);
1571        // For debugging
1572
// dumpResult(input, result);
1573
Document expectedResult = builder.build(
1574          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1575        );
1576        assertEquals(expectedResult, result);
1577        
1578    }
1579
1580    
1581    // Make sure XPointer syntax errors are treated as a resource
1582
// error, not a fatal error per section 4.2 of XInclude CR
1583
/* Resources that are unavailable for any reason
1584      (for example the resource doesn't exist, connection
1585      difficulties or security restrictions prevent it from being
1586      fetched, the URI scheme isn't a fetchable one, the resource
1587      is in an unsuppored encoding, the resource is determined
1588      through implementation-specific mechanisms not to be XML, or a
1589      syntax error in an [XPointer Framework]) result in a resource
1590      error. */

1591    public void testXPointerSyntaxErrorIsAResourceError()
1592      throws ParsingException, IOException JavaDoc, XIncludeException {
1593      
1594        File JavaDoc input = new File JavaDoc(
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 JavaDoc(outputDir, "xptrtumblertest.xml")
1601        );
1602        assertEquals(expectedResult, result);
1603        
1604    }
1605
1606    
1607    // Test with 3 element schemes in the XPointer,
1608
// separated by white space.
1609
// The first one points to nothing. The third one
1610
// selects something.
1611
public void testXPointerTumblerWithWhiteSpace()
1612      throws ParsingException, IOException JavaDoc, XIncludeException {
1613      
1614        File JavaDoc input
1615          = new File JavaDoc(inputDir, "xptrtumblertest3.xml");
1616        Document doc = builder.build(input);
1617        Document result = XIncluder.resolve(doc);
1618        Document expectedResult = builder.build(
1619          new File JavaDoc(outputDir, "xptrtumblertest.xml")
1620        );
1621        assertEquals(expectedResult, result);
1622        
1623    }
1624
1625    
1626    public void testXPointerTumblerMatchesNothing()
1627      throws ParsingException, IOException JavaDoc {
1628      
1629        File JavaDoc input = new File JavaDoc(
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 JavaDoc u1 = input.toURL();
1640            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
1641            assertEquals(u1, u2);
1642        }
1643        
1644    }
1645    
1646    
1647    public void testMalformedXPointer()
1648      throws ParsingException, IOException JavaDoc {
1649        
1650        File JavaDoc input = new File JavaDoc(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 JavaDoc u1 = input.toURL();
1659            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
1660            assertEquals(u1, u2);
1661        }
1662        
1663    }
1664    
1665    
1666    public void testAnotherMalformedXPointer()
1667      throws ParsingException, IOException JavaDoc {
1668        
1669        // testing use of non NCNAME as ID
1670
File JavaDoc input = new File JavaDoc(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 JavaDoc u1 = input.toURL();
1679            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
1680            assertEquals(u1, u2);
1681        }
1682        
1683    }
1684    
1685    
1686    public void testMalformedXPointerWithFallback()
1687      throws ParsingException, IOException JavaDoc, XIncludeException {
1688      
1689        File JavaDoc input = new File JavaDoc(inputDir, "xptrfallback.xml");
1690        Document doc = builder.build(input);
1691        Document result = XIncluder.resolve(doc);
1692        Document expectedResult = builder.build(new File JavaDoc(
1693          "data/xinclude/output/xptrfallback.xml")
1694        );
1695        assertEquals(expectedResult, result);
1696                
1697    }
1698    
1699    
1700    public void testIDAndTumbler()
1701      throws ParsingException, IOException JavaDoc, XIncludeException {
1702      
1703        File JavaDoc input = new File JavaDoc(
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 JavaDoc(
1709          "data/xinclude/output/xptridandtumblertest.xml")
1710        );
1711        assertEquals(expectedResult, result);
1712                
1713    }
1714
1715    
1716    public void testAutoDetectUTF16BigEndianWithByteOrderMark()
1717      throws ParsingException, IOException JavaDoc, XIncludeException {
1718      
1719        File JavaDoc input = new File JavaDoc(
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 JavaDoc(
1725          "data/xinclude/output/UTF16BigEndianWithByteOrderMark.xml")
1726        );
1727        assertEquals(expectedResult, result);
1728                
1729    }
1730
1731    
1732    public void testAutoDetectUTF16LittleEndianWithByteOrderMark()
1733      throws ParsingException, IOException JavaDoc, XIncludeException {
1734      
1735        File JavaDoc input = new File JavaDoc(
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 JavaDoc(
1741          "data/xinclude/output/UTF16LittleEndianWithByteOrderMark.xml"
1742        ));
1743        assertEquals(expectedResult, result);
1744                
1745    }
1746
1747    
1748    public void testAutoDetectUTF8WithByteOrderMark()
1749      throws ParsingException, IOException JavaDoc, XIncludeException {
1750      
1751        File JavaDoc input = new File JavaDoc(
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 JavaDoc(outputDir, "UTF8WithByteOrderMark.xml")
1758        );
1759        assertEquals(expectedResult, result);
1760                
1761    }
1762
1763    
1764    public void testAutoDetectUnicodeBigUnmarked()
1765      throws ParsingException, IOException JavaDoc, XIncludeException {
1766      
1767        File JavaDoc input = new File JavaDoc(
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 JavaDoc(outputDir, "UnicodeBigUnmarked.xml")
1774        );
1775        assertEquals(expectedResult, result);
1776                
1777    }
1778
1779    
1780    public void testUnicodeLittleUnmarked()
1781      throws ParsingException, IOException JavaDoc, XIncludeException {
1782      
1783        File JavaDoc input = new File JavaDoc(
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 JavaDoc(outputDir, "UnicodeLittleUnmarked.xml")
1790        );
1791        assertEquals(expectedResult, result);
1792                
1793    }
1794
1795    
1796/*// Java doesn't yet support the UTF-32BE and UTF32LE encodings
1797    public void testUTF32BE()
1798      throws ParsingException, IOException, XIncludeException {
1799      
1800        File input = new File(
1801          "data/xinclude/input/UTF32BE.xml"
1802        );
1803        Document doc = builder.build(input);
1804        Document result = XIncluder.resolve(doc);
1805        Document expectedResult = builder.build(
1806          new File(outputDir, "UTF32BE.xml")
1807        );
1808        assertEquals(expectedResult, result);
1809                
1810    }
1811
1812    public void testUTF32LE()
1813      throws ParsingException, IOException, XIncludeException {
1814      
1815        File input = new File(
1816          "data/xinclude/input/UTF32LE.xml"
1817        );
1818        Document doc = builder.build(input);
1819        Document result = XIncluder.resolve(doc);
1820        Document expectedResult = builder.build(
1821          new File(outputDir, "UTF32LE.xml")
1822        );
1823        assertEquals(expectedResult, result);
1824                
1825    }
1826*/

1827    
1828    
1829    public void testEBCDIC()
1830      throws ParsingException, IOException JavaDoc, XIncludeException {
1831      
1832        File JavaDoc input = new File JavaDoc(inputDir, "EBCDIC.xml");
1833        Document doc = builder.build(input);
1834        Document result = XIncluder.resolve(doc);
1835        Document expected = builder.build(new File JavaDoc(outputDir, "EBCDIC.xml"));
1836        assertEquals(expected, result);
1837                
1838    }
1839
1840    
1841    // This test requires files that I have not received permission
1842
// to distribute so for the moment you won't be able to run it.
1843
// For my own use it checks to see if the files are present
1844
// and runs if it does find them. You can't just install the
1845
// XInclude-Test-Suite data as distributed by the W3C here.
1846
// Some of those tests rely on optional features XOM does not
1847
// support such as the xpointer() scheme and notations.
1848
// Plus some of those tests have mistakes. You need my patched
1849
// version of the tests.
1850
public void testXIncludeTestSuite()
1851      throws ParsingException, IOException JavaDoc, XIncludeException {
1852     
1853        File JavaDoc testDescription = new File JavaDoc("data");
1854        testDescription = new File JavaDoc(testDescription, "XInclude-Test-Suite");
1855        testDescription = new File JavaDoc(testDescription, "testdescr.xml");
1856        URL JavaDoc baseURL = testDescription.toURL();
1857        if (!testDescription.exists()) {
1858            baseURL = new URL JavaDoc(
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 JavaDoc basedir = group.getAttributeValue("basedir");
1870            if (basedir.startsWith("Harold")) {
1871                // These tests are listed in the catalog but haven't
1872
// yet been checked into CVS. besides, these are all
1873
// based on individual tests in this class anyway, so
1874
// running these is duplicated effort.
1875
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 JavaDoc id = testcase.getAttributeValue("id");
1881                String JavaDoc 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 JavaDoc description
1888                  = testcase.getFirstChildElement("description").getValue();
1889                if (!basedir.endsWith("/")) basedir += '/';
1890                URL JavaDoc input = new URL JavaDoc(baseURL, basedir);
1891                input = new URL JavaDoc(input, testcase.getAttributeValue("href"));
1892                Element output = testcase.getFirstChildElement("output");
1893                if (output == null) { // test failure
1894
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 JavaDoc 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 JavaDoc result = new URL JavaDoc(baseURL, basedir);
1913                    result = new URL JavaDoc(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                      // If it fails, try it without a doctype in result.
1923
// A lot of the test cases have incorrect DOCTYPE
1924
// declarations.
1925
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 JavaDoc expected, File JavaDoc input)
1949      throws IOException JavaDoc, 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  // Turn off these tests because Java doesn't support UCS4 yet
1961
/* public void testAutoDetectUCS4BE()
1962      throws ParsingException, IOException, XIncludeException {
1963      
1964        File input = new File(inputDir, "UCS4BE.xml");
1965        Document doc = builder.build(input);
1966        Document result = XIncluder.resolve(doc);
1967        Document expectedResult = builder.build(
1968          new File(outputDir, "UTF8WithByteOrderMark.xml")
1969        );
1970        assertEquals(expectedResult, result);
1971                
1972    }
1973
1974    public void testAutoDetectUCS4LE()
1975      throws ParsingException, IOException, XIncludeException {
1976      
1977        File input = new File(inputDir, "UCS4LE.xml");
1978        Document doc = builder.build(input);
1979        Document result = XIncluder.resolve(doc);
1980        Document expectedResult = builder.build(
1981          new File(outputDir, "UTF8WithByteOrderMark.xml")
1982        );
1983        assertEquals(expectedResult, result);
1984                
1985    } */

1986    
1987    
1988    // Need a test case where A includes B, B includes C
1989
// and B encounters the error (e.g. a missing href)
1990
// to make sure B's URL is in the error message, not A's
1991
public void testChildDocumentSetsErrorURI()
1992      throws ParsingException, IOException JavaDoc, XIncludeException {
1993      
1994        File JavaDoc input = new File JavaDoc(inputDir, "toplevel.xml");
1995        File JavaDoc error = new File JavaDoc(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 JavaDoc u1 = error.toURL();
2004            URL JavaDoc u2 = new URL JavaDoc(success.getURI());
2005            assertEquals(u1, u2);
2006        }
2007                
2008    }
2009
2010    
2011    public void testColonizedNameForIdValueInElementScheme()
2012      throws ParsingException, IOException JavaDoc {
2013      
2014        File JavaDoc input = new File JavaDoc(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 JavaDoc {
2029      
2030        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
2045      
2046        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
2061      
2062        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
2078      
2079        File JavaDoc input = new File JavaDoc(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    // This also tests that the base URI applied to an element is as set by the xml:base
2093
// attribute, not the document.
2094
public void testFragmentIDsAreRemovedFromElementBaseURIsAfterInclusion()
2095      throws ParsingException, IOException JavaDoc, XIncludeException {
2096      
2097        File JavaDoc input = new File JavaDoc(inputDir, "basewithfragmentid.xml");
2098        Document doc = builder.build(input);
2099        Document result = XIncluder.resolve(doc);
2100        Document expectedResult = builder.build(
2101          new File JavaDoc(outputDir, "basewithfragmentid.xml")
2102        );
2103        assertEquals(expectedResult, result);
2104        
2105    }
2106    
2107    
2108    public void testIncludeLowerCaseFileNames()
2109      throws ParsingException, IOException JavaDoc, XIncludeException {
2110      
2111        File JavaDoc input = new File JavaDoc(inputDir, "lowercasealphabet.xml");
2112        Document doc = builder.build(input);
2113        Document result = XIncluder.resolve(doc);
2114        Document expectedResult = builder.build(
2115          new File JavaDoc(outputDir, "lowercasealphabet.xml")
2116        );
2117        assertEquals(expectedResult, result);
2118        
2119    }
2120    
2121    
2122    public void testIncludeUpperCaseFileNames()
2123      throws ParsingException, IOException JavaDoc, XIncludeException {
2124      
2125        File JavaDoc input = new File JavaDoc(inputDir, "uppercasealphabet.xml");
2126        Document doc = builder.build(input);
2127        Document result = XIncluder.resolve(doc);
2128        Document expectedResult = builder.build(
2129          new File JavaDoc(outputDir, "uppercasealphabet.xml")
2130        );
2131        assertEquals(expectedResult, result);
2132        
2133    }
2134    
2135    
2136    public void testIncludeDigitFileNames()
2137      throws ParsingException, IOException JavaDoc, XIncludeException {
2138      
2139        File JavaDoc input = new File JavaDoc(inputDir, "numeric.xml");
2140        Document doc = builder.build(input);
2141        Document result = XIncluder.resolve(doc);
2142        Document expectedResult = builder.build(
2143          new File JavaDoc(outputDir, "numeric.xml")
2144        );
2145        assertEquals(expectedResult, result);
2146        
2147    }
2148    
2149    
2150    public void testIncludeHighPunctuationFileNames()
2151      throws ParsingException, IOException JavaDoc, XIncludeException {
2152      
2153        // Windows has a problem with some of these file names so
2154
// first we have to generate the file, just to avoid storing
2155
// it in the zip archive
2156
try {
2157            File JavaDoc f = new File JavaDoc(inputDir, "{|}.txt");
2158            Writer JavaDoc out = new OutputStreamWriter JavaDoc(
2159              new FileOutputStream JavaDoc(f), "UTF8");
2160            out.write("{|}");
2161            out.flush();
2162            out.close();
2163            
2164            File JavaDoc input = new File JavaDoc(inputDir, "punctuation.xml");
2165            Document doc = builder.build(input);
2166            Document result = XIncluder.resolve(doc);
2167            Document expectedResult = builder.build(
2168              new File JavaDoc(outputDir, "punctuation.xml")
2169            );
2170            f.delete();
2171            assertEquals(expectedResult, result);
2172        }
2173        catch (FileNotFoundException JavaDoc ex) {
2174            // This file can't even exist on Windows.
2175
// We can only test this on Unix.
2176
if (!windows) throw ex;
2177        }
2178        
2179    }
2180    
2181    
2182    public void testMiddlePunctuationError()
2183      throws ParsingException, IOException JavaDoc, XIncludeException {
2184      
2185        File JavaDoc input = new File JavaDoc(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 JavaDoc, XIncludeException {
2200      
2201        try {
2202            File JavaDoc f = new File JavaDoc(inputDir, "!\"$&'+,.txt");
2203            Writer JavaDoc out = new OutputStreamWriter JavaDoc(
2204              new FileOutputStream JavaDoc(f), "UTF8");
2205            out.write("!\"$&'+,");
2206            out.flush();
2207            out.close();
2208
2209            File JavaDoc input = new File JavaDoc(inputDir, "lowerpunctuation.xml");
2210            Document doc = builder.build(input);
2211            Document result = XIncluder.resolve(doc);
2212            Document expectedResult = builder.build(
2213              new File JavaDoc(outputDir, "lowerpunctuation.xml")
2214            );
2215            f.delete();
2216            assertEquals(expectedResult, result);
2217        }
2218        catch (FileNotFoundException JavaDoc ex) {
2219            // This file can't even exist on Windows.
2220
// We can only test this on Unix.
2221
if (!windows) throw ex;
2222        }
2223        
2224    }
2225    
2226    
2227    public void testLineEnds()
2228      throws ParsingException, IOException JavaDoc, XIncludeException {
2229      
2230        File JavaDoc input = new File JavaDoc(inputDir, "lineends.xml");
2231        Document doc = builder.build(input);
2232        Document result = XIncluder.resolve(doc);
2233        Document expected = builder.build(
2234          new File JavaDoc(outputDir, "lineends.xml")
2235        );
2236        assertEquals(expected, result);
2237                
2238    }
2239    
2240 
2241    // This is semantically bad; but still meets the
2242
// syntax of fragment IDs from RFC 2396
2243
public void testBadXPointerInFragmentIDIsFatalError()
2244      throws ParsingException, IOException JavaDoc, XIncludeException {
2245      
2246        File JavaDoc input = new File JavaDoc(
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    // These tests actually connect to IBiblio to load the included
2261
// data. This is necessary because file URLs don't support
2262
// content negotiation
2263
public void testAcceptLanguageFrench()
2264      throws ParsingException, IOException JavaDoc, XIncludeException {
2265      
2266        File JavaDoc input = new File JavaDoc(inputDir, "acceptfrench.xml");
2267        Document doc = builder.build(input);
2268        Document result = XIncluder.resolve(doc);
2269        Document expectedResult = builder.build(
2270          new File JavaDoc(outputDir, "acceptfrench.xml")
2271        );
2272        assertEquals(expectedResult, result);
2273        
2274    }
2275    
2276    
2277    public void testAcceptLanguageEnglish()
2278      throws ParsingException, IOException JavaDoc, XIncludeException {
2279      
2280        File JavaDoc input = new File JavaDoc(inputDir, "acceptenglish.xml");
2281        Document doc = builder.build(input);
2282        Document result = XIncluder.resolve(doc);
2283        Document expectedResult = builder.build(
2284          new File JavaDoc(outputDir, "acceptenglish.xml")
2285        );
2286        assertEquals(expectedResult, result);
2287        
2288    }
2289    
2290    
2291    public void testAcceptPlainText()
2292      throws ParsingException, IOException JavaDoc, XIncludeException {
2293      
2294        File JavaDoc input = new File JavaDoc(inputDir, "acceptplaintext.xml");
2295        Document doc = builder.build(input);
2296        Document result = XIncluder.resolve(doc);
2297        Document expectedResult = builder.build(
2298          new File JavaDoc(outputDir, "acceptplaintext.xml")
2299        );
2300        assertEquals(expectedResult, result);
2301        
2302    }
2303    
2304    
2305    public void testAcceptHTML()
2306      throws ParsingException, IOException JavaDoc, XIncludeException {
2307      
2308        File JavaDoc input = new File JavaDoc(inputDir, "accepthtml.xml");
2309        Document doc = builder.build(input);
2310        Document result = XIncluder.resolve(doc);
2311        Document expectedResult = builder.build(
2312          new File JavaDoc(outputDir, "accepthtml.xml")
2313        );
2314        assertEquals(expectedResult, result);
2315        
2316    }
2317    
2318    
2319    public void testBadHTTPHeaderExceptionConstructor() {
2320     
2321        String JavaDoc 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 JavaDoc message = "test";
2333        Exception JavaDoc ex = new BadHrefAttributeException(message);
2334        assertEquals(message, ex.getMessage());
2335        
2336    }
2337 
2338    
2339    public void testPercentEscapesAreNotAllowedInXPointerAttributes()
2340      throws ParsingException, IOException JavaDoc, XIncludeException {
2341      
2342        File JavaDoc input = new File JavaDoc(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 JavaDoc cause = (Exception JavaDoc) success.getCause();
2351            assertNotNull(cause);
2352        }
2353                
2354    }
2355        
2356    
2357    // WARNING: this test is one interpretation of the XInclude
2358
// proposed recommendation. It asserts that encoding attributes
2359
// that do not contain legal encoding names are fatal errors.
2360
// This is far from certain. It is also possible the working group
2361
// will choose to interpret these as resource errors.
2362
public void testMalformedEncodingAttribute()
2363      throws IOException JavaDoc, ParsingException, XIncludeException {
2364      
2365        File JavaDoc input = new File JavaDoc(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    // Test that a malformed parse attribute is not thrown when the
2380
// fallback element containing it is not activated.
2381
public void testHiddenError()
2382      throws ParsingException, IOException JavaDoc, XIncludeException {
2383      
2384        File JavaDoc input = new File JavaDoc(inputDir, "hiddenerror.xml");
2385        Document doc = builder.build(input);
2386        XIncluder.resolve(doc);
2387                
2388    }
2389        
2390
2391    // Test that an href attribute that has a fragment identifier
2392
// is not a fatal error when the fallback element containing
2393
// it is not activated.
2394
public void testHiddenError2()
2395      throws ParsingException, IOException JavaDoc, XIncludeException {
2396      
2397        File JavaDoc input = new File JavaDoc(inputDir, "hiddenerror2.xml");
2398        Document doc = builder.build(input);
2399        XIncluder.resolve(doc);
2400                
2401    }
2402
2403    
2404    // Test that a fallback element with a non-include parent is not a
2405
// fatal error when the ancestor fallback element containing it is
2406
// not activated.
2407
public void testHiddenError3()
2408      throws ParsingException, IOException JavaDoc, XIncludeException {
2409      
2410        File JavaDoc input = new File JavaDoc(inputDir, "hiddenerror3.xml");
2411        Document doc = builder.build(input);
2412        XIncluder.resolve(doc);
2413                
2414    }
2415
2416   
2417    // Test that an xpointer attribute that uses percent escapes
2418
// is a not a fatal error when the
2419
// fallback element containing it is not activated. See
2420
// http://lists.w3.org/Archives/Public/www-xml-xinclude-comments/2004Oct/0008.html
2421
public void testXpointerAttributeContainsPercentEscapeInUnactivatedFallback()
2422      throws ParsingException, IOException JavaDoc, XIncludeException {
2423      
2424        File JavaDoc input = new File JavaDoc(inputDir, "hiddenerror3.xml");
2425        Document doc = builder.build(input);
2426        XIncluder.resolve(doc);
2427                
2428    }
2429    
2430    
2431}
Popular Tags