From: <bo...@us...> - 2010-07-06 10:43:46
|
Revision: 410 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=410&view=rev Author: bodewig Date: 2010-07-06 10:43:39 +0000 (Tue, 06 Jul 2010) Log Message: ----------- tests for document and document type level comparisions Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java Added Paths: ----------- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/NullNode.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java 2010-06-29 04:26:39 UTC (rev 409) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java 2010-07-06 10:43:39 UTC (rev 410) @@ -53,6 +53,7 @@ case NAMESPACE_PREFIX: case ATTR_VALUE_EXPLICITLY_SPECIFIED: case CHILD_NODELIST_SEQUENCE: + case XML_ENCODING: outcome = ComparisonResult.SIMILAR; break; } Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/NullNode.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/NullNode.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/NullNode.java 2010-07-06 10:43:39 UTC (rev 410) @@ -0,0 +1,133 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit; + +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.UserDataHandler; + +public class NullNode implements Node { + public Node appendChild(Node n) { + return n; + } + public Node cloneNode(boolean deep) { + return this; + } + public short compareDocumentPosition(Node other) { + return 0; + } + public NamedNodeMap getAttributes() { + return null; + } + public String getBaseURI() { + return null; + } + public NodeList getChildNodes() { + return new NodeList() { + public int getLength() { + return 0; + } + public Node item(int idx) { + throw new IndexOutOfBoundsException(); + } + }; + } + public Object getFeature(String f, String v) { + return null; + } + public Node getFirstChild() { + return null; + } + public Node getLastChild() { + return null; + } + public String getLocalName() { + return null; + } + public String getNamespaceURI() { + return null; + } + public Node getNextSibling() { + return null; + } + public String getNodeName() { + return null; + } + public short getNodeType() { + return 0; + } + public String getNodeValue() { + return null; + } + public Document getOwnerDocument() { + return null; + } + public Node getParentNode() { + return null; + } + public String getPrefix() { + return null; + } + public Node getPreviousSibling() { + return null; + } + public String getTextContent() { + return null; + } + public Object getUserData(String key) { + return null; + } + public boolean hasAttributes() { + return false; + } + public boolean hasChildNodes() { + return false; + } + public Node insertBefore(Node n, Node r) { + return n; + } + public boolean isDefaultNamespace(String u) { + return false; + } + public boolean isEqualNode(Node n) { + return isSameNode(n); + } + public boolean isSameNode(Node n) { + return this == n; + } + public boolean isSupported(String f, String v) { + return false; + } + public String lookupNamespaceURI(String s) { + return null; + } + public String lookupPrefix(String s) { + return null; + } + public void normalize() { } + public Node removeChild(Node n) { + return n; + } + public Node replaceChild(Node n, Node o) { + return o; + } + public void setNodeValue(String s) { } + public void setPrefix(String s) { } + public void setTextContent(String s) { } + public Object setUserData(String k, Object d, UserDataHandler h) { + return null; + } +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/NullNode.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java 2010-06-29 04:26:39 UTC (rev 409) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/TestResources.java 2010-07-06 10:43:39 UTC (rev 410) @@ -19,6 +19,8 @@ public static final String ANIMAL_XSL = "src/tests/resources/animal.xsl"; public static final String DOG_FILE = "src/tests/resources/testAnimal.xml"; + public static final String BOOK_DTD = "src/tests/resources/Book.dtd"; + private TestResources() { } } Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java 2010-06-29 04:26:39 UTC (rev 409) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java 2010-07-06 10:43:39 UTC (rev 410) @@ -15,12 +15,18 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +import net.sf.xmlunit.NullNode; +import net.sf.xmlunit.TestResources; +import net.sf.xmlunit.builder.Input; +import net.sf.xmlunit.util.Convert; import org.junit.Before; import org.junit.Test; 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.NamedNodeMap; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; import static org.junit.Assert.*; @@ -216,4 +222,150 @@ d.nodeTypeSpecificComparison(foo1, foo2)); assertEquals(1, ex.invoked); } + + @Test public void compareDocuments() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + DiffExpecter ex = new DiffExpecter(ComparisonType.HAS_DOCTYPE_DECLARATION); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (comparison.getType() + == ComparisonType.HAS_DOCTYPE_DECLARATION) { + assertEquals(ComparisonResult.DIFFERENT, outcome); + return ComparisonResult.CRITICAL; + } + assertEquals(ComparisonResult.EQUAL, outcome); + return ComparisonResult.EQUAL; + } + }); + Document d1 = Convert.toDocument(Input.fromMemory("<Book/>").build()); + Document d2 = + Convert.toDocument(Input.fromMemory("<!DOCTYPE Book PUBLIC " + + "\"XMLUNIT/TEST/PUB\" " + + "\"" + TestResources.BOOK_DTD + + "\">" + + "<Book/>") + .build()); + assertEquals(ComparisonResult.CRITICAL, d.compareDocuments(d1, d2)); + assertEquals(1, ex.invoked); + + d = new DOMDifferenceEngine(); + ex = new DiffExpecter(ComparisonType.XML_VERSION); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(DifferenceEvaluators.DefaultStopWhenDifferent); + + d1 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.0\"" + + " encoding=\"UTF-8\"?>" + + "<Book/>").build()); + d2 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.1\"" + + " encoding=\"UTF-8\"?>" + + "<Book/>").build()); + assertEquals(ComparisonResult.CRITICAL, d.compareDocuments(d1, d2)); + assertEquals(1, ex.invoked); + + d = new DOMDifferenceEngine(); + ex = new DiffExpecter(ComparisonType.XML_STANDALONE); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(DifferenceEvaluators.DefaultStopWhenDifferent); + + d1 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.0\"" + + " standalone=\"yes\"?>" + + "<Book/>").build()); + d2 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.0\"" + + " standalone=\"no\"?>" + + "<Book/>").build()); + assertEquals(ComparisonResult.CRITICAL, d.compareDocuments(d1, d2)); + assertEquals(1, ex.invoked); + + d = new DOMDifferenceEngine(); + ex = new DiffExpecter(ComparisonType.XML_ENCODING); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (comparison.getType() + == ComparisonType.XML_ENCODING) { + assertEquals(ComparisonResult.DIFFERENT, outcome); + return ComparisonResult.CRITICAL; + } + assertEquals(ComparisonResult.EQUAL, outcome); + return ComparisonResult.EQUAL; + } + }); + + d1 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.0\"" + + " encoding=\"UTF-8\"?>" + + "<Book/>").build()); + d2 = Convert.toDocument(Input.fromMemory("<?xml version=\"1.0\"" + + " encoding=\"UTF-16\"?>" + + "<Book/>").build()); + assertEquals(ComparisonResult.CRITICAL, d.compareDocuments(d1, d2)); + assertEquals(1, ex.invoked); + } + + private static class DocType extends NullNode implements DocumentType { + private final String name, publicId, systemId; + private DocType(String name, String publicId, String systemId) { + this.name = name; + this.publicId = publicId; + this.systemId = systemId; + } + public NamedNodeMap getEntities() { + return null; + } + public String getInternalSubset() { + return null; + } + public String getName() { + return name; + } + public NamedNodeMap getNotations() { + return null; + } + public String getPublicId() { + return publicId; + } + public String getSystemId() { + return systemId; + } + } + + @Test public void compareDocTypes() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + DiffExpecter ex = new DiffExpecter(ComparisonType.DOCTYPE_NAME); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(DifferenceEvaluators.DefaultStopWhenDifferent); + DocumentType dt1 = new DocType("name", "pub", "system"); + DocumentType dt2 = new DocType("name2", "pub", "system"); + assertEquals(ComparisonResult.CRITICAL, d.compareDocTypes(dt1, dt2)); + assertEquals(1, ex.invoked); + + d = new DOMDifferenceEngine(); + ex = new DiffExpecter(ComparisonType.DOCTYPE_PUBLIC_ID); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(DifferenceEvaluators.DefaultStopWhenDifferent); + dt2 = new DocType("name", "pub2", "system"); + assertEquals(ComparisonResult.CRITICAL, d.compareDocTypes(dt1, dt2)); + assertEquals(1, ex.invoked); + + d = new DOMDifferenceEngine(); + ex = new DiffExpecter(ComparisonType.DOCTYPE_SYSTEM_ID); + d.addDifferenceListener(ex); + d.setDifferenceEvaluator(new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (comparison.getType() + == ComparisonType.DOCTYPE_SYSTEM_ID) { + assertEquals(ComparisonResult.DIFFERENT, outcome); + return ComparisonResult.CRITICAL; + } + assertEquals(ComparisonResult.EQUAL, outcome); + return ComparisonResult.EQUAL; + } + }); + dt2 = new DocType("name", "pub", "system2"); + assertEquals(ComparisonResult.CRITICAL, d.compareDocTypes(dt1, dt2)); + assertEquals(1, ex.invoked); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |