001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import java.io.StringReader;
040:
041: import javax.xml.parsers.SAXParser;
042: import javax.xml.parsers.SAXParserFactory;
043: import junit.framework.TestSuite;
044:
045: import org.w3c.dom.Document;
046: import org.xml.sax.InputSource;
047:
048: /**
049: * JUnit test for TolerantSaxDocumentBuilder
050: */
051: public class test_TolerantSaxDocumentBuilder extends XMLTestCase {
052: private TolerantSaxDocumentBuilder builder;
053: private SAXParser parser;
054:
055: private static final String SIMPLEST_XML = "<root><node>text</node></root>";
056:
057: public void testSimpleDocument() throws Exception {
058: String simpleXML = XML_DECLARATION + SIMPLEST_XML;
059: Document simpleXMLDocument = XMLUnit
060: .buildControlDocument(simpleXML);
061: assertParsedDocumentEqual(simpleXMLDocument, simpleXML);
062: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
063: "WARNING") == -1);
064: }
065:
066: private void assertParsedDocumentEqual(Document control, String test)
067: throws Exception {
068: InputSource parseSource = new InputSource(
069: new StringReader(test));
070: parser.setProperty(
071: "http://xml.org/sax/properties/lexical-handler",
072: builder);
073: parser.parse(parseSource, builder);
074: assertXMLEqual(control, builder.getDocument());
075: }
076:
077: public void testSimpleDocumentWithComments() throws Exception {
078: String xmlWithComments = XML_DECLARATION + "<more>"
079: + SIMPLEST_XML + "<!--this is a comment -->"
080: + SIMPLEST_XML + "</more>";
081: Document documentWithComments = XMLUnit
082: .buildControlDocument(xmlWithComments);
083: assertParsedDocumentEqual(documentWithComments, xmlWithComments);
084: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
085: "WARNING") == -1);
086: }
087:
088: public void testSimpleDocumentWithProcessingInstruction()
089: throws Exception {
090: String xmlWithProcInstruction = XML_DECLARATION + "<more>"
091: + SIMPLEST_XML + "<?processing instruction?>"
092: + SIMPLEST_XML + "</more>";
093: Document documentWithProcInstruction = XMLUnit
094: .buildControlDocument(xmlWithProcInstruction);
095: assertParsedDocumentEqual(documentWithProcInstruction,
096: xmlWithProcInstruction);
097: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
098: "WARNING") == -1);
099: }
100:
101: public void testStartElementWithNoEnd() throws Exception {
102: builder.startDocument();
103: builder.startElement(null, null, "root", null);
104:
105: Document oneElementDocument = XMLUnit
106: .buildControlDocument("<root/>");
107: assertXMLEqual(oneElementDocument, builder.getDocument());
108: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
109: "WARNING") == -1);
110: }
111:
112: public void testEndElementWithNoStart() throws Exception {
113: builder.startDocument();
114: builder.startElement(null, null, "root", null);
115: builder.endElement(null, null, "node");
116: builder.endElement(null, null, "root");
117:
118: Document oneElementDocument = XMLUnit
119: .buildControlDocument("<root/>");
120: assertXMLEqual(oneElementDocument, builder.getDocument());
121: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
122: "WARNING") != -1);
123: }
124:
125: public void testEndElementBeforeStart() throws Exception {
126: builder.startDocument();
127: builder.endElement(null, null, "root");
128: builder.startElement(null, null, "root", null);
129:
130: Document oneElementDocument = XMLUnit
131: .buildControlDocument("<root/>");
132: assertXMLEqual(oneElementDocument, builder.getDocument());
133: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
134: "WARNING") != -1);
135: }
136:
137: public void testTextBeforeStartElement() throws Exception {
138: String someText = "how could this happen?!";
139: builder.startDocument();
140: builder
141: .characters(someText.toCharArray(), 0, someText
142: .length());
143: builder.startElement(null, null, "root", null);
144:
145: Document oneElementDocument = XMLUnit
146: .buildControlDocument("<root/>");
147: assertXMLEqual(oneElementDocument, builder.getDocument());
148: assertTrue(builder.getTrace(), builder.getTrace().indexOf(
149: "WARNING") != -1);
150: }
151:
152: public void setUp() throws Exception {
153: builder = new TolerantSaxDocumentBuilder(XMLUnit
154: .newTestParser());
155: parser = SAXParserFactory.newInstance().newSAXParser();
156: }
157:
158: public test_TolerantSaxDocumentBuilder(String name) {
159: super (name);
160: }
161:
162: public static TestSuite suite() {
163: return new TestSuite(test_TolerantSaxDocumentBuilder.class);
164: }
165:
166: }
|