From: <bo...@us...> - 2010-09-10 15:11:06
|
Revision: 462 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=462&view=rev Author: bodewig Date: 2010-09-10 15:11:00 +0000 (Fri, 10 Sep 2010) Log Message: ----------- skip DocType (and XmlDeclaration) nodes in child lists since they have already been dealt with at the Document node Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml trunk/xmlunit/src/main/net-core/util/Linqy.cs trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_Diff.java 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-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java.xml 2010-09-10 15:11:00 UTC (rev 462) @@ -71,13 +71,17 @@ <compare type="NAMESPACE_URI" property="getNamespaceURI()"/> <compare type="NAMESPACE_PREFIX" property="getPrefix()"/> <literal><![CDATA[ - NodeList controlChildren = control.getChildNodes(); - NodeList testChildren = test.getChildNodes(); + Iterable<Node> controlChildren = + Linqy.filter(new IterableNodeList(control.getChildNodes()), + INTERESTING_NODES); + Iterable<Node> testChildren = + Linqy.filter(new IterableNodeList(test.getChildNodes()), + INTERESTING_NODES); if (control.getNodeType() != Node.ATTRIBUTE_NODE) { ]]></literal> <compareExpr type="CHILD_NODELIST_LENGTH" - controlExpr="controlChildren.getLength()" - testExpr="testChildren.getLength()"/> + controlExpr="Linqy.count(controlChildren)" + testExpr="Linqy.count(testChildren)"/> <literal><![CDATA[ } ]]></literal> @@ -85,11 +89,9 @@ <literal><![CDATA[ if (control.getNodeType() != Node.ATTRIBUTE_NODE) { controlContext - .setChildren(Linqy.map(new IterableNodeList(controlChildren), - TO_NODE_INFO)); + .setChildren(Linqy.map(controlChildren, TO_NODE_INFO)); testContext - .setChildren(Linqy.map(new IterableNodeList(testChildren), - TO_NODE_INFO)); + .setChildren(Linqy.map(testChildren, TO_NODE_INFO)); ]]></literal> <compareMethodExpr method="compareNodeLists" controlExpr="controlChildren" @@ -309,12 +311,12 @@ test.getData())); } - private ComparisonResult compareNodeLists(NodeList control, + private ComparisonResult compareNodeLists(Iterable<Node> controlSeq, XPathContext controlContext, - NodeList test, + Iterable<Node> testSeq, XPathContext testContext) { - List<Node> controlList = IterableNodeList.asList(control); - List<Node> testList = IterableNodeList.asList(test); + List<Node> controlList = Linqy.asList(controlSeq); + List<Node> testList = Linqy.asList(testSeq); final int testSize = testList.size(); Set<Integer> unmatchedTestIndexes = new TreeSet<Integer>(); for (int i = 0; i < testSize; i++) { @@ -336,14 +338,16 @@ ]]></literal> testContext.navigateToChild(testMatch.index); try { + Node control = controlList.get(i); + Node test = testMatch.node; Integer testIndex = Integer.valueOf(testMatch.index); <compareExpr type="CHILD_NODELIST_SEQUENCE" controlExpr="Integer.valueOf(i)" testExpr="testIndex" /> <compareMethodExpr method="compareNodes" - controlExpr="controlList.get(i)" - testExpr="testMatch.node"/> + controlExpr="control" + testExpr="test"/> <literal><![CDATA[ unmatchedTestIndexes.remove(testIndex); lastMatch = testMatch; @@ -503,5 +507,12 @@ return new XPathContext.DOMNodeInfo(n); } }; + + private static final Linqy.Predicate<Node> INTERESTING_NODES = + new Linqy.Predicate<Node>() { + public boolean matches(Node n) { + return n.getNodeType() != Node.DOCUMENT_TYPE_NODE; + } + }; ]]></literal> </class> \ No newline at end of file Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Linqy.java 2010-09-10 15:11:00 UTC (rev 462) @@ -59,6 +59,29 @@ T map(F from); } + public static <T> Iterable<T> filter(final Iterable<T> sequence, + final Predicate<? super T> filter) { + return new Iterable<T>() { + public Iterator<T> iterator() { + return new FilteringIterator<T>(sequence.iterator(), filter); + } + }; + } + + public interface Predicate<T> { + boolean matches(T toTest); + } + + public static int count(Iterable seq) { + int c = 0; + Iterator it = seq.iterator(); + while (it.hasNext()) { + c++; + it.next(); + } + return c; + } + private static class OnceOnlyIterator<E> implements Iterator<E> { private final E element; private boolean iterated = false; @@ -98,4 +121,34 @@ } } + private static class FilteringIterator<T> implements Iterator<T> { + private final Iterator<T> i; + private final Predicate<? super T> filter; + private T lookAhead = null; + private FilteringIterator(Iterator<T> i, Predicate<? super T> filter) { + this.i = i; + this.filter = filter; + } + public void remove() { + i.remove(); + } + public T next() { + if (lookAhead == null) { + throw new NoSuchElementException(); + } + T next = lookAhead; + lookAhead = null; + return next; + } + public boolean hasNext() { + while (lookAhead == null && i.hasNext()) { + T next = i.next(); + if (filter.matches(next)) { + lookAhead = next; + } + } + return lookAhead != null; + } + } + } \ No newline at end of file Modified: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.xml 2010-09-10 15:11:00 UTC (rev 462) @@ -61,28 +61,30 @@ <compare type="NAMESPACE_URI" property="NamespaceURI"/> <compare type="NAMESPACE_PREFIX" property="Prefix"/> <literal><![CDATA[ - XmlNodeList controlChildren = control.ChildNodes; - XmlNodeList testChildren = test.ChildNodes; + IEnumerable<XmlNode> controlChildren = + Linqy.Filter(Linqy.Cast<XmlNode>(control.ChildNodes), + INTERESTING_NODES); + IEnumerable<XmlNode> testChildren = + Linqy.Filter(Linqy.Cast<XmlNode>(test.ChildNodes), + INTERESTING_NODES); if (control.NodeType != XmlNodeType.Attribute) { ]]></literal> <compareExpr type="CHILD_NODELIST_LENGTH" - controlExpr="controlChildren.Count" - testExpr="testChildren.Count"/> + controlExpr="Linqy.Count(controlChildren)" + testExpr="Linqy.Count(testChildren)"/> <literal><![CDATA[ } ]]></literal> <compareMethod method="NodeTypeSpecificComparison"/> <literal><![CDATA[ if (control.NodeType != XmlNodeType.Attribute) { - IEnumerable<XmlNode> cc = Linqy.Cast<XmlNode>(controlChildren); controlContext .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(cc, + XPathContext.INodeInfo>(controlChildren, TO_NODE_INFO)); - IEnumerable<XmlNode> tc = Linqy.Cast<XmlNode>(testChildren); testContext .SetChildren(Linqy.Map<XmlNode, - XPathContext.INodeInfo>(tc, + XPathContext.INodeInfo>(testChildren, TO_NODE_INFO)); ]]></literal> <compareMethodExpr method="CompareNodeLists" @@ -347,14 +349,12 @@ test.Data)); } - private ComparisonResult CompareNodeLists(XmlNodeList control, + private ComparisonResult CompareNodeLists(IEnumerable<XmlNode> controlSeq, XPathContext controlContext, - XmlNodeList test, + IEnumerable<XmlNode> testSeq, XPathContext testContext) { - IList<XmlNode> controlList = - new List<XmlNode>(Linqy.Cast<XmlNode>(control)); - IList<XmlNode> testList = - new List<XmlNode>(Linqy.Cast<XmlNode>(test)); + IList<XmlNode> controlList = new List<XmlNode>(controlSeq); + IList<XmlNode> testList = new List<XmlNode>(testSeq); IDictionary<int, object> unmatchedTestIndexes = new SortedDictionary<int, object>(); for (int i = 0; i < testList.Count; i++) { @@ -372,6 +372,8 @@ controlContext.NavigateToChild(i); try { if (testMatch != null) { + XmlNode control = controlList[i]; + XmlNode test = testMatch.Node; testContext.NavigateToChild(testMatch.Index); try { ]]></literal> @@ -380,8 +382,8 @@ testExpr="testMatch.Index" /> <compareMethodExpr method="CompareNodes" - controlExpr="controlList[i]" - testExpr="testMatch.Node"/> + controlExpr="control" + testExpr="test"/> <literal><![CDATA[ unmatchedTestIndexes.Remove(testMatch.Index); lastMatch = testMatch; @@ -528,5 +530,10 @@ private static XPathContext.INodeInfo TO_NODE_INFO(XmlNode n) { return new XPathContext.DOMNodeInfo(n); } + + private static bool INTERESTING_NODES(XmlNode n) { + return n.NodeType != XmlNodeType.DocumentType + && n.NodeType != XmlNodeType.XmlDeclaration; + } ]]></literal> </class> \ No newline at end of file Modified: trunk/xmlunit/src/main/net-core/util/Linqy.cs =================================================================== --- trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/main/net-core/util/Linqy.cs 2010-09-10 15:11:00 UTC (rev 462) @@ -38,5 +38,24 @@ yield return mapper(f); } } + + public delegate bool Predicate<T>(T toTest); + + public static IEnumerable<T> Filter<T>(IEnumerable<T> sequence, + Predicate<T> filter) { + foreach (T t in sequence) { + if (filter(t)) { + yield return t; + } + } + } + + public static int Count(IEnumerable e) { + int c = 0; + foreach (object o in e) { + c++; + } + return c; + } } } \ No newline at end of file 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-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java 2010-09-10 15:11:00 UTC (rev 462) @@ -302,11 +302,6 @@ public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (comparison.getType() - == ComparisonType.CHILD_NODELIST_LENGTH) { - assertEquals(ComparisonResult.DIFFERENT, outcome); - return ComparisonResult.EQUAL; - } - if (comparison.getType() == ComparisonType.HAS_DOCTYPE_DECLARATION) { assertEquals(ComparisonResult.DIFFERENT, outcome); return ComparisonResult.CRITICAL; Modified: trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_Diff.java =================================================================== --- trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_Diff.java 2010-09-09 15:42:13 UTC (rev 461) +++ trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_Diff.java 2010-09-10 15:11:00 UTC (rev 462) @@ -210,8 +210,7 @@ diff.toString()); } - public void NOtestXMLWithDTD() throws Exception { - XMLUnit.setCompareUnmatched(true); + public void testXMLWithDTD() throws Exception { String aDTDpart = "<!DOCTYPE test [" + "<!ELEMENT assertion EMPTY>" + "<!ATTLIST assertion result (pass|fail) \"fail\">" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |