From: <bo...@us...> - 2014-12-14 18:56:40
|
Revision: 578 http://sourceforge.net/p/xmlunit/code/578 Author: bodewig Date: 2014-12-14 18:56:33 +0000 (Sun, 14 Dec 2014) Log Message: ----------- adapt POM to changed SCM locations Modified Paths: -------------- trunk/build.xml trunk/src/etc/xmlunit-ivy.xml trunk/src/etc/xmlunit.pom Modified: trunk/build.xml =================================================================== --- trunk/build.xml 2014-12-14 18:51:15 UTC (rev 577) +++ trunk/build.xml 2014-12-14 18:56:33 UTC (rev 578) @@ -222,7 +222,7 @@ <filter token="DATE" value="${ivy.publication.datetime}"/> <filter token="DESCRIPTION" value="XMLUnit compares a control XML document to a test document or the result of a transformation, validates documents, and compares the results of XPath expressions."/> <filter token="LICENSE" value="BSD License"/> - <filter token="LICENSE_URL" value="http://xmlunit.svn.sourceforge.net/viewvc/*checkout*/xmlunit/trunk/xmlunit/LICENSE.txt"/> + <filter token="LICENSE_URL" value="https://sourceforge.net/p/xmlunit/code/HEAD/tree/trunk/LICENSE.txt"/> <filter token="GROUP" value="xmlunit"/> <filter token="ARTIFACT" value="xmlunit"/> <filter token="TYPE" value="jar"/> Modified: trunk/src/etc/xmlunit-ivy.xml =================================================================== --- trunk/src/etc/xmlunit-ivy.xml 2014-12-14 18:51:15 UTC (rev 577) +++ trunk/src/etc/xmlunit-ivy.xml 2014-12-14 18:56:33 UTC (rev 578) @@ -1,6 +1,6 @@ <?xml version="1.0"?> <!-- -Copyright (c) 2007-2013, Jeff Martin, Tim Bacon +Copyright (c) 2007-2014, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ publication="@DATE@"> <license name="@LICENSE@" url="@LICENSE_URL@"/> - <description homepage="http://xmlunit.sourceforge.net/">@DESCRIPTION@</description> + <description homepage="http://www.xmlunit.org/">@DESCRIPTION@</description> </info> <publications> <artifact name="@ARTIFACT@" type="@TYPE@"/> Modified: trunk/src/etc/xmlunit.pom =================================================================== --- trunk/src/etc/xmlunit.pom 2014-12-14 18:51:15 UTC (rev 577) +++ trunk/src/etc/xmlunit.pom 2014-12-14 18:56:33 UTC (rev 578) @@ -1,6 +1,6 @@ <?xml version="1.0"?> <!-- -Copyright (c) 2007-2013, Jeff Martin, Tim Bacon +Copyright (c) 2007-2014, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -46,7 +46,7 @@ <packaging>@TYPE@</packaging> <name>XMLUnit for Java</name> <version>@VERSION@</version> - <url>http://xmlunit.sourceforge.net/</url> + <url>http://www.xmlunit.org/</url> <description>@DESCRIPTION@</description> <licenses> <license> @@ -55,8 +55,8 @@ </license> </licenses> <scm> - <url>http://xmlunit.svn.sourceforge.net/viewvc/xmlunit/</url> - <connection>scm:svn:https://xmlunit.svn.sourceforge.net/svnroot/xmlunit/branches/xmlunit-1.x</connection> + <url>https://sourceforge.net/p/xmlunit/code/HEAD/tree/trunk/</url> + <connection>scm:svn:https://svn.code.sf.net/p/xmlunit/code/trunk</connection> </scm> <dependencies> <dependency> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-12-29 15:36:34
|
Revision: 580 http://sourceforge.net/p/xmlunit/code/580 Author: bodewig Date: 2014-12-29 15:36:31 +0000 (Mon, 29 Dec 2014) Log Message: ----------- For missing attributes, the XPath should point to the attribute itself. https://sourceforge.net/p/xmlunit/feature-requests/33/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/src/user-guide/XMLUnit-Java.xml trunk/tests/java/org/custommonkey/xmlunit/test_Difference.java trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java Modified: trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2014-12-28 19:07:04 UTC (rev 579) +++ trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2014-12-29 15:36:31 UTC (rev 580) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2013, Jeff Martin, Tim Bacon +Copyright (c) 2001-2014, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -623,6 +623,7 @@ } } else { controlTracker.clearTrackedAttribute(); + controlTracker.visited(nextAttr); testTracker.clearTrackedAttribute(); compare(attrName, null, control, test, listener, ATTR_NAME_NOT_FOUND); @@ -638,6 +639,7 @@ } else { controlTracker.clearTrackedAttribute(); testTracker.clearTrackedAttribute(); + testTracker.visited(nextAttr); compare(null, getUnNamespacedNodeName(nextAttr, isNamespaced(nextAttr)), @@ -660,7 +662,6 @@ return aNode.getNodeName(); } - /** * @param attribute * @return true if the attribute represents a namespace declaration Modified: trunk/src/user-guide/XMLUnit-Java.xml =================================================================== --- trunk/src/user-guide/XMLUnit-Java.xml 2014-12-28 19:07:04 UTC (rev 579) +++ trunk/src/user-guide/XMLUnit-Java.xml 2014-12-29 15:36:31 UTC (rev 580) @@ -3793,6 +3793,15 @@ href="https://sourceforge.net/p/xmlunit/bugs/64/">Issue 64</ulink> </listitem> + <listitem> + When an attribute cannot be found (a + <literal>ATTR_NAME_NOT_FOUND</literal> difference) the + XPath on the side where the attribute exists will now + point to the attribute itself rather than its owning element. + <ulink + href="https://sourceforge.net/p/xmlunit/feature-requests/33/">Feature + Request 33</ulink> + </listitem> </itemizedlist> </section> </section> Modified: trunk/tests/java/org/custommonkey/xmlunit/test_Difference.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_Difference.java 2014-12-28 19:07:04 UTC (rev 579) +++ trunk/tests/java/org/custommonkey/xmlunit/test_Difference.java 2014-12-29 15:36:31 UTC (rev 580) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2009, Jeff Martin, Tim Bacon +Copyright (c) 2001-2009,2014, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -115,12 +115,34 @@ Difference d2 = (Difference) diffs.get(1); assertEquals(DifferenceConstants.ATTR_NAME_NOT_FOUND_ID, d2.getId()); - assertEquals("/foo[1]/bar[1]", + assertEquals("/foo[1]/bar[1]/@y", d2.getControlNodeDetail().getXpathLocation()); assertEquals("/foo[1]/bar[1]", d2.getTestNodeDetail().getXpathLocation()); } + public void testXpathOfMissingTestAttribute() throws Exception { + Diff d = new Diff("<foo><bar a=\"x\"/></foo>", + "<foo><bar a=\"x\" y=\"z\"/></foo>"); + DetailedDiff dd = new DetailedDiff(d); + List diffs = dd.getAllDifferences(); + assertEquals(2, diffs.size()); + Difference d1 = (Difference) diffs.get(0); + assertEquals(DifferenceConstants.ELEMENT_NUM_ATTRIBUTES_ID, + d1.getId()); + assertEquals("/foo[1]/bar[1]", + d1.getControlNodeDetail().getXpathLocation()); + assertEquals("/foo[1]/bar[1]", + d1.getTestNodeDetail().getXpathLocation()); + Difference d2 = (Difference) diffs.get(1); + assertEquals(DifferenceConstants.ATTR_NAME_NOT_FOUND_ID, + d2.getId()); + assertEquals("/foo[1]/bar[1]", + d2.getControlNodeDetail().getXpathLocation()); + assertEquals("/foo[1]/bar[1]/@y", + d2.getTestNodeDetail().getXpathLocation()); + } + /** * Constructor for test_Difference. * @param name Modified: trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2014-12-28 19:07:04 UTC (rev 579) +++ trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2014-12-29 15:36:31 UTC (rev 580) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2008,2013 Jeff Martin, Tim Bacon +Copyright (c) 2001-2008,2013-2014 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -841,6 +841,8 @@ test.setAttribute("baz", "bar"); engine.compare(control, test, listener, null); assertEquals(ATTR_NAME_NOT_FOUND_ID, listener.comparingWhat); + assertEquals("/foo[1]/@bar", listener.controlXpath); + assertEquals("/foo[1]", listener.testXpath); } public void testMatchTrackerSetViaConstructor() throws Exception { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-12-29 15:57:04
|
Revision: 581 http://sourceforge.net/p/xmlunit/code/581 Author: bodewig Date: 2014-12-29 15:56:36 +0000 (Mon, 29 Dec 2014) Log Message: ----------- Provide the NS-information as part of the difference's value for ATTR_NAME_NOT_FOUND and CHILD_NODE_NOT_FOUND differences. https://sourceforge.net/p/xmlunit/bugs/65/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/src/user-guide/XMLUnit-Java.xml trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java Modified: trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2014-12-29 15:36:31 UTC (rev 580) +++ trunk/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2014-12-29 15:56:36 UTC (rev 581) @@ -512,11 +512,11 @@ throws DifferenceFoundException { if (control != null) { controlTracker.visited(control); - compare(control.getNodeName(), null, control, null, + compare(getQName(control), null, control, null, listener, CHILD_NODE_NOT_FOUND, controlTracker, null); } else { testTracker.visited(test); - compare(null, test.getNodeName(), null, test, listener, + compare(null, getQName(test), null, test, listener, CHILD_NODE_NOT_FOUND, null, testTracker); } } @@ -625,7 +625,8 @@ controlTracker.clearTrackedAttribute(); controlTracker.visited(nextAttr); testTracker.clearTrackedAttribute(); - compare(attrName, null, control, test, listener, + compare(getQName(nextAttr, isNamespacedAttr), null, + control, test, listener, ATTR_NAME_NOT_FOUND); } } @@ -641,8 +642,7 @@ testTracker.clearTrackedAttribute(); testTracker.visited(nextAttr); compare(null, - getUnNamespacedNodeName(nextAttr, - isNamespaced(nextAttr)), + getQName(nextAttr), control, test, listener, ATTR_NAME_NOT_FOUND); } } @@ -662,6 +662,17 @@ return aNode.getNodeName(); } + private String getQName(Node aNode) { + return getQName(aNode, isNamespaced(aNode)); + } + + private String getQName(Node aNode, boolean isNamespacedNode) { + if (isNamespacedNode) { + return "{" + aNode.getNamespaceURI() + "}" + aNode.getLocalName(); + } + return aNode.getNodeName(); + } + /** * @param attribute * @return true if the attribute represents a namespace declaration Modified: trunk/src/user-guide/XMLUnit-Java.xml =================================================================== --- trunk/src/user-guide/XMLUnit-Java.xml 2014-12-29 15:36:31 UTC (rev 580) +++ trunk/src/user-guide/XMLUnit-Java.xml 2014-12-29 15:56:36 UTC (rev 581) @@ -1516,7 +1516,10 @@ <row> <entry><literal>CHILD_NODE_NOT_FOUND_ID</literal></entry> <entry>The name of the unmatched node or - <literal>"null"</literal>.</entry> + <literal>"null"</literal>. If the node is an element + inside an XML namespace the name will be QName-like + <literal>{NS-URI}LOCAL-NAME</literal> - in all other + cases it is the node's local name.</entry> </row> <row> <entry><literal>ATTR_SEQUENCE_ID</literal></entry> @@ -1530,7 +1533,11 @@ </row> <row> <entry><literal>ATTR_NAME_NOT_FOUND_ID</literal></entry> - <entry>The attribute's name or <literal>"null"</literal>.</entry> + <entry>The attribute's name or + <literal>"null"</literal>. If the attribute belongs to + an XML namespace the name will be QName-like + <literal>{NS-URI}LOCAL-NAME</literal> - in all other + cases it is the attribute's local name.</entry> </row> <row> <entry><literal>ATTR_VALUE_ID</literal></entry> @@ -3765,6 +3772,15 @@ <itemizedlist> <listitem> + In cases of <literal>ATTR_NAME_NOT_FOUND</literal> and + <literal>CHILD_NODE_NOT_FOUND</literal> differences the + value used to be the local name of the missing attribute + or node. It will now be a QName-like + <literal>{NS-URI}LOCAL-NAME</literal> string if the + attribute or node belonged to an XML namespace. + <ulink + href="https://sourceforge.net/p/xmlunit/bugs/65/">Issue + 65</ulink> </listitem> </itemizedlist> </section> Modified: trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2014-12-29 15:36:31 UTC (rev 580) +++ trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2014-12-29 15:56:36 UTC (rev 581) @@ -621,6 +621,8 @@ String test = "<stuff><?item data?></stuff>"; listenToDifferences(control, test); // mutiple Differences, we only see the last one, missing second element + assertEquals("item", listener.expected); + assertEquals("null", listener.actual); assertEquals("13 difference type", DifferenceConstants.CHILD_NODE_NOT_FOUND_ID, listener.comparingWhat); @@ -629,6 +631,23 @@ assertNull("13th test xpath", listener.testXpath); } + public void testMissingChildNS() throws Exception { + engine = new DifferenceEngine(PSEUDO_DETAILED_DIFF); + String control = "<stuff xmlns=\"http://example.org/\">" + + "<item id=\"1\"/><item id=\"2\"/></stuff>"; + String test = "<stuff xmlns=\"http://example.org/\"><?item data?></stuff>"; + listenToDifferences(control, test); + // mutiple Differences, we only see the last one, missing second element + assertEquals("{http://example.org/}item", listener.expected); + assertEquals("null", listener.actual); + assertEquals("13 difference type", + DifferenceConstants.CHILD_NODE_NOT_FOUND_ID, + listener.comparingWhat); + assertEquals("13th control xpath", "/stuff[1]/item[2]", + listener.controlXpath); + assertNull("13th test xpath", listener.testXpath); + } + public void testXpathLocation14() throws Exception { engine = new DifferenceEngine(PSEUDO_DETAILED_DIFF); String control = "<stuff><thing id=\"1\"/><item id=\"2\"/></stuff>"; @@ -841,10 +860,25 @@ test.setAttribute("baz", "bar"); engine.compare(control, test, listener, null); assertEquals(ATTR_NAME_NOT_FOUND_ID, listener.comparingWhat); + assertEquals("bar", listener.expected); + assertEquals("null", listener.actual); assertEquals("/foo[1]/@bar", listener.controlXpath); assertEquals("/foo[1]", listener.testXpath); } + public void testMissingAttributeNS() throws Exception { + Element control = document.createElement("foo"); + control.setAttributeNS("http://example.org/", "bar", "baz"); + Element test = document.createElement("foo"); + test.setAttributeNS("http://example.org/", "baz", "bar"); + engine.compare(control, test, listener, null); + assertEquals(ATTR_NAME_NOT_FOUND_ID, listener.comparingWhat); + assertEquals("{http://example.org/}bar", listener.expected); + assertEquals("null", listener.actual); + assertEquals("/foo[1]/@bar", listener.controlXpath); + assertEquals("/foo[1]", listener.testXpath); + } + public void testMatchTrackerSetViaConstructor() throws Exception { Element control = document.createElement("foo"); Element test = document.createElement("foo"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-12-30 19:02:45
|
Revision: 584 http://sourceforge.net/p/xmlunit/code/584 Author: bodewig Date: 2014-12-30 19:02:43 +0000 (Tue, 30 Dec 2014) Log Message: ----------- XMLUnit's own stripped down QName Needed for https://sourceforge.net/p/xmlunit/feature-requests/25/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java Added Paths: ----------- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java Added: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java (rev 0) +++ trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) @@ -0,0 +1,140 @@ +/* +***************************************************************** +Copyright (c) 2014 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; + +/** + * Since javax.xml.namespace.QName is not present prior to Java5, this + * is XMLUnit's own abstraction. + */ +public final class QualifiedName { + private final String namespaceUri; + private final String localName; + + public QualifiedName(String localName) { + this(XMLConstants.NULL_NS_URI, localName); + } + + public QualifiedName(String namespaceUri, String localName) { + if (localName == null) { + throw new IllegalArgumentException("localName must not be null"); + } + this.namespaceUri = + namespaceUri == null ? XMLConstants.NULL_NS_URI : namespaceUri; + this.localName = localName; + } + + public String getNamespaceURI() { + return namespaceUri; + } + + public String getLocalName() { + return localName; + } + + public int hashCode() { + return 7 * namespaceUri.hashCode() + localName.hashCode(); + } + + public boolean equals(Object o) { + if (!(o instanceof QualifiedName)) { + return false; + } + QualifiedName other = (QualifiedName) o; + return namespaceUri.equals(other.namespaceUri) + && localName.equals(other.localName); + } + + /** + * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. + * + * <p>When using the prefix-version the prefix must be defined + * inside the current NamespaceContext.</p> + * + * @see XMLUnit#setXpathNamespaceContext + */ + public static QualifiedName valueOf(String value) { + return valueOf(value, XMLUnit.getXpathNamespaceContext()); + } + + /** + * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. + * + * <p>When using the prefix-version the prefix must be defined + * inside the NamespaceContext given as argument.</p> + */ + public static QualifiedName valueOf(String value, NamespaceContext ctx) { + if (value == null) { + throw new IllegalArgumentException("value must not be null"); + } + int colon = value.indexOf(':'); + int closingBrace = value.indexOf('}'); + boolean qnameToStringStyle = value.startsWith("{") && closingBrace > 0; + if (!qnameToStringStyle && colon < 0) { + return new QualifiedName(value); // null namespace + } + return qnameToStringStyle ? parseQNameToString(value, closingBrace) + : parsePrefixFormat(value, colon, ctx); + } + + private static QualifiedName parseQNameToString(String value, int closingBrace) { + if (closingBrace + 1 == value.length()) { + throw new IllegalArgumentException("localName must not be empty in " + + value); + } + return new QualifiedName(value.substring(1, closingBrace), + value.substring(closingBrace + 1)); + } + + private static QualifiedName parsePrefixFormat(String value, int colon, + NamespaceContext ctx) { + if (colon + 1 == value.length()) { + throw new IllegalArgumentException("localName must not be empty in " + + value); + } + if (ctx == null) { + throw new IllegalArgumentException("Cannot parse " + value + + " without a NamespaceContext"); + } + String prefix = value.substring(0, colon); + String nsUri = ctx.getNamespaceURI(prefix); + if (nsUri == null) { + throw new IllegalArgumentException(prefix + " is unknown to " + + "NamespaceContext"); + } + return new QualifiedName(nsUri, value.substring(colon + 1)); + } +} Property changes on: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java 2014-12-30 18:43:48 UTC (rev 583) +++ trunk/src/java/org/custommonkey/xmlunit/XMLConstants.java 2014-12-30 19:02:43 UTC (rev 584) @@ -168,4 +168,9 @@ */ public static final String W3C_XML_SCHEMA_INSTANCE_TYPE_ATTR = "type"; + + /** + * "" + */ + String NULL_NS_URI = ""; } Added: trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java (rev 0) +++ trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) @@ -0,0 +1,114 @@ +/* +***************************************************************** +Copyright (c) 2014 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; + +import java.util.HashMap; +import java.util.Map; +import junit.framework.Assert; +import junit.framework.TestCase; + +public class test_QualifiedName extends TestCase { + + public void testParseBareLocalName() { + Assert.assertEquals(new QualifiedName("foo"), + QualifiedName.valueOf("foo")); + } + + public void testParseQNameToStringStyle() { + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("{foo}bar")); + } + + public void testParsePrefixStyle() { + Map m = new HashMap(); + m.put("pre", "foo"); + NamespaceContext ctx = new SimpleNamespaceContext(m); + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("pre:bar", ctx)); + } + + public void testParsePrefixStyleImplicitContext() { + Map m = new HashMap(); + m.put("pre", "foo"); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(m)); + try { + Assert.assertEquals(new QualifiedName("foo", "bar"), + QualifiedName.valueOf("pre:bar")); + } finally { + XMLUnit.setXpathNamespaceContext(null); + } + } + + public void testValueMustNotBeNull() { + shouldThrowWithMessage(null, "null"); + } + + public void testLocalPartMustNotBeEmptyQNameStyle() { + shouldThrowWithMessage("{foo}", "must not be empty"); + } + + public void testLocalPartMustNotBeEmptyPrefixStyle() { + shouldThrowWithMessage("foo:", "must not be empty"); + } + + public void testPrefixStyleRequiresNamespaceContext() { + shouldThrowWithMessage("foo:bar", "without a NamespaceContext"); + } + + public void testPrefixMustBeKnownToContext() { + Map m = new HashMap(); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(m)); + try { + shouldThrowWithMessage("foo:bar", "foo is unknown"); + } finally { + XMLUnit.setXpathNamespaceContext(null); + } + } + + private void shouldThrowWithMessage(String valueToParse, + String messagePart) { + try { + QualifiedName.valueOf(valueToParse); + Assert.fail("expected IllegalArgumentException"); + } catch (IllegalArgumentException ex) { + String msg = ex.getMessage(); + Assert.assertTrue("exception message should contain '" + + messagePart + "' but was " + msg, + msg.indexOf(messagePart) >= 0); + } + } +} Property changes on: trunk/tests/java/org/custommonkey/xmlunit/test_QualifiedName.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2014-12-31 11:25:55
|
Revision: 585 http://sourceforge.net/p/xmlunit/code/585 Author: bodewig Date: 2014-12-31 11:25:54 +0000 (Wed, 31 Dec 2014) Log Message: ----------- Add new overloads for XPath expressions that evaluate to QNames https://sourceforge.net/p/xmlunit/feature-requests/25/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java Modified: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-31 11:25:54 UTC (rev 585) @@ -91,6 +91,16 @@ } /** + * Represents the QualifiedName as {NS-URI}LOCAL-NAME. + * + * <p>If the NS-URI is equal to NULL_NS_URI only the local name is returned.</p> + */ + public String toString() { + return XMLConstants.NULL_NS_URI.equals(namespaceUri) ? + localName : "{" + namespaceUri + "}" + localName; + } + + /** * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. * * <p>When using the prefix-version the prefix must be defined Modified: trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2014-12-31 11:25:54 UTC (rev 585) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007,2011 Jeff Martin, Tim Bacon +Copyright (c) 2001-2007,2011,2014 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,6 +42,8 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.util.Iterator; +import java.util.NoSuchElementException; import javax.xml.parsers.DocumentBuilder; @@ -862,6 +864,67 @@ } /** + * Assert the value of an Xpath expression in an XML document. + * @param expectedValue + * @param xpathExpression + * @param control + * @throws SAXException + * @throws IOException + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + InputSource control) + throws SAXException, IOException, + XpathException { + Document document = XMLUnit.buildControlDocument(control); + assertXpathEvaluatesTo(expectedValue, xpathExpression, document); + } + + /** + * Assert the value of an Xpath expression in an XML String + * @param expectedValue + * @param xpathExpression + * @param inXMLString + * @throws SAXException + * @throws IOException + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + String inXMLString) + throws SAXException, IOException, + XpathException { + Document document = XMLUnit.buildControlDocument(inXMLString); + assertXpathEvaluatesTo(expectedValue, xpathExpression, document); + } + + /** + * Assert the value of an Xpath expression in an DOM Document + * @param expectedValue + * @param xpathExpression + * @param inDocument + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + Document inDocument) + throws XpathException { + XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine(); + NodeList nl = + simpleXpathEngine.getMatchingNodes(xpathExpression, inDocument); + NamespaceContext ctx = null; + if (nl.getLength() > 0) { + ctx = new NodeBasedNamespaceContext(nl.item(0)); + } + assertEquals(expectedValue, + QualifiedName + .valueOf(simpleXpathEngine.evaluate(xpathExpression, + inDocument), + ctx)); + } + + /** * Assert that a specific XPath exists in some given XML * @param inXpathExpression * @param control @@ -1125,4 +1188,29 @@ } return d; } + + private static class NodeBasedNamespaceContext implements NamespaceContext { + private final Node node; + NodeBasedNamespaceContext(Node n) { + node = n; + } + public String getNamespaceURI(String prefix) { + return node.lookupNamespaceURI(prefix); + } + + // not used in context of #assertXpathEvaluatesTo + public Iterator getPrefixes() { + return new Iterator() { + public boolean hasNext() { + return false; + } + public Object next() { + throw new NoSuchElementException(); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + } } Modified: trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2014-12-31 11:25:54 UTC (rev 585) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2011, Jeff Martin, Tim Bacon +Copyright (c) 2001-2011,2014 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -40,6 +40,7 @@ import java.io.FileReader; import java.io.StringReader; import java.util.HashMap; +import java.util.Map; import junit.framework.AssertionFailedError; import junit.framework.TestSuite; @@ -582,6 +583,22 @@ "<foo><Bar a=\"1\" b=\"2\"/></foo>"); } + // https://sourceforge.net/p/xmlunit/feature-requests/25/ + public void testXpathEvaluatesToQualifiedName() throws Exception { + String faultDocument = "<env:Envelope " + + "xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" + + "<env:Body><env:Fault><faultcode>env:Server</faultcode>" + + "<faultstring>marche pas</faultstring><detail/></env:Fault>" + + "</env:Body></env:Envelope>"; + Map namespaces = new HashMap(); + namespaces.put("env11", "http://schemas.xmlsoap.org/soap/envelope/"); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(namespaces)); + XMLAssert.assertXpathEvaluatesTo(QualifiedName.valueOf("env11:Server"), + "//env11:Envelope/env11:Body/" + + "env11:Fault/faultcode", + faultDocument); + } + public test_XMLTestCase(String name) { super(name); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bo...@us...> - 2015-03-08 06:00:04
|
Revision: 593 http://sourceforge.net/p/xmlunit/code/593 Author: bodewig Date: 2015-03-08 05:59:53 +0000 (Sun, 08 Mar 2015) Log Message: ----------- backport tests and a fix from 2.0 Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/Diff.java trunk/src/java/org/custommonkey/xmlunit/DoctypeInputStream.java trunk/src/java/org/custommonkey/xmlunit/DoctypeReader.java trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java trunk/src/java/org/custommonkey/xmlunit/XMLTestCase.java trunk/src/java/org/custommonkey/xmlunit/XMLUnit.java trunk/src/user-guide/XMLUnit-Java.xml trunk/tests/java/org/custommonkey/xmlunit/AbstractDoctypeTests.java trunk/tests/java/org/custommonkey/xmlunit/test_AbstractNodeTester.java trunk/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java trunk/tests/java/org/custommonkey/xmlunit/test_Diff.java trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java trunk/tests/java/org/custommonkey/xmlunit/test_TolerantSaxDocumentBuilder.java trunk/tests/java/org/custommonkey/xmlunit/test_Transform.java trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java trunk/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java Added Paths: ----------- trunk/tests/java/org/custommonkey/xmlunit/test_DoctypeSupport.java trunk/tests/java/org/custommonkey/xmlunit/test_XMLAssert.java Modified: trunk/src/java/org/custommonkey/xmlunit/Diff.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/Diff.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/Diff.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2008,2014 Jeff Martin, Tim Bacon +Copyright (c) 2001-2008,2014-2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -132,8 +132,7 @@ * Construct a Diff that compares the XML in two JAXP DOMSources */ public Diff(DOMSource control, DOMSource test) { - this(control.getNode().getOwnerDocument(), - test.getNode().getOwnerDocument()); + this(toDocument(control), toDocument(test)); } /** @@ -425,4 +424,8 @@ : differenceEngine; } + private static Document toDocument(DOMSource d) { + Node n = d.getNode(); + return n instanceof Document ? (Document) n : n.getOwnerDocument(); + } } Modified: trunk/src/java/org/custommonkey/xmlunit/DoctypeInputStream.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/DoctypeInputStream.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/DoctypeInputStream.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001, Jeff Martin, Tim Bacon +Copyright (c) 2001,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -46,7 +46,7 @@ * conforms to a different DTD. * Combines InputStream semantics with the ability to specify a target doctype * for a byte stream containing XML markup. - * Used by Validator class to wrap an InputStrea, when performing validation of a + * Used by Validator class to wrap an InputStream, when performing validation of a * document against a DTD. * <br />Examples and more at <a href="http://xmlunit.sourceforge.net"/>xmlunit.sourceforge.net</a> */ Modified: trunk/src/java/org/custommonkey/xmlunit/DoctypeReader.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/DoctypeReader.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/DoctypeReader.java 2015-03-08 05:59:53 UTC (rev 593) @@ -51,7 +51,7 @@ */ public class DoctypeReader extends Reader { - private final Reader originalReader; + private final BufferedReader originalReader; private final StringBuffer sourceBuffer = new StringBuffer(1024); private final DoctypeSupport support; @@ -67,12 +67,12 @@ public DoctypeReader(Reader originalSource, String doctypeName, String systemID) { originalReader = originalSource instanceof BufferedReader - ? originalSource : new BufferedReader(originalSource); + ? (BufferedReader) originalSource : new BufferedReader(originalSource); support = new DoctypeSupport(doctypeName, systemID, new DoctypeSupport.Readable() { public int read() throws IOException { - return originalReader.read(); + return DoctypeReader.this.originalReader.read(); } }, true, null); @@ -92,19 +92,13 @@ * @return the contents of the originalSource within a StringBuffer * @throws IOException if thrown while reading from the original source */ - private StringBuffer getContent(Reader originalSource) + private StringBuffer getContent(BufferedReader originalSource) throws IOException { if (sourceBuffer.length() == 0) { - BufferedReader bufferedReader; - if (originalSource instanceof BufferedReader) { - bufferedReader = (BufferedReader) originalSource; - } else { - bufferedReader = new BufferedReader(originalSource); - } String newline = System.getProperty("line.separator"); String source; boolean atFirstLine = true; - while ((source = bufferedReader.readLine()) != null) { + while ((source = originalSource.readLine()) != null) { if (atFirstLine) { atFirstLine = false; } else { @@ -113,7 +107,7 @@ sourceBuffer.append(source); } - bufferedReader.close(); + originalSource.close(); } return sourceBuffer; Modified: trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2015-03-08 05:59:53 UTC (rev 593) @@ -809,9 +809,8 @@ fail("Expected test value NOT to be equal to control but both were " + test); } - } else if (test != null) { - fail("control xPath evaluated to empty node set, " - + "but test xPath evaluated to " + test); + } else if (test == null) { + fail("Expected test value NOT to be equal to control but both were empty"); } } Modified: trunk/src/java/org/custommonkey/xmlunit/XMLTestCase.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLTestCase.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/XMLTestCase.java 2015-03-08 05:59:53 UTC (rev 593) @@ -41,7 +41,6 @@ import java.io.IOException; import java.io.Reader; -import java.io.StringReader; import junit.framework.TestCase; import org.w3c.dom.Document; @@ -113,7 +112,7 @@ */ public Diff compareXML(String control, Reader test) throws SAXException, IOException { - return XMLUnit.compareXML(new StringReader(control), test); + return XMLUnit.compareXML(control, test); } /** @@ -126,7 +125,7 @@ */ public Diff compareXML(Reader control, String test) throws SAXException, IOException { - return XMLUnit.compareXML(control, new StringReader(test)); + return XMLUnit.compareXML(control, test); } /** Modified: trunk/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLUnit.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/java/org/custommonkey/xmlunit/XMLUnit.java 2015-03-08 05:59:53 UTC (rev 593) @@ -178,6 +178,13 @@ return controlEntityResolver; } + /** + * Obtains the EntityResolver to be added to all new test parsers. + */ + public static EntityResolver getTestEntityResolver() { + return testEntityResolver; + } + /** * Get the <code>DocumentBuilderFactory</code> instance used to instantiate * parsers for the control XML in an XMLTestCase. @@ -559,7 +566,8 @@ * @return current version */ public static String getVersion() { - return "1.3alpha"; + // FIXME should read a property + return "1.7alpha"; } /** Modified: trunk/src/user-guide/XMLUnit-Java.xml =================================================================== --- trunk/src/user-guide/XMLUnit-Java.xml 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/src/user-guide/XMLUnit-Java.xml 2015-03-08 05:59:53 UTC (rev 593) @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE article PUBLIC "-//OASIS//DTD Simplified DocBook XML V1.1b1//EN" "http://docbook.org/xml/simple/1.1b1/sdocbook.dtd"> <!-- -Copyright (c) 2001-2014, Jeff Martin, Tim Bacon +Copyright (c) 2001-2015, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -3861,6 +3861,40 @@ </section> </section> + <section id="Changes 1.7"> + <title>Changes from XMLUnit 1.6 to 1.7</title> + + <section id="Breaking Changes 1.7"> + <title>Breaking Changes</title> + + <itemizedlist> + <listitem> + </listitem> + </itemizedlist> + </section> + + <section id="New Features 1.7"> + <title>New Features</title> + + <itemizedlist> + <listitem> + </listitem> + </itemizedlist> + </section> + + <section id="Bugfixes 1.7"> + <title>Important Bug Fixes</title> + + <itemizedlist> + <listitem> + The <literal>Diff</literal> constructor using + <literal>DOMSource</literal> didn't work when the source's + <literal>Node</literals> was a <literal>Document</literals>. + </listitem> + </itemizedlist> + </section> + </section> + </appendix> </article> Modified: trunk/tests/java/org/custommonkey/xmlunit/AbstractDoctypeTests.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/AbstractDoctypeTests.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/AbstractDoctypeTests.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2001-2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -48,6 +48,7 @@ private static final String COMMENT = "<!-- comment -->"; protected static final String NO_DTD = "<document><element>one</element></document>"; + private static final String PI = "<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>"; public abstract void testGetContent() throws IOException; @@ -95,6 +96,30 @@ "ni", "shrubbery"); } + public void testInternalDTDWithTwoComments() throws IOException { + assertEquals(test_Constants.XML_DECLARATION + + "<!DOCTYPE ni SYSTEM \"shrubbery\">" + + COMMENT + + COMMENT, + test_Constants.XML_DECLARATION + + COMMENT + + COMMENT + + test_Constants.CHUCK_JONES_RIP_DTD_DECL, + "ni", "shrubbery"); + } + + public void testInternalDTDWithPIAfterComments() throws IOException { + assertEquals(test_Constants.XML_DECLARATION + + "<!DOCTYPE ni SYSTEM \"shrubbery\">" + + COMMENT + + PI, + test_Constants.XML_DECLARATION + + COMMENT + + PI + + test_Constants.CHUCK_JONES_RIP_DTD_DECL, + "ni", "shrubbery"); + } + public void testExternalDTDWithComment() throws IOException { assertEquals("<!DOCTYPE ni SYSTEM \"shrubbery\">" + COMMENT, @@ -114,6 +139,18 @@ "ni", "shrubbery"); } + public void testLeadingWhitespace() throws IOException { + assertEquals(" <!DOCTYPE ni SYSTEM \"shrubbery\">" + NO_DTD, + " " + NO_DTD, "ni", "shrubbery"); + } + + public void testWhitespaceAfterComment() throws IOException { + assertEquals("<!DOCTYPE ni SYSTEM \"shrubbery\">" + + COMMENT + " " + + NO_DTD, + COMMENT + " " + NO_DTD, "ni", "shrubbery"); + } + public AbstractDoctypeTests(String name) { super(name); } Modified: trunk/tests/java/org/custommonkey/xmlunit/test_AbstractNodeTester.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_AbstractNodeTester.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_AbstractNodeTester.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2007, Jeff Martin, Tim Bacon +Copyright (c) 2007,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -39,8 +39,10 @@ import junit.framework.Assert; import junit.framework.TestCase; +import org.w3c.dom.Attr; import org.w3c.dom.CDATASection; import org.w3c.dom.Comment; +import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.Element; import org.w3c.dom.Entity; @@ -55,6 +57,12 @@ */ public class test_AbstractNodeTester extends TestCase { + private Document doc; + + public void setUp() { + doc = XMLUnit.newControlParser().newDocument(); + } + public void testExactlyOncePerMethod() throws Exception { String testXml = "<!DOCTYPE foo [" + "<!ELEMENT foo (#PCDATA)>" @@ -88,6 +96,99 @@ tester.verify(); } + // seems to never get called in real tests + public void testAttribute() { + AbstractNodeTester t = new AbstractNodeTester() { }; + Attr n = doc.createAttribute("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testCDATASection() { + AbstractNodeTester t = new AbstractNodeTester() { }; + CDATASection n = doc.createCDATASection("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testComment() { + AbstractNodeTester t = new AbstractNodeTester() { }; + Comment n = doc.createComment("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testElement() { + AbstractNodeTester t = new AbstractNodeTester() { }; + Element n = doc.createElement("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testEntityReference() { + AbstractNodeTester t = new AbstractNodeTester() { }; + EntityReference n = doc.createEntityReference("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testProcessingInstruction() { + AbstractNodeTester t = new AbstractNodeTester() { }; + ProcessingInstruction n = doc.createProcessingInstruction("foo", "bar"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + public void testTextNode() { + AbstractNodeTester t = new AbstractNodeTester() { }; + Text n = doc.createTextNode("foo"); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + + // never called as NodeTest directly jumps to the document element + public void testDocumentType() throws Exception { + AbstractNodeTester t = new AbstractNodeTester() { }; + DocumentType n = XMLUnit + .buildControlDocument("<!DOCTYPE Book PUBLIC \"XMLUNIT/TESTS/PUB1\" \"../test-resources/Book.dtd\">" + + "<Book/>") + .getDoctype(); + try { + t.testNode(n, null); + fail("expected exception"); + } catch (NodeTestException ex) { + assertSame(n, ex.getNode()); + } + } + private class ExactlyOncePerMethod extends AbstractNodeTester { private boolean cdataCalled; Modified: trunk/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2008,2010,2013 Jeff Martin, Tim Bacon +Copyright (c) 2001-2008,2010,2013,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,6 +41,7 @@ import java.io.Reader; import java.util.Iterator; import java.util.List; +import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.examples.MultiLevelElementNameAndTextQualifier; import org.w3c.dom.Document; @@ -330,6 +331,10 @@ return new DetailedDiff(super.buildDiff(control, test, engine)); } + protected Diff buildDiff(DOMSource control, DOMSource test) { + return new DetailedDiff(super.buildDiff(control, test)); + } + public test_DetailedDiff(String name) { super(name); firstForecast = "<weather><today icon=\"clouds\" temp=\"17\">" Modified: trunk/tests/java/org/custommonkey/xmlunit/test_Diff.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_Diff.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_Diff.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2013, Jeff Martin, Tim Bacon +Copyright (c) 2001-2013,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,6 +42,7 @@ import java.io.Reader; import java.util.HashSet; import java.util.Set; +import javax.xml.transform.dom.DOMSource; import junit.framework.TestCase; @@ -475,6 +476,29 @@ assertFalse(diff.toString(), diff.identical()); } + public void testUsingDOMSourceFromDocument() throws Exception { + Document control = + XMLUnit.buildTestDocument("<!DOCTYPE skinconfig []>" + + "<!--abcd--><root></root>"); + Document test = + XMLUnit.buildControlDocument("<!DOCTYPE skinconfig [<!--abcd-->]>" + + "<root></root>"); + Diff diff = buildDiff(new DOMSource(control), new DOMSource(test)); + assertTrue(diff.toString(), diff.identical()); + } + + public void testUsingDOMSourceFromElement() throws Exception { + Document control = + XMLUnit.buildTestDocument("<!DOCTYPE skinconfig []>" + + "<!--abcd--><root></root>"); + Document test = + XMLUnit.buildControlDocument("<!DOCTYPE skinconfig [<!--abcd-->]>" + + "<root></root>"); + Diff diff = buildDiff(new DOMSource(control.getDocumentElement()), + new DOMSource(test.getDocumentElement())); + assertTrue(diff.toString(), diff.identical()); + } + protected Diff buildDiff(Document control, Document test) { return new Diff(control, test); } @@ -493,6 +517,10 @@ XMLUnit.buildTestDocument(test), engine); } + protected Diff buildDiff(DOMSource control, DOMSource test) { + return new Diff(control, test); + } + /** * Construct a test * @param name Test name @@ -906,7 +934,7 @@ "<child amount=\"100\" />" + "</tag>"; - Diff diff = new Diff(control, test); + Diff diff = buildDiff(control, test); diff.overrideElementQualifier(new ElementNameAndAttributeQualifier()); assertTrue(diff.toString(), diff.similar()); @@ -947,7 +975,7 @@ String control = "<ns2:Square xsi:type=\"ns2:Shape\" " + "xmlns:ns2=\"http://example.com/\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; - Diff diff = new Diff(control, test); + Diff diff = buildDiff(control, test); assertTrue(diff.toString(), diff.similar()); } @@ -961,7 +989,7 @@ + "<ns2:Square xsi:type=\"ns2:Shape\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>" + "</ns2:Shapes>"; - Diff diff = new Diff(control, test); + Diff diff = buildDiff(control, test); assertTrue(diff.toString(), diff.similar()); } @@ -975,7 +1003,7 @@ + "xmlns:ns1=\"http://example.com/\" " + "xmlns:ns2=\"http://example.com/another-uri/\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; - Diff diff = new Diff(control, test); + Diff diff = buildDiff(control, test); assertFalse(diff.toString(), diff.similar()); } public void testXsiNil() throws Exception { @@ -984,7 +1012,7 @@ String control = "<foo xsi:nil=\"false\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>"; - Diff diff = new Diff(control, test); + Diff diff = buildDiff(control, test); assertFalse(diff.toString(), diff.similar()); } } Modified: trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2008,2013-2014 Jeff Martin, Tim Bacon +Copyright (c) 2001-2008,2013-2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -631,6 +631,22 @@ assertNull("13th test xpath", listener.testXpath); } + public void testXpathLocation13Reversed() throws Exception { + engine = new DifferenceEngine(PSEUDO_DETAILED_DIFF); + String control = "<stuff><?item data?></stuff>"; + String test = "<stuff><item id=\"1\"/><item id=\"2\"/></stuff>"; + listenToDifferences(control, test); + // mutiple Differences, we only see the last one, missing second element + assertEquals("null", listener.expected); + assertEquals("item", listener.actual); + assertEquals("13 difference type", + DifferenceConstants.CHILD_NODE_NOT_FOUND_ID, + listener.comparingWhat); + assertNull("13th-r control xpath", listener.controlXpath); + assertEquals("13th-r test xpath", "/stuff[1]/item[2]", + listener.testXpath); + } + public void testMissingChildNS() throws Exception { engine = new DifferenceEngine(PSEUDO_DETAILED_DIFF); String control = "<stuff xmlns=\"http://example.org/\">" @@ -1002,6 +1018,40 @@ listener.comparingWhat); } + public void testDoctypeDifferences() throws Exception { + String control = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!DOCTYPE Book PUBLIC \"XMLUNIT/TESTS/PUB1\" \"../test-resources/Book.dtd\">" + + "<Book/>"; + String test = "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!DOCTYPE Book PUBLIC \"XMLUNIT/TESTS/PUB2\" \"../test-resources/Book.dtd\">" + + "<Book/>"; + listenToDifferences(control, test); + assertTrue(listener.different); + } + + public void testTextAndCDATA() throws Exception { + String control = "<stuff><![CDATA[foo]]></stuff>"; + String test = "<stuff>foo</stuff>"; + listenToDifferences(control, test); + assertTrue(listener.different); + assertEquals("textAndCdata control xpath", "/stuff[1]/text()[1]", + listener.controlXpath); + assertEquals("textAndCdata test xpath", "/stuff[1]/text()[1]", + listener.testXpath); + } + + public void testIgnoreDiffBetweenTextAndCDATA() throws Exception { + String control = "<stuff><![CDATA[foo]]></stuff>"; + String test = "<stuff>foo</stuff>"; + try { + XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); + listenToDifferences(control, test); + assertFalse(listener.different); + } finally { + XMLUnit.setIgnoreDiffBetweenTextAndCDATA(false); + } + } + private void listenToDifferences(String control, String test) throws SAXException, IOException { Document controlDoc = XMLUnit.buildControlDocument(control); Added: trunk/tests/java/org/custommonkey/xmlunit/test_DoctypeSupport.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_DoctypeSupport.java (rev 0) +++ trunk/tests/java/org/custommonkey/xmlunit/test_DoctypeSupport.java 2015-03-08 05:59:53 UTC (rev 593) @@ -0,0 +1,57 @@ +/* +****************************************************************** +Copyright (c) 2015 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 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; + +import junit.framework.TestCase; + +import org.custommonkey.xmlunit.exceptions.XMLUnitRuntimeException; + +public class test_DoctypeSupport extends TestCase { + + public void testUnsupportedEncoding() { + try { + new DoctypeSupport("foo", "foo", new DoctypeSupport.Readable() { + public int read() { + return 1; + } + }, false, "foo"); + fail("should throw an exception"); + } catch (XMLUnitRuntimeException ex) { + // expected + } + } +} Property changes on: trunk/tests/java/org/custommonkey/xmlunit/test_DoctypeSupport.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/tests/java/org/custommonkey/xmlunit/test_TolerantSaxDocumentBuilder.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_TolerantSaxDocumentBuilder.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_TolerantSaxDocumentBuilder.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2001-2015, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -131,6 +131,11 @@ assertTrue(builder.getTrace(), builder.getTrace().indexOf("WARNING") != -1); } + // silly, but a no-op + public void testCharactersWithNegativeLength() { + builder.characters(null, 0, -1); + } + public void setUp() throws Exception { builder = new TolerantSaxDocumentBuilder(XMLUnit.newTestParser()); parser = SAXParserFactory.newInstance().newSAXParser(); Modified: trunk/tests/java/org/custommonkey/xmlunit/test_Transform.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_Transform.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_Transform.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -38,8 +38,10 @@ import java.io.File; import java.io.FileReader; +import java.io.StringReader; import org.custommonkey.xmlunit.exceptions.ConfigurationException; +import org.custommonkey.xmlunit.exceptions.XMLUnitRuntimeException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; @@ -48,6 +50,7 @@ import junit.framework.TestSuite; import org.w3c.dom.Document; +import org.xml.sax.InputSource; /** * Test a Transform @@ -66,6 +69,17 @@ assertEquals(DOG, stripLineFeeds(transform.getResultString())); } + public void testGetResultStringViaInputSource() throws Exception { + transform = new Transform(new InputSource(new StringReader(FLEABALL)), animal); + assertEquals(DOG, stripLineFeeds(transform.getResultString())); + } + + public void testGetResultStringViaTwoInputSources() throws Exception { + transform = new Transform(new InputSource(new StringReader(FLEABALL)), + new InputSource(new FileReader(animal))); + assertEquals(DOG, stripLineFeeds(transform.getResultString())); + } + public void testGetResultDocument() throws Exception { transform = new Transform(FLEABALL, animal); Diff diff = new Diff(DOG, transform); @@ -144,6 +158,14 @@ } } + public void testParameterHandling() { + transform = new Transform(FLEABALL, animal); + transform.setParameter("foo", "bar"); + assertEquals("bar", transform.getParameter("foo")); + transform.clearParameters(); + assertNull(transform.getParameter("foo")); + } + private void assertNotEquals(Object expected, Object actual) { if (expected.equals(actual)) { fail("Expected " + expected + " different to actual!"); Added: trunk/tests/java/org/custommonkey/xmlunit/test_XMLAssert.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_XMLAssert.java (rev 0) +++ trunk/tests/java/org/custommonkey/xmlunit/test_XMLAssert.java 2015-03-08 05:59:53 UTC (rev 593) @@ -0,0 +1,174 @@ +/* +****************************************************************** +Copyright (c) 2015 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 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; + +import java.io.StringReader; +import java.util.HashMap; +import java.util.Map; +import org.xml.sax.InputSource; +import org.w3c.dom.Node; + +import junit.framework.AssertionFailedError; +import junit.framework.TestCase; + +import org.custommonkey.xmlunit.examples.CountingNodeTester; + +public class test_XMLAssert extends TestCase { + public void testAssertXMLEqualStringDiffBooleanWithMatch() throws Exception { + Diff d = XMLUnit.compareXML("<foo/>", "<foo/>"); + XMLAssert.assertXMLEqual("msg", d, true); + try { + XMLAssert.assertXMLEqual("msg", d, false); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertXMLEqualStringDiffBooleanWithDiff() throws Exception { + Diff d = XMLUnit.compareXML("<foo/>", "<bar/>"); + XMLAssert.assertXMLEqual("msg", d, false); + try { + XMLAssert.assertXMLEqual("msg", d, true); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertXMIdenticalDiffBooleanWithMatch() throws Exception { + Diff d = XMLUnit.compareXML("<foo/>", "<foo/>"); + XMLAssert.assertXMLIdentical(d, true); + try { + XMLAssert.assertXMLIdentical(d, false); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertXMIdenticalDiffBooleanWithDiff() throws Exception { + Diff d = XMLUnit.compareXML("<foo/>", "<bar/>"); + XMLAssert.assertXMLIdentical(d, false); + try { + XMLAssert.assertXMLIdentical(d, true); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertXpathsNotEqual() throws Exception { + String xpathXML = "<foo><bar/></foo>"; + XMLAssert.assertXpathsNotEqual("/foo", + new InputSource(new StringReader(xpathXML)), + "/foo/bar", + new InputSource(new StringReader(xpathXML))); + } + + public void testAssertXpathValuesNotEqual() throws Exception { + String controlXML = "<foo>bar</foo>"; + try { + XMLAssert.assertXpathValuesNotEqual("/foo", controlXML, + "/foo", controlXML); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertXpathValuesNotEqualNoMatch() throws Exception { + String controlXML = "<foo>bar</foo>"; + try { + XMLAssert.assertXpathValuesNotEqual("/baz", controlXML, + "/baz", controlXML); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testXpathEvaluatesToQualifiedName() throws Exception { + String faultDocument = "<env:Envelope " + + "xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" + + "<env:Body><env:Fault><faultcode>env:Server</faultcode>" + + "<faultstring>marche pas</faultstring><detail/></env:Fault>" + + "</env:Body></env:Envelope>"; + Map namespaces = new HashMap(); + namespaces.put("env11", "http://schemas.xmlsoap.org/soap/envelope/"); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(namespaces)); + XMLAssert.assertXpathEvaluatesTo(QualifiedName.valueOf("env11:Server"), + "//env11:Envelope/env11:Body/" + + "env11:Fault/faultcode", + new InputSource(new StringReader(faultDocument))); + } + + public void testAssertNodeTestPassesWithMatch() throws Exception { + NodeTest test = new NodeTest(new StringReader(test_XMLTestCase.xpathValuesTestXML)); + NodeTester tester = new CountingNodeTester(1); + XMLAssert.assertNodeTestPasses(test, tester, new short[] { Node.TEXT_NODE }, true); + try { + XMLAssert.assertNodeTestPasses(test, tester, new short[] { Node.TEXT_NODE }, + false); + } catch (AssertionFailedError e) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testAssertNodeTestPassesWithDiff() throws Exception { + NodeTest test = new NodeTest(new StringReader(test_XMLTestCase.xpathValuesTestXML)); + NodeTester tester = new CountingNodeTester(1); + XMLAssert.assertNodeTestPasses(test, tester, new short[] { Node.ELEMENT_NODE }, + false); + try { + XMLAssert.assertNodeTestPasses(test, tester, new short[] { Node.ELEMENT_NODE }, + true); + } catch (AssertionFailedError e) { + // expected + return; + } + fail("should have thrown an exception"); + } +} Property changes on: trunk/tests/java/org/custommonkey/xmlunit/test_XMLAssert.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2011,2014 Jeff Martin, Tim Bacon +Copyright (c) 2001-2011,2014-2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,6 +47,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; +import org.xml.sax.InputSource; /** * Test case used to test the XMLTestCase @@ -99,6 +100,78 @@ } /** + * Test for the compareXML method for readers. + */ + public void testCompareXMLReaders() throws Exception { + for(int i=0;i<control.length;i++){ + assertEquals("compareXML case " + i + " failed", true, + compareXML(new StringReader(control[i]), + new StringReader(control[i])).similar()); + assertEquals("!compareXML case " + i + " failed", false, + compareXML(new StringReader(control[i]), + new StringReader(test[i])).similar()); + } + } + + /** + * Test for the compareXML method for InputSources. + */ + public void testCompareXMLInputSources() throws Exception { + for(int i=0;i<control.length;i++){ + assertEquals("compareXML case " + i + " failed", true, + compareXML(new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(control[i]))) + .similar()); + assertEquals("!compareXML case " + i + " failed", false, + compareXML(new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(test[i]))) + .similar()); + } + } + + /** + * Test for the compareXML method for Reader/String combination. + */ + public void testCompareXMLStringReader() throws Exception { + for(int i=0;i<control.length;i++){ + assertEquals("compareXML case " + i + " failed", true, + compareXML(new StringReader(control[i]), + control[i]).similar()); + assertEquals("!compareXML case " + i + " failed", false, + compareXML(new StringReader(control[i]), + test[i]).similar()); + } + } + + /** + * Test for the compareXML method for String/Reader combination. + */ + public void testCompareXMLReaderString() throws Exception { + for(int i=0;i<control.length;i++){ + assertEquals("compareXML case " + i + " failed", true, + compareXML(control[i], + new StringReader(control[i])).similar()); + assertEquals("!compareXML case " + i + " failed", false, + compareXML(control[i], + new StringReader(test[i])).similar()); + } + } + + /** + * Test for the compareXML method for Documents. + */ + public void testCompareXMLDocumentss() throws Exception { + for(int i=0;i<control.length;i++){ + Document controlDocument = XMLUnit.buildControlDocument(control[i]); + assertEquals("compareXML case " + i + " failed", true, + compareXML(controlDocument, controlDocument).similar()); + Document testDocument = XMLUnit.buildTestDocument(test[i]); + assertEquals("!compareXML case " + i + " failed", false, + compareXML(controlDocument, testDocument).similar()); + } + } + + /** * Test the comparision of two files */ public void testXMLEqualsFiles() throws Exception { @@ -129,36 +202,149 @@ } /** - * Test for the assertXMLEquals method. + * Test for the assertXMLEqual method. */ public void testXMLEqualsStrings() throws Exception { for(int i=0;i<control.length;i++){ - assertXMLEqual("assertXMLEquals test case " + i + " failed", + assertXMLEqual("assertXMLEqual test case " + i + " failed", control[i], control[i]); - assertXMLNotEqual("assertXMLNotEquals test case" + i + " failed", + assertXMLNotEqual("assertXMLNotEqual test case" + i + " failed", control[i], test[i]); } } /** - * Test for the assertXMLEquals method. + * Test for the assertXMLEqual method for String and InputSources. */ - public void testXMLEqualsDocuments() throws Exception { + public void testXMLEqualsStringInputSources() throws Exception { + for(int i=0;i<control.length;i++){ + assertXMLEqual("assertXMLEqual test case " + i + " failed", + new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(control[i]))); + assertXMLNotEqual("assertXMLNotEqual test case" + i + " failed", + new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(test[i]))); + } + } + + /** + * Test for the assertXMLEqual method for InputSources. + */ + public void testXMLEqualsInputSources() throws Exception { + for(int i=0;i<control.length;i++){ + assertXMLEqual(new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(control[i]))); + assertXMLNotEqual(new InputSource(new StringReader(control[i])), + new InputSource(new StringReader(test[i]))); + } + } + + /** + * Test for the assertXMLEqual method. + */ + public void testXMLEqualsStringDocuments() throws Exception { Document controlDocument, testDocument; for(int i=0;i<control.length;i++){ controlDocument = XMLUnit.buildControlDocument(control[i]); - assertXMLEqual("assertXMLEquals test case " + i + " failed", + assertXMLEqual("assertXMLEqual test case " + i + " failed", controlDocument, controlDocument); testDocument = XMLUnit.buildTestDocument(test[i]); - assertXMLNotEqual("assertXMLNotEquals test case" + i + " failed", + assertXMLNotEqual("assertXMLNotEqual test case" + i + " failed", controlDocument, testDocument); } } + /** + * Test for the assertXMLEqual method. + */ + public void testXMLEqualsDocuments() throws Exception { + Document controlDocument, testDocument; + for(int i=0;i<control.length;i++){ + controlDocument = XMLUnit.buildControlDocument(control[i]); + assertXMLEqual(controlDocument, controlDocument); + testDocument = XMLUnit.buildTestDocument(test[i]); + assertXMLNotEqual(controlDocument, testDocument); + } + } + + /** + * Test for the assertXMLEqual method with Diff and boolean. + */ + public void testXMLEqualDiffBoolean() throws Exception { + for(int i=0;i<control.length;i++){ + Diff d = compareXML(control[i], control[i]); + assertXMLEqual(d, true); + d = compareXML(control[i], test[i]); + assertXMLEqual(d, false); + } + } + + /** + * Test for the assertXMLEqual method with String, Diff and boolean. + */ + public void testXMLEqualStringDiffBoolean() throws Exception { + for(int i=0;i<control.length;i++){ + Diff d = compareXML(control[i], control[i]); + assertXMLEqual("assertXMLEqual test case " + i + " failed", + d, true); + d = compareXML(control[i], test[i]); + assertXMLEqual("assertXMLEqual test case " + i + " failed", + d, false); + } + } + + public void testXMLEqualThrows() throws Exception { + Diff d = compareXML(control[0], test[0]); + try { + assertXMLEqual("should throw exception", d, true); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + public void testXMLIdenticalThrows() throws Exception { + Diff d = compareXML(control[0], test[0]); + try { + assertXMLIdentical("should throw exception", d, true); + } catch (AssertionFailedError f) { + // expected + return; + } + fail("should have thrown an exception"); + } + + /** + * Test for the assertXMLIdentical method with Diff and boolean. + */ + public void testXMLIdenticalDiffBoolean() throws Exception { + for(int i=0;i<control.length;i++){ + Diff d = compareXML(control[i], control[i]); + assertXMLIdentical(d, true); + d = compareXML(control[i], test[i]); + assertXMLIdentical(d, false); + } + } + + /** + * Test for the assertXMLIdentical method with String, Diff and boolean. + */ + public void testXMLIdenticalStringDiffBoolean() throws Exception { + for(int i=0;i<control.length;i++){ + Diff d = compareXML(control[i], control[i]); + assertXMLIdentical("assertXMLIdentical test case " + i + " failed", + d, true); + d = compareXML(control[i], test[i]); + assertXMLIdentical("assertXMLIdentical test case " + i + " failed", + d, false); + } + } + private static final String xpathValuesControlXML = "<root><outer attr=\"urk\"><inner attr=\"urk\">" + "controlDocument</inner></outer></root>"; - private static final String xpathValuesTestXML = + static final String xpathValuesTestXML = "<root><outer attr=\"urk\"><inner attr=\"ugh\">" + "testDocument</inner></outer></root>"; private static final String xpathValuesControlXMLNS = @@ -231,6 +417,22 @@ "//text()", xpathValuesTestXMLNS); } + public void testXpathValuesEqualUsingInputSource() throws Exception { + assertXpathValuesEqual("//text()", "//inner/text()", + new InputSource(new StringReader(xpathValuesControlXML))); + assertXpathValuesEqual("//inner/@attr", + new InputSource(new StringReader(xpathValuesControlXML)), + "//outer/@attr", + new InputSource(new StringReader(xpathValuesTestXML))); + + assertXpathValuesNotEqual("//inner/text()", "//outer/@attr", + new InputSource(new StringReader(xpathValuesControlXML))); + assertXpathValuesNotEqual("//inner/text()", + new InputSource(new StringReader(xpathValuesControlXML)), + "//text()", + new InputSource(new StringReader(xpathValuesTestXML))); + } + public void testXpathEvaluatesTo() throws Exception { assertXpathEvaluatesTo("urk", "//outer/@attr", xpathValuesControlXML); try { @@ -248,6 +450,15 @@ } catch (AssertionFailedError e) { } + assertXpathEvaluatesTo("ugh", "//inner/@attr", + new InputSource(new StringReader(xpathValuesTestXML))); + try { + assertXpathEvaluatesTo("yeah", "//outer/@attr", + new InputSource(new StringReader(xpathValuesTestXML))); + fail("Expected assertion to fail #3"); + } catch (AssertionFailedError e) { + } + } public void testXpathEvaluatesToNS() throws Exception { @@ -308,8 +519,16 @@ } } - public void testXMLValid() { - // see test_Validator class + public void testNodeTestInputSource() throws Exception { + NodeTester tester = new CountingNodeTester(1); + assertNodeTestPasses(new InputSource(new StringReader(xpathValuesControlXML)), + tester, Node.TEXT_NODE); + try { + assertNodeTestPasses(new InputSource(new StringReader(xpathValuesControlXML)), + tester, Node.ELEMENT_NODE); + fail("Expected node test failure #1!"); + } catch (AssertionFailedError e) { + } } private static final String TREES_OPEN = "<trees>"; @@ -358,6 +577,10 @@ testXpath[i], testDoc); assertXpathsEqual(controlXpath[i], xpathNodesControlXML, testXpath[i], xpathNodesTestXML); + assertXpathsEqual(controlXpath[i], + new InputSource(new StringReader(xpathNodesControlXML)), + testXpath[i], + new InputSource(new StringReader(xpathNodesTestXML))); assertXpathsEqual(controlXpath[i], testXpath[i], controlDoc); assertXpathsEqual(controlXpath[i], testXpath[i], xpathNodesControlXML); } @@ -376,6 +599,14 @@ } catch (AssertionFailedError e) { } try { + assertXpathsNotEqual(controlXpath[i], + new InputSource(new StringReader(xpathNodesControlXML)), + testXpath[i], + new InputSource(new StringReader(xpathNodesTestXML))); + fail("should not be notEqual!"); + } catch (AssertionFailedError e) { + } + try { assertXpathsNotEqual(controlXpath[i], testXpath[i], controlDoc); fail("should not be notEqual!"); } catch (AssertionFailedError e) { @@ -460,6 +691,20 @@ } } + public void testInputSourceAssertXpathExists() throws Exception { + assertXpathExists("/trees/fruit/apples/yum", + new InputSource(new StringReader(xpathNodesControlXML))); + assertXpathExists("//tree[@evergreen='false']", + new InputSource(new StringReader(xpathNodesControlXML))); + try { + assertXpathExists("//tree[@evergreen='idunno']", + new InputSource(new StringReader(xpathNodesControlXML))); + fail("Xpath does not exist"); + } catch (AssertionFailedError e) { + // expected + } + } + public void testDocumentAssertNotXpathExists() throws Exception { Document controlDoc = XMLUnit.buildControlDocument(xpathNodesControlXML); assertXpathNotExists("//tree[@evergreen='idunno']", controlDoc); @@ -493,6 +738,25 @@ } } + public void testInputSourceAssertNotXpathExists() throws Exception { + assertXpathNotExists("//tree[@evergreen='idunno']", + new InputSource(new StringReader(xpathNodesControlXML))); + try { + assertXpathNotExists("/trees/fruit/apples/yum", + new InputSource(new StringReader(xpathNodesControlXML))); + fail("Xpath does exist, once"); + } catch (AssertionFailedError e) { + // expected + } + try { + assertXpathNotExists("//tree[@evergreen='false']", + new InputSource(new StringReader(xpathNodesControlXML))); + fail("Xpath does exist many times"); + } catch (AssertionFailedError e) { + // expected + } + } + // Bug 585555 public void testUnusedNamespacesDontMatter() throws Exception { @@ -583,6 +847,15 @@ "<foo><Bar a=\"1\" b=\"2\"/></foo>"); } + public void testAssertXpathEqualsInputSource() throws Exception { + InputSource control = new InputSource(new StringReader("<foo><Bar a=\"1\" /></foo>")); + assertXpathsNotEqual("/foo/Bar/@a", "/foo/Bar", control); + control = new InputSource(new StringReader("<foo><Bar a=\"1\" b=\"1\"/></foo>")); + assertXpathsNotEqual("/foo/Bar/@a", "/foo/Bar/@b", control); + control = new InputSource(new StringReader("<foo><Bar a=\"1\" b=\"2\"/></foo>")); + assertXpathsEqual("/foo/Bar/@a", "/foo/Bar/@a", control); + } + // https://sourceforge.net/p/xmlunit/feature-requests/25/ public void testXpathEvaluatesToQualifiedName() throws Exception { String faultDocument = "<env:Envelope " @@ -599,10 +872,6 @@ faultDocument); } - public test_XMLTestCase(String name) { - super(name); - } - private static String addNamespaceToDocument(String original) { int pos = original.indexOf(">"); return original.substring(0, pos) + " xmlns='" + TEST_NS + "'" Modified: trunk/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java 2015-01-13 19:42:26 UTC (rev 592) +++ trunk/tests/java/org/custommonkey/xmlunit/test_XMLUnit.java 2015-03-08 05:59:53 UTC (rev 593) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007,2015 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -135,4 +135,24 @@ XMLUnit.setXSLTVersion("1.0"); } } + + public void testCantSetNullControlDocumentBuilderFactory() throws Exception { + try { + XMLUnit.setControlDocumentBuilderFactory(null); + fail("should have thrown an exception"); + } catch (IllegalArgumentException f) { + // expected + return; + } + } + + public void testCantSetNullTestDocumentBuilderFactory() throws Exception { + try { + XMLUnit.setTestDocumentBuilderFactory(null); + fail("should have thrown an exception"); + } catch (IllegalArgumentException f) { + // expected + return; + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |