From: <bo...@us...> - 2007-04-13 11:31:16
|
Revision: 180 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=180&view=rev Author: bodewig Date: 2007-04-13 04:31:15 -0700 (Fri, 13 Apr 2007) Log Message: ----------- Make ignoreComments work at the DifferenceEngine level Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathNodeTracker.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-04-13 08:11:02 UTC (rev 179) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-04-13 11:31:15 UTC (rev 180) @@ -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 @@ -36,8 +36,10 @@ package org.custommonkey.xmlunit; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import org.w3c.dom.Attr; import org.w3c.dom.CharacterData; @@ -246,15 +248,42 @@ */ protected void compareHasChildNodes(Node control, Node test, DifferenceListener listener) throws DifferenceFoundException { - Boolean controlHasChildren = control.hasChildNodes() - ? Boolean.TRUE : Boolean.FALSE; - Boolean testHasChildren = test.hasChildNodes() - ? Boolean.TRUE : Boolean.FALSE; + Boolean controlHasChildren = hasChildNodes(control); + Boolean testHasChildren = hasChildNodes(test); compare(controlHasChildren, testHasChildren, control, test, listener, HAS_CHILD_NODES); } /** + * Tests whether a Node has children, taking ignoreComments + * setting into account. + */ + private Boolean hasChildNodes(Node n) { + boolean flag = n.hasChildNodes(); + if (flag && XMLUnit.getIgnoreComments()) { + List nl = nodeList2List(n.getChildNodes()); + flag = nl.size() > 0; + } + return flag ? Boolean.TRUE : Boolean.FALSE; + } + + /** + * Returns the NodeList's Nodes as List, taking ignoreComments + * into account. + */ + static List nodeList2List(NodeList nl) { + int len = nl.getLength(); + ArrayList l = new ArrayList(len); + for (int i = 0; i < len; i++) { + Node n = nl.item(i); + if (!XMLUnit.getIgnoreComments() || !(n instanceof Comment)) { + l.add(n); + } + } + return l; + } + + /** * Compare the number of children, and if the same, compare the actual * children via their NodeLists. * @param control @@ -267,11 +296,11 @@ DifferenceListener listener, ElementQualifier elementQualifier) throws DifferenceFoundException { if (control.hasChildNodes() && test.hasChildNodes()) { - NodeList controlChildren = control.getChildNodes(); - NodeList testChildren = test.getChildNodes(); + List controlChildren = nodeList2List(control.getChildNodes()); + List testChildren = nodeList2List(test.getChildNodes()); - Integer controlLength = new Integer(controlChildren.getLength()); - Integer testLength = new Integer(testChildren.getLength()); + Integer controlLength = new Integer(controlChildren.size()); + Integer testLength = new Integer(testChildren.size()); compare(controlLength, testLength, control, test, listener, CHILD_NODELIST_LENGTH); compareNodeList(controlChildren, testChildren, @@ -292,23 +321,48 @@ * the test NodeList should be compared to the current child element in the * control NodeList. * @throws DifferenceFoundException + * @deprecated Use the version with List arguments instead */ protected void compareNodeList(final NodeList control, final NodeList test, final int numNodes, final DifferenceListener listener, final ElementQualifier elementQualifier) throws DifferenceFoundException { + compareNodeList(nodeList2List(control), nodeList2List(test), + numNodes, listener, elementQualifier); + } + /** + * Compare the contents of two node list one by one, assuming that order + * of children is NOT important: matching begins at same position in test + * list as control list. + * @param control + * @param test + * @param numNodes convenience parameter because the calling method should + * know the value already + * @param listener + * @param elementQualifier used to determine which of the child elements in + * the test NodeList should be compared to the current child element in the + * control NodeList. + * @throws DifferenceFoundException + */ + protected void compareNodeList(final List controlChildren, + final List testChildren, + final int numNodes, + final DifferenceListener listener, + final ElementQualifier elementQualifier) + throws DifferenceFoundException { + int j = 0; - final int lastTestNode = test.getLength() - 1; - testTracker.preloadNodeList(test); + final int lastTestNode = testChildren.size() - 1; + testTracker.preloadChildList(testChildren); HashMap/*<Node, Node>*/ matchingNodes = new HashMap(); HashMap/*<Node, Integer>*/ matchingNodeIndexes = new HashMap(); // first pass to find the matching nodes in control and test docs for (int i=0; i < numNodes; ++i) { - Node nextControl = control.item(i); + Node nextControl = (Node) controlChildren.get(i); boolean matchOnElement = nextControl instanceof Element; short findNodeType = nextControl.getNodeType(); int startAt = ( i > lastTestNode ? lastTestNode : i); @@ -317,12 +371,12 @@ boolean matchFound = false; while (!matchFound) { - if (findNodeType == test.item(j).getNodeType()) { + if (findNodeType == ((Node)testChildren.get(j)).getNodeType()) { matchFound = !matchOnElement || elementQualifier == null || elementQualifier - .qualifyForComparison((Element)nextControl, - (Element)test.item(j)); + .qualifyForComparison((Element) nextControl, + (Element) testChildren.get(j)); } if (!matchFound) { ++j; @@ -336,7 +390,7 @@ } } if (matchFound) { - matchingNodes.put(nextControl, test.item(j)); + matchingNodes.put(nextControl, testChildren.get(j)); matchingNodeIndexes.put(nextControl, new Integer(j)); } } @@ -346,7 +400,7 @@ // any other control nodes Collection matchingTestNodes = matchingNodes.values(); for (int i=0; i < numNodes; ++i) { - Node nextControl = control.item(i); + Node nextControl = (Node) controlChildren.get(i); Node nextTest = (Node) matchingNodes.get(nextControl); Integer testIndex = (Integer) matchingNodeIndexes.get(nextControl); if (nextTest == null) { @@ -357,8 +411,9 @@ boolean matchFound = false; while (!matchFound) { - if (test.item(j).getNodeType() == findNodeType - && !matchingTestNodes.contains(test.item(j))) { + if (((Node) testChildren.get(j)) + .getNodeType() == findNodeType + && !matchingTestNodes.contains(testChildren.get(j))) { matchFound = true; } else { ++j; @@ -371,7 +426,7 @@ } } } - nextTest = test.item(j); + nextTest = (Node) testChildren.get(j); testIndex = new Integer(j); } compareNode(nextControl, nextTest, listener, elementQualifier); @@ -531,7 +586,9 @@ */ protected void compareComment(Comment control, Comment test, DifferenceListener listener) throws DifferenceFoundException { - compareCharacterData(control, test, listener, COMMENT_VALUE); + if (!XMLUnit.getIgnoreComments()) { + compareCharacterData(control, test, listener, COMMENT_VALUE); + } } /** Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathNodeTracker.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathNodeTracker.java 2007-04-13 08:11:02 UTC (rev 179) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathNodeTracker.java 2007-04-13 11:31:15 UTC (rev 180) @@ -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 @@ -35,10 +35,10 @@ */ package org.custommonkey.xmlunit; +import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; import org.w3c.dom.Attr; @@ -167,6 +167,21 @@ } /** + * Preload the items in a List by visiting each in turn + * Required for pieces of test XML whose node children can be visited + * out of sequence by a DifferenceEngine comparison + * @param nodeList the items to preload + */ + public void preloadChildList(List nodeList) { + currentEntry.trackNodesAsWellAsValues(true); + int length = nodeList.size(); + for (int i=0; i < length; ++i) { + visited((Node) nodeList.get(i)); + } + currentEntry.trackNodesAsWellAsValues(false); + } + + /** * @return the last visited node as an xpath-location String */ public String toXpathString() { Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-13 08:11:02 UTC (rev 179) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-13 11:31:15 UTC (rev 180) @@ -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 @@ -731,6 +731,55 @@ assertEquals(expected, listener.comparingWhat); } + public void testExtraComment() { + testExtraComment(true); + resetListener(); + XMLUnit.setIgnoreComments(true); + try { + testExtraComment(false); + } finally { + XMLUnit.setIgnoreComments(false); + } + } + + private void testExtraComment(boolean expectDifference) { + Element control = document.createElement("foo"); + Element test = document.createElement("foo"); + Comment c = document.createComment("bar"); + control.appendChild(c); + Element cChild = document.createElement("baz"); + control.appendChild(cChild); + Element tChild = document.createElement("baz"); + test.appendChild(tChild); + engine.compare(control, test, listener, null); + assertEquals(expectDifference, listener.different); + resetListener(); + engine.compare(test, control, listener, null); + assertEquals(expectDifference, listener.different); + } + + public void testCommentContent() { + testCommentContent(true); + resetListener(); + XMLUnit.setIgnoreComments(true); + try { + testCommentContent(false); + } finally { + XMLUnit.setIgnoreComments(false); + } + } + + private void testCommentContent(boolean expectDifference) { + Element control = document.createElement("foo"); + Element test = document.createElement("foo"); + Comment c = document.createComment("bar"); + control.appendChild(c); + Comment c2 = document.createComment("baz"); + test.appendChild(c2); + engine.compare(control, test, listener, null); + assertEquals(expectDifference, listener.different); + } + 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. |