From: <bo...@us...> - 2010-06-18 14:51:32
|
Revision: 401 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=401&view=rev Author: bodewig Date: 2010-06-18 14:51:25 +0000 (Fri, 18 Jun 2010) Log Message: ----------- very early steps to a DOM based DifferenceEngine Modified Paths: -------------- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java 2010-06-18 14:51:25 UTC (rev 401) @@ -0,0 +1,99 @@ +/* + 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 net.sf.xmlunit.util.Convert; +import javax.xml.transform.Source; +import org.w3c.dom.Node; +import org.w3c.dom.Document; + +/** + * Difference engine based on DOM. + */ +public final class DOMDifferenceEngine implements DifferenceEngine { + private final ComparisonListenerSupport listeners = + new ComparisonListenerSupport(); + private ElementSelector elementSelector = ElementSelectors.Default; + private DifferenceEvaluator diffEvaluator = DifferenceEvaluators.Default; + + public void addComparisonListener(ComparisonListener l) { + if (l == null) { + throw new IllegalArgumentException("listener must not be null"); + } + listeners.addComparisonListener(l); + } + + public void addMatchListener(ComparisonListener l) { + if (l == null) { + throw new IllegalArgumentException("listener must not be null"); + } + listeners.addMatchListener(l); + } + + public void addDifferenceListener(ComparisonListener l) { + if (l == null) { + throw new IllegalArgumentException("listener must not be null"); + } + listeners.addDifferenceListener(l); + } + + public void setElementSelector(ElementSelector s) { + if (s == null) { + throw new IllegalArgumentException("element selector must" + + " not be null"); + } + elementSelector = s; + } + + public void setDifferenceEvaluator(DifferenceEvaluator e) { + if (e == null) { + throw new IllegalArgumentException("difference evaluator must" + + " not be null"); + } + diffEvaluator = e; + } + + public void compare(Source control, Source test) { + if (control == null) { + throw new IllegalArgumentException("control must not be null"); + } + if (test == null) { + throw new IllegalArgumentException("test must not be null"); + } + compareNodes(Convert.toNode(control), Convert.toNode(test)); + } + + private ComparisonResult compareNodes(Node control, Node test) { + return ComparisonResult.EQUAL; + } + + /** + * Compares the detail values for object equality, lets the + * difference evaluator evaluate the result, notifies all + * listeners and returns the outcome. + * + * <p>package private to support tests.</p> + */ + ComparisonResult compare(Comparison comp) { + Object controlValue = comp.getControlNodeDetails().getValue(); + Object testValue = comp.getTestNodeDetails().getValue(); + boolean equal = controlValue == null + ? testValue == null : controlValue.equals(testValue); + ComparisonResult initial = + equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; + ComparisonResult altered = diffEvaluator.evaluate(comp, initial); + listeners.fireComparisonPerformed(comp, altered); + return altered; + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/DOMDifferenceEngine.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java 2010-05-21 15:19:15 UTC (rev 400) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java 2010-06-18 14:51:25 UTC (rev 401) @@ -51,12 +51,12 @@ s.fireComparisonPerformed(null, ComparisonResult.EQUAL); } - private static class Listener implements ComparisonListener { + static class Listener implements ComparisonListener { private final HashSet<ComparisonResult> acceptable = new HashSet<ComparisonResult>(); private int invocations = 0; - private Listener(ComparisonResult... accept) { + Listener(ComparisonResult... accept) { acceptable.addAll(Arrays.asList(accept)); } @@ -67,5 +67,9 @@ fail("unexpected outcome: " + outcome); } } + + int getInvocations() { + return invocations; + } } } Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java 2010-06-18 14:51:25 UTC (rev 401) @@ -0,0 +1,114 @@ +/* + 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.junit.Test; +import static org.junit.Assert.*; + +public class DOMDifferenceEngineTest { + + private static class ResultGrabber implements DifferenceEvaluator { + private ComparisonResult outcome = ComparisonResult.CRITICAL; + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + this.outcome = outcome; + return outcome; + } + } + + @Test public void compareTwoNulls() { + ResultGrabber g = new ResultGrabber(); + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.setDifferenceEvaluator(g); + assertEquals(ComparisonResult.EQUAL, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, null, + null, null, null))); + assertEquals(ComparisonResult.EQUAL, g.outcome); + } + + @Test public void compareControlNullTestNonNull() { + ResultGrabber g = new ResultGrabber(); + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.setDifferenceEvaluator(g); + assertEquals(ComparisonResult.DIFFERENT, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, null, + null, null, ""))); + assertEquals(ComparisonResult.DIFFERENT, g.outcome); + } + + @Test public void compareControlNonNullTestNull() { + ResultGrabber g = new ResultGrabber(); + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.setDifferenceEvaluator(g); + assertEquals(ComparisonResult.DIFFERENT, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, "", + null, null, null))); + assertEquals(ComparisonResult.DIFFERENT, g.outcome); + } + + @Test public void compareTwoDifferentNonNulls() { + ResultGrabber g = new ResultGrabber(); + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.setDifferenceEvaluator(g); + assertEquals(ComparisonResult.DIFFERENT, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, new Short("1"), + null, null, new Short("2")))); + assertEquals(ComparisonResult.DIFFERENT, g.outcome); + } + + @Test public void compareTwoEqualNonNulls() { + ResultGrabber g = new ResultGrabber(); + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.setDifferenceEvaluator(g); + assertEquals(ComparisonResult.EQUAL, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, new Short("2"), + null, null, new Short("2")))); + assertEquals(ComparisonResult.EQUAL, g.outcome); + } + + @Test public void compareNotifiesListener() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + ComparisonListenerSupportTest.Listener l = + new ComparisonListenerSupportTest.Listener(ComparisonResult.EQUAL); + d.addComparisonListener(l); + assertEquals(ComparisonResult.EQUAL, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, new Short("2"), + null, null, new Short("2")))); + assertEquals(1, l.getInvocations()); + } + + @Test public void compareUsesResultOfEvaluator() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + ComparisonListenerSupportTest.Listener l = + new ComparisonListenerSupportTest.Listener(ComparisonResult.SIMILAR); + d.addComparisonListener(l); + d.setDifferenceEvaluator(new DifferenceEvaluator() { + public ComparisonResult evaluate(Comparison comparison, + ComparisonResult outcome) { + return ComparisonResult.SIMILAR; + } + }); + assertEquals(ComparisonResult.SIMILAR, + d.compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, new Short("2"), + null, null, new Short("2")))); + assertEquals(1, l.getInvocations()); + } +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |