KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > xpath > test > XPathTestCase


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.xpath.test;
18
19 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
20 import org.apache.avalon.framework.component.Component;
21 import org.apache.excalibur.xml.dom.DOMParser;
22 import org.apache.excalibur.xml.xpath.XPathProcessor;
23 import org.apache.excalibur.xml.xpath.PrefixResolver;
24 import org.xml.sax.InputSource JavaDoc;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 import java.io.StringReader JavaDoc;
30
31 /**
32  * XPath test case.
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:30 $
35  */

36 public class XPathTestCase extends ExcaliburTestCase
37 {
38     /** A small test document. */
39     static final String JavaDoc CONTENT1 =
40         "<?xml version=\"1.0\"?>" +
41         "<test:root xmlns:test=\"http://localhost/test\">" +
42             "<test:element1/>" +
43             "<test:element2/>" +
44         "</test:root>";
45
46     /** Second test document, has a different namespace than {@link #CONTENT1}. */
47     static final String JavaDoc CONTENT2 =
48         "<?xml version=\"1.0\"?>" +
49         "<test:root xmlns:test=\"http://localhost/test2\">" +
50             "<test:element1/>" +
51             "<test:element2/>" +
52         "</test:root>";
53
54     public XPathTestCase(String JavaDoc name) {
55         super(name);
56     }
57
58     public void testXPath() throws Exception JavaDoc {
59         DOMParser parser = null;
60         XPathProcessor processor = null;
61         try {
62             parser = (DOMParser)manager.lookup(DOMParser.ROLE);
63             processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
64
65             Document JavaDoc document1 = parser.parseDocument(new InputSource JavaDoc(new StringReader JavaDoc(CONTENT1)));
66             Document JavaDoc document2 = parser.parseDocument(new InputSource JavaDoc(new StringReader JavaDoc(CONTENT2)));
67
68             // 1. Test single node expression
69
String JavaDoc expr = "/test:root/test:element1";
70             Node JavaDoc node = processor.selectSingleNode(document1, expr);
71             assertNotNull("Must select <test:element1/> node, but got null", node);
72             assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
73             assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());
74
75             // 2. Test single node expression with no expected result
76
expr = "/test:root/test:element3";
77             node = processor.selectSingleNode(document1, expr);
78             assertNull("Must be null", node);
79
80             // 3. Test multiple node expression
81
expr = "/test:root/test:*";
82             NodeList JavaDoc list = processor.selectNodeList(document1, expr);
83             assertNotNull("Must select two nodes, but got null", list);
84             assertEquals("Must select two nodes", 2, list.getLength());
85             assertEquals("Must select <test:element1/> node", "element1", list.item(0).getLocalName());
86             assertEquals("Must select <test:element2/> node", "element2", list.item(1).getLocalName());
87
88             // 4. Test with a namespace prefix configured in the component configuration
89
expr = "count(/test:root/*)";
90             Number JavaDoc number = processor.evaluateAsNumber(document1, expr);
91             assertEquals(2, number.intValue());
92
93             // 5. Test with a custom prefix resolver using a different document in a different namespace,
94
// to be sure the custom prefix resolver is used
95
number = processor.evaluateAsNumber(document2, expr, new PrefixResolver() {
96                 public String JavaDoc prefixToNamespace(String JavaDoc prefix)
97                 {
98                     if (prefix.equals("test"))
99                         return "http://localhost/test2";
100                     return null;
101                 }
102             });
103             assertEquals(2, number.intValue());
104
105             // 6. Test boolean
106
expr = "count(/test:root/*) = 2";
107             boolean bool = processor.evaluateAsBoolean(document1, expr);
108             assertEquals(true, bool);
109
110             // 7. Test expression in the root element context
111
expr = "/test:root/test:element1";
112             node = processor.selectSingleNode(document1.getDocumentElement(), expr);
113             assertNotNull("Must select <test:element1/> node, but got null", node);
114             assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
115             assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());
116
117             // 8. Test expression in the child node context
118
node = processor.selectSingleNode(document1.getDocumentElement().getFirstChild(), expr);
119             assertNotNull("Must select <test:element1/> node, but got null", node);
120             assertEquals("Must select <test:element1/> node", Node.ELEMENT_NODE, node.getNodeType());
121             assertEquals("Must select <test:element1/> node", "element1", node.getLocalName());
122
123         } finally {
124             if (parser != null) {
125                 manager.release((Component)parser);
126             }
127             if (processor != null) {
128                 manager.release((Component)processor);
129             }
130         }
131     }
132 }
133
Popular Tags