001: /*
002: * Copyright 2006 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.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026:
027: import junit.framework.TestCase;
028: import org.springframework.util.StringUtils;
029: import org.w3c.dom.DOMException;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Node;
032: import org.xml.sax.SAXException;
033:
034: public abstract class AbstractXPathExpressionFactoryTestCase extends
035: TestCase {
036:
037: private Document noNamespacesDocument;
038:
039: private Document namespacesDocument;
040:
041: private Map namespaces = new HashMap();
042:
043: protected void setUp() throws Exception {
044: namespaces.put("prefix1", "namespace1");
045: namespaces.put("prefix2", "namespace2");
046: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
047: .newInstance();
048: documentBuilderFactory.setNamespaceAware(true);
049: DocumentBuilder documentBuilder = documentBuilderFactory
050: .newDocumentBuilder();
051: InputStream inputStream = getClass().getResourceAsStream(
052: "nonamespaces.xml");
053: try {
054: noNamespacesDocument = documentBuilder.parse(inputStream);
055: } finally {
056: inputStream.close();
057: }
058: inputStream = getClass().getResourceAsStream("namespaces.xml");
059: try {
060: namespacesDocument = documentBuilder.parse(inputStream);
061: } finally {
062: inputStream.close();
063: }
064: }
065:
066: public void testEvaluateAsBooleanInvalidNamespaces()
067: throws IOException, SAXException {
068: XPathExpression expression = createXPathExpression(
069: "/prefix1:root/prefix2:otherchild", namespaces);
070: boolean result = expression
071: .evaluateAsBoolean(namespacesDocument);
072: assertFalse("Invalid result [" + result + "]", result);
073: }
074:
075: public void testEvaluateAsBooleanInvalidNoNamespaces()
076: throws IOException, SAXException {
077: XPathExpression expression = createXPathExpression("/root/otherchild");
078: boolean result = expression
079: .evaluateAsBoolean(noNamespacesDocument);
080: assertFalse("Invalid result [" + result + "]", result);
081: }
082:
083: public void testEvaluateAsBooleanNamespaces() throws IOException,
084: SAXException {
085: XPathExpression expression = createXPathExpression(
086: "/prefix1:root/prefix2:child/prefix2:boolean/text()",
087: namespaces);
088: boolean result = expression
089: .evaluateAsBoolean(namespacesDocument);
090: assertTrue("Invalid result", result);
091: }
092:
093: public void testEvaluateAsBooleanNoNamespaces() throws IOException,
094: SAXException {
095: XPathExpression expression = createXPathExpression("/root/child/boolean/text()");
096: boolean result = expression
097: .evaluateAsBoolean(noNamespacesDocument);
098: assertTrue("Invalid result", result);
099: }
100:
101: public void testEvaluateAsDoubleInvalidNamespaces()
102: throws IOException, SAXException {
103: XPathExpression expression = createXPathExpression(
104: "/prefix1:root/prefix2:otherchild", namespaces);
105: double result = expression
106: .evaluateAsNumber(noNamespacesDocument);
107: assertTrue("Invalid result [" + result + "]", Double
108: .isNaN(result));
109: }
110:
111: public void testEvaluateAsDoubleInvalidNoNamespaces()
112: throws IOException, SAXException {
113: XPathExpression expression = createXPathExpression("/root/otherchild");
114: double result = expression
115: .evaluateAsNumber(noNamespacesDocument);
116: assertTrue("Invalid result [" + result + "]", Double
117: .isNaN(result));
118: }
119:
120: public void testEvaluateAsDoubleNamespaces() throws IOException,
121: SAXException {
122: XPathExpression expression = createXPathExpression(
123: "/prefix1:root/prefix2:child/prefix2:number/text()",
124: namespaces);
125: double result = expression.evaluateAsNumber(namespacesDocument);
126: assertEquals("Invalid result", 42D, result, 0D);
127: }
128:
129: public void testEvaluateAsDoubleNoNamespaces() throws IOException,
130: SAXException {
131: XPathExpression expression = createXPathExpression("/root/child/number/text()");
132: double result = expression
133: .evaluateAsNumber(noNamespacesDocument);
134: assertEquals("Invalid result", 42D, result, 0D);
135: }
136:
137: public void testEvaluateAsNodeInvalidNamespaces()
138: throws IOException, SAXException {
139: XPathExpression expression = createXPathExpression(
140: "/prefix1:root/prefix2:otherchild", namespaces);
141: Node result = expression.evaluateAsNode(namespacesDocument);
142: assertNull("Invalid result [" + result + "]", result);
143: }
144:
145: public void testEvaluateAsNodeInvalidNoNamespaces()
146: throws IOException, SAXException {
147: XPathExpression expression = createXPathExpression("/root/otherchild");
148: Node result = expression.evaluateAsNode(noNamespacesDocument);
149: assertNull("Invalid result [" + result + "]", result);
150: }
151:
152: public void testEvaluateAsNodeNamespaces() throws IOException,
153: SAXException {
154: XPathExpression expression = createXPathExpression(
155: "/prefix1:root/prefix2:child", namespaces);
156: Node result = expression.evaluateAsNode(namespacesDocument);
157: assertNotNull("Invalid result", result);
158: assertEquals("Invalid localname", "child", result
159: .getLocalName());
160: }
161:
162: public void testEvaluateAsNodeNoNamespaces() throws IOException,
163: SAXException {
164: XPathExpression expression = createXPathExpression("/root/child");
165: Node result = expression.evaluateAsNode(noNamespacesDocument);
166: assertNotNull("Invalid result", result);
167: assertEquals("Invalid localname", "child", result
168: .getLocalName());
169: }
170:
171: public void testEvaluateAsNodeListNamespaces() throws IOException,
172: SAXException {
173: XPathExpression expression = createXPathExpression(
174: "/prefix1:root/prefix2:child/*", namespaces);
175: List results = expression
176: .evaluateAsNodeList(namespacesDocument);
177: assertNotNull("Invalid result", results);
178: assertEquals("Invalid amount of results", 3, results.size());
179: }
180:
181: public void testEvaluateAsNodeListNoNamespaces()
182: throws IOException, SAXException {
183: XPathExpression expression = createXPathExpression("/root/child/*");
184: List results = expression
185: .evaluateAsNodeList(noNamespacesDocument);
186: assertNotNull("Invalid result", results);
187: assertEquals("Invalid amount of results", 3, results.size());
188: }
189:
190: public void testEvaluateAsStringInvalidNamespaces()
191: throws IOException, SAXException {
192: XPathExpression expression = createXPathExpression(
193: "/prefix1:root/prefix2:otherchild", namespaces);
194: String result = expression.evaluateAsString(namespacesDocument);
195: assertFalse("Invalid result [" + result + "]", StringUtils
196: .hasText(result));
197: }
198:
199: public void testEvaluateAsStringInvalidNoNamespaces()
200: throws IOException, SAXException {
201: XPathExpression expression = createXPathExpression("/root/otherchild");
202: String result = expression
203: .evaluateAsString(noNamespacesDocument);
204: assertFalse("Invalid result [" + result + "]", StringUtils
205: .hasText(result));
206: }
207:
208: public void testEvaluateAsStringNamespaces() throws IOException,
209: SAXException {
210: XPathExpression expression = createXPathExpression(
211: "/prefix1:root/prefix2:child/prefix2:text/text()",
212: namespaces);
213: String result = expression.evaluateAsString(namespacesDocument);
214: assertEquals("Invalid result", "text", result);
215: }
216:
217: public void testEvaluateAsStringNoNamespaces() throws IOException,
218: SAXException {
219: XPathExpression expression = createXPathExpression("/root/child/text/text()");
220: String result = expression
221: .evaluateAsString(noNamespacesDocument);
222: assertEquals("Invalid result", "text", result);
223: }
224:
225: public void testEvaluateAsObject() throws Exception {
226: XPathExpression expression = createXPathExpression("/root/child");
227: String result = (String) expression.evaluateAsObject(
228: noNamespacesDocument, new NodeMapper() {
229: public Object mapNode(Node node, int nodeNum)
230: throws DOMException {
231: return node.getLocalName();
232: }
233: });
234: assertNotNull("Invalid result", result);
235: assertEquals("Invalid localname", "child", result);
236: }
237:
238: public void testEvaluate() throws Exception {
239: XPathExpression expression = createXPathExpression("/root/child/*");
240: List results = expression.evaluate(noNamespacesDocument,
241: new NodeMapper() {
242: public Object mapNode(Node node, int nodeNum)
243: throws DOMException {
244: return node.getLocalName();
245: }
246: });
247: assertNotNull("Invalid result", results);
248: assertEquals("Invalid amount of results", 3, results.size());
249: assertEquals("Invalid first result", "text", results.get(0));
250: assertEquals("Invalid first result", "number", results.get(1));
251: assertEquals("Invalid first result", "boolean", results.get(2));
252: }
253:
254: public void testInvalidExpression() {
255: try {
256: createXPathExpression("\\");
257: fail("No XPathParseException thrown");
258: } catch (XPathParseException ex) {
259: // Expected behaviour
260: }
261: }
262:
263: protected abstract XPathExpression createXPathExpression(
264: String expression);
265:
266: protected abstract XPathExpression createXPathExpression(
267: String expression, Map namespaces);
268: }
|