From: <bo...@us...> - 2007-04-18 01:36:08
|
Revision: 191 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=191&view=rev Author: bodewig Date: 2007-04-17 18:36:07 -0700 (Tue, 17 Apr 2007) Log Message: ----------- Implement new difference type for unmatched nodes Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 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/DifferenceConstants.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java 2007-04-17 04:04:57 UTC (rev 190) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceConstants.java 2007-04-18 01:36:07 UTC (rev 191) @@ -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 @@ -84,6 +84,9 @@ int CHILD_NODELIST_SEQUENCE_ID = 20; /** Comparing 2 Documents only one of which has a doctype */ int HAS_DOCTYPE_DECLARATION_ID = 21; + /** 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 an implied attribute value against an explicit value */ public static final Difference ATTR_VALUE_EXPLICITLY_SPECIFIED = @@ -173,4 +176,9 @@ public static final Difference HAS_DOCTYPE_DECLARATION = new Difference(HAS_DOCTYPE_DECLARATION_ID, "presence of doctype declaration", true); + + /** Comparing 2 nodes and one holds more childnodes than can be + * 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"); } \ 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-04-17 04:04:57 UTC (rev 190) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DifferenceEngine.java 2007-04-18 01:36:07 UTC (rev 191) @@ -39,6 +39,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import org.w3c.dom.Attr; @@ -360,6 +361,8 @@ HashMap/*<Node, Node>*/ matchingNodes = new HashMap(); HashMap/*<Node, Integer>*/ matchingNodeIndexes = new HashMap(); + List/*<Node>*/ unmatchedTestNodes = new ArrayList(testChildren); + // first pass to find the matching nodes in control and test docs for (int i=0; i < numNodes; ++i) { Node nextControl = (Node) controlChildren.get(i); @@ -371,12 +374,14 @@ boolean matchFound = false; while (!matchFound) { - if (findNodeType == ((Node)testChildren.get(j)).getNodeType()) { + Node t = (Node) testChildren.get(j); + if (findNodeType == t.getNodeType() + || comparingTextAndCDATA(findNodeType, t.getNodeType())) { matchFound = !matchOnElement || elementQualifier == null || elementQualifier .qualifyForComparison((Element) nextControl, - (Element) testChildren.get(j)); + (Element) t); } if (!matchFound) { ++j; @@ -392,47 +397,38 @@ if (matchFound) { matchingNodes.put(nextControl, testChildren.get(j)); matchingNodeIndexes.put(nextControl, new Integer(j)); + unmatchedTestNodes.remove(testChildren.get(j)); } } // next, do the actual comparision on those that matched - or // match them against the first test nodes that didn't match // any other control nodes - Collection matchingTestNodes = matchingNodes.values(); for (int i=0; i < numNodes; ++i) { Node nextControl = (Node) controlChildren.get(i); Node nextTest = (Node) matchingNodes.get(nextControl); Integer testIndex = (Integer) matchingNodeIndexes.get(nextControl); - if (nextTest == null) { - short findNodeType = nextControl.getNodeType(); - int startAt = ( i > lastTestNode ? lastTestNode : i); - j = startAt; - - boolean matchFound = false; - - while (!matchFound) { - if (((Node) testChildren.get(j)) - .getNodeType() == findNodeType - && !matchingTestNodes.contains(testChildren.get(j))) { - matchFound = true; - } else { - ++j; - if (j > lastTestNode) { - j = 0; - } - if (j == startAt) { - // been through all children - break; - } - } - } - nextTest = (Node) testChildren.get(j); - testIndex = new Integer(j); + if (nextTest == null && !unmatchedTestNodes.isEmpty()) { + nextTest = (Node) unmatchedTestNodes.get(0); + testIndex = new Integer(testChildren.indexOf(nextTest)); + unmatchedTestNodes.remove(0); } + if (nextTest != null) { compareNode(nextControl, nextTest, listener, elementQualifier); compare(new Integer(i), testIndex, nextControl, nextTest, listener, CHILD_NODELIST_SEQUENCE); + } else { + compare(nextControl.getNodeName(), null, nextControl, null, + listener, CHILD_NODE_NOT_FOUND); + } } + + // 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); + } } /** Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2007-04-17 04:04:57 UTC (rev 190) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DetailedDiff.java 2007-04-18 01:36:07 UTC (rev 191) @@ -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 @@ -110,15 +110,33 @@ DetailedDiff differencesWithWhitespace = new DetailedDiff( new Diff(new InputSource(new FileReader(control)), new InputSource(new FileReader(test))) ); - assertEquals(1402, differencesWithWhitespace.getAllDifferences().size()); + List l = differencesWithWhitespace.getAllDifferences(); + int unmatchedNodes = 0; + for (Iterator iter = l.iterator(); iter.hasNext();) { + Difference d = (Difference) iter.next(); + if (d.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID) { + unmatchedNodes++; + } + } + + assertEquals(1402 + unmatchedNodes, + differencesWithWhitespace.getAllDifferences().size()); + try { XMLUnit.setIgnoreWhitespace(true); Diff prototype = new Diff(new FileReader(control), new FileReader(test)); DetailedDiff detailedDiff = new DetailedDiff(prototype); List differences = detailedDiff.getAllDifferences(); - assertEquals(40, differences.size()); + unmatchedNodes = 0; + for (Iterator iter = differences.iterator(); iter.hasNext();) { + Difference d = (Difference) iter.next(); + if (d.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID) { + unmatchedNodes++; + } + } + assertEquals(40 + unmatchedNodes, differences.size()); SimpleXpathEngine xpathEngine = new SimpleXpathEngine(); Document controlDoc = @@ -217,8 +235,9 @@ DetailedDiff diff = new DetailedDiff(new Diff(control, test)); List changes = diff.getAllDifferences(); - // number of children, text of first child - assertEquals(2, changes.size()); + // number of children, text of first child, unexpected second + // test child + assertEquals(3, changes.size()); } protected Diff buildDiff(Document control, Document test) { Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-17 04:04:57 UTC (rev 190) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_DifferenceEngine.java 2007-04-18 01:36:07 UTC (rev 191) @@ -620,7 +620,7 @@ 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[2]", + assertEquals("13th control xpath", "/stuff[1]/item[1]", listener.controlXpath); assertEquals("13th test xpath", "/stuff[1]/processing-instruction()[1]", listener.testXpath); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |