From: <bo...@us...> - 2010-05-07 11:45:57
|
Revision: 383 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=383&view=rev Author: bodewig Date: 2010-05-07 11:45:51 +0000 (Fri, 07 May 2010) Log Message: ----------- default difference evaluators Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java 2010-05-07 05:48:03 UTC (rev 382) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonType.java 2010-05-07 11:45:51 UTC (rev 383) @@ -29,12 +29,12 @@ * If the documents both have DOCTYPEs, compare the PUBLIC * identifiers. */ - DOCTYPE_PUBLIC, + DOCTYPE_PUBLIC_ID, /** * If the documents both have DOCTYPEs, compare the SYSTEM * identifiers. */ - DOCTYPE_SYSTEM, + DOCTYPE_SYSTEM_ID, /** * Check whether both documents provide the same values for Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java 2010-05-07 11:45:51 UTC (rev 383) @@ -0,0 +1,92 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.diff; + +import org.w3c.dom.Node; + +/** + * Evaluators used for the base cases. + */ +public final class DifferenceEvaluators { + private DifferenceEvaluators() { } + + private static final Short CDATA = Node.TEXT_NODE; + private static final Short TEXT = Node.CDATA_SECTION_NODE; + + /** + * The "standard" difference evaluator which decides which + * differences make two XML documents really different and which + * still leave them similar. + */ + public static final DifferenceEvaluator Default = + new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + if (outcome == ComparisonResult.DIFFERENT) { + switch (comparison.getType()) { + case NODE_TYPE: + Short control = (Short) comparison + .getControlNodeDetails().getValue(); + Short test = (Short) comparison + .getTestNodeDetails().getValue(); + if ((control.equals(TEXT) && test.equals(CDATA)) + || + (control.equals(CDATA) && test.equals(TEXT))) { + outcome = ComparisonResult.SIMILAR; + } + break; + case HAS_DOCTYPE_DECLARATION: + case DOCTYPE_SYSTEM_ID: + case SCHEMA_LOCATION: + case NO_NAMESPACE_SCHEMA_LOCATION: + case NAMESPACE_PREFIX: + case ATTR_VALUE_EXPLICITLY_SPECIFIED: + case CHILD_NODELIST_SEQUENCE: + outcome = ComparisonResult.SIMILAR; + break; + } + } + return outcome; + } + }; + + /** + * Makes the comparison stop as soon as the first "real" + * difference is encountered, uses the {@link #Default default} + * evaluator to decide which differences leave the documents + * simlar. + */ + public static final DifferenceEvaluator defaultStopWhenDifferent + = stopWhenDifferent(Default); + + /** + * Makes the comparison stop as soon as the first "real" + * difference is encountered. + * @param nestedEvaluator provides the initial decision whether a + * difference is "real" or still leaves the documents in a similar + * state. + */ + public static DifferenceEvaluator + stopWhenDifferent(final DifferenceEvaluator nestedEvaluator) { + return new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + ComparisonResult r = nestedEvaluator.evaluate(comparison, + outcome); + return r == ComparisonResult.DIFFERENT + ? ComparisonResult.CRITICAL : r; + } + }; + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DifferenceEvaluators.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs 2010-05-07 05:48:03 UTC (rev 382) +++ trunk/xmlunit/src/main/net-core/diff/ComparisonType.cs 2010-05-07 11:45:51 UTC (rev 383) @@ -30,12 +30,12 @@ /// If the documents both have DOCTYPEs, compare the PUBLIC /// identifiers. /// </summary> - DOCTYPE_PUBLIC, + DOCTYPE_PUBLIC_ID, /// <summary> /// If the documents both have DOCTYPEs, compare the SYSTEM /// identifiers. /// </summary> - DOCTYPE_SYSTEM, + DOCTYPE_SYSTEM_ID, /// <summary> /// Check whether both documents provide the same values for Added: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs 2010-05-07 11:45:51 UTC (rev 383) @@ -0,0 +1,91 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +using System.Xml; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Evaluators used for the base cases. + /// </summary> + public sealed class DifferenceEvaluators { + private DifferenceEvaluators() { } + + /// <summary> + /// The "standard" difference evaluator which decides which + /// differences make two XML documents really different and which + /// still leave them similar. + /// </summary> + public static ComparisonResult Default(Comparison comparison, + ComparisonResult outcome) { + if (outcome == ComparisonResult.DIFFERENT) { + switch (comparison.Type) { + case ComparisonType.NODE_TYPE: + XmlNodeType control = + (XmlNodeType) comparison.ControlNodeDetails.Value; + XmlNodeType test = + (XmlNodeType) comparison.TestNodeDetails.Value; + if ((control == XmlNodeType.Text && test == XmlNodeType.CDATA) + || + (control == XmlNodeType.CDATA && test == XmlNodeType.Text) + ) { + outcome = ComparisonResult.SIMILAR; + } + break; + case ComparisonType.HAS_DOCTYPE_DECLARATION: + case ComparisonType.DOCTYPE_SYSTEM_ID: + case ComparisonType.SCHEMA_LOCATION: + case ComparisonType.NO_NAMESPACE_SCHEMA_LOCATION: + case ComparisonType.NAMESPACE_PREFIX: + case ComparisonType.ATTR_VALUE_EXPLICITLY_SPECIFIED: + case ComparisonType.CHILD_NODELIST_SEQUENCE: + outcome = ComparisonResult.SIMILAR; + break; + } + } + return outcome; + } + + private static readonly DifferenceEvaluator defaultStopWhenDifferent + = StopWhenDifferent(Default); + + /// <summary> + /// Makes the comparison stop as soon as the first "real" + /// difference is encountered, uses the {@link #Default default} + /// evaluator to decide which differences leave the documents + /// simlar. + /// </summary> + public static DifferenceEvaluator DefaultStopWhenDifferent { + get { + return defaultStopWhenDifferent; + } + } + + /// <summary> + /// Makes the comparison stop as soon as the first "real" + /// difference is encountered. + /// </summary> + /// <param name="nestedEvaluator">provides the initial + /// decision whether a difference is "real" or still leaves + /// the documents in a similar state.</param> + public static DifferenceEvaluator + StopWhenDifferent(DifferenceEvaluator nestedEvaluator) { + return delegate(Comparison comparison, ComparisonResult outcome) { + ComparisonResult r = nestedEvaluator(comparison, outcome); + return r == ComparisonResult.DIFFERENT + ? ComparisonResult.CRITICAL : r; + }; + } + } +} \ No newline at end of file Property changes on: trunk/xmlunit/src/main/net-core/diff/DifferenceEvaluators.cs ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |