001: /*
002: ******************************************************************
003: Copyright (c) 2007, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: import java.util.HashMap;
040: import junit.framework.TestCase;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.NamedNodeMap;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.NodeList;
045:
046: public abstract class AbstractXpathEngineTests extends TestCase {
047:
048: protected static final String[] testAttrNames = { "attrOne",
049: "attrTwo" };
050:
051: protected static final String testString = "<test><nodeWithoutAttributes>intellectual property rights"
052: + " </nodeWithoutAttributes>"
053: + "<nodeWithoutAttributes>make us all poorer </nodeWithoutAttributes>"
054: + "<nodeWithAttributes "
055: + testAttrNames[0]
056: + "=\"open source \" "
057: + testAttrNames[1]
058: + "=\"is the answer \">free your code from its chains"
059: + "</nodeWithAttributes></test>";
060: protected Document testDocument;
061:
062: protected abstract XpathEngine newXpathEngine();
063:
064: public void testGetMatchingNodesNoMatches() throws Exception {
065: NodeList nodeList = newXpathEngine().getMatchingNodes("toast",
066: testDocument);
067: assertEquals(0, nodeList.getLength());
068: }
069:
070: public void testGetMatchingNodesMatchRootElement() throws Exception {
071: NodeList nodeList = newXpathEngine().getMatchingNodes("test",
072: testDocument);
073: assertEquals(1, nodeList.getLength());
074: assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType());
075: }
076:
077: public void testGetMatchingNodesMatchElement() throws Exception {
078: NodeList nodeList = newXpathEngine().getMatchingNodes(
079: "test/nodeWithoutAttributes", testDocument);
080: assertEquals(2, nodeList.getLength());
081: assertEquals(Node.ELEMENT_NODE, nodeList.item(0).getNodeType());
082: }
083:
084: public void testGetMatchingNodesMatchText() throws Exception {
085: NodeList nodeList = newXpathEngine().getMatchingNodes(
086: "test//text()", testDocument);
087: assertEquals(3, nodeList.getLength());
088: assertEquals(Node.TEXT_NODE, nodeList.item(0).getNodeType());
089: }
090:
091: public void testGetMatchingNodesCheckSubNodes() throws Exception {
092: NodeList nodeList = newXpathEngine().getMatchingNodes(
093: "test/nodeWithAttributes", testDocument);
094: assertEquals(1, nodeList.getLength());
095: Node aNode;
096:
097: aNode = nodeList.item(0);
098: assertEquals(Node.ELEMENT_NODE, aNode.getNodeType());
099: assertEquals(true, aNode.hasAttributes());
100: assertEquals(true, aNode.hasChildNodes());
101:
102: NodeList children = aNode.getChildNodes();
103: int length = children.getLength();
104: assertEquals(1, length);
105: for (int i = 0; i < length; ++i) {
106: assertEquals(Node.TEXT_NODE, children.item(i).getNodeType());
107: }
108:
109: NamedNodeMap attributes = aNode.getAttributes();
110: int numAttrs = attributes.getLength();
111: assertEquals(testAttrNames.length, numAttrs);
112: for (int i = 0; i < testAttrNames.length; ++i) {
113: Node attrNode = attributes.getNamedItem(testAttrNames[i]);
114: assertNotNull(attrNode);
115: assertEquals(Node.ATTRIBUTE_NODE, attrNode.getNodeType());
116: }
117: }
118:
119: public void testEvaluate() throws Exception {
120: String result = newXpathEngine().evaluate(
121: "count(test//node())", testDocument);
122: assertEquals("3 elements and 3 text nodes", "6", result);
123: }
124:
125: public void testXpathPrefixChange() throws Exception {
126: String testDoc = "<t:test xmlns:t=\"urn:foo\"><t:bar/></t:test>";
127: Document d = XMLUnit.buildControlDocument(testDoc);
128: HashMap m = new HashMap();
129: m.put("foo", "urn:foo");
130: NamespaceContext ctx = new SimpleNamespaceContext(m);
131: XpathEngine engine = newXpathEngine();
132: engine.setNamespaceContext(ctx);
133:
134: NodeList l = engine.getMatchingNodes("//foo:bar", d);
135: assertEquals(1, l.getLength());
136: assertEquals(Node.ELEMENT_NODE, l.item(0).getNodeType());
137:
138: String s = engine.evaluate("count(foo:test//node())", d);
139: assertEquals("1", s);
140: }
141:
142: public void setUp() throws Exception {
143: testDocument = XMLUnit.buildControlDocument(testString);
144: }
145:
146: public AbstractXpathEngineTests(String name) {
147: super(name);
148: }
149:
150: }
|