From: <bo...@us...> - 2010-09-12 06:07:35
|
Revision: 463 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=463&view=rev Author: bodewig Date: 2010-09-12 06:07:29 +0000 (Sun, 12 Sep 2010) Log Message: ----------- ignore anything between the document node and the root element in legacy layer Modified Paths: -------------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_DifferenceEngine.java trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_NewDifferenceEngine.java Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-09-10 15:11:00 UTC (rev 462) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-09-12 06:07:29 UTC (rev 463) @@ -53,6 +53,7 @@ import org.w3c.dom.CDATASection; import org.w3c.dom.Comment; +import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -121,16 +122,22 @@ public void compare(Node control, Node test, DifferenceListener listener, ElementQualifier elementQualifier) { DOMDifferenceEngine engine = new DOMDifferenceEngine(); + + IsBetweenDocumentNodeAndRootElement checkPrelude = + new IsBetweenDocumentNodeAndRootElement(); + engine.addComparisonListener(checkPrelude); + if (matchTracker != null) { engine .addMatchListener(new MatchTracker2ComparisonListener(matchTracker)); } DifferenceEvaluator controllerAsEvaluator = - new ComparisonController2DifferenceEvaluator(controller); + new ComparisonController2DifferenceEvaluator(controller, + checkPrelude); if (listener != null) { DifferenceEvaluator l = - new DifferenceListener2DifferenceEvaluator(listener); + new DifferenceListener2DifferenceEvaluator(listener, checkPrelude); engine .setDifferenceEvaluator(DifferenceEvaluators.first(l, controllerAsEvaluator)); @@ -309,10 +316,18 @@ Short.valueOf(Node.CDATA_SECTION_NODE); private static boolean swallowComparison(Comparison comparison, - ComparisonResult outcome) { + ComparisonResult outcome, + IsBetweenDocumentNodeAndRootElement + checkPrelude) { if (outcome == ComparisonResult.EQUAL) { return true; } + if ((comparison.getType() == ComparisonType.CHILD_NODELIST_LENGTH + && comparison.getControlDetails().getTarget() instanceof Document) + || checkPrelude.shouldSkip() + ) { + return true; + } if (XMLUnit.getIgnoreDiffBetweenTextAndCDATA() && comparison.getType() == ComparisonType.NODE_TYPE) { return ( @@ -332,13 +347,16 @@ public static class ComparisonController2DifferenceEvaluator implements DifferenceEvaluator { private final ComparisonController cc; - public ComparisonController2DifferenceEvaluator(ComparisonController c) { + private final IsBetweenDocumentNodeAndRootElement checkPrelude; + public ComparisonController2DifferenceEvaluator(ComparisonController c, + IsBetweenDocumentNodeAndRootElement checkPrelude) { cc = c; + this.checkPrelude = checkPrelude; } public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { - if (!swallowComparison(comparison, outcome)) { + if (!swallowComparison(comparison, outcome, checkPrelude)) { Difference diff = toDifference(comparison); if (diff != null && cc.haltComparison(diff)) { return ComparisonResult.CRITICAL; @@ -367,14 +385,17 @@ public static class DifferenceListener2DifferenceEvaluator implements DifferenceEvaluator { private final DifferenceListener dl; + private final IsBetweenDocumentNodeAndRootElement checkPrelude; - public DifferenceListener2DifferenceEvaluator(DifferenceListener dl) { + public DifferenceListener2DifferenceEvaluator(DifferenceListener dl, + IsBetweenDocumentNodeAndRootElement checkPrelude) { this.dl = dl; + this.checkPrelude = checkPrelude; } public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { - if (!swallowComparison(comparison, outcome)) { + if (!swallowComparison(comparison, outcome, checkPrelude)) { Difference diff = toDifference(comparison); if (diff != null) { switch (dl.differenceFound(diff)) { @@ -394,4 +415,50 @@ return ComparisonResult.EQUAL; } } + + /** + * Tests whether the DifferenceEngine is currently processing + * comparisons of "things" between the document node and the + * document's root element (comments or PIs, mostly) since these + * must be ignored for backwards compatibility reasons. + * + * <p>Relies on the following assumptions: + * <ul> + + * <li>the last comparison DOMDifferenceEngine performs on the + * document node is an XML_ENCODING comparison.</li> + * <li>the first comparison DOMDifferenceEngine performs on matching + * root elements is a NODE_TYPE comparison. The control Node + * is an Element Node.</li> + * <li>the first comparison DOMDifferenceEngine performs if the + * root elements don't match is a CHILD_LOOKUP comparison. + * The control Node is an Element Node.</li> + * </ul> + * </p> + */ + private static class IsBetweenDocumentNodeAndRootElement + implements ComparisonListener { + + private boolean haveSeenXmlEncoding = false; + private boolean haveSeenElementNodeComparison = false; + + public void comparisonPerformed(Comparison comparison, + ComparisonResult outcome) { + if (comparison.getType() == ComparisonType.XML_ENCODING) { + haveSeenXmlEncoding = true; + } else if (comparison.getControlDetails().getTarget() + instanceof Element + && + (comparison.getType() == ComparisonType.NODE_TYPE + || comparison.getType() == ComparisonType.CHILD_LOOKUP) + ) { + haveSeenElementNodeComparison = true; + } + } + + private boolean shouldSkip() { + return haveSeenXmlEncoding && !haveSeenElementNodeComparison; + } + } + } Modified: trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_DifferenceEngine.java =================================================================== --- trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_DifferenceEngine.java 2010-09-10 15:11:00 UTC (rev 462) +++ trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_DifferenceEngine.java 2010-09-12 06:07:29 UTC (rev 463) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2008, Jeff Martin, Tim Bacon +Copyright (c) 2001-2008,2010 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -925,11 +925,46 @@ + "</abc:XMLContent>" + "</abc:EventBody>" + "</abc:Message>"; - listener.tracing = true; listenToDifferences(control, test); assertFalse(listener.different); } + /** + * XMLUnit 1.3 jumps from the document node straight to the root + * element, ignoring any other children the document might have. + * Some people consider this a bug (Issue 2770386) others rely on + * it. + * + * <p>XMLUnit 2.x doesn't ignore differences in the prelude but we + * want to keep the behavior for the legacy code base.</p> + */ + public void testIgnoresDifferencesBetweenDocAndRootElement() + throws Throwable { + String control = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!-- some comment -->" + + "<?foo some PI ?>" + + "<bar/>"; + String test = "<bar/>"; + listenToDifferences(control, test); + assertFalse("unexpected difference: " + listener.comparingWhat, + listener.different); + resetListener(); + control = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!-- some comment -->" + + "<?foo some PI ?>" + + "<bar/>"; + test = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<?foo some other PI ?>" + + "<!-- some other comment -->" + + "<bar/>"; + listenToDifferences(control, test); + assertFalse("unexpected difference: " + listener.comparingWhat, + listener.different); + } + private void listenToDifferences(String control, String test) throws SAXException, IOException { Document controlDoc = XMLUnit.buildControlDocument(control); Modified: trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_NewDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_NewDifferenceEngine.java 2010-09-10 15:11:00 UTC (rev 462) +++ trunk/xmlunit/src/tests/java-legacy/org/custommonkey/xmlunit/test_NewDifferenceEngine.java 2010-09-12 06:07:29 UTC (rev 463) @@ -466,6 +466,42 @@ assertFalse(listener.different); } + /** + * XMLUnit 1.3 jumps from the document node straight to the root + * element, ignoring any other children the document might have. + * Some people consider this a bug (Issue 2770386) others rely on + * it. + * + * <p>XMLUnit 2.x doesn't ignore differences in the prelude but we + * want to keep the behavior for the legacy code base.</p> + */ + public void testIgnoresDifferencesBetweenDocAndRootElement() + throws Throwable { + String control = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!-- some comment -->" + + "<?foo some PI ?>" + + "<bar/>"; + String test = "<bar/>"; + listenToDifferences(control, test); + assertFalse("unexpected difference: " + listener.comparingWhat, + listener.different); + resetListener(); + control = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<!-- some comment -->" + + "<?foo some PI ?>" + + "<bar/>"; + test = + "<?xml version = \"1.0\" encoding = \"UTF-8\"?>" + + "<?foo some other PI ?>" + + "<!-- some other comment -->" + + "<bar/>"; + listenToDifferences(control, test); + assertFalse("unexpected difference: " + listener.comparingWhat, + listener.different); + } + private void listenToDifferences(String control, String test) throws SAXException, IOException { listenToDifferences(control, test, DEFAULT_ELEMENT_QUALIFIER); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |