From: <bo...@us...> - 2007-02-02 21:36:25
|
Revision: 146 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=146&view=rev Author: bodewig Date: 2007-02-02 13:36:23 -0800 (Fri, 02 Feb 2007) Log Message: ----------- Move MultiLevelElementNameAndTextQualifier to examples package Added Paths: ----------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/examples/MultiLevelElementNameAndTextQualifier.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/examples/test_MultiLevelElementNameAndTextQualifier.java Removed Paths: ------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/MultiLevelElementNameAndTextQualifier.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_MultiLevelElementNameAndTextQualifier.java Deleted: trunk/xmlunit/src/java/org/custommonkey/xmlunit/MultiLevelElementNameAndTextQualifier.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/MultiLevelElementNameAndTextQualifier.java 2007-02-02 20:44:21 UTC (rev 145) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/MultiLevelElementNameAndTextQualifier.java 2007-02-02 21:36:23 UTC (rev 146) @@ -1,117 +0,0 @@ -/* -****************************************************************** -Copyright (c) 2001, 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; - -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.w3c.dom.Text; - -/** - * Per popular request an interface implementation that uses element - * names and the text node containes in the n'th child node to compare - * elements. - * - * <p>This means {@link ElementNameAndTextQualifier - * ElementNameQualifier} and MultiLevelElementNameQualifier(1) should - * lead to the same results.</p> - * - * <p>Any attribute values ar completely ignored. Only works on - * elements with exactly one child element at each level.</p> - * - * <p>This class mostly exists as an example for custom ElementQualifiers.</p> - */ -public class MultiLevelElementNameAndTextQualifier - implements ElementQualifier { - - private final int levels; - - private static final ElementNameQualifier NAME_QUALIFIER = - new ElementNameQualifier(); - private static final ElementNameAndTextQualifier NAME_AND_TEXT_QUALIFIER = - new ElementNameAndTextQualifier(); - - /** - * Uses element names and the text nested <code>levels</code> - * child elements deeper into the element to compare elements. - */ - public MultiLevelElementNameAndTextQualifier(int levels) { - if (levels < 1) { - throw new IllegalArgumentException("levels must be equal or" - + " greater than one"); - } - this.levels = levels; - } - - public boolean qualifyForComparison(Element control, Element test) { - boolean stillSimilar = true; - Element currentControl = control; - Element currentTest = test; - - // match on element names only for leading levels - for (int currentLevel = 0; stillSimilar && currentLevel <= levels - 2; - currentLevel++) { - stillSimilar = NAME_QUALIFIER.qualifyForComparison(currentControl, - currentTest); - if (stillSimilar) { - if (currentControl.hasChildNodes() - && currentTest.hasChildNodes()) { - Node n1 = currentControl.getFirstChild(); - Node n2 = currentTest.getFirstChild(); - if (n1.getNodeType() == Node.ELEMENT_NODE - && n2.getNodeType() == Node.ELEMENT_NODE) { - currentControl = (Element) n1; - currentTest = (Element) n2; - } else { - stillSimilar = false; - } - } else { - stillSimilar = false; - } - } - } - - // finally compare the level containing the text child node - if (stillSimilar) { - stillSimilar = NAME_AND_TEXT_QUALIFIER - .qualifyForComparison(currentControl, currentTest); - } - - return stillSimilar; - } - -} Copied: trunk/xmlunit/src/java/org/custommonkey/xmlunit/examples/MultiLevelElementNameAndTextQualifier.java (from rev 144, trunk/xmlunit/src/java/org/custommonkey/xmlunit/MultiLevelElementNameAndTextQualifier.java) =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/examples/MultiLevelElementNameAndTextQualifier.java (rev 0) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/examples/MultiLevelElementNameAndTextQualifier.java 2007-02-02 21:36:23 UTC (rev 146) @@ -0,0 +1,121 @@ +/* +****************************************************************** +Copyright (c) 2001, 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.examples; + +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; + +import org.custommonkey.xmlunit.ElementNameAndTextQualifier; +import org.custommonkey.xmlunit.ElementNameQualifier; +import org.custommonkey.xmlunit.ElementQualifier; + +/** + * Per popular request an interface implementation that uses element + * names and the text node containes in the n'th child node to compare + * elements. + * + * <p>This means {@link ElementNameAndTextQualifier + * ElementNameQualifier} and MultiLevelElementNameQualifier(1) should + * lead to the same results.</p> + * + * <p>Any attribute values ar completely ignored. Only works on + * elements with exactly one child element at each level.</p> + * + * <p>This class mostly exists as an example for custom ElementQualifiers.</p> + */ +public class MultiLevelElementNameAndTextQualifier + implements ElementQualifier { + + private final int levels; + + private static final ElementNameQualifier NAME_QUALIFIER = + new ElementNameQualifier(); + private static final ElementNameAndTextQualifier NAME_AND_TEXT_QUALIFIER = + new ElementNameAndTextQualifier(); + + /** + * Uses element names and the text nested <code>levels</code> + * child elements deeper into the element to compare elements. + */ + public MultiLevelElementNameAndTextQualifier(int levels) { + if (levels < 1) { + throw new IllegalArgumentException("levels must be equal or" + + " greater than one"); + } + this.levels = levels; + } + + public boolean qualifyForComparison(Element control, Element test) { + boolean stillSimilar = true; + Element currentControl = control; + Element currentTest = test; + + // match on element names only for leading levels + for (int currentLevel = 0; stillSimilar && currentLevel <= levels - 2; + currentLevel++) { + stillSimilar = NAME_QUALIFIER.qualifyForComparison(currentControl, + currentTest); + if (stillSimilar) { + if (currentControl.hasChildNodes() + && currentTest.hasChildNodes()) { + Node n1 = currentControl.getFirstChild(); + Node n2 = currentTest.getFirstChild(); + if (n1.getNodeType() == Node.ELEMENT_NODE + && n2.getNodeType() == Node.ELEMENT_NODE) { + currentControl = (Element) n1; + currentTest = (Element) n2; + } else { + stillSimilar = false; + } + } else { + stillSimilar = false; + } + } + } + + // finally compare the level containing the text child node + if (stillSimilar) { + stillSimilar = NAME_AND_TEXT_QUALIFIER + .qualifyForComparison(currentControl, currentTest); + } + + return stillSimilar; + } + +} Copied: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/examples/test_MultiLevelElementNameAndTextQualifier.java (from rev 144, trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_MultiLevelElementNameAndTextQualifier.java) =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/examples/test_MultiLevelElementNameAndTextQualifier.java (rev 0) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/examples/test_MultiLevelElementNameAndTextQualifier.java 2007-02-02 21:36:23 UTC (rev 146) @@ -0,0 +1,147 @@ +/* +****************************************************************** +Copyright (c) 2001, 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.examples; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.ElementNameAndTextQualifier; +import org.custommonkey.xmlunit.ElementQualifier; +import org.custommonkey.xmlunit.XMLUnit; + +/** + * JUnit testcase for MultiLevelElementNameAndTextQualifier + * @see test_Diff#testRepeatedElementNamesWithTextQualification() + */ +public class test_MultiLevelElementNameAndTextQualifier extends TestCase { + private static final String TAG_NAME = "tagYoureIt"; + private static final String TAG_NAME2 = "tagYoureIt2"; + private static final String TEXT_A = "textA"; + private static final String TEXT_B = "textB"; + private Document document; + + // copy of ElementNameAndTextQualifier test + public void testSingleTextValue() throws Exception { + ElementQualifier qualifier = + new MultiLevelElementNameAndTextQualifier(1); + + Element control = document.createElement(TAG_NAME); + control.appendChild(document.createTextNode(TEXT_A)); + + Element test = document.createElement(TAG_NAME); + + assertFalse("control text not comparable to empty text", + qualifier.qualifyForComparison(control, test)); + + test.appendChild(document.createTextNode(TEXT_A)); + assertTrue("control textA comparable to test textA", + qualifier.qualifyForComparison(control, test)); + + test = document.createElement(TAG_NAME); + + test.appendChild(document.createTextNode(TEXT_B)); + assertFalse("control textA not comparable to test textB", + qualifier.qualifyForComparison(control, test)); + } + + // copy of ElementNameAndTextQualifier test + public void testMultipleTextValues() throws Exception { + ElementQualifier qualifier = + new MultiLevelElementNameAndTextQualifier(1); + + Element control = document.createElement(TAG_NAME); + control.appendChild(document.createTextNode(TEXT_A)); + control.appendChild(document.createTextNode(TEXT_B)); + + Element test = document.createElement(TAG_NAME); + test.appendChild(document.createTextNode(TEXT_A + TEXT_B)); + assertTrue("denormalised control text comparable to normalised test text", + qualifier.qualifyForComparison(control, test)); + } + + // three levels + public void testThreeLevels() throws Exception { + ElementQualifier qualifier = + new MultiLevelElementNameAndTextQualifier(3); + + Element control = document.createElement(TAG_NAME); + Element child = document.createElement(TAG_NAME2); + control.appendChild(child); + Element child2 = document.createElement(TAG_NAME); + child.appendChild(child2); + child2.appendChild(document.createTextNode(TEXT_B)); + + Element test = document.createElement(TAG_NAME); + child = document.createElement(TAG_NAME2); + test.appendChild(child); + child2 = document.createElement(TAG_NAME); + child.appendChild(child2); + child2.appendChild(document.createTextNode(TEXT_B)); + + assertTrue(qualifier.qualifyForComparison(control, test)); + } + + /** + * @see https://sourceforge.net/forum/forum.php?thread_id=1440169&forum_id=73274 + */ + public void testThread1440169() throws Exception { + String s1 = "<a><b><c>foo</c></b><b><c>bar</c></b></a>"; + String s2 = "<a><b><c>bar</c></b><b><c>foo</c></b></a>"; + Diff d = new Diff(s1, s2); + assertFalse(d.similar()); + + // reset + d = new Diff(s1, s2); + d.overrideElementQualifier(new ElementNameAndTextQualifier()); + assertFalse(d.similar()); + + // reset once again + d = new Diff(s1, s2); + d.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(2)); + assertTrue(d.similar()); + + } + + public void setUp() throws Exception { + document = XMLUnit.newControlParser().newDocument(); + } + +} Deleted: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_MultiLevelElementNameAndTextQualifier.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_MultiLevelElementNameAndTextQualifier.java 2007-02-02 20:44:21 UTC (rev 145) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_MultiLevelElementNameAndTextQualifier.java 2007-02-02 21:36:23 UTC (rev 146) @@ -1,142 +0,0 @@ -/* -****************************************************************** -Copyright (c) 2001, 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; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; - -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * JUnit testcase for MultiLevelElementNameAndTextQualifier - * @see test_Diff#testRepeatedElementNamesWithTextQualification() - */ -public class test_MultiLevelElementNameAndTextQualifier extends TestCase { - private static final String TAG_NAME = "tagYoureIt"; - private static final String TAG_NAME2 = "tagYoureIt2"; - private static final String TEXT_A = "textA"; - private static final String TEXT_B = "textB"; - private Document document; - - // copy of ElementNameAndTextQualifier test - public void testSingleTextValue() throws Exception { - ElementQualifier qualifier = - new MultiLevelElementNameAndTextQualifier(1); - - Element control = document.createElement(TAG_NAME); - control.appendChild(document.createTextNode(TEXT_A)); - - Element test = document.createElement(TAG_NAME); - - assertFalse("control text not comparable to empty text", - qualifier.qualifyForComparison(control, test)); - - test.appendChild(document.createTextNode(TEXT_A)); - assertTrue("control textA comparable to test textA", - qualifier.qualifyForComparison(control, test)); - - test = document.createElement(TAG_NAME); - - test.appendChild(document.createTextNode(TEXT_B)); - assertFalse("control textA not comparable to test textB", - qualifier.qualifyForComparison(control, test)); - } - - // copy of ElementNameAndTextQualifier test - public void testMultipleTextValues() throws Exception { - ElementQualifier qualifier = - new MultiLevelElementNameAndTextQualifier(1); - - Element control = document.createElement(TAG_NAME); - control.appendChild(document.createTextNode(TEXT_A)); - control.appendChild(document.createTextNode(TEXT_B)); - - Element test = document.createElement(TAG_NAME); - test.appendChild(document.createTextNode(TEXT_A + TEXT_B)); - assertTrue("denormalised control text comparable to normalised test text", - qualifier.qualifyForComparison(control, test)); - } - - // three levels - public void testThreeLevels() throws Exception { - ElementQualifier qualifier = - new MultiLevelElementNameAndTextQualifier(3); - - Element control = document.createElement(TAG_NAME); - Element child = document.createElement(TAG_NAME2); - control.appendChild(child); - Element child2 = document.createElement(TAG_NAME); - child.appendChild(child2); - child2.appendChild(document.createTextNode(TEXT_B)); - - Element test = document.createElement(TAG_NAME); - child = document.createElement(TAG_NAME2); - test.appendChild(child); - child2 = document.createElement(TAG_NAME); - child.appendChild(child2); - child2.appendChild(document.createTextNode(TEXT_B)); - - assertTrue(qualifier.qualifyForComparison(control, test)); - } - - /** - * @see https://sourceforge.net/forum/forum.php?thread_id=1440169&forum_id=73274 - */ - public void testThread1440169() throws Exception { - String s1 = "<a><b><c>foo</c></b><b><c>bar</c></b></a>"; - String s2 = "<a><b><c>bar</c></b><b><c>foo</c></b></a>"; - Diff d = new Diff(s1, s2); - assertFalse(d.similar()); - - // reset - d = new Diff(s1, s2); - d.overrideElementQualifier(new ElementNameAndTextQualifier()); - assertFalse(d.similar()); - - // reset once again - d = new Diff(s1, s2); - d.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(2)); - assertTrue(d.similar()); - - } - - public void setUp() throws Exception { - document = XMLUnit.newControlParser().newDocument(); - } - -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |