From: <bo...@us...> - 2010-06-18 15:21:31
|
Revision: 402 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=402&view=rev Author: bodewig Date: 2010-06-18 15:21:25 +0000 (Fri, 18 Jun 2010) Log Message: ----------- very early steps to a DOM based DifferenceEngine Added Paths: ----------- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs Added: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs =================================================================== --- trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs (rev 0) +++ trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs 2010-06-18 15:21:25 UTC (rev 402) @@ -0,0 +1,94 @@ +/* + 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; +using System.Xml; + +namespace net.sf.xmlunit.diff { + + /// <summary> + /// Difference engine based on DOM. + /// </summary> + public sealed class DOMDifferenceEngine : IDifferenceEngine { + public event ComparisonListener ComparisonListener; + public event ComparisonListener MatchListener; + public event ComparisonListener DifferenceListener; + + private ElementSelector elementSelector = ElementSelectors.Default; + public ElementSelector ElementSelector { + set { + if (value == null) { + throw new ArgumentNullException("element selector"); + } + elementSelector = value; + } + } + + private DifferenceEvaluator diffEvaluator = DifferenceEvaluators.Default; + public DifferenceEvaluator DifferenceEvaluator { + set { + if (value == null) { + throw new ArgumentNullException("difference evaluator"); + } + diffEvaluator = value; + } + } + + public void Compare(ISource control, ISource test) { + if (control == null) { + throw new ArgumentNullException("control"); + } + if (test == null) { + throw new ArgumentNullException("test"); + } + + CompareNodes(net.sf.xmlunit.util.Convert.ToNode(control), + net.sf.xmlunit.util.Convert.ToNode(test)); + } + + private ComparisonResult CompareNodes(XmlNode control, XmlNode test) { + return ComparisonResult.EQUAL; + } + + /// <summary> + /// Compares the detail values for object equality, lets the + /// difference evaluator evaluate the result, notifies all + /// listeners and returns the outcome. + /// </summary> + public ComparisonResult Compare(Comparison comp) { + object controlValue = comp.ControlNodeDetails.Value; + object testValue = comp.TestNodeDetails.Value; + bool equal = controlValue == null + ? testValue == null : controlValue.Equals(testValue); + ComparisonResult initial = + equal ? ComparisonResult.EQUAL : ComparisonResult.DIFFERENT; + ComparisonResult altered = diffEvaluator(comp, initial); + FireComparisonPerformed(comp, altered); + return altered; + } + + public void FireComparisonPerformed(Comparison comp, + ComparisonResult outcome) { + if (ComparisonListener != null) { + ComparisonListener(comp, outcome); + } + if (outcome == ComparisonResult.EQUAL && MatchListener != null) { + MatchListener(comp, outcome); + } else if (outcome != ComparisonResult.EQUAL + && DifferenceListener != null) { + DifferenceListener(comp, outcome); + } + } + } +} Property changes on: trunk/xmlunit/src/main/net-core/diff/DOMDifferenceEngine.cs ___________________________________________________________________ Added: svn:eol-style + native Copied: trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs (from rev 401, trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/DOMDifferenceEngineTest.java) =================================================================== --- trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs (rev 0) +++ trunk/xmlunit/src/tests/net-core/diff/DOMDifferenceEngineTest.cs 2010-06-18 15:21:25 UTC (rev 402) @@ -0,0 +1,128 @@ +/* + 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; +using NUnit.Framework; + +namespace net.sf.xmlunit.diff { + + [TestFixture] + public class DOMDifferenceEngineTest { + + private ComparisonResult outcome = ComparisonResult.CRITICAL; + private ComparisonResult ResultGrabber(Comparison comparison, + ComparisonResult outcome) { + this.outcome = outcome; + return outcome; + } + + [Test] + public void CompareTwoNulls() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.DifferenceEvaluator = ResultGrabber; + Assert.AreEqual(ComparisonResult.EQUAL, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, null, + null, null, null))); + Assert.AreEqual(ComparisonResult.EQUAL, outcome); + } + + [Test] + public void CompareControlNullTestNonNull() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.DifferenceEvaluator = ResultGrabber; + Assert.AreEqual(ComparisonResult.DIFFERENT, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, null, + null, null, ""))); + Assert.AreEqual(ComparisonResult.DIFFERENT, outcome); + } + + [Test] + public void CompareControlNonNullTestNull() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.DifferenceEvaluator = ResultGrabber; + Assert.AreEqual(ComparisonResult.DIFFERENT, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, "", + null, null, null))); + Assert.AreEqual(ComparisonResult.DIFFERENT, outcome); + } + + [Test] + public void CompareTwoDifferentNonNulls() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.DifferenceEvaluator = ResultGrabber; + Assert.AreEqual(ComparisonResult.DIFFERENT, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, + Convert.ToInt16("1"), + null, null, + Convert.ToInt16("2")))); + Assert.AreEqual(ComparisonResult.DIFFERENT, outcome); + } + + [Test] + public void CompareTwoEqualNonNulls() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + d.DifferenceEvaluator = ResultGrabber; + Assert.AreEqual(ComparisonResult.EQUAL, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, + Convert.ToInt16("2"), + null, null, + Convert.ToInt16("2")))); + Assert.AreEqual(ComparisonResult.EQUAL, outcome); + } + + [Test] + public void CompareNotifiesListener() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + int invocations = 0; + d.ComparisonListener += delegate(Comparison comp, + ComparisonResult r) { + invocations++; + Assert.AreEqual(ComparisonResult.EQUAL, r); + }; + Assert.AreEqual(ComparisonResult.EQUAL, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, + Convert.ToInt16("2"), + null, null, + Convert.ToInt16("2")))); + Assert.AreEqual(1, invocations); + } + + [Test] + public void CompareUsesResultOfEvaluator() { + DOMDifferenceEngine d = new DOMDifferenceEngine(); + int invocations = 0; + d.ComparisonListener += delegate(Comparison comp, + ComparisonResult r) { + invocations++; + Assert.AreEqual(ComparisonResult.SIMILAR, r); + }; + d.DifferenceEvaluator = delegate(Comparison comparison, + ComparisonResult outcome) { + return ComparisonResult.SIMILAR; + }; + Assert.AreEqual(ComparisonResult.SIMILAR, + d.Compare(new Comparison(ComparisonType.HAS_DOCTYPE_DECLARATION, + null, null, + Convert.ToInt16("2"), + null, null, + Convert.ToInt16("2")))); + Assert.AreEqual(1, invocations); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |