001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.xpath;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.util.List;
022: import javax.xml.parsers.DocumentBuilder;
023: import javax.xml.parsers.DocumentBuilderFactory;
024: import javax.xml.parsers.ParserConfigurationException;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamSource;
028:
029: import junit.framework.TestCase;
030: import org.springframework.core.io.ClassPathResource;
031: import org.springframework.xml.sax.SaxUtils;
032: import org.springframework.xml.transform.ResourceSource;
033: import org.w3c.dom.DOMException;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Node;
036: import org.xml.sax.SAXException;
037:
038: public abstract class AbstractXPathTemplateTestCase extends TestCase {
039:
040: XPathOperations template;
041:
042: private Source namespaces;
043:
044: private Source nonamespaces;
045:
046: protected final void setUp() throws Exception {
047: template = createTemplate();
048: namespaces = new ResourceSource(new ClassPathResource(
049: "namespaces.xml", AbstractXPathTemplateTestCase.class));
050: nonamespaces = new ResourceSource(
051: new ClassPathResource("nonamespaces.xml",
052: AbstractXPathTemplateTestCase.class));
053: }
054:
055: protected abstract XPathOperations createTemplate()
056: throws Exception;
057:
058: public void testEvaluateAsBoolean() {
059: boolean result = template.evaluateAsBoolean(
060: "/root/child/boolean", nonamespaces);
061: assertTrue("Invalid result", result);
062: }
063:
064: public void testEvaluateAsBooleanNamespaces() {
065: boolean result = template.evaluateAsBoolean(
066: "/prefix1:root/prefix2:child/prefix2:boolean",
067: namespaces);
068: assertTrue("Invalid result", result);
069: }
070:
071: public void testEvaluateAsDouble() {
072: double result = template.evaluateAsDouble("/root/child/number",
073: nonamespaces);
074: assertEquals("Invalid result", 42D, result, 0D);
075: }
076:
077: public void testEvaluateAsDoubleNamespaces() {
078: double result = template.evaluateAsDouble(
079: "/prefix1:root/prefix2:child/prefix2:number",
080: namespaces);
081: assertEquals("Invalid result", 42D, result, 0D);
082: }
083:
084: public void testEvaluateAsNode() {
085: Node result = template.evaluateAsNode("/root/child",
086: nonamespaces);
087: assertNotNull("Invalid result", result);
088: assertEquals("Invalid localname", "child", result
089: .getLocalName());
090: }
091:
092: public void testEvaluateAsNodeNamespaces() {
093: Node result = template.evaluateAsNode(
094: "/prefix1:root/prefix2:child", namespaces);
095: assertNotNull("Invalid result", result);
096: assertEquals("Invalid localname", "child", result
097: .getLocalName());
098: }
099:
100: public void testEvaluateAsNodes() {
101: List results = template.evaluateAsNodeList("/root/child/*",
102: nonamespaces);
103: assertNotNull("Invalid result", results);
104: assertEquals("Invalid amount of results", 3, results.size());
105: }
106:
107: public void testEvaluateAsNodesNamespaces() {
108: List results = template.evaluateAsNodeList(
109: "/prefix1:root/prefix2:child/*", namespaces);
110: assertNotNull("Invalid result", results);
111: assertEquals("Invalid amount of results", 3, results.size());
112: }
113:
114: public void testEvaluateAsStringNamespaces() throws IOException,
115: SAXException {
116: String result = template.evaluateAsString(
117: "/prefix1:root/prefix2:child/prefix2:text", namespaces);
118: assertEquals("Invalid result", "text", result);
119: }
120:
121: public void testEvaluateAsString() throws IOException, SAXException {
122: String result = template.evaluateAsString("/root/child/text",
123: nonamespaces);
124: assertEquals("Invalid result", "text", result);
125: }
126:
127: public void testEvaluateDomSource() throws IOException,
128: SAXException, ParserConfigurationException {
129: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
130: .newInstance();
131: documentBuilderFactory.setNamespaceAware(true);
132: DocumentBuilder documentBuilder = documentBuilderFactory
133: .newDocumentBuilder();
134: Document document = documentBuilder.parse(SaxUtils
135: .createInputSource(new ClassPathResource(
136: "nonamespaces.xml",
137: AbstractXPathTemplateTestCase.class)));
138:
139: String result = template.evaluateAsString("/root/child/text",
140: new DOMSource(document));
141: assertEquals("Invalid result", "text", result);
142: }
143:
144: public void testEvaluateStreamSource() throws IOException,
145: SAXException, ParserConfigurationException {
146: InputStream in = AbstractXPathTemplateTestCase.class
147: .getResourceAsStream("nonamespaces.xml");
148: String result = template.evaluateAsString("/root/child/text",
149: new StreamSource(in));
150: assertEquals("Invalid result", "text", result);
151: }
152:
153: public void testInvalidExpression() {
154: try {
155: template.evaluateAsBoolean("\\", namespaces);
156: fail("No XPathException thrown");
157: } catch (XPathException ex) {
158: // Expected behaviour
159: }
160: }
161:
162: public void testEvaluateAsObject() throws Exception {
163: String result = (String) template.evaluateAsObject(
164: "/root/child", nonamespaces, new NodeMapper() {
165: public Object mapNode(Node node, int nodeNum)
166: throws DOMException {
167: return node.getLocalName();
168: }
169: });
170: assertNotNull("Invalid result", result);
171: assertEquals("Invalid localname", "child", result);
172: }
173:
174: public void testEvaluate() throws Exception {
175: List results = template.evaluate("/root/child/*", nonamespaces,
176: new NodeMapper() {
177: public Object mapNode(Node node, int nodeNum)
178: throws DOMException {
179: return node.getLocalName();
180: }
181: });
182: assertNotNull("Invalid result", results);
183: assertEquals("Invalid amount of results", 3, results.size());
184: assertEquals("Invalid first result", "text", results.get(0));
185: assertEquals("Invalid first result", "number", results.get(1));
186: assertEquals("Invalid first result", "boolean", results.get(2));
187: }
188: }
|