From: <bo...@us...> - 2007-04-03 04:28:28
|
Revision: 170 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=170&view=rev Author: bodewig Date: 2007-04-02 21:28:28 -0700 (Mon, 02 Apr 2007) Log Message: ----------- Refactor XPath tests a little Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathEngine.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Jaxp13XpathEngine.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_SimpleXpathEngine.java Added Paths: ----------- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathEngine.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathEngine.java 2007-04-03 04:12:54 UTC (rev 169) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XpathEngine.java 2007-04-03 04:28:28 UTC (rev 170) @@ -55,10 +55,9 @@ * @param select * @param document * @return list of matching nodes - * @throws TransformerException */ NodeList getMatchingNodes(String select, Document document) - throws ConfigurationException, XpathException; + throws XpathException; /** * Evaluate the result of executing the specified xpath syntax @@ -66,10 +65,9 @@ * @param select * @param document * @return evaluated result - * @throws TransformerException */ String evaluate(String select, Document document) - throws ConfigurationException, XpathException; + throws XpathException; /** * Establish a namespace context. Added: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java (rev 0) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java 2007-04-03 04:28:28 UTC (rev 170) @@ -0,0 +1,149 @@ +/* +****************************************************************** +Copyright (c) 2007, 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 java.util.HashMap; +import junit.framework.TestCase; +import org.w3c.dom.Document; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public abstract class AbstractXpathEngineTests extends TestCase { + + protected static final String[] testAttrNames = {"attrOne", "attrTwo"}; + + protected static final String testString = + "<test><nodeWithoutAttributes>intellectual property rights" + + " </nodeWithoutAttributes>" + + "<nodeWithoutAttributes>make us all poorer </nodeWithoutAttributes>" + + "<nodeWithAttributes " + testAttrNames[0] + "=\"open source \" " + + testAttrNames[1] + + "=\"is the answer \">free your code from its chains" + + "</nodeWithAttributes></test>"; + protected Document testDocument; + + protected abstract XpathEngine newXpathEngine(); + + public void testGetMatchingNodesNoMatches() throws Exception { + NodeList nodeList = newXpathEngine().getMatchingNodes("toast", + testDocument); + assertEquals(0, nodeList.getLength()); + } + + public void testGetMatchingNodesMatchRootElement() throws Exception { + NodeList nodeList = newXpathEngine().getMatchingNodes("test", + testDocument); + assertEquals(1, nodeList.getLength()); + assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); + } + + public void testGetMatchingNodesMatchElement() throws Exception { + NodeList nodeList = newXpathEngine() + .getMatchingNodes("test/nodeWithoutAttributes", testDocument); + assertEquals(2, nodeList.getLength()); + assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); + } + + public void testGetMatchingNodesMatchText() throws Exception { + NodeList nodeList = newXpathEngine().getMatchingNodes("test//text()", + testDocument); + assertEquals(3, nodeList.getLength()); + assertEquals(Node.TEXT_NODE, nodeList.item(0).getNodeType()); + } + + public void testGetMatchingNodesCheckSubNodes() throws Exception { + NodeList nodeList = newXpathEngine() + .getMatchingNodes("test/nodeWithAttributes", testDocument); + assertEquals(1, nodeList.getLength()); + Node aNode; + + aNode = nodeList.item(0); + assertEquals(Node.ELEMENT_NODE, aNode.getNodeType()); + assertEquals(true, aNode.hasAttributes()); + assertEquals(true, aNode.hasChildNodes()); + + NodeList children = aNode.getChildNodes(); + int length = children.getLength(); + assertEquals(1, length); + for (int i=0; i < length; ++i) { + assertEquals(Node.TEXT_NODE, children.item(i).getNodeType()); + } + + NamedNodeMap attributes = aNode.getAttributes(); + int numAttrs = attributes.getLength(); + assertEquals(testAttrNames.length, numAttrs); + for (int i=0; i < testAttrNames.length; ++i) { + Node attrNode = attributes.getNamedItem(testAttrNames[i]); + assertNotNull(attrNode); + assertEquals(Node.ATTRIBUTE_NODE, attrNode.getNodeType()); + } + } + + public void testEvaluate() throws Exception { + String result = newXpathEngine().evaluate("count(test//node())", + testDocument); + assertEquals("3 elements and 3 text nodes", "6", result); + } + + public void testXpathPrefixChange() throws Exception { + String testDoc = "<t:test xmlns:t=\"urn:foo\"><t:bar/></t:test>"; + Document d = XMLUnit.buildControlDocument(testDoc); + HashMap m = new HashMap(); + m.put("foo", "urn:foo"); + NamespaceContext ctx = new SimpleNamespaceContext(m); + XpathEngine engine = newXpathEngine(); + engine.setNamespaceContext(ctx); + + NodeList l = engine.getMatchingNodes("//foo:bar", d); + assertEquals(1, l.getLength()); + assertEquals(Node.ELEMENT_NODE, l.item(0).getNodeType()); + + String s = engine.evaluate("count(foo:test//node())", d); + assertEquals("1", s); + } + + public void setUp() throws Exception { + testDocument = XMLUnit.buildControlDocument(testString); + } + + public AbstractXpathEngineTests(String name) { + super(name); + } + + +} \ No newline at end of file Property changes on: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/AbstractXpathEngineTests.java ___________________________________________________________________ Name: svn:eol-style + native Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Jaxp13XpathEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Jaxp13XpathEngine.java 2007-04-03 04:12:54 UTC (rev 169) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/jaxp13/test_Jaxp13XpathEngine.java 2007-04-03 04:28:28 UTC (rev 170) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 200, Jeff Martin, Tim Bacon +Copyright (c) 2006-2007, Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -36,94 +36,18 @@ package org.custommonkey.xmlunit.jaxp13; -import javax.xml.transform.OutputKeys; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.custommonkey.xmlunit.AbstractXpathEngineTests; +import org.custommonkey.xmlunit.XpathEngine; -import org.custommonkey.xmlunit.XMLUnit; - -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - /** * JUnit test for Jaxp13XpathEngine */ -public class test_Jaxp13XpathEngine extends TestCase { - private String[] testAttrNames = {"attrOne", "attrTwo"}; - private String testString = - "<test><nodeWithoutAttributes>intellectual property rights </nodeWithoutAttributes>" - + "<nodeWithoutAttributes>make us all poorer </nodeWithoutAttributes>" - + "<nodeWithAttributes " + testAttrNames[0] + "=\"open source \" " - + testAttrNames[1] + "=\"is the answer \">free your code from its chains" - + "</nodeWithAttributes></test>"; - private Document testDocument; - private Jaxp13XpathEngine simpleXpathEngine = new Jaxp13XpathEngine(); +public class test_Jaxp13XpathEngine extends AbstractXpathEngineTests { - public void testGetMatchingNodesNoMatches() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes("toast", testDocument); - assertEquals(0, nodeList.getLength()); + protected XpathEngine newXpathEngine() { + return new Jaxp13XpathEngine(); } - public void testGetMatchingNodesMatchRootElement() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes("test", testDocument); - assertEquals(1, nodeList.getLength()); - assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); - } - - public void testGetMatchingNodesMatchElement() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test/nodeWithoutAttributes", testDocument); - assertEquals(2, nodeList.getLength()); - assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); - } - - public void testGetMatchingNodesMatchText() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test//text()", testDocument); - assertEquals(3, nodeList.getLength()); - assertEquals(Node.TEXT_NODE, nodeList.item(0).getNodeType()); - } - - public void testGetMatchingNodesCheckSubNodes() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test/nodeWithAttributes", testDocument); - assertEquals(1, nodeList.getLength()); - Node aNode; - - aNode = nodeList.item(0); - assertEquals(Node.ELEMENT_NODE, aNode.getNodeType()); - assertEquals(true, aNode.hasAttributes()); - assertEquals(true, aNode.hasChildNodes()); - - NodeList children = aNode.getChildNodes(); - int length = children.getLength(); - assertEquals(1, length); - for (int i=0; i < length; ++i) { - assertEquals(Node.TEXT_NODE, children.item(i).getNodeType()); - } - - NamedNodeMap attributes = aNode.getAttributes(); - int numAttrs = attributes.getLength(); - assertEquals(testAttrNames.length, numAttrs); - for (int i=0; i < testAttrNames.length; ++i) { - Node attrNode = attributes.getNamedItem(testAttrNames[i]); - assertNotNull(attrNode); - assertEquals(Node.ATTRIBUTE_NODE, attrNode.getNodeType()); - } - } - - public void testEvaluate() throws Exception { - String result = simpleXpathEngine.evaluate( - "count(test//node())", testDocument); - assertEquals("3 elements and 3 text nodes", "6", result); - } - - public void setUp() throws Exception { - testDocument = XMLUnit.buildControlDocument(testString); - } - public test_Jaxp13XpathEngine(String name) { super(name); } Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_SimpleXpathEngine.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_SimpleXpathEngine.java 2007-04-03 04:12:54 UTC (rev 169) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_SimpleXpathEngine.java 2007-04-03 04:28:28 UTC (rev 170) @@ -37,114 +37,47 @@ package org.custommonkey.xmlunit; import javax.xml.transform.OutputKeys; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.w3c.dom.Document; -import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; -import org.w3c.dom.NodeList; /** * JUnit test for SimpleXpathEngine */ -public class test_SimpleXpathEngine extends TestCase { - private String[] testAttrNames = {"attrOne", "attrTwo"}; - private String testString = - "<test><nodeWithoutAttributes>intellectual property rights </nodeWithoutAttributes>" - + "<nodeWithoutAttributes>make us all poorer </nodeWithoutAttributes>" - + "<nodeWithAttributes " + testAttrNames[0] + "=\"open source \" " - + testAttrNames[1] + "=\"is the answer \">free your code from its chains" - + "</nodeWithAttributes></test>"; - private Document testDocument; +public class test_SimpleXpathEngine extends AbstractXpathEngineTests { + private SimpleXpathEngine simpleXpathEngine = new SimpleXpathEngine(); + protected XpathEngine newXpathEngine() { + return simpleXpathEngine; + } + public void testGetXPathResultNode() throws Exception { - Node result = simpleXpathEngine.getXPathResultNode("test", testDocument); + Node result = simpleXpathEngine.getXPathResultNode("test", + testDocument); SimpleSerializer serializer = new SimpleSerializer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); assertEquals(testString, serializer.serialize(result.getFirstChild())); } - public void testGetMatchingNodesNoMatches() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes("toast", testDocument); - assertEquals(0, nodeList.getLength()); - } - - public void testGetMatchingNodesMatchRootElement() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes("test", testDocument); - assertEquals(1, nodeList.getLength()); - assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); - } - - public void testGetMatchingNodesMatchElement() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test/nodeWithoutAttributes", testDocument); - assertEquals(2, nodeList.getLength()); - assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType()); - } - public void testGetMatchingNodesMatchText() throws Exception { if (isJava5OrNewer()) { // fails with "more recent" version of Xalan shipping with Java5 return; } - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test//text()", testDocument); - assertEquals(3, nodeList.getLength()); - assertEquals(Node.TEXT_NODE, nodeList.item(0).getNodeType()); + super.testGetMatchingNodesMatchText(); } - public void testGetMatchingNodesCheckSubNodes() throws Exception { - NodeList nodeList = simpleXpathEngine.getMatchingNodes( - "test/nodeWithAttributes", testDocument); - assertEquals(1, nodeList.getLength()); - Node aNode; - - aNode = nodeList.item(0); - assertEquals(Node.ELEMENT_NODE, aNode.getNodeType()); - assertEquals(true, aNode.hasAttributes()); - assertEquals(true, aNode.hasChildNodes()); - - NodeList children = aNode.getChildNodes(); - int length = children.getLength(); - assertEquals(1, length); - for (int i=0; i < length; ++i) { - assertEquals(Node.TEXT_NODE, children.item(i).getNodeType()); - } - - NamedNodeMap attributes = aNode.getAttributes(); - int numAttrs = attributes.getLength(); - assertEquals(testAttrNames.length, numAttrs); - for (int i=0; i < testAttrNames.length; ++i) { - Node attrNode = attributes.getNamedItem(testAttrNames[i]); - assertNotNull(attrNode); - assertEquals(Node.ATTRIBUTE_NODE, attrNode.getNodeType()); - } - } - public void testEvaluate() throws Exception { if (isJava5OrNewer()) { // fails with "more recent" version of Xalan shipping with Java5 return; } - String result = simpleXpathEngine.evaluate( - "count(test//node())", testDocument); - assertEquals("3 elements and 3 text nodes", "6", result); + super.testEvaluate(); } - public void setUp() throws Exception { - testDocument = XMLUnit.buildControlDocument(testString); - } - public test_SimpleXpathEngine(String name) { super(name); } - public static TestSuite suite() { - return new TestSuite(test_SimpleXpathEngine.class); - } - private static boolean isJava5OrNewer() { try { Class.forName("java.net.Proxy"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |