From: <bo...@us...> - 2008-03-03 16:05:05
|
Revision: 240 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=240&view=rev Author: bodewig Date: 2008-03-03 08:04:35 -0800 (Mon, 03 Mar 2008) Log Message: ----------- fix XPath expressions when missing a node, issue 1860681 Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java trunk/xmlunit/src/user-guide/XMLUnit-Java.xml trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.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 2008-02-04 12:04:58 UTC (rev 239) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2008-03-03 16:04:35 UTC (rev 240) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +Copyright (c) 2001-2008, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -356,6 +356,7 @@ int j = 0; final int lastTestNode = testChildren.size() - 1; + controlTracker.preloadChildList(controlChildren); testTracker.preloadChildList(testChildren); HashMap/*<Node, Node>*/ matchingNodes = new HashMap(); @@ -418,19 +419,30 @@ compare(new Integer(i), testIndex, nextControl, nextTest, listener, CHILD_NODELIST_SEQUENCE); } else { - compare(nextControl.getNodeName(), null, nextControl, null, - listener, CHILD_NODE_NOT_FOUND); + missingNode(nextControl, null, listener); } } // now handle remaining unmatched test nodes for (Iterator iter = unmatchedTestNodes.iterator(); iter.hasNext();) { - Node n = (Node) iter.next(); - compare(null, n.getNodeName(), null, n, listener, - CHILD_NODE_NOT_FOUND); + missingNode(null, (Node) iter.next(), listener); } } + private void missingNode(Node control, Node test, + DifferenceListener listener) + throws DifferenceFoundException { + if (control != null) { + controlTracker.visited(control); + compare(control.getNodeName(), null, control, null, + listener, CHILD_NODE_NOT_FOUND, controlTracker, null); + } else { + testTracker.visited(test); + compare(null, test.getNodeName(), null, test, listener, + CHILD_NODE_NOT_FOUND, null, testTracker); + } + } + /** * @param aNode * @return true if the node has a namespace @@ -760,11 +772,35 @@ protected void compare(Object expected, Object actual, Node control, Node test, DifferenceListener listener, Difference difference) throws DifferenceFoundException { + compare(expected, actual, control, test, listener, difference, + controlTracker, testTracker); + } + + /** + * If the expected and actual values are unequal then inform the listener of + * a difference and throw a DifferenceFoundException. + * @param expected + * @param actual + * @param control + * @param test + * @param listener + * @param differenceType + * @throws DifferenceFoundException + */ + protected void compare(Object expected, Object actual, + Node control, Node test, DifferenceListener listener, + Difference difference, XpathNodeTracker controlLoc, + XpathNodeTracker testLoc) + throws DifferenceFoundException { if (unequal(expected, actual)) { NodeDetail controlDetail = new NodeDetail(String.valueOf(expected), - control, controlTracker.toXpathString()); + control, + controlLoc == null ? null + : controlLoc.toXpathString()); NodeDetail testDetail = new NodeDetail(String.valueOf(actual), - test, testTracker.toXpathString()); + test, + testLoc == null ? null + : testLoc.toXpathString()); Difference differenceInstance = new Difference(difference, controlDetail, testDetail); listener.differenceFound(differenceInstance); Modified: trunk/xmlunit/src/user-guide/XMLUnit-Java.xml =================================================================== --- trunk/xmlunit/src/user-guide/XMLUnit-Java.xml 2008-02-04 12:04:58 UTC (rev 239) +++ trunk/xmlunit/src/user-guide/XMLUnit-Java.xml 2008-03-03 16:04:35 UTC (rev 240) @@ -3236,6 +3236,34 @@ <section id="Changes 1.2"> <title>Changes from XMLUnit 1.1 to 1.2</title> + + <section id="Breaking Changes 1.2"> + <title>Breaking Changes</title> + + <itemizedlist> + <listitem> + If XMLUnit detects that it cannot match a certain node + (i.e. it encounters + a <literal>CHILD_NODE_NOT_FOUND</literal> + kind of difference) the XPath for the "missing" node will + be null. It used to be some random XPath of a different node. + </listitem> + </itemizedlist> + </section> + + <section id="Bugfixes 1.2"> + <title>Important Bug Fixes</title> + + <itemizedlist> + <listitem> + If XMLUnit couldn't match nodes (i.e. it encountered + a <literal>CHILD_NODE_NOT_FOUND</literal> kind of + difference), the XPath expressions of the node details + have been random. <ulink + url="https://sourceforge.net/tracker/index.php?func=detail&aid=1860681&group_id=23187&atid=377768">Issue 1860681</ulink>. + </listitem> + </itemizedlist> + </section> </section> </appendix> Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2008-02-04 12:04:58 UTC (rev 239) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2008-03-03 16:04:35 UTC (rev 240) @@ -248,7 +248,7 @@ * Bug 1860681 * @see https://sourceforge.net/tracker/index.php?func=detail&aid=1860681&group_id=23187&atid=377768 */ - public void XtestXpathOfMissingNode() throws Exception { + public void testXpathOfMissingNode() throws Exception { String control = "<books>" + " <book>" @@ -277,7 +277,9 @@ d.getId()); assertEquals("/books[1]/book[1]", d.getControlNodeDetail().getXpathLocation()); - assertNull(d.getTestNodeDetail().getXpathLocation()); + assertNull("should be null but is " + + d.getTestNodeDetail().getXpathLocation(), + d.getTestNodeDetail().getXpathLocation()); // and reverse diff = new Diff(test, control); Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2008-02-04 12:04:58 UTC (rev 239) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2008-03-03 16:04:35 UTC (rev 240) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007, Jeff Martin, Tim Bacon +Copyright (c) 2001-2008, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -620,10 +620,13 @@ String control = "<stuff><item id=\"1\"/><item id=\"2\"/></stuff>"; String test = "<stuff><?item data?></stuff>"; listenToDifferences(control, test); - assertEquals("13th control xpath", "/stuff[1]/item[1]", + // mutiple Differences, we only see the last one, missing second element + assertEquals("13 difference type", + DifferenceConstants.CHILD_NODE_NOT_FOUND_ID, + listener.comparingWhat); + assertEquals("13th control xpath", "/stuff[1]/item[2]", listener.controlXpath); - assertEquals("13th test xpath", "/stuff[1]/processing-instruction()[1]", - listener.testXpath); + assertNull("13th test xpath", listener.testXpath); } public void testXpathLocation14() throws Exception { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |