001: /*
002: * Copyright 2007 Volantis Systems Ltd., All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package net.n3.nanoxml.xinclude;
017:
018: import junit.framework.TestCase;
019: import net.n3.nanoxml.IXMLReader;
020: import net.n3.nanoxml.NonValidator;
021: import net.n3.nanoxml.StdXMLParser;
022: import net.n3.nanoxml.StdXMLReader;
023: import net.n3.nanoxml.XMLBuilderFactory;
024: import net.n3.nanoxml.XMLElement;
025:
026: import java.util.Enumeration;
027:
028: /**
029: * Test the XInclude style functionality added to the XMLBuilder
030: */
031: public class XIncludeXMLBuilderTestCase extends TestCase {
032:
033: /**
034: * This method takes the fileBase name and attempts to find two files
035: * called <fileBase>-input.xml and <fileBase>-expected.xml
036: *
037: * @param fileBase the base of the test file names
038: * @throws Exception
039: */
040: public void doTest(String fileBase) throws Exception {
041:
042: String baseURL = getClass()
043: .getResource(fileBase + "-input.xml").toExternalForm();
044: // set up a new parser to parse the input xml (with includes)
045: StdXMLParser inputParser = new StdXMLParser();
046: inputParser.setBuilder(XMLBuilderFactory.createXMLBuilder());
047: IXMLReader inputReader = new StdXMLReader(getClass()
048: .getResourceAsStream(fileBase + "-input.xml"));
049: inputReader.setSystemID(baseURL);
050: inputParser.setReader(inputReader);
051: inputParser.setValidator(new NonValidator());
052:
053: // set up a new parser to parse the expected xml (without includes)
054: StdXMLParser expectedParser = new StdXMLParser();
055: expectedParser.setBuilder(XMLBuilderFactory.createXMLBuilder());
056: IXMLReader expectedReader = new StdXMLReader(getClass()
057: .getResourceAsStream(fileBase + "-expect.xml"));
058: expectedReader.setSystemID(baseURL);
059: expectedParser.setReader(expectedReader);
060: expectedParser.setValidator(new NonValidator());
061:
062: XMLElement inputElement = (XMLElement) inputParser.parse();
063: XMLElement expectedElement = (XMLElement) expectedParser
064: .parse();
065:
066: deepEqual(expectedElement, inputElement);
067:
068: }
069:
070: /**
071: * This method is used to ensure that the contents of the specified file
072: * (when having "-input.xml" appended) cause the parser to fail
073: *
074: * @param fileBase the base name of the input file.
075: * @throws Exception
076: */
077: public void ensureFailure(String fileBase) throws Exception {
078: try {
079: String baseURL = getClass().getResource(
080: fileBase + "-input.xml").toExternalForm();
081: // set up a new parser to parse the input xml (with includes)
082: StdXMLParser inputParser = new StdXMLParser();
083: inputParser
084: .setBuilder(XMLBuilderFactory.createXMLBuilder());
085: IXMLReader inputReader = new StdXMLReader(getClass()
086: .getResourceAsStream(fileBase + "-input.xml"));
087: inputReader.setSystemID(baseURL);
088: inputParser.setReader(inputReader);
089: inputParser.setValidator(new NonValidator());
090:
091: inputParser.parse();
092: fail("an exception should have been thrown");
093: } catch (Throwable t) {
094: // success
095: }
096: }
097:
098: /**
099: * Perform a deep equality check on the two nodes.
100: *
101: */
102: public void deepEqual(XMLElement a, XMLElement b) {
103:
104: assertEquals("element names", a.getName(), b.getName());
105: assertEquals("element attributes for " + a.getName(), a
106: .getAttributes(), b.getAttributes());
107: assertEquals("content for " + a.getName(), a.getContent(), b
108: .getContent());
109: assertEquals("equal number of children" + a.getName(), a
110: .getChildrenCount(), b.getChildrenCount());
111:
112: Enumeration aChildren = a.enumerateChildren();
113: Enumeration bChildren = b.enumerateChildren();
114: while (aChildren.hasMoreElements()) {
115: XMLElement aChild = (XMLElement) aChildren.nextElement();
116: XMLElement bChild = (XMLElement) bChildren.nextElement();
117: deepEqual(aChild, bChild);
118: }
119: }
120:
121: /**
122: * Test Empty document with include
123: * @throws Exception
124: */
125: public void testIncludeOnly() throws Exception {
126: doTest("include-only");
127: }
128:
129: /**
130: * Test that a fragment included as the root node does not have the
131: * "fragment" element removed
132: * @throws Exception
133: */
134: public void testIncludeFragmentOnly() throws Exception {
135: doTest("include-fragment-only");
136: }
137:
138: /**
139: * Test to ensure that content is correctly included when the include
140: * element is not the root element
141: * @throws Exception
142: */
143: public void testIncludeInElement() throws Exception {
144: doTest("include-in-element");
145: }
146:
147: /**
148: * Test to ensure that content is correctly included when the include
149: * element is not the root element
150: * @throws Exception
151: */
152: public void testIncludeFragmentInElement() throws Exception {
153: doTest("include-fragment-in-element");
154: }
155:
156: /**
157: * Test text inclusion
158: * @throws Exception
159: */
160: public void testIncludeTextInElement() throws Exception {
161: doTest("include-fragment-in-element");
162: }
163:
164: /**
165: * Ensure that the parse attribute accepts "text" and treats it like text
166: * @throws Exception
167: */
168: public void testParseAttributeText() throws Exception {
169: doTest("include-xml-as-text");
170: }
171:
172: /**
173: * Ensure that the parse attribute accepts "xml" and treats like xml
174: * (most other tests do not explicitly set the parse parameter and let it
175: * default to "xml"
176: * @throws Exception
177: */
178: public void testParseAttributeXML() throws Exception {
179: doTest("include-xml-as-xml");
180: }
181:
182: /**
183: * Make sure that a failure occurs for a parse valid that is not "xml"
184: * or "text"
185: *
186: * @throws Exception
187: */
188: public void testParseInvalidAttribute() throws Exception {
189: ensureFailure("invalid-parse-attrib");
190: }
191:
192: /**
193: * Ensure fallbacks work correctly
194: *
195: * @throws Exception
196: */
197: public void testFallback() throws Exception {
198: doTest("fallback");
199: }
200:
201: /**
202: * Test that an empty fallback just removes the include and fallback
203: * elements
204: *
205: * @throws Exception
206: */
207: public void testEmptyFallback() throws Exception {
208: doTest("empty-fallback");
209: }
210:
211: /**
212: * Ensure that two includes in the same element both get included
213: * @throws Exception
214: */
215: public void testMultipleIncludes() throws Exception {
216: doTest("multiple-include");
217: }
218:
219: }
|