KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXPath


1 /*
2  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: TestXPath.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
8  */

9
10 package test.dom4j;
11
12 import junit.framework.Test;
13 import junit.framework.TestSuite;
14 import junit.textui.TestRunner;
15 import org.dom4j.Element;
16 import org.dom4j.Node;
17 import org.dom4j.XPath;
18 import org.dom4j.tree.DefaultElement;
19 import org.ozoneDB.xml.dom4j.O3DocumentHelper;
20
21 import java.util.List JavaDoc;
22
23 /** A test harness to test XPath expression evaluation in DOM4J
24   *
25   * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan</a>
26   * @version $Revision: 1.2 $
27   */

28
29 public class TestXPath extends AbstractTestCase {
30
31     protected static boolean VERBOSE = true;
32
33     protected static String JavaDoc[] paths =
34         {
35             ".",
36             "*",
37             "/",
38             "/.",
39             "/*",
40             "/node()",
41             "/child::node()",
42             "/self::node()",
43             "root",
44             "/root",
45             "/root/author",
46             "text()",
47             "//author",
48             "//author/text()",
49             "//@location",
50             "//attribute::*",
51             "//namespace::*",
52             "normalize-space(/root)",
53             "//author[@location]",
54             "//author[@location='UK']",
55             "root|author",
56             "//*[.='James Strachan']",
57             "//root/author[1]",
58             "normalize-space(/root/author)",
59             "normalize-space(' a b c d ')",
60             "//root|//author[1]|//author[2]",
61             "//root/author[2]",
62             "//root/author[3]" };
63
64     public static void main(String JavaDoc[] args) {
65
66         TestRunner.run(suite());
67
68     }
69
70     public static Test suite() {
71
72         return new TestSuite(TestXPath.class);
73
74     }
75
76     public TestXPath(String JavaDoc name) {
77
78         super(name);
79
80     }
81
82     // Test case(s)
83

84     //-------------------------------------------------------------------------
85

86     public void testXPaths() throws Exception JavaDoc {
87         try {
88             int size = paths.length;
89             for (int i = 0; i < size; i++) {
90                 testXPath(paths[i]);
91             }
92         } catch (Exception JavaDoc e) {
93             e.printStackTrace();
94         }
95     }
96
97     public void testCreateXPathBug() throws Exception JavaDoc {
98
99         Element element = new DefaultElement( "foo" );
100         XPath xpath = element.createXPath( "//bar" );
101
102         assertTrue("created a valid XPath: " + xpath != null );
103     }
104
105     // Implementation methods
106

107     //-------------------------------------------------------------------------
108

109     protected void testXPath(String JavaDoc xpathExpression) {
110
111         log("Searched path: " + xpathExpression);
112
113         XPath xpath = O3DocumentHelper.createXPath(xpathExpression);
114
115         List JavaDoc list = xpath.selectNodes(document);
116
117         log("Found : " + list.size() + " result(s)");
118
119         if (VERBOSE) {
120
121             log("...........................................");
122
123             log("XPath: :" + xpath);
124
125             log("...........................................");
126
127         }
128
129         log("Results");
130
131         if (list == null) {
132
133             log("null");
134
135         }
136
137         else {
138
139             log("[");
140
141             for (int i = 0, size = list.size(); i < size; i++) {
142
143                 Object JavaDoc object = list.get(i);
144
145                 String JavaDoc text = "null";
146
147                 if (object instanceof Node) {
148
149                     Node node = (Node) object;
150
151                     text = node.asXML();
152
153                 }
154
155                 else if (object != null) {
156
157                     text = object.toString();
158
159                 }
160
161                 log(" " + text);
162
163             }
164
165             log("]");
166
167         }
168
169         log("...........................................");
170
171     }
172
173 }
174
175 /*
176  * Redistribution and use of this software and associated documentation
177  * ("Software"), with or without modification, are permitted provided
178  * that the following conditions are met:
179  *
180  * 1. Redistributions of source code must retain copyright
181  * statements and notices. Redistributions must also contain a
182  * copy of this document.
183  *
184  * 2. Redistributions in binary form must reproduce the
185  * above copyright notice, this list of conditions and the
186  * following disclaimer in the documentation and/or other
187  * materials provided with the distribution.
188  *
189  * 3. The name "DOM4J" must not be used to endorse or promote
190  * products derived from this Software without prior written
191  * permission of MetaStuff, Ltd. For written permission,
192  * please contact dom4j-info@metastuff.com.
193  *
194  * 4. Products derived from this Software may not be called "DOM4J"
195  * nor may "DOM4J" appear in their names without prior written
196  * permission of MetaStuff, Ltd. DOM4J is a registered
197  * trademark of MetaStuff, Ltd.
198  *
199  * 5. Due credit should be given to the DOM4J Project
200  * (http://dom4j.org/).
201  *
202  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
203  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
204  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
205  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
206  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
207  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
208  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
209  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
211  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
212  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
213  * OF THE POSSIBILITY OF SUCH DAMAGE.
214  *
215  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
216  *
217  * $Id: TestXPath.java,v 1.2 2003/11/02 18:31:28 per_nyfelt Exp $
218  */

219
Popular Tags