From: <bo...@us...> - 2010-09-13 10:41:18
|
Revision: 467 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=467&view=rev Author: bodewig Date: 2010-09-13 10:41:11 +0000 (Mon, 13 Sep 2010) Log Message: ----------- tear evaluation of difference and differencelistener apart in legacy difference engine Modified Paths: -------------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/Diff.java trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java Added Paths: ----------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/DifferenceEvaluator.java Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/Diff.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/Diff.java 2010-09-13 09:49:28 UTC (rev 466) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/Diff.java 2010-09-13 10:41:11 UTC (rev 467) @@ -72,7 +72,7 @@ * <br />Examples and more at <a href="http://xmlunit.sourceforge.net"/>xmlunit.sourceforge.net</a> */ public class Diff - implements DifferenceListener, ComparisonController { + implements DifferenceEvaluator, DifferenceListener, ComparisonController { private final Document controlDoc; private final Document testDoc; private boolean similar = true; @@ -287,10 +287,7 @@ * Always RETURN_ACCEPT_DIFFERENCE if the call is not delegated. */ public int differenceFound(Difference difference) { - int returnValue = RETURN_ACCEPT_DIFFERENCE; - if (differenceListenerDelegate != null) { - returnValue = differenceListenerDelegate.differenceFound(difference); - } + int returnValue = evaluate(difference); switch (returnValue) { case RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL: @@ -325,6 +322,14 @@ return returnValue; } + public int evaluate(Difference difference) { + int returnValue = RETURN_ACCEPT_DIFFERENCE; + if (differenceListenerDelegate != null) { + returnValue = differenceListenerDelegate.differenceFound(difference); + } + return returnValue; + } + /** * DifferenceListener implementation. * If the {@link Diff#overrideDifferenceListener overrideDifferenceListener} @@ -422,6 +427,8 @@ && !XMLUnit.getNormalizeWhitespace() && + differenceListenerDelegate == null + && !usesUnknownElementQualifier() ) { return new NewDifferenceEngine(this, matchTrackerDelegate); Added: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/DifferenceEvaluator.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/DifferenceEvaluator.java (rev 0) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/DifferenceEvaluator.java 2010-09-13 10:41:11 UTC (rev 467) @@ -0,0 +1,55 @@ +/* +****************************************************************** +Copyright (c) 2010, Jeff Martin, Tim Bacon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of the xmlunit.sourceforge.net nor the names + of its contributors may be used to endorse or promote products + derived from this software without specific prior written + permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +****************************************************************** +*/ + +package org.custommonkey.xmlunit; + +/** + * Contains the pure evaluation logic of DifferenceListener + */ +public interface DifferenceEvaluator { + /** + * Receive notification of a difference and decides whether 2 + * nodes are different. + * @param difference a Difference instance as defined in {@link + * DifferenceConstants DifferenceConstants} describing the cause + * of the difference and containing the detail of the nodes that + * differ + * @return int one of the RETURN_... constants of + * DifferenceListener describing how this difference was + * interpreted + */ + int evaluate(Difference difference); +} Property changes on: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/DifferenceEvaluator.java ___________________________________________________________________ Added: svn:eol-style + native 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-13 09:49:28 UTC (rev 466) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/NewDifferenceEngine.java 2010-09-13 10:41:11 UTC (rev 467) @@ -45,7 +45,6 @@ import net.sf.xmlunit.diff.ComparisonResult; import net.sf.xmlunit.diff.ComparisonType; import net.sf.xmlunit.diff.DOMDifferenceEngine; -import net.sf.xmlunit.diff.DifferenceEvaluator; import net.sf.xmlunit.diff.DifferenceEvaluators; import net.sf.xmlunit.diff.ElementSelector; import net.sf.xmlunit.input.CommentLessSource; @@ -123,7 +122,7 @@ ElementQualifier elementQualifier) { DOMDifferenceEngine engine = new DOMDifferenceEngine(); - IsBetweenDocumentNodeAndRootElement checkPrelude = + final IsBetweenDocumentNodeAndRootElement checkPrelude = new IsBetweenDocumentNodeAndRootElement(); engine.addComparisonListener(checkPrelude); @@ -132,19 +131,44 @@ .addMatchListener(new MatchTracker2ComparisonListener(matchTracker)); } - DifferenceEvaluator controllerAsEvaluator = - new ComparisonController2DifferenceEvaluator(controller, - checkPrelude); + net.sf.xmlunit.diff.DifferenceEvaluator controllerAsEvaluator = + new ComparisonController2DifferenceEvaluator(controller); + net.sf.xmlunit.diff.DifferenceEvaluator ev = null; if (listener != null) { - DifferenceEvaluator l = - new DifferenceListener2DifferenceEvaluator(listener, checkPrelude); - engine - .setDifferenceEvaluator(DifferenceEvaluators.first(l, - controllerAsEvaluator)); + net.sf.xmlunit.diff.DifferenceEvaluator e = null; + if (listener instanceof org.custommonkey.xmlunit.DifferenceEvaluator) { + e = new DifferenceEvaluatorAdapter((DifferenceEvaluator) listener); + final ComparisonListener l = new DifferenceListener2ComparisonListener(listener); + engine + .addDifferenceListener(new ComparisonListener() { + public void comparisonPerformed(Comparison comparison, + ComparisonResult outcome) { + if (!swallowComparison(comparison, outcome, + checkPrelude)) { + l.comparisonPerformed(comparison, outcome); + } + } + }); + } else { + e = new DifferenceListener2DifferenceEvaluator(listener); + } + ev = DifferenceEvaluators.first(e, controllerAsEvaluator); } else { - engine - .setDifferenceEvaluator(controllerAsEvaluator); + ev = controllerAsEvaluator; } + final net.sf.xmlunit.diff.DifferenceEvaluator evaluator = ev; + engine + .setDifferenceEvaluator(new net.sf.xmlunit.diff.DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (!swallowComparison(comparison, outcome, + checkPrelude)) { + return evaluator.evaluate(comparison, outcome); + } + return outcome; + } + }); + if (elementQualifier != null) { engine .setElementSelector(new ElementQualifier2ElementSelector(elementQualifier)); @@ -345,25 +369,46 @@ } public static class ComparisonController2DifferenceEvaluator - implements DifferenceEvaluator { + implements net.sf.xmlunit.diff.DifferenceEvaluator { private final ComparisonController cc; - private final IsBetweenDocumentNodeAndRootElement checkPrelude; - public ComparisonController2DifferenceEvaluator(ComparisonController c, - IsBetweenDocumentNodeAndRootElement checkPrelude) { + public ComparisonController2DifferenceEvaluator(ComparisonController c) { cc = c; - this.checkPrelude = checkPrelude; } public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { - if (!swallowComparison(comparison, outcome, checkPrelude)) { - Difference diff = toDifference(comparison); - if (diff != null && cc.haltComparison(diff)) { - return ComparisonResult.CRITICAL; + Difference diff = toDifference(comparison); + if (diff != null && cc.haltComparison(diff)) { + return ComparisonResult.CRITICAL; + } + return outcome; + } + } + + public static class DifferenceEvaluatorAdapter + implements net.sf.xmlunit.diff.DifferenceEvaluator { + private final DifferenceEvaluator de; + public DifferenceEvaluatorAdapter(DifferenceEvaluator d) { + de = d; + } + + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + Difference diff = toDifference(comparison); + if (diff != null) { + switch (de.evaluate(diff)) { + case DifferenceListener + .RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL: + return ComparisonResult.EQUAL; + case DifferenceListener + .RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR: + return ComparisonResult.SIMILAR; + case DifferenceListener + .RETURN_UPGRADE_DIFFERENCE_NODES_DIFFERENT: + return ComparisonResult.DIFFERENT; } - return outcome; } - return ComparisonResult.EQUAL; + return outcome; } } @@ -383,36 +428,30 @@ } public static class DifferenceListener2DifferenceEvaluator - implements DifferenceEvaluator { + implements net.sf.xmlunit.diff.DifferenceEvaluator { private final DifferenceListener dl; - private final IsBetweenDocumentNodeAndRootElement checkPrelude; - public DifferenceListener2DifferenceEvaluator(DifferenceListener dl, - IsBetweenDocumentNodeAndRootElement checkPrelude) { + public DifferenceListener2DifferenceEvaluator(DifferenceListener dl) { this.dl = dl; - this.checkPrelude = checkPrelude; } public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { - if (!swallowComparison(comparison, outcome, checkPrelude)) { - Difference diff = toDifference(comparison); - if (diff != null) { - switch (dl.differenceFound(diff)) { - case DifferenceListener - .RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL: - return ComparisonResult.EQUAL; - case DifferenceListener - .RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR: - return ComparisonResult.SIMILAR; - case DifferenceListener - .RETURN_UPGRADE_DIFFERENCE_NODES_DIFFERENT: - return ComparisonResult.DIFFERENT; - } + Difference diff = toDifference(comparison); + if (diff != null) { + switch (dl.differenceFound(diff)) { + case DifferenceListener + .RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL: + return ComparisonResult.EQUAL; + case DifferenceListener + .RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR: + return ComparisonResult.SIMILAR; + case DifferenceListener + .RETURN_UPGRADE_DIFFERENCE_NODES_DIFFERENT: + return ComparisonResult.DIFFERENT; } - return outcome; } - return ComparisonResult.EQUAL; + return outcome; } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |