001: /*
002: ******************************************************************
003: Copyright (c) 2001-2007, 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 javax.xml.parsers.DocumentBuilderFactory;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: import org.custommonkey.xmlunit.exceptions.ConfigurationException;
044: import org.w3c.dom.Document;
045: import org.xml.sax.EntityResolver;
046: import org.xml.sax.helpers.DefaultHandler;
047:
048: /**
049: * Test case for XMLUnit
050: */
051: public class test_XMLUnit extends TestCase {
052: /**
053: * Contructs a new test case.
054: */
055: public test_XMLUnit(String name) {
056: super (name);
057: }
058:
059: private String getDocumentBuilderFactoryImplClass() {
060: return DocumentBuilderFactory.newInstance().getClass()
061: .getName();
062: }
063:
064: /**
065: * Test overiding the SAX parser used to parse control documents
066: */
067: public void testSetControlParser() throws Exception {
068: Object before = XMLUnit.newControlParser();
069: XMLUnit.setControlParser(getDocumentBuilderFactoryImplClass());
070: assertEquals("should be different", false, before == XMLUnit
071: .newControlParser());
072: }
073:
074: public void testIgnoreWhitespace() throws Exception {
075: assertEquals("should not ignore whitespace by default", false,
076: XMLUnit.getIgnoreWhitespace());
077: XMLUnit.setIgnoreWhitespace(true);
078: String test = "<test> monkey </test>";
079: String control = "<test>monkey</test>";
080: assertEquals("Should be similar", true, new Diff(control, test)
081: .similar());
082: try {
083: XMLUnit.setIgnoreWhitespace(false);
084: assertEquals("Should be different", false, new Diff(
085: control, test).similar());
086: } finally {
087: // restore default setting
088: XMLUnit.setIgnoreWhitespace(false);
089: }
090: }
091:
092: /**
093: * Test overiding the SAX parser used to parse test documents
094: */
095: public void testSetTestParser() throws Exception {
096: Object before = XMLUnit.newTestParser();
097: XMLUnit.setTestParser(getDocumentBuilderFactoryImplClass());
098: assertEquals("should be different", false, before == XMLUnit
099: .newTestParser());
100: }
101:
102: public void testSetTransformerFactory() throws Exception {
103: Object before = XMLUnit.getTransformerFactory();
104: XMLUnit.setTransformerFactory(before.getClass().getName());
105: assertEquals("should be different", false, before == XMLUnit
106: .getTransformerFactory());
107: }
108:
109: public void testStripWhitespaceTransform() throws Exception {
110: Document doc = XMLUnit
111: .buildTestDocument(test_Constants.XML_WITH_WHITESPACE);
112: Transform transform = XMLUnit.getStripWhitespaceTransform(doc);
113: Diff diff = new Diff(test_Constants.XML_WITHOUT_WHITESPACE,
114: transform);
115: assertTrue(diff.similar());
116: }
117:
118: public void testXSLTVersion() {
119: try {
120: assertEquals("1.0", XMLUnit.getXSLTVersion());
121: assertEquals(XSLTConstants.XSLT_START, XMLUnit
122: .getXSLTStart());
123: XMLUnit.setXSLTVersion("2.0");
124: assertTrue(XMLUnit.getXSLTStart().startsWith(
125: XSLTConstants.XSLT_START_NO_VERSION));
126: assertTrue(XMLUnit.getXSLTStart().endsWith("\"2.0\">"));
127: try {
128: XMLUnit.setXSLTVersion("foo");
129: fail("foo is not a number");
130: } catch (ConfigurationException expected) {
131: }
132: try {
133: XMLUnit.setXSLTVersion("-1.0");
134: fail("-1.0 is negative");
135: } catch (ConfigurationException expected) {
136: }
137: } finally {
138: XMLUnit.setXSLTVersion("1.0");
139: }
140: }
141: }
|