From: <bo...@us...> - 2010-08-19 14:04:23
|
Revision: 429 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=429&view=rev Author: bodewig Date: 2010-08-19 14:04:16 +0000 (Thu, 19 Aug 2010) Log Message: ----------- treat XML NS Declarations and XML Schema Instance Namespaces differently Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-18 14:45:50 UTC (rev 428) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-08-19 14:04:16 UTC (rev 429) @@ -17,9 +17,12 @@ summary="Difference engine based on DOM." extends="AbstractDifferenceEngine"> + <import reference="java.util.HashSet"/> + <import reference="java.util.LinkedList"/> <import reference="java.util.List"/> <import reference="java.util.Set"/> <import reference="java.util.TreeSet"/> + <import reference="javax.xml.XMLConstants"/> <import reference="javax.xml.transform.Source"/> <import reference="net.sf.xmlunit.util.Convert"/> <import reference="net.sf.xmlunit.util.IterableNodeList"/> @@ -180,51 +183,41 @@ <lastResultDef/> <compare type="ELEMENT_TAG_NAME" property="getTagName()"/> <literal><![CDATA[ - NamedNodeMap controlAttributes = control.getAttributes(); - NamedNodeMap testAttributes = test.getAttributes(); - final int controlAttrLen = controlAttributes.getLength(); - final int testAttrLen = testAttributes.getLength(); + Attributes controlAttributes = splitAttributes(control.getAttributes()); + Attributes testAttributes = splitAttributes(test.getAttributes()); + Set<Attr> foundTestAttributes = new HashSet<Attr>(); ]]></literal> <compareExpr type="ELEMENT_NUM_ATTRIBUTES" - controlExpr="controlAttrLen" - testExpr="testAttrLen"/> + controlExpr="controlAttributes.remainingAttributes.size()" + testExpr="testAttributes.remainingAttributes.size()"/> <literal><![CDATA[ - for (int i = 0; i < controlAttrLen; i++) { - final Attr controlAttr = (Attr) controlAttributes.item(i); - final Attr testAttr = findMatchingAttr(testAttributes, controlAttr); + for (Attr controlAttr : controlAttributes.remainingAttributes) { + final Attr testAttr = + findMatchingAttr(testAttributes.remainingAttributes, + controlAttr); ]]></literal> <compareExpr type="ATTR_NAME_LOOKUP" controlExpr="Boolean.TRUE" testExpr="Boolean.valueOf(testAttr != null)"/> <literal><![CDATA[ - } -]]></literal> - <literal><![CDATA[ - for (int i = 0; i < testAttrLen; i++) { - final Attr testAttr = (Attr) testAttributes.item(i); - final Attr controlAttr = findMatchingAttr(controlAttributes, - testAttr); -]]></literal> - <compareExpr type="ATTR_NAME_LOOKUP" - controlExpr="Boolean.valueOf(controlAttr != null)" - testExpr="Boolean.TRUE"/> - <literal><![CDATA[ - } -]]></literal> - <literal><![CDATA[ - for (int i = 0; i < controlAttrLen; i++) { - final Attr controlAttr = (Attr) controlAttributes.item(i); - final Attr testAttr = findMatchingAttr(testAttributes, controlAttr); if (testAttr != null) { ]]></literal> <compareMethodExpr method="compareNodes" controlExpr="controlAttr" testExpr="testAttr"/> <literal><![CDATA[ + foundTestAttributes.add(testAttr); } } ]]></literal> <literal><![CDATA[ + for (Attr testAttr : testAttributes.remainingAttributes) { +]]></literal> + <compareExpr type="ATTR_NAME_LOOKUP" + controlExpr="Boolean.valueOf(foundTestAttributes.contains(testAttr))" + testExpr="Boolean.TRUE"/> + <literal><![CDATA[ + } return lastResult; } @@ -300,14 +293,58 @@ test, null, test.getValue())); } - private static Attr findMatchingAttr(final NamedNodeMap map, + private static Attributes splitAttributes(final NamedNodeMap map) { + Attr sLoc = (Attr) map.getNamedItemNS(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NS_URI, + "schemaLocation"); + Attr nNsLoc = (Attr) map.getNamedItemNS(XMLConstants + .W3C_XML_SCHEMA_INSTANCE_NS_URI, + "noNamespaceSchemaLocation"); + List<Attr> rest = new LinkedList<Attr>(); + final int len = map.getLength(); + for (int i = 0; i < len; i++) { + Attr a = (Attr) map.item(i); + if (!XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(a.getNamespaceURI()) + && + !XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI + .equals(a.getNamespaceURI())) { + rest.add(a); + } + } + return new Attributes(sLoc, nNsLoc, rest); + } + + private static class Attributes { + private final Attr schemaLocation; + private final Attr noNamespaceSchemaLocation; + private final List<Attr> remainingAttributes; + private Attributes(Attr schemaLocation, Attr noNamespaceSchemaLocation, + List<Attr> remainingAttributes) { + this.schemaLocation = schemaLocation; + this.noNamespaceSchemaLocation = noNamespaceSchemaLocation; + this.remainingAttributes = remainingAttributes; + } + } + + private static Attr findMatchingAttr(final List<Attr> attrs, final Attr attrToMatch) { - if (attrToMatch.getNamespaceURI() == null) { - return (Attr) map.getNamedItem(attrToMatch.getName()); - } else { - return (Attr) map.getNamedItemNS(attrToMatch.getNamespaceURI(), - attrToMatch.getLocalName()); + final boolean hasNs = attrToMatch.getNamespaceURI() != null; + final String nsToMatch = attrToMatch.getNamespaceURI(); + final String nameToMatch = hasNs ? attrToMatch.getLocalName() + : attrToMatch.getName(); + for (Attr a : attrs) { + if (((!hasNs && a.getNamespaceURI() == null) + || + (hasNs && nsToMatch.equals(a.getNamespaceURI()))) + && + ((hasNs && nameToMatch.equals(a.getLocalName())) + || + (!hasNs && nameToMatch.equals(a.getName()))) + ) { + return a; + } } + return null; } private Match findMatchingNode(final Node searchFor, Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-08-18 14:45:50 UTC (rev 428) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-08-19 14:04:16 UTC (rev 429) @@ -20,9 +20,12 @@ <import reference="System"/> <import reference="System.Collections.Generic"/> <import reference="System.Xml"/> + <import reference="System.Xml.Schema"/> <literal><![CDATA[ + private static readonly object DUMMY = new object(); + public override void Compare(ISource control, ISource test) { if (control == null) { throw new ArgumentNullException("control"); @@ -171,55 +174,46 @@ <lastResultDef/> <compare type="ELEMENT_TAG_NAME" property="Name"/> <literal><![CDATA[ - XmlAttributeCollection controlAttributes = control.Attributes; - XmlAttributeCollection testAttributes = test.Attributes; - int controlAttrLen = controlAttributes.Count; - int testAttrLen = testAttributes.Count; + Attributes controlAttributes = SplitAttributes(control.Attributes); + Attributes testAttributes = SplitAttributes(test.Attributes); + IDictionary<XmlAttribute, object> foundTestAttributes = + new Dictionary<XmlAttribute, object>(); ]]></literal> <compareExpr type="ELEMENT_NUM_ATTRIBUTES" - controlExpr="controlAttrLen" - testExpr="testAttrLen"/> + controlExpr="controlAttributes.RemainingAttributes.Count" + testExpr="testAttributes.RemainingAttributes.Count"/> <literal><![CDATA[ - for (int i = 0; i < controlAttrLen; i++) { - XmlAttribute controlAttr = controlAttributes[i]; - XmlAttribute testAttr = FindMatchingAttr(testAttributes, - controlAttr); + foreach (XmlAttribute controlAttr in controlAttributes.RemainingAttributes) { + XmlAttribute testAttr = + FindMatchingAttr(testAttributes.RemainingAttributes, + controlAttr); ]]></literal> <compareExpr type="ATTR_NAME_LOOKUP" controlExpr="true" testExpr="testAttr != null"/> <literal><![CDATA[ - } + if (testAttr != null) { ]]></literal> + <compareMethodExpr method="CompareNodes" + controlExpr="controlAttr" + testExpr="testAttr"/> <literal><![CDATA[ - for (int i = 0; i < testAttrLen; i++) { - XmlAttribute testAttr = testAttributes[i]; - XmlAttribute controlAttr = FindMatchingAttr(controlAttributes, - testAttr); + foundTestAttributes[testAttr] = DUMMY; + } + } ]]></literal> + <literal><![CDATA[ + foreach (XmlAttribute testAttr in testAttributes.RemainingAttributes) { +]]></literal> <compareExpr type="ATTR_NAME_LOOKUP" - controlExpr="controlAttr != null" + controlExpr="foundTestAttributes.ContainsKey(testAttr)" testExpr="true"/> <literal><![CDATA[ - } + } ]]></literal> <literal><![CDATA[ - for (int i = 0; i < controlAttrLen; i++) { - XmlAttribute controlAttr = controlAttributes[i]; - XmlAttribute testAttr = FindMatchingAttr(testAttributes, - controlAttr); - if (testAttr != null) { -]]></literal> - <compareMethodExpr method="CompareNodes" - controlExpr="controlAttr" - testExpr="testAttr"/> - <literal><![CDATA[ - } + return lastResult; } -]]></literal> - <literal><![CDATA[ - return lastResult; - } private ComparisonResult CompareProcessingInstructions(XmlProcessingInstruction control, @@ -233,8 +227,6 @@ test, null, test.Data)); } - private static readonly object DUMMY = new object(); - private ComparisonResult CompareNodeLists(XmlNodeList control, XmlNodeList test) { IList<XmlNode> controlList = @@ -299,15 +291,56 @@ test, null, test.Value)); } - private static XmlAttribute FindMatchingAttr(XmlAttributeCollection map, + private static Attributes SplitAttributes(XmlAttributeCollection map) { + XmlAttribute sLoc = map.GetNamedItem("schemaLocation", + XmlSchema.InstanceNamespace) + as XmlAttribute; + XmlAttribute nNsLoc = map.GetNamedItem("noNamespaceSchemaLocation", + XmlSchema.InstanceNamespace) + as XmlAttribute; + List<XmlAttribute> rest = new List<XmlAttribute>(); + foreach (XmlAttribute a in map) { + if (XmlSchema.InstanceNamespace != a.NamespaceURI + && + "http://www.w3.org/2000/xmlns/" != a.NamespaceURI) { + rest.Add(a); + } + } + return new Attributes(sLoc, nNsLoc, rest); + } + + internal class Attributes { + internal readonly XmlAttribute SchemaLocation; + internal readonly XmlAttribute NoNamespaceSchemaLocation; + internal readonly IList<XmlAttribute> RemainingAttributes; + internal Attributes(XmlAttribute schemaLocation, + XmlAttribute noNamespaceSchemaLocation, + IList<XmlAttribute> remainingAttributes) { + this.SchemaLocation = schemaLocation; + this.NoNamespaceSchemaLocation = noNamespaceSchemaLocation; + this.RemainingAttributes = remainingAttributes; + } + } + + private static XmlAttribute FindMatchingAttr(IList<XmlAttribute> attrs, XmlAttribute attrToMatch) { - if (attrToMatch.NamespaceURI == null) { - return map.GetNamedItem(attrToMatch.Name) as XmlAttribute; - } else { - return map.GetNamedItem(attrToMatch.LocalName, - attrToMatch.NamespaceURI) - as XmlAttribute; + bool hasNs = !string.IsNullOrEmpty(attrToMatch.NamespaceURI); + string nsToMatch = attrToMatch.NamespaceURI; + string nameToMatch = hasNs ? attrToMatch.LocalName + : attrToMatch.Name; + foreach (XmlAttribute a in attrs) { + if (((!hasNs && string.IsNullOrEmpty(a.NamespaceURI)) + || + (hasNs && nsToMatch == a.NamespaceURI)) + && + ((hasNs && nameToMatch == a.LocalName) + || + (!hasNs && nameToMatch == a.Name)) + ) { + return a; + } } + return null; } private Match FindMatchingNode(XmlNode searchFor, Modified: trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs =================================================================== --- trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs 2010-08-18 14:45:50 UTC (rev 428) +++ trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs 2010-08-19 14:04:16 UTC (rev 429) @@ -423,7 +423,8 @@ + " with outcome " + r + " and values '" + comp.ControlDetails.Value + "' and '" - + comp.TestDetails.Value + "'"); + + comp.TestDetails.Value + "'" + + " on '" + comp.ControlDetails.Target + "'"); }; d.DifferenceEvaluator = DifferenceEvaluators.DefaultStopWhenDifferent; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |