Source Code Cross Referenced for XIncludeTest.java in  » XML » xom » nu » xom » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xom » nu.xom.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* Copyright 2002-2005 Elliotte Rusty Harold
0002:           
0003:           This library is free software; you can redistribute it and/or modify
0004:           it under the terms of version 2.1 of the GNU Lesser General Public 
0005:           License as published by the Free Software Foundation.
0006:           
0007:           This library is distributed in the hope that it will be useful,
0008:           but WITHOUT ANY WARRANTY; without even the implied warranty of
0009:           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
0010:           GNU Lesser General Public License for more details.
0011:           
0012:           You should have received a copy of the GNU Lesser General Public
0013:           License along with this library; if not, write to the 
0014:           Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
0015:           Boston, MA 02111-1307  USA
0016:           
0017:           You can contact Elliotte Rusty Harold by sending e-mail to
0018:           elharo@metalab.unc.edu. Please include the word "XOM" in the
0019:           subject line. The XOM home page is located at http://www.xom.nu/
0020:         */
0021:
0022:        package nu.xom.tests;
0023:
0024:        import java.io.ByteArrayOutputStream;
0025:        import java.io.File;
0026:        import java.io.FileNotFoundException;
0027:        import java.io.FileOutputStream;
0028:        import java.io.IOException;
0029:        import java.io.OutputStreamWriter;
0030:        import java.io.PrintStream;
0031:        import java.io.Reader;
0032:        import java.io.StringReader;
0033:        import java.io.Writer;
0034:        import java.net.URL;
0035:
0036:        import junit.framework.AssertionFailedError;
0037:
0038:        import nu.xom.Attribute;
0039:        import nu.xom.Builder;
0040:        import nu.xom.Comment;
0041:        import nu.xom.DocType;
0042:        import nu.xom.Document;
0043:        import nu.xom.Element;
0044:        import nu.xom.Elements;
0045:        import nu.xom.Namespace;
0046:        import nu.xom.Node;
0047:        import nu.xom.NodeFactory;
0048:        import nu.xom.Nodes;
0049:        import nu.xom.ParsingException;
0050:        import nu.xom.Serializer;
0051:        import nu.xom.Text;
0052:
0053:        import nu.xom.xinclude.BadEncodingAttributeException;
0054:        import nu.xom.xinclude.BadHTTPHeaderException;
0055:        import nu.xom.xinclude.BadHrefAttributeException;
0056:        import nu.xom.xinclude.BadParseAttributeException;
0057:        import nu.xom.xinclude.InclusionLoopException;
0058:        import nu.xom.xinclude.NoIncludeLocationException;
0059:        import nu.xom.xinclude.XIncludeException;
0060:        import nu.xom.xinclude.XIncluder;
0061:
0062:        /**
0063:         * <p>
0064:         * Unit tests for the XInclude and XPointer engines.
0065:         * </p>
0066:         * 
0067:         * @author Elliotte Rusty Harold
0068:         * @version 1.1b3
0069:         *
0070:         */
0071:        public class XIncludeTest extends XOMTestCase {
0072:
0073:            private static boolean windows = System.getProperty("os.name",
0074:                    "Unix").indexOf("Windows") >= 0;
0075:
0076:            public XIncludeTest(String name) {
0077:                super (name);
0078:            }
0079:
0080:            private Builder builder = new Builder();
0081:            private File inputDir;
0082:            private File outputDir;
0083:
0084:            // This class tests error conditions, which Xerces
0085:            // annoyingly logs to System.err. This hides System.err 
0086:            // before each test and restores it after each test.
0087:            private PrintStream systemErr = System.err;
0088:
0089:            protected void setUp() {
0090:
0091:                System.setErr(new PrintStream(new ByteArrayOutputStream()));
0092:
0093:                inputDir = new File("data");
0094:                inputDir = new File(inputDir, "xinclude");
0095:                inputDir = new File(inputDir, "input");
0096:
0097:                outputDir = new File("data");
0098:                outputDir = new File(outputDir, "xinclude");
0099:                outputDir = new File(outputDir, "output");
0100:
0101:            }
0102:
0103:            protected void tearDown() {
0104:                System.setErr(systemErr);
0105:            }
0106:
0107:            private void dumpResult(File original, Document result)
0108:                    throws IOException {
0109:
0110:                String name = original.getName();
0111:                File debug = new File("data");
0112:                debug = new File(debug, "xinclude");
0113:                debug = new File(debug, "debug");
0114:                File output = new File(debug, name);
0115:                FileOutputStream out = new FileOutputStream(output);
0116:                Serializer serializer = new Serializer(out);
0117:                serializer.write(result);
0118:
0119:            }
0120:
0121:            public void testXPointersResolvedAgainstAcquiredInfoset()
0122:                    throws ParsingException, IOException, XIncludeException {
0123:
0124:                File input = new File(inputDir, "tobintop.xml");
0125:                Document doc = builder.build(input);
0126:                Document result = XIncluder.resolve(doc);
0127:                Document expected = builder.build(new File(outputDir,
0128:                        "tobintop.xml"));
0129:                assertEquals(expected, result);
0130:
0131:            }
0132:
0133:            public void testXMLBaseUsedToResolveHref() throws ParsingException,
0134:                    IOException, XIncludeException {
0135:
0136:                File input = new File(inputDir, "xmlbasetest.xml");
0137:                Document doc = builder.build(input);
0138:                Document result = XIncluder.resolve(doc);
0139:                Document expected = builder.build(new File(outputDir,
0140:                        "xmlbasetest.xml"));
0141:                assertEquals(expected, result);
0142:
0143:            }
0144:
0145:            // Tests that use XPointer to
0146:            // grab a part of the document that contains an include element
0147:            // and make sure that's fully resolved too
0148:            public void testResolveThroughXPointer() throws ParsingException,
0149:                    IOException, XIncludeException {
0150:
0151:                File input = new File(inputDir, "resolvethruxpointer.xml");
0152:                Document doc = builder.build(input);
0153:                Document result = XIncluder.resolve(doc);
0154:                Document expectedResult = builder.build(new File(outputDir,
0155:                        "resolvethruxpointer.xml"));
0156:                assertEquals(expectedResult, result);
0157:
0158:            }
0159:
0160:            public void testXMLBaseOnIncludeElementUsedToResolveHref()
0161:                    throws ParsingException, IOException, XIncludeException {
0162:
0163:                File input = new File(inputDir, "xmlbasetest2.xml");
0164:                Document doc = builder.build(input);
0165:                Document result = XIncluder.resolve(doc);
0166:                Document expected = builder.build(new File(outputDir,
0167:                        "xmlbasetest2.xml"));
0168:                assertEquals(expected, result);
0169:
0170:            }
0171:
0172:            public void testXMLBaseRetainedFromUnincludedElement()
0173:                    throws ParsingException, IOException, XIncludeException {
0174:
0175:                File input = new File(inputDir, "xmlbasetest3.xml");
0176:                Document doc = builder.build(input);
0177:                Document result = XIncluder.resolve(doc);
0178:                Document expected = builder.build(new File(outputDir,
0179:                        "xmlbasetest3.xml"));
0180:                assertEquals(expected, result);
0181:
0182:            }
0183:
0184:            public void testMarsh() throws ParsingException, IOException,
0185:                    XIncludeException {
0186:
0187:                File input = new File(inputDir, "marshtest.xml");
0188:                Document doc = builder.build(input);
0189:                Document result = XIncluder.resolve(doc);
0190:                Document expectedResult = builder.build(new File(outputDir,
0191:                        "marshtest.xml"));
0192:                assertEquals(expectedResult, result);
0193:
0194:            }
0195:
0196:            public void testIncludeDocumentThatUsesIntradocumentReferences()
0197:                    throws ParsingException, IOException, XIncludeException {
0198:
0199:                File input = new File(inputDir,
0200:                        "includedocumentwithintradocumentreferences.xml");
0201:                Document doc = builder.build(input);
0202:                Document result = XIncluder.resolve(doc);
0203:                Document expectedResult = builder.build(new File(outputDir,
0204:                        "includedocumentwithintradocumentreferences.xml"));
0205:                assertEquals(expectedResult, result);
0206:
0207:            }
0208:
0209:            public void testXMLLangAttributes() throws ParsingException,
0210:                    IOException, XIncludeException {
0211:
0212:                File input = new File(inputDir, "langtest1.xml");
0213:                Document doc = builder.build(input);
0214:                Document result = XIncluder.resolve(doc);
0215:                Document expectedResult = builder.build(new File(outputDir,
0216:                        "langtest1.xml"));
0217:                assertEquals(expectedResult, result);
0218:
0219:            }
0220:
0221:            public void testInheritedXMLLangAttributes()
0222:                    throws ParsingException, IOException, XIncludeException {
0223:
0224:                File input = new File(inputDir, "langtest2.xml");
0225:                Document doc = builder.build(input);
0226:                Document result = XIncluder.resolve(doc);
0227:                Document expectedResult = builder.build(new File(outputDir,
0228:                        "langtest2.xml"));
0229:                assertEquals(expectedResult, result);
0230:
0231:            }
0232:
0233:            public void testNoLanguageSpecified() throws ParsingException,
0234:                    IOException, XIncludeException {
0235:
0236:                File input = new File(inputDir, "langtest3.xml");
0237:                Document doc = builder.build(input);
0238:                Document result = XIncluder.resolve(doc);
0239:                Document expectedResult = builder.build(new File(outputDir,
0240:                        "langtest3.xml"));
0241:                assertEquals(expectedResult, result);
0242:
0243:            }
0244:
0245:            // According to RFC 2396 empty string URI always refers to the 
0246:            // current document irrespective of base URI
0247:            public void testXMLBaseNotUsedToResolveMissingHref()
0248:                    throws ParsingException, IOException, XIncludeException {
0249:
0250:                File input = new File(inputDir, "marshtestwithxmlbase.xml");
0251:                Document doc = builder.build(input);
0252:                Document result = XIncluder.resolve(doc);
0253:                Document expectedResult = builder.build(new File(outputDir,
0254:                        "marshtestwithxmlbase.xml"));
0255:                assertEquals(expectedResult, result);
0256:
0257:            }
0258:
0259:            public void testEmptyHrefTreatedSameAsMissingHref()
0260:                    throws ParsingException, IOException, XIncludeException {
0261:
0262:                File input = new File(inputDir,
0263:                        "marshtestwithxmlbaseandemptyhref.xml");
0264:                Document doc = builder.build(input);
0265:                Document result = XIncluder.resolve(doc);
0266:                Document expectedResult = builder.build(new File(outputDir,
0267:                        "marshtestwithxmlbase.xml"));
0268:                assertEquals(expectedResult, result);
0269:
0270:            }
0271:
0272:            public void testBaselessDocument() throws IOException,
0273:                    ParsingException, XIncludeException {
0274:
0275:                Element root = new Element("root");
0276:                Element child1 = new Element("xi:include",
0277:                        XIncluder.XINCLUDE_NS);
0278:                child1.addAttribute(new Attribute("xpointer", "p1"));
0279:                Element child2 = new Element("child2");
0280:                root.appendChild(child1);
0281:                root.appendChild(child2);
0282:                child2
0283:                        .addAttribute(new Attribute("id", "p1",
0284:                                Attribute.Type.ID));
0285:                Document in = new Document(root);
0286:                Document out = XIncluder.resolve(in);
0287:                String result = out.toXML();
0288:                assertEquals(
0289:                        "<?xml version=\"1.0\"?>\n"
0290:                                + "<root><child2 id=\"p1\" /><child2 id=\"p1\" /></root>\n",
0291:                        result);
0292:
0293:            }
0294:
0295:            public void testRelativeURLInBaselessDocument() throws IOException,
0296:                    ParsingException, XIncludeException {
0297:
0298:                Element root = new Element("root");
0299:                Element child1 = new Element("xi:include",
0300:                        XIncluder.XINCLUDE_NS);
0301:                child1.addAttribute(new Attribute("href", "test.xml"));
0302:                Element child2 = new Element("child2");
0303:                root.appendChild(child1);
0304:                root.appendChild(child2);
0305:                Document in = new Document(root);
0306:                try {
0307:                    XIncluder.resolve(in);
0308:                    fail("Resolved relative URI in baseless document");
0309:                } catch (BadHrefAttributeException success) {
0310:                    assertEquals(
0311:                            "Could not resolve relative URI test.xml because the "
0312:                                    + "xi:include element does not have a base URI.",
0313:                            success.getMessage());
0314:                }
0315:
0316:            }
0317:
0318:            public void testIncludeTextWithCustomNodeFactory()
0319:                    throws ParsingException, IOException, XIncludeException {
0320:
0321:                File input = new File(inputDir, "c2.xml");
0322:                Builder builder = new Builder(new TextNodeFactory());
0323:                Document doc = builder.build(input);
0324:                Document result = XIncluder.resolve(doc, builder);
0325:                Document expectedResult = builder.build(new File(outputDir,
0326:                        "c2.xml"));
0327:                assertEquals(expectedResult, result);
0328:                Element root = result.getRootElement();
0329:                for (int i = 0; i < root.getChildCount(); i++) {
0330:                    Node node = root.getChild(i);
0331:                    if (node instanceof  Text) {
0332:                        assertTrue(node instanceof  TextSubclass);
0333:                    }
0334:                }
0335:
0336:            }
0337:
0338:            public void testParseEqualsTextWithNodeFactoryThatRemovesAllTextNodes()
0339:                    throws ParsingException, IOException, XIncludeException {
0340:
0341:                File input = new File(inputDir, "c2.xml");
0342:                Document doc = builder.build(input);
0343:                Document result = XIncluder.resolve(doc, new Builder(
0344:                        new TextFilter()));
0345:                Document expectedResult = builder.build(new File(outputDir,
0346:                        "c2a.xml"));
0347:                assertEquals(expectedResult, result);
0348:
0349:            }
0350:
0351:            private static class TextFilter extends NodeFactory {
0352:
0353:                public Nodes makeText(String data) {
0354:                    return new Nodes();
0355:                }
0356:
0357:            }
0358:
0359:            public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithComments()
0360:                    throws ParsingException, IOException, XIncludeException {
0361:
0362:                File input = new File(inputDir, "c2.xml");
0363:                Document doc = builder.build(input);
0364:                Document result = XIncluder.resolve(doc, new Builder(
0365:                        new TextToComment()));
0366:                Document expectedResult = builder.build(new File(outputDir,
0367:                        "c2b.xml"));
0368:                assertEquals(expectedResult, result);
0369:
0370:            }
0371:
0372:            private static class TextToComment extends NodeFactory {
0373:
0374:                public Nodes makeText(String data) {
0375:                    return new Nodes(new Comment(data));
0376:                }
0377:
0378:            }
0379:
0380:            public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithAttributes()
0381:                    throws ParsingException, IOException, XIncludeException {
0382:
0383:                File input = new File(inputDir, "c2.xml");
0384:                Document doc = builder.build(input);
0385:                Document result = XIncluder.resolve(doc, new Builder(
0386:                        new TextToAttribute()));
0387:                Document expectedResult = builder.build(new File(outputDir,
0388:                        "c2c.xml"));
0389:                assertEquals(expectedResult, result);
0390:
0391:            }
0392:
0393:            public void testParseEqualsTextWithNodeFactoryThatReplacesTextNodesWithTwoElements()
0394:                    throws ParsingException, IOException, XIncludeException {
0395:
0396:                File input = new File(inputDir, "c2.xml");
0397:                Document doc = builder.build(input);
0398:                Document result = XIncluder.resolve(doc, new Builder(
0399:                        new TextToElements()));
0400:                Document expectedResult = builder.build(new File(outputDir,
0401:                        "c2d.xml"));
0402:                assertEquals(expectedResult, result);
0403:
0404:            }
0405:
0406:            private static class TextToElements extends NodeFactory {
0407:
0408:                public Nodes makeText(String data) {
0409:                    Nodes result = new Nodes();
0410:                    result.append(new Element("empty1"));
0411:                    result.append(new Element("empty2"));
0412:                    return result;
0413:                }
0414:
0415:            }
0416:
0417:            private static class TextToAttribute extends NodeFactory {
0418:
0419:                public Nodes makeText(String data) {
0420:                    return new Nodes(new Attribute("name", data));
0421:                }
0422:
0423:            }
0424:
0425:            public void testUnrecognizedXPointerScheme()
0426:                    throws ParsingException, IOException {
0427:
0428:                File input = new File(inputDir, "unrecognizedscheme.xml");
0429:                Document doc = builder.build(input);
0430:                try {
0431:                    XIncluder.resolve(doc);
0432:                    fail("Allowed unrecognized scheme");
0433:                } catch (XIncludeException success) {
0434:                    assertNotNull(success.getMessage());
0435:                }
0436:
0437:            }
0438:
0439:            public void testUnrecognizedXPointerSchemeWithFallback()
0440:                    throws IOException, ParsingException, XIncludeException {
0441:
0442:                File input = new File(inputDir,
0443:                        "unrecognizedschemewithfallback.xml");
0444:                File output = new File(outputDir,
0445:                        "unrecognizedschemewithfallback.xml");
0446:                Document doc = builder.build(input);
0447:                Document actual = XIncluder.resolve(doc);
0448:                Document expected = builder.build(output);
0449:                assertEquals(expected, actual);
0450:
0451:            }
0452:
0453:            public void testIncludeTextWithCustomNodeFactoryThatChangesElementNames()
0454:                    throws ParsingException, IOException, XIncludeException {
0455:
0456:                File input = new File(inputDir, "c1.xml");
0457:                Document doc = builder.build(input);
0458:                Document result = XIncluder.resolve(doc, new Builder(
0459:                        new NodeFactoryTest.CFactory()));
0460:                Document expectedResult = builder.build(new File(outputDir,
0461:                        "c1a.xml"));
0462:                assertEquals(expectedResult, result);
0463:
0464:            }
0465:
0466:            public void testIncludeTextWithCustomNodeFactoryThatOnlyReturnsRoot()
0467:                    throws ParsingException, IOException, XIncludeException {
0468:
0469:                File input = new File(inputDir, "c1.xml");
0470:                Document doc = builder.build(input);
0471:                Document result = XIncluder.resolve(doc, new Builder(
0472:                        new NodeFactoryTest.MinimizingFactory()));
0473:                Document expectedResult = builder.build(new File(outputDir,
0474:                        "c1b.xml"));
0475:                assertEquals(expectedResult, result);
0476:
0477:            }
0478:
0479:            public void testIncludeTextWithCustomNodeFactoryThatFiltersElementsNamedB()
0480:                    throws ParsingException, IOException, XIncludeException {
0481:
0482:                File input = new File(inputDir, "d1.xml");
0483:                Document doc = builder.build(input);
0484:                Document result = XIncluder.resolve(doc, new Builder(
0485:                        new NodeFactoryTest.BFilter()));
0486:                Document expectedResult = builder.build(new File(outputDir,
0487:                        "d1.xml"));
0488:                assertEquals(expectedResult, result);
0489:
0490:            }
0491:
0492:            public void testIncludeTextWithCustomNodeFactoryThatReturnsEachNonRootElementThreeTimes()
0493:                    throws ParsingException, IOException, XIncludeException {
0494:
0495:                File input = new File(inputDir, "c1.xml");
0496:                Document doc = builder.build(input);
0497:                Document result = XIncluder.resolve(doc, new Builder(
0498:                        new NodeFactoryTest.TripleElementFilter()));
0499:                Document expectedResult = builder.build(new File(outputDir,
0500:                        "triple.xml"));
0501:                assertEquals(expectedResult, result);
0502:
0503:            }
0504:
0505:            public void test1() throws ParsingException, IOException,
0506:                    XIncludeException {
0507:
0508:                File input = new File(inputDir, "test.xml");
0509:                Document doc = builder.build(input);
0510:                Document result = XIncluder.resolve(doc);
0511:                Document expectedResult = builder.build(new File(outputDir,
0512:                        "test.xml"));
0513:                assertEquals(expectedResult, result);
0514:
0515:            }
0516:
0517:            public void testBaseURIsPreservedInSameDocumentInclusion()
0518:                    throws ParsingException, IOException, XIncludeException {
0519:
0520:                File input = new File(inputDir,
0521:                        "includefromsamedocumentwithbase.xml");
0522:                Document doc = builder.build(input);
0523:                Document result = XIncluder.resolve(doc);
0524:                Document expectedResult = builder.build(new File(outputDir,
0525:                        "includefromsamedocumentwithbase.xml"));
0526:                assertEquals(expectedResult, result);
0527:
0528:            }
0529:
0530:            public void testBaseURIsPreservedInResultDocument()
0531:                    throws ParsingException, IOException, XIncludeException {
0532:
0533:                Document doc = new Document(new Element("root"));
0534:                doc.setBaseURI("http://www.example.org/");
0535:                Document result = XIncluder.resolve(doc);
0536:                assertEquals("http://www.example.org/", result.getBaseURI());
0537:
0538:            }
0539:
0540:            /* public void testResolveNodes() 
0541:              throws IOException, ParsingException, XIncludeException {
0542:                File dir = new File(inputDir, "");
0543:                Element include = new Element("xi:include", XIncluder.XINCLUDE_NS);
0544:                include.setBaseURI(dir.toURL().toExternalForm());
0545:                include.addAttribute(new Attribute("href", "disclaimer.xml"));
0546:                Nodes in = new Nodes(include);  
0547:                Nodes out = XIncluder.resolve(in);
0548:                assertEquals(1, out.size());
0549:                Element result = (Element) out.get(0);
0550:                assertEquals("disclaimer",result.getQualifiedName());
0551:            } */
0552:
0553:            public void testNullBaseURI() throws ParsingException, IOException,
0554:                    XIncludeException {
0555:
0556:                File input = new File(inputDir, "disclaimer.xml");
0557:                String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
0558:                        + "\n  <p>120 Mz is adequate for an average home user.</p>"
0559:                        + "\n  <xi:include href='"
0560:                        + input.toURL()
0561:                        + "'/>\n</document>";
0562:                Reader reader = new StringReader(data);
0563:                Document doc = builder.build(reader);
0564:                Document result = XIncluder.resolve(doc);
0565:                Document expectedResult = builder.build(new File(outputDir,
0566:                        "c1.xml"));
0567:                assertEquals(expectedResult, result);
0568:
0569:            }
0570:
0571:            public void testBadIRIIsAFatalError() throws IOException,
0572:                    ParsingException, XIncludeException {
0573:
0574:                String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
0575:                        + "<xi:include href='http://www.example.com/a%5.html'>"
0576:                        + "<xi:fallback>Ooops!</xi:fallback></xi:include></document>";
0577:                Reader reader = new StringReader(data);
0578:                Document doc = builder.build(reader);
0579:                try {
0580:                    XIncluder.resolve(doc);
0581:                    fail("Resolved fallback when encountering a syntactically incorrect URI");
0582:                } catch (BadHrefAttributeException success) {
0583:                    assertNotNull(success.getMessage());
0584:                }
0585:
0586:            }
0587:
0588:            public void testBadIRIWithUnrecognizedSchemeIsAFatalError()
0589:                    throws IOException, ParsingException, XIncludeException {
0590:
0591:                String data = "<doc xmlns:xi='http://www.w3.org/2001/XInclude'>"
0592:                        + "<xi:include href='scheme://www.example.com/a%5.html'>"
0593:                        + "<xi:fallback>Ooops!</xi:fallback></xi:include></doc>";
0594:                Reader reader = new StringReader(data);
0595:                Document doc = builder.build(reader);
0596:                try {
0597:                    XIncluder.resolve(doc);
0598:                    fail("Resolved fallback when encountering a syntactically incorrect URI");
0599:                } catch (BadHrefAttributeException success) {
0600:                    assertNotNull(success.getMessage());
0601:                }
0602:
0603:            }
0604:
0605:            public void testGoodIRIWithUnrecognizedSchemeIsAResourceError()
0606:                    throws IOException, ParsingException, XIncludeException {
0607:
0608:                String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
0609:                        + "<xi:include href='scheme://www.example.com/a.html'>"
0610:                        + "<xi:fallback>Correct!</xi:fallback></xi:include></document>";
0611:                Reader reader = new StringReader(data);
0612:                Document doc = builder.build(reader);
0613:                Document result = XIncluder.resolve(doc);
0614:                assertEquals(
0615:                        "<?xml version=\"1.0\"?>\n"
0616:                                + "<document xmlns:xi=\"http://www.w3.org/2001/XInclude\">Correct!</document>\n",
0617:                        result.toXML());
0618:
0619:            }
0620:
0621:            public void testBadAcceptAttribute() throws ParsingException,
0622:                    IOException, XIncludeException {
0623:
0624:                String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
0625:                        + "\n  <p>120 MHz is adequate for an average home user.</p>"
0626:                        + "\n  <xi:include href='http://www.example.com' "
0627:                        + "accept='text/html&#x0D;&#x0A;Something: bad'/>\n</document>";
0628:                Reader reader = new StringReader(data);
0629:                Document doc = builder.build(reader);
0630:                try {
0631:                    XIncluder.resolve(doc);
0632:                    fail("Allowed accept header containing carriage return linefeed");
0633:                } catch (BadHTTPHeaderException success) {
0634:                    assertNotNull(success.getMessage());
0635:                }
0636:
0637:            }
0638:
0639:            public void testBadAcceptAttributeWithLatin1Character()
0640:                    throws ParsingException, IOException, XIncludeException {
0641:
0642:                String data = "<document xmlns:xi='http://www.w3.org/2001/XInclude'>"
0643:                        + "\n  <p>120 MHz is adequate for an average home user.</p>"
0644:                        + "\n  <xi:include href='http://www.example.com' "
0645:                        + "accept='text/html&#xA0;Something: bad'/>\n</document>";
0646:                Reader reader = new StringReader(data);
0647:                Document doc = builder.build(reader);
0648:                try {
0649:                    XIncluder.resolve(doc);
0650:                    fail("Allowed accept header containing non-ASCII character");
0651:                } catch (BadHTTPHeaderException success) {
0652:                    assertNotNull(success.getMessage());
0653:                }
0654:
0655:            }
0656:
0657:            public void testUnrecognizedAttributesAreIgnored()
0658:                    throws ParsingException, IOException, XIncludeException {
0659:
0660:                File input = new File(inputDir, "extraattributes.xml");
0661:                Document doc = builder.build(input);
0662:                Document result = XIncluder.resolve(doc);
0663:                Document expectedResult = builder.build(new File(outputDir,
0664:                        "c1.xml"));
0665:                assertEquals(expectedResult, result);
0666:
0667:            }
0668:
0669:            public void testEmptyFallback() throws ParsingException,
0670:                    IOException, XIncludeException {
0671:
0672:                File input = new File(inputDir, "emptyfallback.xml");
0673:                Document doc = builder.build(input);
0674:                Document result = XIncluder.resolve(doc);
0675:                Document expectedResult = builder.build(new File(outputDir,
0676:                        "emptyfallback.xml"));
0677:                assertEquals(expectedResult, result);
0678:
0679:            }
0680:
0681:            public void testFallbackInIncludedDocument()
0682:                    throws ParsingException, IOException, XIncludeException {
0683:
0684:                File input = new File(inputDir, "metafallbacktest.xml");
0685:                Document doc = builder.build(input);
0686:                Document result = XIncluder.resolve(doc);
0687:                Document expectedResult = builder.build(new File(outputDir,
0688:                        "metafallbacktest.xml"));
0689:                assertEquals(expectedResult, result);
0690:
0691:            }
0692:
0693:            public void testFallbackInIncludedDocumentUsesAnIntradocumentXPointer()
0694:                    throws ParsingException, IOException, XIncludeException {
0695:
0696:                File input = new File(inputDir, "metafallbacktest6.xml");
0697:                Document doc = builder.build(input);
0698:                Document result = XIncluder.resolve(doc);
0699:                Document expectedResult = builder.build(new File(outputDir,
0700:                        "metafallbacktest6.xml"));
0701:                assertEquals(expectedResult, result);
0702:
0703:            }
0704:
0705:            // changed for b5
0706:            public void testFallbackInIncludedDocumentIncludesADocumentWithParseEqualsText()
0707:                    throws ParsingException, IOException, XIncludeException {
0708:
0709:                File input = new File(inputDir, "metafallbacktest2.xml");
0710:                Document doc = builder.build(input);
0711:                Document result = XIncluder.resolve(doc);
0712:                Document expectedResult = builder.build(new File(outputDir,
0713:                        "metafallbacktest2.xml"));
0714:                assertEquals(expectedResult, result);
0715:
0716:            }
0717:
0718:            public void testFallbackInIncludedDocumentWithBadParseAttribute()
0719:                    throws ParsingException, IOException, XIncludeException {
0720:
0721:                File input = new File(inputDir, "metafallbacktest3.xml");
0722:                Document doc = builder.build(input);
0723:                try {
0724:                    XIncluder.resolve(doc);
0725:                    fail("Allowed bad parse attribute");
0726:                } catch (BadParseAttributeException success) {
0727:                    assertNotNull(success.getMessage());
0728:                }
0729:
0730:            }
0731:
0732:            public void testFallbackInIncludedDocumentWithMissingHrefAndParseAttributes()
0733:                    throws ParsingException, IOException, XIncludeException {
0734:
0735:                File input = new File(inputDir, "metafallbacktest4.xml");
0736:                Document doc = builder.build(input);
0737:                try {
0738:                    XIncluder.resolve(doc);
0739:                    fail("Allowed bad parse attribute");
0740:                } catch (NoIncludeLocationException success) {
0741:                    assertNotNull(success.getMessage());
0742:                }
0743:
0744:            }
0745:
0746:            public void testFallbackInIncludedDocumentWithFragmentID()
0747:                    throws ParsingException, IOException, XIncludeException {
0748:
0749:                File input = new File(inputDir,
0750:                        "metafallbacktestwithfragmentid.xml");
0751:                Document doc = builder.build(input);
0752:                try {
0753:                    XIncluder.resolve(doc);
0754:                    fail("Allowed document with fragment ID in href attribute");
0755:                } catch (BadHrefAttributeException success) {
0756:                    assertNotNull(success.getMessage());
0757:                }
0758:
0759:            }
0760:
0761:            // changed in b5
0762:            public void testXPointerIsNotResolvedAgainstTheSourceInfoset()
0763:                    throws ParsingException, IOException, XIncludeException {
0764:
0765:                File input = new File(inputDir, "metafallbacktest5.xml");
0766:                Document doc = builder.build(input);
0767:                try {
0768:                    XIncluder.resolve(doc);
0769:                    fail("Allowed XPointer that doesn't resolve against the acquired infoset but does resolve against the source infoset");
0770:                } catch (XIncludeException ex) {
0771:                    assertNotNull(ex.getMessage());
0772:                }
0773:
0774:            }
0775:
0776:            public void testFallbackInIncludedDocumentThatResolvesToNonElement()
0777:                    throws ParsingException, IOException, XIncludeException {
0778:
0779:                File input = new File(inputDir, "metafallbacktotexttest.xml");
0780:                Document doc = builder.build(input);
0781:                Document result = XIncluder.resolve(doc);
0782:                Document expectedResult = builder.build(new File(outputDir,
0783:                        "metafallbacktotexttest.xml"));
0784:                assertEquals(expectedResult, result);
0785:
0786:            }
0787:
0788:            public void testFallbackInIncludedDocumentWithXPointer()
0789:                    throws ParsingException, IOException, XIncludeException {
0790:                // This test case activates processFallbackSilently
0791:                File input = new File(inputDir,
0792:                        "metafallbacktestwithxpointer.xml");
0793:                Document doc = builder.build(input);
0794:                Document result = XIncluder.resolve(doc);
0795:                Document expectedResult = builder.build(new File(outputDir,
0796:                        "metafallbacktestwithxpointer.xml"));
0797:                assertEquals(expectedResult, result);
0798:
0799:            }
0800:
0801:            // changed in b5
0802:            // test case where fallback falls back to text and comments rather than
0803:            // an element
0804:            public void testFallbackInIncludedDocumentWithXPointer2()
0805:                    throws ParsingException, IOException, XIncludeException {
0806:
0807:                // This test case activates processFallbackSilently
0808:                File input = new File(inputDir,
0809:                        "metafallbacktestwithxpointer2.xml");
0810:                Document doc = builder.build(input);
0811:                Document result = XIncluder.resolve(doc);
0812:                Document expectedResult = builder.build(new File(outputDir,
0813:                        "metafallbacktestwithxpointer2.xml"));
0814:                assertEquals(expectedResult, result);
0815:
0816:            }
0817:
0818:            public void testNoFallbackInIncludedDocumentWithXPointer()
0819:                    throws ParsingException, IOException, XIncludeException {
0820:
0821:                // This test case activates processFallbackSilently
0822:                File input = new File(inputDir,
0823:                        "metamissingfallbacktestwithxpointer.xml");
0824:                Document doc = builder.build(input);
0825:                try {
0826:                    XIncluder.resolve(doc);
0827:                    fail("Should have thrown IOException");
0828:                } catch (IOException success) {
0829:                    assertNotNull(success.getMessage());
0830:                }
0831:
0832:            }
0833:
0834:            public void testFallbackInIncludedDocumentHasBadXPointer()
0835:                    throws ParsingException, IOException, XIncludeException {
0836:                // This test case activates processFallbackSilently
0837:                File input = new File(inputDir,
0838:                        "metafallbackwithbadxpointertest.xml");
0839:                Document doc = builder.build(input);
0840:                try {
0841:                    XIncluder.resolve(doc);
0842:                    fail("Should have thrown XIncludeException");
0843:                } catch (XIncludeException success) {
0844:                    assertNotNull(success.getMessage());
0845:                    assertNotNull(success.getCause());
0846:                }
0847:
0848:            }
0849:
0850:            // from the XInclude CR
0851:            public void testC1() throws ParsingException, IOException,
0852:                    XIncludeException {
0853:
0854:                File input = new File(inputDir, "c1.xml");
0855:                Document doc = builder.build(input);
0856:                Document result = XIncluder.resolve(doc);
0857:                Document expectedResult = builder.build(new File(outputDir,
0858:                        "c1.xml"));
0859:                assertEquals(expectedResult, result);
0860:
0861:            }
0862:
0863:            public void testRelativeURLBaseURIFixup() throws ParsingException,
0864:                    IOException, XIncludeException {
0865:
0866:                File input = new File(inputDir, "relative.xml");
0867:                Document doc = builder.build(input);
0868:                Document result = XIncluder.resolve(doc);
0869:                assertEquals(doc.getBaseURI(), result.getBaseURI());
0870:                Document expectedResult = builder.build(new File(outputDir,
0871:                        "relative.xml"));
0872:                assertEquals(expectedResult, result);
0873:                Element root = result.getRootElement();
0874:                Element red = root.getFirstChildElement("red");
0875:                String base = red.getAttributeValue("base",
0876:                        Namespace.XML_NAMESPACE);
0877:                assertEquals("basedata/red.xml", base);
0878:
0879:            }
0880:
0881:            // same test with explicit parse="xml"
0882:            public void testParseEqualsXML() throws ParsingException,
0883:                    IOException, XIncludeException {
0884:
0885:                File input = new File(inputDir, "parseequalxml.xml");
0886:                Document doc = builder.build(input);
0887:                Document result = XIncluder.resolve(doc);
0888:                Document expectedResult = builder.build(new File(outputDir,
0889:                        "c1.xml"));
0890:                assertEquals(expectedResult, result);
0891:
0892:            }
0893:
0894:            // changed in b5
0895:            public void testAcceptableCirclePointer() throws ParsingException,
0896:                    IOException, XIncludeException {
0897:
0898:                File input = new File(inputDir, "legalcircle.xml");
0899:                Document doc = builder.build(input);
0900:                try {
0901:                    XIncluder.resolve(doc);
0902:                    fail("Allowed circular reference");
0903:                } catch (InclusionLoopException success) {
0904:                    assertNotNull(success.getMessage());
0905:                }
0906:
0907:            }
0908:
0909:            // from the XInclude CR
0910:            public void testC2() throws ParsingException, IOException,
0911:                    XIncludeException {
0912:
0913:                File input = new File(inputDir, "c2.xml");
0914:                Document doc = builder.build(input);
0915:                Document result = XIncluder.resolve(doc);
0916:                Document expectedResult = builder.build(new File(outputDir,
0917:                        "c2.xml"));
0918:                assertEquals(expectedResult, result);
0919:
0920:            }
0921:
0922:            // from the XInclude CR
0923:            public void testC3() throws ParsingException, IOException,
0924:                    XIncludeException {
0925:
0926:                File input = new File(inputDir, "c3.xml");
0927:                Document doc = builder.build(input);
0928:                Document result = XIncluder.resolve(doc);
0929:                Document expectedResult = builder.build(new File(outputDir,
0930:                        "c3.xml"));
0931:                assertEquals(expectedResult, result);
0932:
0933:            }
0934:
0935:            // C4 skipped for the moment because it uses XPointers
0936:            // that XOM doesn't yet support
0937:
0938:            // from the XInclude CR
0939:            // Don't use this one yet, because there appear to be 
0940:            // mistakes in the spec examples
0941:            /*public void testC5() throws ParsingException, IOException, XIncludeException {
0942:              
0943:                File input = new File(inputDir, "c5.xml");
0944:                Document doc = builder.build(input);
0945:                Document result = XIncluder.resolve(doc);
0946:                Document expectedResult = builder.build(new File(outputDir, "c5.xml"));
0947:                XMLAssert.assertEquals(expectedResult, result);
0948:                
0949:            } */
0950:
0951:            private static class TextNodeFactory extends NodeFactory {
0952:
0953:                public Nodes makeText(String data) {
0954:                    return new Nodes(new TextSubclass(data));
0955:                }
0956:
0957:            }
0958:
0959:            private static class TextSubclass extends Text {
0960:
0961:                TextSubclass(String data) {
0962:                    super (data);
0963:                }
0964:
0965:                public Node copy() {
0966:                    return new TextSubclass(this .getValue());
0967:                }
0968:
0969:            }
0970:
0971:            public void testRecurseWithinSameDocument()
0972:                    throws ParsingException, IOException, XIncludeException {
0973:
0974:                File input = new File(inputDir, "recursewithinsamedocument.xml");
0975:                Document doc = builder.build(input);
0976:                Document result = XIncluder.resolve(doc);
0977:                Document expectedResult = builder.build(new File(outputDir,
0978:                        "recursewithinsamedocument.xml"));
0979:                assertEquals(expectedResult, result);
0980:
0981:            }
0982:
0983:            public void testSiblingIncludes() throws ParsingException,
0984:                    IOException, XIncludeException {
0985:
0986:                File input = new File(inputDir, "paralleltest.xml");
0987:                Document doc = builder.build(input);
0988:                Document result = XIncluder.resolve(doc);
0989:                Document expectedResult = builder.build(new File(outputDir,
0990:                        "paralleltest.xml"));
0991:                assertEquals(expectedResult, result);
0992:
0993:            }
0994:
0995:            public void testNamespaces() throws ParsingException, IOException,
0996:                    XIncludeException {
0997:
0998:                File input = new File(inputDir, "namespacetest.xml");
0999:                Document doc = builder.build(input);
1000:                Document result = XIncluder.resolve(doc);
1001:                Document expectedResult = builder.build(new File(outputDir,
1002:                        "namespacetest.xml"));
1003:                assertEquals(expectedResult, result);
1004:
1005:            }
1006:
1007:            public void testIncludeReferencesItItself()
1008:                    throws ParsingException, IOException, XIncludeException {
1009:
1010:                File input = new File(inputDir, "internalcircular.xml");
1011:                Document doc = builder.build(input);
1012:                try {
1013:                    XIncluder.resolve(doc);
1014:                    fail("Allowed include element to reference itself");
1015:                } catch (InclusionLoopException success) {
1016:                    assertNotNull(success.getMessage());
1017:                }
1018:
1019:            }
1020:
1021:            public void testIncludeReferencesItsAncestor()
1022:                    throws ParsingException, IOException, XIncludeException {
1023:
1024:                File input = new File(inputDir,
1025:                        "internalcircularviaancestor.xml");
1026:                Document doc = builder.build(input);
1027:                try {
1028:                    XIncluder.resolve(doc);
1029:                    fail("Allowed include element to reference its own ancestor");
1030:                } catch (InclusionLoopException success) {
1031:                    assertNotNull(success.getMessage());
1032:                }
1033:
1034:            }
1035:
1036:            public void testNoInclusions() throws ParsingException,
1037:                    IOException, XIncludeException {
1038:
1039:                File input = new File(inputDir, "latin1.xml");
1040:                Document doc = builder.build(input);
1041:                Document result = XIncluder.resolve(doc);
1042:                Document expectedResult = builder.build(new File(outputDir,
1043:                        "latin1.xml"));
1044:                assertEquals(expectedResult, result);
1045:
1046:            }
1047:
1048:            public void test2() throws ParsingException, IOException,
1049:                    XIncludeException {
1050:
1051:                File input = new File(inputDir, "simple.xml");
1052:                Document doc = builder.build(input);
1053:                Document result = XIncluder.resolve(doc);
1054:                Document expectedResult = builder.build(new File(outputDir,
1055:                        "simple.xml"));
1056:                assertEquals(expectedResult, result);
1057:
1058:            }
1059:
1060:            public void testReplaceRoot() throws ParsingException, IOException,
1061:                    XIncludeException {
1062:
1063:                File input = new File(inputDir, "roottest.xml");
1064:                Document doc = builder.build(input);
1065:                Document result = XIncluder.resolve(doc);
1066:                Document expectedResult = builder.build(new File(outputDir,
1067:                        "roottest.xml"));
1068:                assertEquals(expectedResult, result);
1069:
1070:            }
1071:
1072:            // In this test the included document has a prolog and an epilog
1073:            public void testReplaceRoot2() throws ParsingException,
1074:                    IOException, XIncludeException {
1075:
1076:                File input = new File(inputDir, "roottest2.xml");
1077:                Document doc = builder.build(input);
1078:                Document result = XIncluder.resolve(doc);
1079:                Document expectedResult = builder.build(new File(outputDir,
1080:                        "roottest2.xml"));
1081:                assertEquals(expectedResult, result);
1082:
1083:            }
1084:
1085:            public void testIncludeElementsCannotHaveIncludeChildren()
1086:                    throws ParsingException, IOException, XIncludeException {
1087:                File input = new File(inputDir, "nestedxinclude.xml");
1088:                Document doc = builder.build(input);
1089:                try {
1090:                    XIncluder.resolve(doc);
1091:                    fail("allowed include element to contain another include element");
1092:                } catch (XIncludeException success) {
1093:                    assertNotNull(success.getMessage());
1094:                }
1095:            }
1096:
1097:            public void testIncludeElementsCannotHaveChildrenFromXIncludeNamespace()
1098:                    throws ParsingException, IOException, XIncludeException {
1099:                File input = new File(inputDir, "nestedxincludenamespace.xml");
1100:                Document doc = builder.build(input);
1101:                try {
1102:                    XIncluder.resolve(doc);
1103:                    fail("allowed include element to contain another include element");
1104:                } catch (XIncludeException success) {
1105:                    assertNotNull(success.getMessage());
1106:                }
1107:            }
1108:
1109:            public void testFallbackIsNotChildOfIncludeElement()
1110:                    throws ParsingException, IOException, XIncludeException {
1111:                File input = new File(inputDir, "nakedfallback.xml");
1112:                Document doc = builder.build(input);
1113:                try {
1114:                    XIncluder.resolve(doc);
1115:                    fail("allowed fallback that was not child of an include element");
1116:                } catch (XIncludeException success) {
1117:                    assertNotNull(success.getMessage());
1118:                }
1119:            }
1120:
1121:            public void testFallbackCantContainFallbackElement()
1122:                    throws ParsingException, IOException, XIncludeException {
1123:                File input = new File(inputDir, "fallbackcontainsfallback.xml");
1124:                Document doc = builder.build(input);
1125:                try {
1126:                    XIncluder.resolve(doc);
1127:                    fail("allowed fallback inside another fallback element");
1128:                } catch (XIncludeException success) {
1129:                    assertNotNull(success.getMessage());
1130:                }
1131:            }
1132:
1133:            // In this test the fallback is activated.
1134:            public void testMultipleFallbacks() throws ParsingException,
1135:                    IOException, XIncludeException {
1136:                File input = new File(inputDir, "multiplefallbacks.xml");
1137:                Document doc = builder.build(input);
1138:                try {
1139:                    XIncluder.resolve(doc);
1140:                    fail("allowed multiple fallback elements");
1141:                } catch (XIncludeException success) {
1142:                    assertNotNull(success.getMessage());
1143:                }
1144:            }
1145:
1146:            // In this test the fallback is not needed.
1147:            public void testMultipleFallbacks2() throws ParsingException,
1148:                    IOException, XIncludeException {
1149:                File input = new File(inputDir, "multiplefallbacks2.xml");
1150:                Document doc = builder.build(input);
1151:                try {
1152:                    XIncluder.resolve(doc);
1153:                    fail("allowed multiple fallback elements");
1154:                } catch (XIncludeException success) {
1155:                    assertNotNull(success.getMessage());
1156:                }
1157:            }
1158:
1159:            public void testDocumentIncludesItself() throws ParsingException,
1160:                    IOException, XIncludeException {
1161:                File input = new File(inputDir, "circle1.xml");
1162:                Document doc = builder.build(input);
1163:                try {
1164:                    XIncluder.resolve(doc);
1165:                    fail("allowed parsed include of self");
1166:                } catch (InclusionLoopException success) {
1167:                    assertNotNull(success.getMessage());
1168:                    assertEquals(input.toURL().toExternalForm(), success
1169:                            .getURI());
1170:                }
1171:            }
1172:
1173:            public void testInclusionLoopWithLength2Cycle()
1174:                    throws ParsingException, IOException, XIncludeException {
1175:
1176:                File input = new File(inputDir, "circle2a.xml");
1177:                File errorFile = new File(inputDir, "circle2b.xml");
1178:                Document doc = builder.build(input);
1179:                try {
1180:                    XIncluder.resolve(doc);
1181:                    fail("allowed circular include, cycle length 1");
1182:                } catch (InclusionLoopException success) {
1183:                    assertTrue(success.getMessage().indexOf(
1184:                            errorFile.toURL().toExternalForm()) > 1);
1185:                    assertTrue(success.getMessage().indexOf(
1186:                            input.toURL().toExternalForm()) > 1);
1187:                    assertEquals(errorFile.toURL().toExternalForm(), success
1188:                            .getURI());
1189:                }
1190:
1191:            }
1192:
1193:            public void testMissingHref() throws ParsingException, IOException,
1194:                    XIncludeException {
1195:
1196:                File input = new File(inputDir, "missinghref.xml");
1197:                Document doc = builder.build(input);
1198:                try {
1199:                    XIncluder.resolve(doc);
1200:                    fail("allowed missing href");
1201:                } catch (NoIncludeLocationException success) {
1202:                    assertNotNull(success.getMessage());
1203:                    assertEquals(doc.getBaseURI(), success.getURI());
1204:                }
1205:
1206:            }
1207:
1208:            public void testBadParseAttribute() throws ParsingException,
1209:                    IOException, XIncludeException {
1210:
1211:                File input = new File(inputDir, "badparseattribute.xml");
1212:                Document doc = builder.build(input);
1213:                try {
1214:                    XIncluder.resolve(doc);
1215:                    fail("allowed bad parse attribute");
1216:                } catch (BadParseAttributeException success) {
1217:                    assertNotNull(success.getMessage());
1218:                    URL u1 = input.toURL();
1219:                    URL u2 = new URL(success.getURI());
1220:                    assertEquals(u1, u2);
1221:                }
1222:
1223:            }
1224:
1225:            public void testUnavailableResource() throws ParsingException,
1226:                    IOException, XIncludeException {
1227:                File input = new File(inputDir, "missingfile.xml");
1228:                Document doc = builder.build(input);
1229:                try {
1230:                    XIncluder.resolve(doc);
1231:                    fail("allowed unresolvable resource");
1232:                } catch (IOException success) {
1233:                    assertNotNull(success.getMessage());
1234:                }
1235:
1236:            }
1237:
1238:            public void testFallback() throws ParsingException, IOException,
1239:                    XIncludeException {
1240:
1241:                File input = new File(inputDir, "fallbacktest.xml");
1242:                Document doc = builder.build(input);
1243:                Document result = XIncluder.resolve(doc);
1244:                Document expectedResult = builder.build(new File(outputDir,
1245:                        "fallbacktest.xml"));
1246:                assertEquals(expectedResult, result);
1247:
1248:            }
1249:
1250:            public void testFallbackWithRecursiveInclude()
1251:                    throws ParsingException, IOException, XIncludeException {
1252:
1253:                File input = new File(inputDir, "fallbacktest2.xml");
1254:                Document doc = builder.build(input);
1255:                Document result = XIncluder.resolve(doc);
1256:                Document expectedResult = builder.build(new File(outputDir,
1257:                        "fallbacktest2.xml"));
1258:                assertEquals(expectedResult, result);
1259:
1260:            }
1261:
1262:            public void testEncodingAttribute() throws ParsingException,
1263:                    IOException, XIncludeException {
1264:
1265:                File input = new File(inputDir, "utf16.xml");
1266:                Document doc = builder.build(input);
1267:                Document result = XIncluder.resolve(doc);
1268:                Document expectedResult = builder.build(new File(outputDir,
1269:                        "utf16.xml"));
1270:                assertEquals(expectedResult, result);
1271:
1272:            }
1273:
1274:            public void testXPointerBareNameID() throws ParsingException,
1275:                    IOException, XIncludeException {
1276:
1277:                File input = new File(inputDir, "xptridtest.xml");
1278:                Document doc = builder.build(input);
1279:                Document result = XIncluder.resolve(doc);
1280:                Document expectedResult = builder.build(new File(outputDir,
1281:                        "xptridtest.xml"));
1282:                assertEquals(expectedResult, result);
1283:
1284:            }
1285:
1286:            public void testXPointerXMLID() throws ParsingException,
1287:                    IOException, XIncludeException {
1288:
1289:                File input = new File(inputDir, "xmlidtest.xml");
1290:                Document doc = builder.build(input);
1291:                Document result = XIncluder.resolve(doc);
1292:                Document expectedResult = builder.build(new File(outputDir,
1293:                        "xmlidtest.xml"));
1294:                assertEquals(expectedResult, result);
1295:
1296:            }
1297:
1298:            public void testShorthandXPointerMatchesNothing()
1299:                    throws ParsingException, IOException {
1300:
1301:                File input = new File(inputDir, "xptridtest2.xml");
1302:                Document doc = builder.build(input);
1303:                try {
1304:                    XIncluder.resolve(doc);
1305:                    fail("Resolved a document with an XPointer "
1306:                            + "that selects no subresource");
1307:                } catch (XIncludeException success) {
1308:                    assertNotNull(success.getMessage());
1309:                    // Must compare URLs instead of strings here to avoid 
1310:                    // issues of whether a file URL begins file:/ or file:///
1311:                    URL u1 = input.toURL();
1312:                    URL u2 = new URL(success.getURI());
1313:                    assertEquals(u1, u2);
1314:                }
1315:
1316:                /* I used to think this case included nothing.
1317:                   Now I think an XPointer that matches no
1318:                   subresource, and does not have a fallback is in error.
1319:                Document expectedResult = builder.build(
1320:                  new File(outputDir, "xptridtest2.xml")
1321:                );
1322:                assertEquals(expectedResult, result);
1323:                 */
1324:
1325:            }
1326:
1327:            public void testXPointerPureTumbler() throws ParsingException,
1328:                    IOException, XIncludeException {
1329:
1330:                File input = new File(inputDir, "xptrtumblertest.xml");
1331:                Document doc = builder.build(input);
1332:                Document result = XIncluder.resolve(doc);
1333:                Document expectedResult = builder.build(new File(outputDir,
1334:                        "xptrtumblertest.xml"));
1335:                assertEquals(expectedResult, result);
1336:
1337:            }
1338:
1339:            public void testUnrecognizedColonizedSchemeNameBackedUpByTumbler()
1340:                    throws ParsingException, IOException, XIncludeException {
1341:
1342:                File input = new File(inputDir, "colonizedschemename.xml");
1343:                Document doc = builder.build(input);
1344:                Document result = XIncluder.resolve(doc);
1345:                Document expectedResult = builder.build(new File(outputDir,
1346:                        "xptrtumblertest.xml"));
1347:                assertEquals(expectedResult, result);
1348:
1349:            }
1350:
1351:            public void testXPointerSyntaxErrorInSecondPart()
1352:                    throws ParsingException, IOException {
1353:
1354:                File input = new File(inputDir, "laterfailure.xml");
1355:                Document doc = builder.build(input);
1356:                try {
1357:                    XIncluder.resolve(doc);
1358:                    fail("Didn't find syntax error in 2nd XPointer part"
1359:                            + " when the first part resolved successfully");
1360:                } catch (XIncludeException success) {
1361:                    assertNotNull(success.getMessage());
1362:                }
1363:
1364:            }
1365:
1366:            public void testBadElementSchemeDataIsNotAnError()
1367:                    throws ParsingException, IOException, XIncludeException {
1368:
1369:                File input = new File(inputDir, "badelementschemedata.xml");
1370:                Document doc = builder.build(input);
1371:                Document result = XIncluder.resolve(doc);
1372:                Document expectedResult = builder.build(new File(outputDir,
1373:                        "badelementschemedata.xml"));
1374:                // dumpResult(input, result); 
1375:
1376:                assertEquals(expectedResult, result);
1377:
1378:            }
1379:
1380:            public void testXPointerSyntaxErrorMissingFinalParenthesis()
1381:                    throws ParsingException, IOException {
1382:
1383:                File input = new File(inputDir, "laterfailure2.xml");
1384:                Document doc = builder.build(input);
1385:                try {
1386:                    XIncluder.resolve(doc);
1387:                    fail("Didn't find syntax error in 2nd XPointer part"
1388:                            + " when the first part resolved successfully");
1389:                } catch (XIncludeException success) {
1390:                    assertNotNull(success.getMessage());
1391:                }
1392:
1393:            }
1394:
1395:            // Test we can include from same document using only 
1396:            // an xpointer attribute
1397:            public void testOnlyXPointer() throws ParsingException,
1398:                    IOException, XIncludeException {
1399:
1400:                File input = new File(inputDir, "onlyxpointer.xml");
1401:                Document doc = builder.build(input);
1402:                Document result = XIncluder.resolve(doc);
1403:                Document expectedResult = builder.build(new File(outputDir,
1404:                        "onlyxpointer.xml"));
1405:
1406:                assertEquals(expectedResult, result);
1407:
1408:            }
1409:
1410:            // Test with 3 element schemes in the XPointer.
1411:            // The first and second one point to nothing. The third one
1412:            // selects something.
1413:            public void testXPointerTripleTumbler() throws ParsingException,
1414:                    IOException, XIncludeException {
1415:
1416:                File input = new File(inputDir, "xptr2tumblertest.xml");
1417:                Document doc = builder.build(input);
1418:                Document result = XIncluder.resolve(doc);
1419:                Document expectedResult = builder.build(new File(outputDir,
1420:                        "xptrtumblertest.xml"));
1421:                assertEquals(expectedResult, result);
1422:
1423:            }
1424:
1425:            // Test with 2 element schemes in the XPointer.
1426:            // The first one uses an ID that doesn't exist 
1427:            // and points to nothing. The second one
1428:            // selects something.
1429:            public void testXPointerDoubleTumbler() throws ParsingException,
1430:                    IOException, XIncludeException {
1431:
1432:                File input = new File(inputDir, "xptrdoubletumblertest.xml");
1433:                Document doc = builder.build(input);
1434:                Document result = XIncluder.resolve(doc);
1435:                Document expectedResult = builder.build(new File(outputDir,
1436:                        "xptrtumblertest.xml"));
1437:                assertEquals(expectedResult, result);
1438:
1439:            }
1440:
1441:            // Test with 2 element schemes in the XPointer.
1442:            // The first one uses an ID that points to something. 
1443:            // The second one points to something too. Both element schemes
1444:            // use IDs exclusively, no child sequences.
1445:            public void testXPointerDoubleElementByID()
1446:                    throws ParsingException, IOException, XIncludeException {
1447:
1448:                File input = new File(inputDir, "xptrdoubleelementtest.xml");
1449:                Document doc = builder.build(input);
1450:                Document result = XIncluder.resolve(doc);
1451:                Document expectedResult = builder.build(new File(outputDir,
1452:                        "xptrdoubleelementtest.xml"));
1453:                assertEquals(expectedResult, result);
1454:
1455:            }
1456:
1457:            // Test with 2 element schemes in the XPointer.
1458:            // The first one uses a child sequence that points to something. 
1459:            // The second one points to something too. Both element schemes
1460:            // use child sequences exclusively, no IDs.
1461:            public void testXPointerDoubleElementByChildSequence()
1462:                    throws ParsingException, IOException, XIncludeException {
1463:
1464:                File input = new File(inputDir, "xptrdoublechildsequence.xml");
1465:                Document doc = builder.build(input);
1466:                Document result = XIncluder.resolve(doc);
1467:                Document expectedResult = builder.build(new File(outputDir,
1468:                        "xptrdoubleelementtest.xml"));
1469:                assertEquals(expectedResult, result);
1470:
1471:            }
1472:
1473:            // Make sure XPointer failures are treated as a resource error,
1474:            // not a fatal error.
1475:            public void testXPointerFailureIsAResourceError()
1476:                    throws ParsingException, IOException, XIncludeException {
1477:
1478:                File input = new File(
1479:                        "data/xinclude/input/xptrtumblerfailsbutfallback.xml");
1480:                Document doc = builder.build(input);
1481:                Document result = XIncluder.resolve(doc);
1482:                // For debugging
1483:                // dumpResult(input, result); 
1484:                Document expectedResult = builder.build(new File(outputDir,
1485:                        "xptrtumblertest.xml"));
1486:                assertEquals(expectedResult, result);
1487:
1488:            }
1489:
1490:            // Make sure XPointer syntax errors are treated as a resource 
1491:            // error, not a fatal error per section 4.2 of XInclude CR
1492:            /* Resources that are unavailable for any reason 
1493:              (for example the resource doesn't exist, connection 
1494:              difficulties or security restrictions prevent it from being 
1495:              fetched, the URI scheme isn't a fetchable one, the resource 
1496:              is in an unsuppored encoding, the resource is determined 
1497:              through implementation-specific mechanisms not to be XML, or a 
1498:              syntax error in an [XPointer Framework]) result in a resource 
1499:              error.  */
1500:            public void testXPointerSyntaxErrorIsAResourceError()
1501:                    throws ParsingException, IOException, XIncludeException {
1502:
1503:                File input = new File(
1504:                        "data/xinclude/input/xptrsyntaxerrorbutfallback.xml");
1505:                Document doc = builder.build(input);
1506:                Document result = XIncluder.resolve(doc);
1507:                Document expectedResult = builder.build(new File(outputDir,
1508:                        "xptrtumblertest.xml"));
1509:                assertEquals(expectedResult, result);
1510:
1511:            }
1512:
1513:            // Test with 3 element schemes in the XPointer,
1514:            // separated by white space.
1515:            // The first one points to nothing. The third one
1516:            // selects something.
1517:            public void testXPointerTumblerWithWhiteSpace()
1518:                    throws ParsingException, IOException, XIncludeException {
1519:
1520:                File input = new File(inputDir, "xptrtumblertest3.xml");
1521:                Document doc = builder.build(input);
1522:                Document result = XIncluder.resolve(doc);
1523:                Document expectedResult = builder.build(new File(outputDir,
1524:                        "xptrtumblertest.xml"));
1525:                assertEquals(expectedResult, result);
1526:
1527:            }
1528:
1529:            public void testXPointerTumblerMatchesNothing()
1530:                    throws ParsingException, IOException {
1531:
1532:                File input = new File(
1533:                        "data/xinclude/input/xptrtumblertest2.xml");
1534:                Document doc = builder.build(input);
1535:                try {
1536:                    XIncluder.resolve(doc);
1537:                    fail("Did not error on XPointer matching nothing");
1538:                } catch (XIncludeException success) {
1539:                    assertNotNull(success.getMessage());
1540:                    URL u1 = input.toURL();
1541:                    URL u2 = new URL(success.getURI());
1542:                    assertEquals(u1, u2);
1543:                }
1544:
1545:            }
1546:
1547:            public void testMalformedXPointer() throws ParsingException,
1548:                    IOException {
1549:
1550:                File input = new File(inputDir, "badxptr.xml");
1551:                Document doc = builder.build(input);
1552:                try {
1553:                    XIncluder.resolve(doc);
1554:                    fail("Allowed malformed XPointer");
1555:                } catch (XIncludeException success) {
1556:                    assertNotNull(success.getMessage());
1557:                    URL u1 = input.toURL();
1558:                    URL u2 = new URL(success.getURI());
1559:                    assertEquals(u1, u2);
1560:                }
1561:
1562:            }
1563:
1564:            public void testXPointerExceptionSelfCausation()
1565:                    throws ParsingException, IOException {
1566:
1567:                File input = new File(inputDir, "badxptr.xml");
1568:                Document doc = builder.build(input);
1569:                try {
1570:                    XIncluder.resolve(doc);
1571:                    fail("Allowed malformed XPointer");
1572:                } catch (XIncludeException success) {
1573:                    Exception cause = (Exception) success.getCause();
1574:                    assertNotNull(cause.getMessage());
1575:                    try {
1576:                        cause.initCause(cause);
1577:                        fail("Self causation");
1578:                    } catch (IllegalArgumentException ex) {
1579:                        assertNotNull(ex.getMessage());
1580:                    }
1581:                }
1582:
1583:            }
1584:
1585:            public void testXPointerExceptionGetCause()
1586:                    throws ParsingException, IOException {
1587:
1588:                File input = new File(inputDir, "badxptr.xml");
1589:                Document doc = builder.build(input);
1590:                try {
1591:                    XIncluder.resolve(doc);
1592:                    fail("Allowed malformed XPointer");
1593:                } catch (XIncludeException success) {
1594:                    Exception cause = (Exception) success.getCause();
1595:                    Exception ex = new Exception();
1596:                    cause.initCause(ex);
1597:                    assertEquals(ex, cause.getCause());
1598:                }
1599:
1600:            }
1601:
1602:            public void testAnotherMalformedXPointer() throws ParsingException,
1603:                    IOException {
1604:
1605:                // testing use of non NCNAME as ID
1606:                File input = new File(inputDir, "badxptr2.xml");
1607:                Document doc = builder.build(input);
1608:                try {
1609:                    XIncluder.resolve(doc);
1610:                    fail("Allowed another malformed XPointer");
1611:                } catch (XIncludeException success) {
1612:                    assertNotNull(success.getMessage());
1613:                    URL u1 = input.toURL();
1614:                    URL u2 = new URL(success.getURI());
1615:                    assertEquals(u1, u2);
1616:                }
1617:
1618:            }
1619:
1620:            public void testMalformedXPointerWithFallback()
1621:                    throws ParsingException, IOException, XIncludeException {
1622:
1623:                File input = new File(inputDir, "xptrfallback.xml");
1624:                Document doc = builder.build(input);
1625:                Document result = XIncluder.resolve(doc);
1626:                Document expectedResult = builder.build(new File(
1627:                        "data/xinclude/output/xptrfallback.xml"));
1628:                assertEquals(expectedResult, result);
1629:
1630:            }
1631:
1632:            public void testIDAndTumbler() throws ParsingException,
1633:                    IOException, XIncludeException {
1634:
1635:                File input = new File(
1636:                        "data/xinclude/input/xptridandtumblertest.xml");
1637:                Document doc = builder.build(input);
1638:                Document result = XIncluder.resolve(doc);
1639:                Document expectedResult = builder.build(new File(
1640:                        "data/xinclude/output/xptridandtumblertest.xml"));
1641:                assertEquals(expectedResult, result);
1642:
1643:            }
1644:
1645:            public void testAutoDetectUTF16BigEndianWithByteOrderMark()
1646:                    throws ParsingException, IOException, XIncludeException {
1647:
1648:                File input = new File(
1649:                        "data/xinclude/input/UTF16BigEndianWithByteOrderMark.xml");
1650:                Document doc = builder.build(input);
1651:                Document result = XIncluder.resolve(doc);
1652:                Document expectedResult = builder
1653:                        .build(new File(
1654:                                "data/xinclude/output/UTF16BigEndianWithByteOrderMark.xml"));
1655:                assertEquals(expectedResult, result);
1656:
1657:            }
1658:
1659:            public void testAutoDetectUTF16LittleEndianWithByteOrderMark()
1660:                    throws ParsingException, IOException, XIncludeException {
1661:
1662:                File input = new File(
1663:                        "data/xinclude/input/UTF16LittleEndianWithByteOrderMark.xml");
1664:                Document doc = builder.build(input);
1665:                Document result = XIncluder.resolve(doc);
1666:                Document expectedResult = builder
1667:                        .build(new File(
1668:                                "data/xinclude/output/UTF16LittleEndianWithByteOrderMark.xml"));
1669:                assertEquals(expectedResult, result);
1670:
1671:            }
1672:
1673:            public void testAutoDetectUTF8WithByteOrderMark()
1674:                    throws ParsingException, IOException, XIncludeException {
1675:
1676:                File input = new File(
1677:                        "data/xinclude/input/UTF8WithByteOrderMark.xml");
1678:                Document doc = builder.build(input);
1679:                Document result = XIncluder.resolve(doc);
1680:                Document expectedResult = builder.build(new File(outputDir,
1681:                        "UTF8WithByteOrderMark.xml"));
1682:                assertEquals(expectedResult, result);
1683:
1684:            }
1685:
1686:            public void testAutoDetectUnicodeBigUnmarked()
1687:                    throws ParsingException, IOException, XIncludeException {
1688:
1689:                File input = new File(
1690:                        "data/xinclude/input/UnicodeBigUnmarked.xml");
1691:                Document doc = builder.build(input);
1692:                Document result = XIncluder.resolve(doc);
1693:                Document expectedResult = builder.build(new File(outputDir,
1694:                        "UnicodeBigUnmarked.xml"));
1695:                assertEquals(expectedResult, result);
1696:
1697:            }
1698:
1699:            public void testUnicodeLittleUnmarked() throws ParsingException,
1700:                    IOException, XIncludeException {
1701:
1702:                File input = new File(
1703:                        "data/xinclude/input/UnicodeLittleUnmarked.xml");
1704:                Document doc = builder.build(input);
1705:                Document result = XIncluder.resolve(doc);
1706:                Document expectedResult = builder.build(new File(outputDir,
1707:                        "UnicodeLittleUnmarked.xml"));
1708:                assertEquals(expectedResult, result);
1709:
1710:            }
1711:
1712:            /*// Java doesn't yet support the UTF-32BE and UTF32LE encodings
1713:             public void testUTF32BE() 
1714:             throws ParsingException, IOException, XIncludeException {
1715:            
1716:             File input = new File(
1717:             "data/xinclude/input/UTF32BE.xml"
1718:             );
1719:             Document doc = builder.build(input);
1720:             Document result = XIncluder.resolve(doc);
1721:             Document expectedResult = builder.build(
1722:             new File(outputDir, "UTF32BE.xml")
1723:             );
1724:             assertEquals(expectedResult, result);
1725:            
1726:             }
1727:
1728:             public void testUTF32LE() 
1729:             throws ParsingException, IOException, XIncludeException {
1730:            
1731:             File input = new File(
1732:             "data/xinclude/input/UTF32LE.xml"
1733:             );
1734:             Document doc = builder.build(input);
1735:             Document result = XIncluder.resolve(doc);
1736:             Document expectedResult = builder.build(
1737:             new File(outputDir, "UTF32LE.xml")
1738:             );
1739:             assertEquals(expectedResult, result);
1740:            
1741:             }
1742:             */
1743:
1744:            public void testEBCDIC() throws ParsingException, IOException,
1745:                    XIncludeException {
1746:
1747:                File input = new File(inputDir, "EBCDIC.xml");
1748:                Document doc = builder.build(input);
1749:                Document result = XIncluder.resolve(doc);
1750:                Document expected = builder.build(new File(outputDir,
1751:                        "EBCDIC.xml"));
1752:                assertEquals(expected, result);
1753:
1754:            }
1755:
1756:            // This test requires files that I have not received permission
1757:            // to distribute so for the moment you won't be able to run it.
1758:            // For my own use it checks to see if the files are present
1759:            // and runs if it does find them. You can't just install the
1760:            // XInclude-Test-Suite data as distributed by the W3C here.
1761:            // Some of those tests rely on optional features XOM does not
1762:            // support such as the xpointer() scheme and notations.
1763:            // Plus some of those tests have mistakes. You need my patched 
1764:            // version of the tests.
1765:            public void testXIncludeTestSuite() throws ParsingException,
1766:                    IOException, XIncludeException {
1767:
1768:                File testDescription = new File("data");
1769:                testDescription = new File(testDescription,
1770:                        "XInclude-Test-Suite");
1771:                testDescription = new File(testDescription, "testdescr.xml");
1772:                URL baseURL = testDescription.toURL();
1773:                if (!testDescription.exists()) {
1774:                    baseURL = new URL(
1775:                            "http://dev.w3.org/cvsweb/~checkout~/2001/"
1776:                                    + "XInclude-Test-Suite/testdescr.xml?content-type=text/"
1777:                                    + "plain&only_with_tag=HEAD");
1778:                }
1779:                Document master = builder.build(baseURL.toExternalForm());
1780:                Element testsuite = master.getRootElement();
1781:                Elements testcases = testsuite.getChildElements("testcases");
1782:                for (int i = 0; i < testcases.size(); i++) {
1783:                    Element group = testcases.get(i);
1784:                    String basedir = group.getAttributeValue("basedir");
1785:                    if (basedir.startsWith("Harold")) {
1786:                        // These tests are listed in the catalog but haven't 
1787:                        // yet been checked into CVS. besides, these are all
1788:                        // based on individual tests in this class anyway, so
1789:                        // running these is duplicated effort.
1790:                        continue;
1791:                    }
1792:                    Elements cases = group.getChildElements("testcase");
1793:                    for (int j = 0; j < cases.size(); j++) {
1794:                        Element testcase = cases.get(j);
1795:                        String id = testcase.getAttributeValue("id");
1796:                        String features = testcase
1797:                                .getAttributeValue("features");
1798:                        if (features != null) {
1799:                            if (features.indexOf("unexpanded-entities") >= 0)
1800:                                continue;
1801:                            if (features.indexOf("unparsed-entities") >= 0)
1802:                                continue;
1803:                            if (features.indexOf("xpointer-scheme") >= 0)
1804:                                continue;
1805:                        }
1806:                        String description = testcase.getFirstChildElement(
1807:                                "description").getValue();
1808:                        if (!basedir.endsWith("/"))
1809:                            basedir += '/';
1810:                        URL input = new URL(baseURL, basedir);
1811:                        input = new URL(input, testcase
1812:                                .getAttributeValue("href"));
1813:                        Element output = testcase
1814:                                .getFirstChildElement("output");
1815:                        if (output == null) { // test failure   
1816:                            try {
1817:                                Document doc = builder.build(input
1818:                                        .toExternalForm());
1819:                                XIncluder.resolveInPlace(doc);
1820:                                fail("Failed test " + id + ": " + description);
1821:                            } catch (XIncludeException success) {
1822:                                assertNotNull(success.getMessage());
1823:                            } catch (IOException success) {
1824:                                if (baseURL.getProtocol().equals("file")) {
1825:                                    assertNotNull(
1826:                                            "Problem processing " + input,
1827:                                            success.getMessage());
1828:                                }
1829:                            } catch (ParsingException success) {
1830:                                assertNotNull(success.getMessage());
1831:                            }
1832:                        } else {
1833:                            URL result = new URL(baseURL, basedir);
1834:                            result = new URL(result, output.getValue());
1835:                            Document expected = builder.build(result
1836:                                    .toExternalForm());
1837:                            Document doc = builder
1838:                                    .build(input.toExternalForm());
1839:                            XIncluder.resolveInPlace(doc);
1840:                            try {
1841:                                assertEquals(
1842:                                        "Error when processing  " + result,
1843:                                        expected, doc);
1844:                            } catch (AssertionFailedError t) {
1845:                                // If it fails, try it without a doctype in result.
1846:                                // A lot of the test cases have incorrect DOCTYPE
1847:                                // declarations.  
1848:                                DocType doctype = expected.getDocType();
1849:                                DocType actualDoctype = doc.getDocType();
1850:                                if (doctype != null) {
1851:                                    expected.removeChild(doctype);
1852:                                    assertEquals("Error when processing  "
1853:                                            + input, expected, doc);
1854:                                } else if (actualDoctype != null) {
1855:                                    doc.removeChild(actualDoctype);
1856:                                    assertEquals("Error when processing  "
1857:                                            + input, expected, doc);
1858:                                } else {
1859:                                    fail();
1860:                                }
1861:                            }
1862:                        }
1863:                    }
1864:                }
1865:
1866:            }
1867:
1868:            private void compare(File expected, File input) throws IOException,
1869:                    ParsingException, XIncludeException {
1870:
1871:                Document expectedDoc = builder.build(expected);
1872:                Document doc = builder.build(input);
1873:                XIncluder.resolveInPlace(doc);
1874:                assertEquals("Error when processing  " + input.getName(),
1875:                        expectedDoc, doc);
1876:
1877:            }
1878:
1879:            // Turn off these tests because Java doesn't support UCS4 yet
1880:            /*   public void testAutoDetectUCS4BE() 
1881:                 throws ParsingException, IOException, XIncludeException {
1882:                 
1883:                   File input = new File(inputDir, "UCS4BE.xml");
1884:                   Document doc = builder.build(input);
1885:                   Document result = XIncluder.resolve(doc);
1886:                   Document expectedResult = builder.build(
1887:                     new File(outputDir, "UTF8WithByteOrderMark.xml")
1888:                   );
1889:                   assertEquals(expectedResult, result);
1890:                           
1891:               }
1892:
1893:               public void testAutoDetectUCS4LE() 
1894:                 throws ParsingException, IOException, XIncludeException {
1895:                 
1896:                   File input = new File(inputDir, "UCS4LE.xml");
1897:                   Document doc = builder.build(input);
1898:                   Document result = XIncluder.resolve(doc);
1899:                   Document expectedResult = builder.build(
1900:                     new File(outputDir, "UTF8WithByteOrderMark.xml")
1901:                   );
1902:                   assertEquals(expectedResult, result);
1903:                           
1904:               } */
1905:
1906:            // Need a test case where A includes B, B includes C
1907:            // and B encounters the error (e.g. a missing href)
1908:            // to make sure B's URL is in the error message, not A's
1909:            public void testChildDocumentSetsErrorURI()
1910:                    throws ParsingException, IOException, XIncludeException {
1911:
1912:                File input = new File(inputDir, "toplevel.xml");
1913:                File error = new File(inputDir, "onedown.xml");
1914:                Document doc = builder.build(input);
1915:                try {
1916:                    XIncluder.resolve(doc);
1917:                    fail("Missing HREF not detected");
1918:                } catch (NoIncludeLocationException success) {
1919:                    assertNotNull(success.getMessage());
1920:                    URL u1 = error.toURL();
1921:                    URL u2 = new URL(success.getURI());
1922:                    assertEquals(u1, u2);
1923:                }
1924:
1925:            }
1926:
1927:            public void testColonizedNameForIdValueInElementScheme()
1928:                    throws ParsingException, IOException {
1929:
1930:                File input = new File(inputDir, "badxptr3.xml");
1931:                Document doc = builder.build(input);
1932:                try {
1933:                    XIncluder.resolve(doc);
1934:                    fail("Bad ID in element not detected");
1935:                } catch (XIncludeException success) {
1936:                    assertNotNull(success.getMessage());
1937:                }
1938:
1939:            }
1940:
1941:            public void testBadIdValueInElementScheme()
1942:                    throws ParsingException, IOException {
1943:
1944:                File input = new File(inputDir, "badxptr4.xml");
1945:                Document doc = builder.build(input);
1946:                try {
1947:                    XIncluder.resolve(doc);
1948:                    fail("Bad ID in element not detected");
1949:                } catch (XIncludeException success) {
1950:                    assertNotNull(success.getMessage());
1951:                }
1952:
1953:            }
1954:
1955:            public void testCirclePointer() throws ParsingException,
1956:                    IOException, XIncludeException {
1957:
1958:                File input = new File(inputDir, "circlepointer1.xml");
1959:                Document doc = builder.build(input);
1960:                try {
1961:                    XIncluder.resolve(doc);
1962:                    fail("Allowed circular reference via XPointer");
1963:                } catch (InclusionLoopException success) {
1964:                    assertNotNull(success.getMessage());
1965:                }
1966:
1967:            }
1968:
1969:            public void testXPointerOverridesFragmentID()
1970:                    throws ParsingException, IOException, XIncludeException {
1971:
1972:                File input = new File(inputDir,
1973:                        "xpointeroverridesfragmentid.xml");
1974:                Document doc = builder.build(input);
1975:                try {
1976:                    XIncluder.resolve(doc);
1977:                    fail("Allowed href attribute with fragment ID");
1978:                } catch (XIncludeException success) {
1979:                    assertNotNull(success.getMessage());
1980:                }
1981:
1982:            }
1983:
1984:            public void testFailsOnFragmentID() throws ParsingException,
1985:                    IOException, XIncludeException {
1986:
1987:                File input = new File(inputDir, "ignoresfragmentid.xml");
1988:                Document doc = builder.build(input);
1989:                try {
1990:                    XIncluder.resolve(doc);
1991:                    fail("Allowed href attribute with fragment ID");
1992:                } catch (XIncludeException success) {
1993:                    assertNotNull(success.getMessage());
1994:                }
1995:
1996:            }
1997:
1998:            // This also tests that the base URI applied to an element is as set by the xml:base
1999:            // attribute, not the document.
2000:            public void testFragmentIDsAreRemovedFromElementBaseURIsAfterInclusion()
2001:                    throws ParsingException, IOException, XIncludeException {
2002:
2003:                File input = new File(inputDir, "basewithfragmentid.xml");
2004:                Document doc = builder.build(input);
2005:                Document result = XIncluder.resolve(doc);
2006:                Document expectedResult = builder.build(new File(outputDir,
2007:                        "basewithfragmentid.xml"));
2008:                assertEquals(expectedResult, result);
2009:
2010:            }
2011:
2012:            public void testIncludeLowerCaseFileNames()
2013:                    throws ParsingException, IOException, XIncludeException {
2014:
2015:                File input = new File(inputDir, "lowercasealphabet.xml");
2016:                Document doc = builder.build(input);
2017:                Document result = XIncluder.resolve(doc);
2018:                Document expectedResult = builder.build(new File(outputDir,
2019:                        "lowercasealphabet.xml"));
2020:                assertEquals(expectedResult, result);
2021:
2022:            }
2023:
2024:            public void testIncludeUpperCaseFileNames()
2025:                    throws ParsingException, IOException, XIncludeException {
2026:
2027:                File input = new File(inputDir, "uppercasealphabet.xml");
2028:                Document doc = builder.build(input);
2029:                Document result = XIncluder.resolve(doc);
2030:                Document expectedResult = builder.build(new File(outputDir,
2031:                        "uppercasealphabet.xml"));
2032:                assertEquals(expectedResult, result);
2033:
2034:            }
2035:
2036:            public void testIncludeDigitFileNames() throws ParsingException,
2037:                    IOException, XIncludeException {
2038:
2039:                File input = new File(inputDir, "numeric.xml");
2040:                Document doc = builder.build(input);
2041:                Document result = XIncluder.resolve(doc);
2042:                Document expectedResult = builder.build(new File(outputDir,
2043:                        "numeric.xml"));
2044:                assertEquals(expectedResult, result);
2045:
2046:            }
2047:
2048:            public void testIncludeHighPunctuationFileNames()
2049:                    throws ParsingException, IOException, XIncludeException {
2050:
2051:                // Windows has a problem with some of these file names so
2052:                // first we have to generate the file, just to avoid storing
2053:                // it in the zip archive
2054:                try {
2055:                    File f = new File(inputDir, "{|}.txt");
2056:                    Writer out = new OutputStreamWriter(
2057:                            new FileOutputStream(f), "UTF8");
2058:                    out.write("{|}");
2059:                    out.flush();
2060:                    out.close();
2061:
2062:                    File input = new File(inputDir, "punctuation.xml");
2063:                    Document doc = builder.build(input);
2064:                    Document result = XIncluder.resolve(doc);
2065:                    Document expectedResult = builder.build(new File(outputDir,
2066:                            "punctuation.xml"));
2067:                    f.delete();
2068:                    assertEquals(expectedResult, result);
2069:                } catch (FileNotFoundException ex) {
2070:                    // This file can't even exist on Windows.
2071:                    // We can only test this on Unix. 
2072:                    if (!windows)
2073:                        throw ex;
2074:                }
2075:
2076:            }
2077:
2078:            public void testMiddlePunctuationError() throws ParsingException,
2079:                    IOException, XIncludeException {
2080:
2081:                File input = new File(inputDir, "middlepunctuationerror.xml");
2082:                Document doc = builder.build(input);
2083:                try {
2084:                    XIncluder.resolve(doc);
2085:                    fail("Allowed illegal IRI with right square bracket ]");
2086:                } catch (BadHrefAttributeException success) {
2087:                    assertNotNull(success.getMessage());
2088:                }
2089:
2090:            }
2091:
2092:            public void testIncludeLowerPunctuationFileNames()
2093:                    throws ParsingException, IOException, XIncludeException {
2094:
2095:                try {
2096:                    File f = new File(inputDir, "!\"$&'+,.txt");
2097:                    Writer out = new OutputStreamWriter(
2098:                            new FileOutputStream(f), "UTF8");
2099:                    out.write("!\"$&'+,");
2100:                    out.flush();
2101:                    out.close();
2102:
2103:                    File input = new File(inputDir, "lowerpunctuation.xml");
2104:                    Document doc = builder.build(input);
2105:                    Document result = XIncluder.resolve(doc);
2106:                    Document expectedResult = builder.build(new File(outputDir,
2107:                            "lowerpunctuation.xml"));
2108:                    f.delete();
2109:                    assertEquals(expectedResult, result);
2110:                } catch (FileNotFoundException ex) {
2111:                    // This file can't even exist on Windows.
2112:                    // We can only test this on Unix. 
2113:                    if (!windows)
2114:                        throw ex;
2115:                }
2116:
2117:            }
2118:
2119:            public void testLineEnds() throws ParsingException, IOException,
2120:                    XIncludeException {
2121:
2122:                File input = new File(inputDir, "lineends.xml");
2123:                Document doc = builder.build(input);
2124:                Document result = XIncluder.resolve(doc);
2125:                Document expected = builder.build(new File(outputDir,
2126:                        "lineends.xml"));
2127:                assertEquals(expected, result);
2128:
2129:            }
2130:
2131:            // This is semantically bad; but still meets the
2132:            // syntax of fragment IDs from RFC 2396
2133:            public void testBadXPointerInFragmentIDIsFatalError()
2134:                    throws ParsingException, IOException, XIncludeException {
2135:
2136:                File input = new File(
2137:                        "data/xinclude/input/meaninglessfragmentid.xml");
2138:                Document doc = builder.build(input);
2139:                try {
2140:                    XIncluder.resolve(doc);
2141:                    fail("Allowed href attribute with fragment ID");
2142:                } catch (XIncludeException success) {
2143:                    assertNotNull(success.getMessage());
2144:                }
2145:
2146:            }
2147:
2148:            // These tests actually connect to IBiblio to load the included
2149:            // data. This is necessary because file URLs don't support
2150:            // content negotiation
2151:            public void testAcceptLanguageFrench() throws ParsingException,
2152:                    IOException, XIncludeException {
2153:
2154:                File input = new File(inputDir, "acceptfrench.xml");
2155:                Document doc = builder.build(input);
2156:                Document result = XIncluder.resolve(doc);
2157:                Document expectedResult = builder.build(new File(outputDir,
2158:                        "acceptfrench.xml"));
2159:                assertEquals(expectedResult, result);
2160:
2161:            }
2162:
2163:            public void testAcceptLanguageEnglish() throws ParsingException,
2164:                    IOException, XIncludeException {
2165:
2166:                File input = new File(inputDir, "acceptenglish.xml");
2167:                Document doc = builder.build(input);
2168:                Document result = XIncluder.resolve(doc);
2169:                Document expectedResult = builder.build(new File(outputDir,
2170:                        "acceptenglish.xml"));
2171:                assertEquals(expectedResult, result);
2172:
2173:            }
2174:
2175:            public void testAcceptPlainText() throws ParsingException,
2176:                    IOException, XIncludeException {
2177:
2178:                File input = new File(inputDir, "acceptplaintext.xml");
2179:                Document doc = builder.build(input);
2180:                Document result = XIncluder.resolve(doc);
2181:                Document expectedResult = builder.build(new File(outputDir,
2182:                        "acceptplaintext.xml"));
2183:                assertEquals(expectedResult, result);
2184:
2185:            }
2186:
2187:            public void testAcceptHTML() throws ParsingException, IOException,
2188:                    XIncludeException {
2189:
2190:                File input = new File(inputDir, "accepthtml.xml");
2191:                Document doc = builder.build(input);
2192:                Document result = XIncluder.resolve(doc);
2193:                Document expectedResult = builder.build(new File(outputDir,
2194:                        "accepthtml.xml"));
2195:                assertEquals(expectedResult, result);
2196:
2197:            }
2198:
2199:            public void testBadHTTPHeaderExceptionConstructor() {
2200:
2201:                String message = "test";
2202:                XIncludeException ex = new BadHTTPHeaderException(message,
2203:                        "http://www.example.com/");
2204:                assertEquals(message, ex.getMessage());
2205:                assertEquals("http://www.example.com/", ex.getURI());
2206:
2207:            }
2208:
2209:            public void testBadHrefAttributerExceptionConstructor() {
2210:
2211:                String message = "test";
2212:                Exception ex = new BadHrefAttributeException(message);
2213:                assertEquals(message, ex.getMessage());
2214:
2215:            }
2216:
2217:            public void testPercentEscapesAreNotAllowedInXPointerAttributes()
2218:                    throws ParsingException, IOException, XIncludeException {
2219:
2220:                File input = new File(inputDir, "xpointerwithpercentescape.xml");
2221:                Document doc = builder.build(input);
2222:                try {
2223:                    XIncluder.resolve(doc);
2224:                    fail("Allowed xpointer attribute with percent escape");
2225:                } catch (XIncludeException success) {
2226:                    assertNotNull(success.getMessage());
2227:                    Exception cause = (Exception) success.getCause();
2228:                    assertNotNull(cause);
2229:                }
2230:
2231:            }
2232:
2233:            public void testXPointerExceptionReinitializeCause()
2234:                    throws ParsingException, IOException, XIncludeException {
2235:
2236:                File input = new File(inputDir, "xpointerwithpercentescape.xml");
2237:                Document doc = builder.build(input);
2238:                try {
2239:                    XIncluder.resolve(doc);
2240:                    fail("Allowed xpointer attribute with percent escape");
2241:                } catch (XIncludeException success) {
2242:                    Exception cause = (Exception) success.getCause();
2243:                    try {
2244:                        cause.initCause(new Exception());
2245:                        fail("Reinitialized cause");
2246:                    } catch (IllegalStateException ex) {
2247:                        assertNotNull(ex.getMessage());
2248:                    }
2249:                }
2250:
2251:            }
2252:
2253:            // WARNING: this test is one interpretation of the XInclude 
2254:            // proposed recommendation. It asserts that encoding attributes 
2255:            // that do not contain legal encoding names are fatal errors.
2256:            // This is far from certain. It is also possible the working group
2257:            // will choose to interpret these as resource errors. 
2258:            public void testMalformedEncodingAttribute() throws IOException,
2259:                    ParsingException, XIncludeException {
2260:
2261:                File input = new File(inputDir, "badencoding.xml");
2262:                Document doc = builder.build(input);
2263:                try {
2264:                    XIncluder.resolve(doc);
2265:                    fail("Allowed encoding attribute with white space");
2266:                } catch (BadEncodingAttributeException success) {
2267:                    assertNotNull(success.getMessage());
2268:                    assertTrue(success.getURI().endsWith(input.getName()));
2269:                }
2270:
2271:            }
2272:
2273:            public void testEmptyEncodingAttribute() throws IOException,
2274:                    ParsingException, XIncludeException {
2275:
2276:                File input = new File(inputDir, "emptyencoding.xml");
2277:                Document doc = builder.build(input);
2278:                try {
2279:                    XIncluder.resolve(doc);
2280:                    fail("Allowed encoding attribute with no value");
2281:                } catch (BadEncodingAttributeException success) {
2282:                    assertNotNull(success.getMessage());
2283:                    assertTrue(success.getURI().endsWith(input.getName()));
2284:                }
2285:
2286:            }
2287:
2288:            public void testEncodingAttributeStartsWithDigit()
2289:                    throws IOException, ParsingException, XIncludeException {
2290:
2291:                File input = new File(inputDir, "digitencoding.xml");
2292:                Document doc = builder.build(input);
2293:                try {
2294:                    XIncluder.resolve(doc);
2295:                    fail("Allowed encoding attribute starting with digit");
2296:                } catch (BadEncodingAttributeException success) {
2297:                    assertNotNull(success.getMessage());
2298:                    assertTrue(success.getURI().endsWith(input.getName()));
2299:                }
2300:
2301:            }
2302:
2303:            // Test that a malformed parse attribute is not thrown when the
2304:            // fallback element containing it is not activated.
2305:            public void testHiddenError() throws ParsingException, IOException,
2306:                    XIncludeException {
2307:
2308:                File input = new File(inputDir, "hiddenerror.xml");
2309:                Document doc = builder.build(input);
2310:                XIncluder.resolve(doc);
2311:
2312:            }
2313:
2314:            // Test that an href attribute that has a fragment identifier
2315:            // is not a fatal error when the fallback element containing 
2316:            // it is not activated.
2317:            public void testHiddenError2() throws ParsingException,
2318:                    IOException, XIncludeException {
2319:
2320:                File input = new File(inputDir, "hiddenerror2.xml");
2321:                Document doc = builder.build(input);
2322:                XIncluder.resolve(doc);
2323:
2324:            }
2325:
2326:            // Test that a fallback element with a non-include parent is not a
2327:            // fatal error when the ancestor fallback element containing it is
2328:            // not activated.
2329:            public void testHiddenError3() throws ParsingException,
2330:                    IOException, XIncludeException {
2331:
2332:                File input = new File(inputDir, "hiddenerror3.xml");
2333:                Document doc = builder.build(input);
2334:                XIncluder.resolve(doc);
2335:
2336:            }
2337:
2338:            // Test that an xpointer attribute that uses percent escapes 
2339:            // is a not a fatal error when the 
2340:            // fallback element containing it is not activated. See
2341:            // http://lists.w3.org/Archives/Public/www-xml-xinclude-comments/2004Oct/0008.html
2342:            public void testXpointerAttributeContainsPercentEscapeInUnactivatedFallback()
2343:                    throws ParsingException, IOException, XIncludeException {
2344:
2345:                File input = new File(inputDir, "hiddenerror3.xml");
2346:                Document doc = builder.build(input);
2347:                XIncluder.resolve(doc);
2348:
2349:            }
2350:
2351:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.