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.examples;
038:
039: import java.io.File;
040: import java.io.FileReader;
041: import java.util.List;
042:
043: import javax.xml.transform.stream.StreamSource;
044:
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.Node;
048: import org.w3c.dom.Text;
049:
050: import org.custommonkey.xmlunit.*;
051:
052: /**
053: * All code snippets from the "A Tour of XMLUnit" section of the the
054: * User Guide.
055: */
056: public class ATourOfXMLUnit extends XMLTestCase {
057: public ATourOfXMLUnit(String name) {
058: super (name);
059: }
060:
061: // never invoked
062: private void configure() {
063: System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
064: "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
065: System.setProperty("javax.xml.parsers.SAXParserFactory",
066: "org.apache.xerces.jaxp.SAXParserFactoryImpl");
067: System.setProperty("javax.xml.transform.TransformerFactory",
068: "org.apache.xalan.processor.TransformerFactoryImpl");
069: XMLUnit
070: .setControlParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
071: XMLUnit
072: .setTestParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
073: XMLUnit
074: .setSAXParserFactory("org.apache.xerces.jaxp.SAXParserFactoryImpl");
075: XMLUnit
076: .setTransformerFactory("org.apache.xalan.processor.TransformerFactoryImpl");
077: }
078:
079: public void testForEquality() throws Exception {
080: String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
081: String myTestXML = "<msg><localId>2376</localId></msg>";
082: assertXMLEqual("Comparing test xml to control xml",
083: myControlXML, myTestXML);
084: }
085:
086: public void testXMLIdentical() throws Exception {
087: String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
088: String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
089: Diff myDiff = new Diff(myControlXML, myTestXML);
090: assertTrue("XML similar " + myDiff.toString(), myDiff.similar());
091: assertTrue("XML identical " + myDiff.toString(), myDiff
092: .identical());
093: }
094:
095: public void testAllDifferences() throws Exception {
096: String myControlXML = "<news><item id=\"1\">War</item>"
097: + "<item id=\"2\">Plague</item>"
098: + "<item id=\"3\">Famine</item></news>";
099: String myTestXML = "<news><item id=\"1\">Peace</item>"
100: + "<item id=\"2\">Health</item>"
101: + "<item id=\"3\">Plenty</item></news>";
102: DetailedDiff myDiff = new DetailedDiff(new Diff(myControlXML,
103: myTestXML));
104: List allDifferences = myDiff.getAllDifferences();
105: assertEquals(myDiff.toString(), 2, allDifferences.size());
106: }
107:
108: public void testCompareToSkeletonXML() throws Exception {
109: String myControlXML = "<location><street-address>22 any street</street-address><postcode>XY00 99Z</postcode></location>";
110: String myTestXML = "<location><street-address>20 east cheap</street-address><postcode>EC3M 1EB</postcode></location>";
111: DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
112: Diff myDiff = new Diff(myControlXML, myTestXML);
113: myDiff.overrideDifferenceListener(myDifferenceListener);
114: assertTrue("test XML matches control skeleton XML", myDiff
115: .similar());
116: }
117:
118: public void testRepeatedChildElements() throws Exception {
119: String myControlXML = "<suite>"
120: + "<test status=\"pass\">FirstTestCase</test>"
121: + "<test status=\"pass\">SecondTestCase</test></suite>";
122: String myTestXML = "<suite>"
123: + "<test status=\"pass\">SecondTestCase</test>"
124: + "<test status=\"pass\">FirstTestCase</test></suite>";
125: assertXMLNotEqual(
126: "Repeated child elements in different sequence order are not equal by default",
127: myControlXML, myTestXML);
128: Diff myDiff = new Diff(myControlXML, myTestXML);
129: myDiff
130: .overrideElementQualifier(new ElementNameAndTextQualifier());
131: assertXMLEqual(
132: "But they are equal when an ElementQualifier controls which test element is compared with each control element",
133: myDiff, true);
134: }
135:
136: public void testXSLTransformation() throws Exception {
137: String myInputXML = "...";
138: File myStylesheetFile = new File("...");
139: Transform myTransform = new Transform(myInputXML,
140: myStylesheetFile);
141: String myExpectedOutputXML = "...";
142: Diff myDiff = new Diff(myExpectedOutputXML, myTransform);
143: assertTrue("XSL transformation worked as expected", myDiff
144: .similar());
145: }
146:
147: public void testAnotherXSLTransformation() throws Exception {
148: File myInputXMLFile = new File("...");
149: File myStylesheetFile = new File("...");
150: Transform myTransform = new Transform(new StreamSource(
151: myInputXMLFile), new StreamSource(myStylesheetFile));
152: Document myExpectedOutputXML = XMLUnit.buildDocument(XMLUnit
153: .getControlParser(), new FileReader("..."));
154: Diff myDiff = new Diff(myExpectedOutputXML, myTransform
155: .getResultDocument());
156: assertTrue("XSL transformation worked as expected", myDiff
157: .similar());
158: }
159:
160: public void testValidation() throws Exception {
161: XMLUnit.getTestDocumentBuilderFactory().setValidating(true);
162: // As the document is parsed it is validated against its referenced DTD
163: Document myTestDocument = XMLUnit.buildTestDocument("...");
164: String mySystemId = "...";
165: String myDTDUrl = new File("...").toURL().toExternalForm();
166: Validator myValidator = new Validator(myTestDocument,
167: mySystemId, myDTDUrl);
168: assertTrue("test document validates against unreferenced DTD",
169: myValidator.isValid());
170: }
171:
172: public void testXPaths() throws Exception {
173: String mySolarSystemXML = "<solar-system>"
174: + "<planet name='Earth' position='3' supportsLife='yes'/>"
175: + "<planet name='Venus' position='4'/></solar-system>";
176: assertXpathExists("//planet[@name='Earth']", mySolarSystemXML);
177: assertXpathNotExists("//star[@name='alpha centauri']",
178: mySolarSystemXML);
179: assertXpathsEqual("//planet[@name='Earth']",
180: "//planet[@position='3']", mySolarSystemXML);
181: assertXpathsNotEqual("//planet[@name='Venus']",
182: "//planet[@supportsLife='yes']", mySolarSystemXML);
183: }
184:
185: public void testXPathValues() throws Exception {
186: String myJavaFlavours = "<java-flavours>"
187: + "<jvm current='some platforms'>1.1.x</jvm>"
188: + "<jvm current='no'>1.2.x</jvm>"
189: + "<jvm current='yes'>1.3.x</jvm>"
190: + "<jvm current='yes' latest='yes'>1.4.x</jvm></javaflavours>";
191: assertXpathEvaluatesTo("2", "count(//jvm[@current='yes'])",
192: myJavaFlavours);
193: assertXpathValuesEqual("//jvm[4]/@latest", "//jvm[4]/@current",
194: myJavaFlavours);
195: assertXpathValuesNotEqual("//jvm[2]/@current",
196: "//jvm[3]/@current", myJavaFlavours);
197: }
198:
199: public void testXpathsInHTML() throws Exception {
200: String someBadlyFormedHTML = "<html><title>Ugh</title>"
201: + "<body><h1>Heading<ul>"
202: + "<li id='1'>Item One<li id='2'>Item Two";
203: TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder = new TolerantSaxDocumentBuilder(
204: XMLUnit.getTestParser());
205: HTMLDocumentBuilder htmlDocumentBuilder = new HTMLDocumentBuilder(
206: tolerantSaxDocumentBuilder);
207: Document wellFormedDocument = htmlDocumentBuilder
208: .parse(someBadlyFormedHTML);
209: assertXpathEvaluatesTo("Item One", "/html/body//li[@id='1']",
210: wellFormedDocument);
211: }
212:
213: public void testCountingNodeTester() throws Exception {
214: String testXML = "<fibonacci><val>1</val><val>2</val><val>3</val>"
215: + "<val>5</val><val>9</val></fibonacci>";
216: CountingNodeTester countingNodeTester = new CountingNodeTester(
217: 4);
218: assertNodeTestPasses(testXML, countingNodeTester,
219: Node.TEXT_NODE);
220: }
221:
222: public void testCustomNodeTester() throws Exception {
223: String testXML = "<fibonacci><val>1</val><val>2</val><val>3</val>"
224: + "<val>5</val><val>9</val></fibonacci>";
225: NodeTest nodeTest = new NodeTest(testXML);
226: assertNodeTestPasses(nodeTest, new FibonacciNodeTester(),
227: new short[] { Node.TEXT_NODE, Node.ELEMENT_NODE }, true);
228: }
229:
230: private class FibonacciNodeTester extends AbstractNodeTester {
231: private int nextVal = 1, lastVal = 1, priorVal = 0;
232:
233: public void testText(Text text) throws NodeTestException {
234: int val = Integer.parseInt(text.getData());
235: if (nextVal != val) {
236: throw new NodeTestException("Incorrect value", text);
237: }
238: nextVal = val + lastVal;
239: priorVal = lastVal;
240: lastVal = val;
241: }
242:
243: public void testElement(Element element)
244: throws NodeTestException {
245: String name = element.getLocalName();
246: if ("fibonacci".equals(name) || "val".equals(name)) {
247: return;
248: }
249: throw new NodeTestException("Unexpected element", element);
250: }
251:
252: public void noMoreNodes(NodeTest nodeTest)
253: throws NodeTestException {
254: }
255: }
256: }
|