From: <bo...@us...> - 2007-05-16 11:25:24
|
Revision: 209 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=209&view=rev Author: bodewig Date: 2007-05-16 04:25:23 -0700 (Wed, 16 May 2007) Log Message: ----------- Treat xsi:schemaLocation and xsi:noNamespaceSchemaLocation in a special way Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/JAXPConstants.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLConstants.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Diff.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java 2007-05-16 11:25:23 UTC (rev 209) @@ -87,6 +87,16 @@ /** Comparing 2 nodes and one holds more childnodes than can be * matched against child nodes of the other. */ int CHILD_NODE_NOT_FOUND_ID = 22; + /** Comparing 2 nodes with different xsi:schemaLocation + * attributes, potentially only one of the two provides such an + * attribute at all. + */ + int SCHEMA_LOCATION_ID = 23; + /** Comparing 2 nodes with different xsi:noNamespaceSchemaLocation + * attributes, potentially only one of the two provides such an + * attribute at all. + */ + int NO_NAMESPACE_SCHEMA_LOCATION_ID = 24; /** Comparing an implied attribute value against an explicit value */ public static final Difference ATTR_VALUE_EXPLICITLY_SPECIFIED = @@ -181,4 +191,20 @@ * matched against child nodes of the other. */ public static final Difference CHILD_NODE_NOT_FOUND = new Difference(CHILD_NODE_NOT_FOUND_ID, "presence of child node"); + + /** Comparing 2 nodes with different xsi:schemaLocation + * attributes, potentially only one of the two provides such an + * attribute at all. + */ + public static final Difference SCHEMA_LOCATION = + new Difference(SCHEMA_LOCATION_ID, "xsi:schemaLocation attribute", + true); + /** Comparing 2 nodes with different xsi:noNamespaceSchemaLocation + * attributes, potentially only one of the two provides such an + * attribute at all. + */ + public static final Difference NO_NAMESPACE_SCHEMA_LOCATION = + new Difference(NO_NAMESPACE_SCHEMA_LOCATION_ID, + "xsi:noNamespaceSchemaLocation attribute", + true); } \ No newline at end of file Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-05-16 11:25:23 UTC (rev 209) @@ -453,9 +453,10 @@ control, test, listener, ELEMENT_TAG_NAME); NamedNodeMap controlAttr = control.getAttributes(); - Integer controlNonXmlnsAttrLength = getNonXmlnsAttrLength(controlAttr); + Integer controlNonXmlnsAttrLength = + getNonSpecialAttrLength(controlAttr); NamedNodeMap testAttr = test.getAttributes(); - Integer testNonXmlnsAttrLength = getNonXmlnsAttrLength(testAttr); + Integer testNonXmlnsAttrLength = getNonSpecialAttrLength(testAttr); compare(controlNonXmlnsAttrLength, testNonXmlnsAttrLength, control, test, listener, ELEMENT_NUM_ATTRIBUTES); @@ -463,10 +464,16 @@ listener); } - private Integer getNonXmlnsAttrLength(NamedNodeMap attributes) { + /** + * The number of attributes not related to namespace declarations + * and/or Schema location. + */ + private Integer getNonSpecialAttrLength(NamedNodeMap attributes) { int length = 0, maxLength = attributes.getLength(); for (int i = 0; i < maxLength; ++i) { - if (!isXMLNSAttribute((Attr) attributes.item(i))) { + Attr a = (Attr) attributes.item(i); + if (!isXMLNSAttribute(a) + && !isRecognizedXMLSchemaInstanceAttribute(a)) { ++length; } } @@ -493,8 +500,13 @@ } else { compareTo = (Attr) testAttr.getNamedItem(attrName); } - - if (compareTo != null) { + + if (isRecognizedXMLSchemaInstanceAttribute(nextAttr)) { + compareRecognizedXMLSchemaInstanceAttribute(nextAttr, + compareTo, + listener); + + } else if (compareTo != null) { compareAttribute(nextAttr, compareTo, listener); if (!XMLUnit.getIgnoreAttributeOrder()) { @@ -539,12 +551,54 @@ } /** + * @param attr + * @return true if the attribute is an XML Schema Instance + * namespace attribute XMLUnit treats in a special way. + */ + private boolean isRecognizedXMLSchemaInstanceAttribute(Attr attr) { + return XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(attr.getNamespaceURI()) + && (XMLConstants + .W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR + .equals(attr.getLocalName()) + || XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR + .equals(attr.getLocalName())); + } + + /** * Compare two attributes * @param control * @param test * @param listener * @throws DifferenceFoundException */ + protected void compareRecognizedXMLSchemaInstanceAttribute(Attr control, + Attr test, + DifferenceListener listener) + throws DifferenceFoundException { + Difference d = + XMLConstants.W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR + .equals(control.getLocalName()) + ? SCHEMA_LOCATION : NO_NAMESPACE_SCHEMA_LOCATION; + + controlTracker.visited(control); + if (test != null) { + testTracker.visited(test); + } + + compare(control.getValue(), + test != null ? test.getValue() : "[not specified]", + control, test, listener, d); + } + + /** + * Compare two attributes + * @param control + * @param test + * @param listener + * @throws DifferenceFoundException + */ protected void compareAttribute(Attr control, Attr test, DifferenceListener listener) throws DifferenceFoundException { controlTracker.visited(control); Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/JAXPConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/JAXPConstants.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/JAXPConstants.java 2007-05-16 11:25:23 UTC (rev 209) @@ -1,6 +1,6 @@ /* ****************************************************************** - Copyright (c) 200, Jeff Martin, Tim Bacon + Copyright (c) 2001-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLConstants.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLConstants.java 2007-05-16 11:25:23 UTC (rev 209) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001, Jeff Martin, Tim Bacon +Copyright (c) 2001-2007 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -139,4 +139,22 @@ */ public static final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema"; + + /** + * http://www.w3.org/2001/XMLSchema-instance + */ + public static final String W3C_XML_SCHEMA_INSTANCE_NS_URI + = "http://www.w3.org/2001/XMLSchema-instance"; + + /** + * "schemaLocation" + */ + public static final String W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR + = "schemaLocation"; + + /** + * "noNamespaceSchemaLocation" + */ + String W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR + = "noNamespaceSchemaLocation"; } Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Diff.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Diff.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Diff.java 2007-05-16 11:25:23 UTC (rev 209) @@ -683,4 +683,27 @@ } } + /** + * inspired by {@link + * http://day-to-day-stuff.blogspot.com/2007/05/comparing-xml-in-junit-test.html + * Erik von Oosten's Weblog }, made us implement special handling + * of schemaLocation. + */ + public void testNamespacePrefixDiff() throws Exception { + String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<Message xmlns=\"http://www.a.nl/a10.xsd\"" + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + + " xsi:schemaLocation=\"C:/longpath/a10.xsd\"" + + ">" + + "<MessageHeader/>" + + "</Message>"; + String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + + "<a:Message xmlns:a=\"http://www.a.nl/a10.xsd\">" + + "<a:MessageHeader/>" + + "</a:Message>"; + Diff d = buildDiff(xml1, xml2); + assertFalse(d.toString(), d.identical()); + assertTrue(d.toString(), d.similar()); + } + } Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-05-12 19:54:41 UTC (rev 208) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-05-16 11:25:23 UTC (rev 209) @@ -780,6 +780,57 @@ assertEquals(expectDifference, listener.different); } + public void testMissingSchemaLocation() throws Exception { + testMissingXSIAttribute(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR, + DifferenceConstants.SCHEMA_LOCATION_ID); + } + + public void testMissingNoNamespaceSchemaLocation() throws Exception { + testMissingXSIAttribute(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR, + DifferenceConstants.NO_NAMESPACE_SCHEMA_LOCATION_ID); + } + + private void testMissingXSIAttribute(String attrName, + int expectedDifference) + throws Exception { + Element control = document.createElement("foo"); + control.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, + attrName, "bar"); + Element test = document.createElement("foo"); + engine.compare(control, test, listener, null); + assertEquals(expectedDifference, listener.comparingWhat); + //resetListener(); + //engine.compare(test, control, listener, null); + //assertEquals(expectedDifference, listener.comparingWhat); + } + + public void testDifferentSchemaLocation() throws Exception { + testDifferentXSIAttribute(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_SCHEMA_LOCATION_ATTR, + DifferenceConstants.SCHEMA_LOCATION_ID); + } + + public void testDifferentNoNamespaceSchemaLocation() throws Exception { + testDifferentXSIAttribute(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NO_NAMESPACE_SCHEMA_LOCATION_ATTR, + DifferenceConstants.NO_NAMESPACE_SCHEMA_LOCATION_ID); + } + + private void testDifferentXSIAttribute(String attrName, + int expectedDifference) + throws Exception { + Element control = document.createElement("foo"); + control.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, + attrName, "bar"); + Element test = document.createElement("foo"); + test.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, + attrName, "baz"); + engine.compare(control, test, listener, null); + assertEquals(expectedDifference, listener.comparingWhat); + } + private void listenToDifferences(String control, String test) throws SAXException, IOException { Document controlDoc = XMLUnit.buildControlDocument(control); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |