From: <bo...@us...> - 2007-04-14 16:16:12
|
Revision: 184 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=184&view=rev Author: bodewig Date: 2007-04-14 09:16:11 -0700 (Sat, 14 Apr 2007) Log Message: ----------- sources of user guide Modified Paths: -------------- trunk/xmlunit/build.xml Added Paths: ----------- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ATourOfXMLUnit.java trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ComparingPiecesOfXML.java trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/DOMTreeWalking.java trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ValidatingXMLDocuments.java trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/XPathTests.java Modified: trunk/xmlunit/build.xml =================================================================== --- trunk/xmlunit/build.xml 2007-04-13 14:43:42 UTC (rev 183) +++ trunk/xmlunit/build.xml 2007-04-14 16:16:11 UTC (rev 184) @@ -8,6 +8,7 @@ <property name="out.dir" value="classes"/> <property name="dist.dir" value="dist"/> <property name="docs.dir" value="doc"/> + <property name="build.user.guide" value="userguide"/> <property file="build.properties"/> <available property="jaxp13+" classname="javax.xml.xpath.XPath"/> @@ -38,6 +39,7 @@ <fileset dir="${out.dir}" includes="**/*.class"/> <fileset dir="${test.report.dir}" includes="**/TEST*.*"/> <fileset dir="${dist.dir}"/> + <fileset dir="${build.user.guide}"/> </delete> </target> @@ -180,4 +182,14 @@ <delete file="${dist.name}-src.tar" /> </target> + <target name="compile-userguide-examples" depends="compile"> + <mkdir dir="${build.user.guide}"/> + <javac srcdir="src/user-guide" includes="org/" + destdir="${build.user.guide}" source="1.3" target="1.2"> + <classpath> + <pathelement location="${junit.lib}"/> + <pathelement location="${out.dir}"/> + </classpath> + </javac> + </target> </project> Added: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ATourOfXMLUnit.java =================================================================== --- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ATourOfXMLUnit.java (rev 0) +++ trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ATourOfXMLUnit.java 2007-04-14 16:16:11 UTC (rev 184) @@ -0,0 +1,255 @@ +/* +****************************************************************** +Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit.examples; + +import java.io.File; +import java.io.FileReader; +import java.util.List; + +import javax.xml.transform.stream.StreamSource; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.Text; + +import org.custommonkey.xmlunit.*; + +/** + * All code snippets from the "A Tour of XMLUnit" section of the the + * User Guide. + */ +public class ATourOfXMLUnit extends XMLTestCase { + public ATourOfXMLUnit(String name) { + super(name); + } + + // never invoked + private void configure() { + System.setProperty("javax.xml.parsers.DocumentBuilderFactory", + "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); + System.setProperty("javax.xml.parsers.SAXParserFactory", + "org.apache.xerces.jaxp.SAXParserFactoryImpl"); + System.setProperty("javax.xml.transform.TransformerFactory", + "org.apache.xalan.processor.TransformerFactoryImpl"); + XMLUnit + .setControlParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); + XMLUnit + .setTestParser("org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); + XMLUnit + .setSAXParserFactory("org.apache.xerces.jaxp.SAXParserFactoryImpl"); + XMLUnit + .setTransformerFactory("org.apache.xalan.processor.TransformerFactoryImpl"); + } + + public void testForEquality() throws Exception { + String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>"; + String myTestXML = "<msg><localId>2376</localId></msg>"; + assertXMLEqual("Comparing test xml to control xml", + myControlXML, myTestXML); + } + + public void testXMLIdentical()throws Exception { + String myControlXML = + "<struct><int>3</int><boolean>false</boolean></struct>"; + String myTestXML = + "<struct><boolean>false</boolean><int>3</int></struct>"; + Diff myDiff = new Diff(myControlXML, myTestXML); + assertTrue("XML similar " + myDiff.toString(), + myDiff.similar()); + assertTrue("XML identical " + myDiff.toString(), + myDiff.identical()); + } + + public void testAllDifferences() throws Exception { + String myControlXML = "<news><item id=\"1\">War</item>" + + "<item id=\"2\">Plague</item>" + + "<item id=\"3\">Famine</item></news>"; + String myTestXML = "<news><item id=\"1\">Peace</item>" + + "<item id=\"2\">Health</item>" + + "<item id=\"3\">Plenty</item></news>"; + DetailedDiff myDiff = new DetailedDiff(new Diff(myControlXML, + myTestXML)); + List allDifferences = myDiff.getAllDifferences(); + assertEquals(myDiff.toString(), 2, allDifferences.size()); + } + + public void testCompareToSkeletonXML() throws Exception { + String myControlXML = "<location><street-address>22 any street</street-address><postcode>XY00 99Z</postcode></location>"; + String myTestXML = "<location><street-address>20 east cheap</street-address><postcode>EC3M 1EB</postcode></location>"; + DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener(); + Diff myDiff = new Diff(myControlXML, myTestXML); + myDiff.overrideDifferenceListener(myDifferenceListener); + assertTrue("test XML matches control skeleton XML", + myDiff.similar()); + } + + public void testRepeatedChildElements() throws Exception { + String myControlXML = "<suite>" + + "<test status=\"pass\">FirstTestCase</test>" + + "<test status=\"pass\">SecondTestCase</test></suite>"; + String myTestXML = "<suite>" + + "<test status=\"pass\">SecondTestCase</test>" + + "<test status=\"pass\">FirstTestCase</test></suite>"; + assertXMLNotEqual("Repeated child elements in different sequence order are not equal by default", + myControlXML, myTestXML); + Diff myDiff = new Diff(myControlXML, myTestXML); + myDiff.overrideElementQualifier(new ElementNameAndTextQualifier()); + assertXMLEqual("But they are equal when an ElementQualifier controls which test element is compared with each control element", + myDiff, true); + } + + public void testXSLTransformation() throws Exception { + String myInputXML = "..."; + File myStylesheetFile = new File("..."); + Transform myTransform = new Transform(myInputXML, myStylesheetFile); + String myExpectedOutputXML = "..."; + Diff myDiff = new Diff(myExpectedOutputXML, myTransform); + assertTrue("XSL transformation worked as expected", myDiff.similar()); + } + + public void testAnotherXSLTransformation() throws Exception { + File myInputXMLFile = new File("..."); + File myStylesheetFile = new File("..."); + Transform myTransform = new Transform( + new StreamSource(myInputXMLFile), + new StreamSource(myStylesheetFile)); + Document myExpectedOutputXML = + XMLUnit.buildDocument(XMLUnit.getControlParser(), + new FileReader("...")); + Diff myDiff = new Diff(myExpectedOutputXML, + myTransform.getResultDocument()); + assertTrue("XSL transformation worked as expected", myDiff.similar()); + } + + public void testValidation() throws Exception { + XMLUnit.getTestDocumentBuilderFactory().setValidating(true); + // As the document is parsed it is validated against its referenced DTD + Document myTestDocument = XMLUnit.buildTestDocument("..."); + String mySystemId = "..."; + String myDTDUrl = new File("...").toURL().toExternalForm(); + Validator myValidator = new Validator(myTestDocument, mySystemId, + myDTDUrl); + assertTrue("test document validates against unreferenced DTD", + myValidator.isValid()); + } + + public void testXPaths() throws Exception { + String mySolarSystemXML = "<solar-system>" + + "<planet name='Earth' position='3' supportsLife='yes'/>" + + "<planet name='Venus' position='4'/></solar-system>"; + assertXpathExists("//planet[@name='Earth']", mySolarSystemXML); + assertNotXpathExists("//star[@name='alpha centauri']", + mySolarSystemXML); + assertXpathsEqual("//planet[@name='Earth']", + "//planet[@position='3']", mySolarSystemXML); + assertXpathsNotEqual("//planet[@name='Venus']", + "//planet[@supportsLife='yes']", + mySolarSystemXML); + } + + public void testXPathValues() throws Exception { + String myJavaFlavours = "<java-flavours>" + + "<jvm current='some platforms'>1.1.x</jvm>" + + "<jvm current='no'>1.2.x</jvm>" + + "<jvm current='yes'>1.3.x</jvm>" + + "<jvm current='yes' latest='yes'>1.4.x</jvm></javaflavours>"; + assertXpathEvaluatesTo("2", "count(//jvm[@current='yes'])", + myJavaFlavours); + assertXpathValuesEqual("//jvm[4]/@latest", "//jvm[4]/@current", + myJavaFlavours); + assertXpathValuesNotEqual("//jvm[2]/@current", + "//jvm[3]/@current", myJavaFlavours); + } + + public void testXpathsInHTML() throws Exception { + String someBadlyFormedHTML = "<html><title>Ugh</title>" + + "<body><h1>Heading<ul>" + + "<li id='1'>Item One<li id='2'>Item Two"; + TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder = + new TolerantSaxDocumentBuilder(XMLUnit.getTestParser()); + HTMLDocumentBuilder htmlDocumentBuilder = + new HTMLDocumentBuilder(tolerantSaxDocumentBuilder); + Document wellFormedDocument = + htmlDocumentBuilder.parse(someBadlyFormedHTML); + assertXpathEvaluatesTo("Item One", "/html/body//li[@id='1']", + wellFormedDocument); + } + + public void testCountingNodeTester() throws Exception { + String testXML = "<fibonacci><val>1</val><val>2</val><val>3</val>" + + "<val>5</val><val>9</val></fibonacci>"; + CountingNodeTester countingNodeTester = new CountingNodeTester(4); + assertNodeTestPasses(testXML, countingNodeTester, Node.TEXT_NODE); + } + + public void testCustomNodeTester() throws Exception { + String testXML = "<fibonacci><val>1</val><val>2</val><val>3</val>" + + "<val>5</val><val>9</val></fibonacci>"; + NodeTest nodeTest = new NodeTest(testXML); + assertNodeTestPasses(nodeTest, new FibonacciNodeTester(), + new short[] {Node.TEXT_NODE, + Node.ELEMENT_NODE}, + true); + } + + private class FibonacciNodeTester extends AbstractNodeTester { + private int nextVal = 1, lastVal = 1, priorVal = 0; + + public void testText(Text text) throws NodeTestException { + int val = Integer.parseInt(text.getData()); + if (nextVal != val) { + throw new NodeTestException("Incorrect value", text); + } + nextVal = val + lastVal; + priorVal = lastVal; + lastVal = val; + } + + public void testElement(Element element) throws NodeTestException { + String name = element.getLocalName(); + if ("fibonacci".equals(name) || "val".equals(name)) { + return; + } + throw new NodeTestException("Unexpected element", element); + } + + public void noMoreNodes(NodeTest nodeTest) throws NodeTestException { + } + } +} + Property changes on: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ATourOfXMLUnit.java ___________________________________________________________________ Name: svn:executable + * Name: svn:eol-style + native Added: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ComparingPiecesOfXML.java =================================================================== --- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ComparingPiecesOfXML.java (rev 0) +++ trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ComparingPiecesOfXML.java 2007-04-14 16:16:11 UTC (rev 184) @@ -0,0 +1,129 @@ +/* +****************************************************************** +Copyright (c) 2007, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit.examples; + +import java.util.Arrays; +import java.util.List; + +import org.custommonkey.xmlunit.*; +import org.w3c.dom.*; + +/** + * Code from "Comparing Pieces of XML" section of User's Guide + */ +public class ComparingPiecesOfXML extends XMLTestCase { + + class MyDifferenceListener implements DifferenceListener { + private boolean calledFlag = false; + public boolean called() { return calledFlag; } + + public int differenceFound(Difference difference) { + calledFlag = true; + return RETURN_ACCEPT_DIFFERENCE; + } + + public void skippedComparison(Node control, Node test) { + } + } + + private void usingDifferenceEngineDirectly() { + ComparisonController myComparisonController = null; + Node controlNode = null; + Node testNode = null; + ElementQualifier myElementQualifier = null; + + DifferenceEngine engine = new DifferenceEngine(myComparisonController); + MyDifferenceListener listener = new MyDifferenceListener(); + engine.compare(controlNode, testNode, listener, + myElementQualifier); + System.err.println("There have been " + + (listener.called() ? "" : "no ") + + "differences."); + } + + public class HaltOnNonRecoverable implements ComparisonController { + public boolean haltComparison(Difference afterDifference) { + return !afterDifference.isRecoverable(); + } + } + + public static class IgnoreDoctype implements DifferenceListener { + private static final int[] IGNORE = new int[] { + DifferenceConstants.HAS_DOCTYPE_DECLARATION_ID, + DifferenceConstants.DOCTYPE_NAME_ID, + DifferenceConstants.DOCTYPE_PUBLIC_ID_ID, + DifferenceConstants.DOCTYPE_SYSTEM_ID_ID + }; + + static { + Arrays.sort(IGNORE); + } + + public int differenceFound(Difference difference) { + return Arrays.binarySearch(IGNORE, difference.getId()) >= 0 + ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL + : RETURN_ACCEPT_DIFFERENCE; + } + + public void skippedComparison(Node control, Node test) { + } + } + + private void comparingTwoPiecesOfXMLUsingDiff() throws Exception { + Diff d = new Diff("<a><b/><c/></a>", "<a><c/><b/></a>"); + assertFalse(d.identical()); // CHILD_NODELIST_SEQUENCE Difference + assertTrue(d.similar()); + } + + private void FindingAllDifferencesUsingDetailedDiff() throws Exception { + Diff d = new Diff("<a><b/><c/></a>", "<a><c/><b/></a>"); + DetailedDiff dd = new DetailedDiff(d); + dd.overrideElementQualifier(null); + assertFalse(dd.similar()); + List l = dd.getAllDifferences(); + assertEquals(2, l.size()); // expexted <b/> but was <c/> and vice versa + } + + private void junit3() throws Exception { + String CONTROL = null; + String TEST = null; + Diff d = new Diff(CONTROL, TEST); + assertTrue("expected pieces to be similar, " + d.toString(), + d.similar()); + assertXMLEqual("expected pieces to be similar", CONTROL, TEST); + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ComparingPiecesOfXML.java ___________________________________________________________________ Name: svn:executable + * Name: svn:eol-style + native Added: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/DOMTreeWalking.java =================================================================== --- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/DOMTreeWalking.java (rev 0) +++ trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/DOMTreeWalking.java 2007-04-14 16:16:11 UTC (rev 184) @@ -0,0 +1,76 @@ +/* +****************************************************************** +Copyright (c) 2007, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit.examples; + +import junit.framework.TestCase; + +import org.custommonkey.xmlunit.*; +import org.w3c.dom.*; + +/** + * Code from "DOM Tree Walking" section of User's Guide + */ +public class DOMTreeWalking extends TestCase { + + private String myXML = null; + static final String ATTRIBUTE_NAME = null; + + private void AccessingAttributesInANodeTest() throws Exception { + NodeTest nt = new NodeTest(myXML); + NodeTester tester = new MyNodeTester(); + nt.performTest(tester, Node.ELEMENT_NODE); + } + + private void AccessingAttributesInANodeTestAbstractNodeTesterVersion() throws Exception { + NodeTest nt = new NodeTest(myXML); + NodeTester tester = new AbstractNodeTester() { + public void testElement(Element element) throws NodeTestException { + Attr attributeToTest = element.getAttributeNode(ATTRIBUTE_NAME); + } + }; + nt.performTest(tester, Node.ELEMENT_NODE); + } + + static class MyNodeTester implements NodeTester { + public void testNode(Node aNode, NodeTest test) { + Element anElement = (Element) aNode; + Attr attributeToTest = anElement.getAttributeNode(ATTRIBUTE_NAME); + } + + public void noMoreNodes(NodeTest test) {} + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/DOMTreeWalking.java ___________________________________________________________________ Name: svn:executable + * Name: svn:eol-style + native Added: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ValidatingXMLDocuments.java =================================================================== --- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ValidatingXMLDocuments.java (rev 0) +++ trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ValidatingXMLDocuments.java 2007-04-14 16:16:11 UTC (rev 184) @@ -0,0 +1,96 @@ +/* +****************************************************************** +Copyright (c) 2007, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit.examples; + +import java.io.*; +import org.custommonkey.xmlunit.*; +import org.xml.sax.*; + +/** + * Code from "Validating XML Documents" section of User's Guide + */ +public class ValidatingXMLDocuments { + + private String myXmlDocument = null; + private String myDTD = null; + + private void ValidatingAgainstTheDTDDefinedInDOCTYPE() throws Exception { + InputSource is = new InputSource(new FileInputStream(myXmlDocument)); + Validator v = new Validator(is); + boolean isValid = v.isValid(); + } + + private void ValidatingAPieceOfXMLThatDoesntContainADOCTYPE() throws Exception { + String myPublicId = null; + + InputSource is = new InputSource(new FileInputStream(myXmlDocument)); + Validator v = new Validator(is, + (new File(myDTD)).toURI().toURL().toString(), + myPublicId); + boolean isValid = v.isValid(); + } + + private void ValidatingAgainstALocalDTD() throws Exception { + InputSource is = new InputSource(new FileInputStream(myXmlDocument)); + Validator v = new Validator(is, + (new File(myDTD)).toURI().toURL().toString()); + boolean isValid = v.isValid(); + } + + private void ValidatingAgainstDTDUsingApachesXMLResolverAndAXMLCatalog() throws Exception { + InputSource is = new InputSource(new FileInputStream(myXmlDocument)); + XMLUnit.setControlEntityResolver(new CatalogResolver()); + Validator v = new Validator(is); + boolean isValid = v.isValid(); + } + + private void ValidatingAgainstALocalXMLSchema() throws Exception { + String myXmlSchemaFile = null; + + InputSource is = new InputSource(new FileInputStream(myXmlDocument)); + Validator v = new Validator(is); + v.useXMLSchema(true); + v.setJAXP12SchemaSource(new File(myXmlSchemaFile)); + boolean isValid = v.isValid(); + } + + private static class CatalogResolver implements EntityResolver { + public InputSource resolveEntity(String p, String s) { + return null; + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/ValidatingXMLDocuments.java ___________________________________________________________________ Name: svn:executable + * Name: svn:eol-style + native Added: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/XPathTests.java =================================================================== --- trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/XPathTests.java (rev 0) +++ trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/XPathTests.java 2007-04-14 16:16:11 UTC (rev 184) @@ -0,0 +1,79 @@ +/* +****************************************************************** +Copyright (c) 2007, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit.examples; + +import java.util.HashMap; +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.custommonkey.xmlunit.*; + +/** + * Code from "XPath Tests" section of User's Guide + */ +public class XPathTests extends TestCase { + + private void MatchingAnXPathSelectionAgainstARegularExpression() throws Exception { + Document doc = null; + String regex = null; + String xpath = null; + String message = null; + + XpathEngine engine = XMLUnit.newXpathEngine(); + String value = engine.evaluate(xpath, doc); + Assert.assertTrue(message, value.matches(regex)); + } + + private void UsingNamespacesInXPathTests() throws Exception { + String testDoc = "<t:test xmlns:t=\"urn:foo\"><t:bar/></t:test>"; + Document d = XMLUnit.buildControlDocument(testDoc); + HashMap m = new HashMap(); + m.put("foo", "urn:foo"); + + NamespaceContext ctx = new SimpleNamespaceContext(m); + XpathEngine engine = XMLUnit.newXpathEngine(); + engine.setNamespaceContext(ctx); + + NodeList l = engine.getMatchingNodes("//foo:bar", d); + assertEquals(1, l.getLength()); + assertEquals(Node.ELEMENT_NODE, l.item(0).getNodeType()); + } + +} Property changes on: trunk/xmlunit/src/user-guide/org/custommonkey/xmlunit/examples/XPathTests.java ___________________________________________________________________ Name: svn:executable + * Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |