1
2
3
4
5
6
7
8
9
10 package org.dom4j.xpath;
11
12 import java.util.List;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16 import junit.textui.TestRunner;
17
18 import org.dom4j.AbstractTestCase;
19 import org.dom4j.Element;
20 import org.dom4j.Node;
21 import org.dom4j.XPath;
22
23 /*** Test harness for the valueOf() function
24 *
25 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
26 * @version $Revision: 1.13 $
27 */
28 public class TestValueOf extends AbstractTestCase {
29
30 protected static boolean VERBOSE = false;
31
32 protected static String[] paths = {
33 "/root",
34 "//author",
35 "//author/@name",
36 "/root/author[1]",
37 "/root/author[1]/@name",
38 "/root/author[2]",
39 "/root/author[2]/@name",
40 "/root/author[3]",
41 "/root/author[3]/@name",
42 "name()",
43 "name(.)",
44 "name(..)",
45 "name(child::node())",
46 "name(parent::*)",
47 "name(../*)",
48 "name(../child::node())",
49 "local-name()",
50 "local-name(..)",
51 "local-name(parent::*)",
52 "local-name(../*)",
53 "parent::*",
54 "name(/.)",
55 "name(/child::node())",
56 "name(/*)",
57 ".",
58 "..",
59 "../*",
60 "../child::node()",
61 "/.",
62 "/*",
63 "*",
64 "/child::node()",
65 };
66
67
68 public static void main( String[] args ) {
69 TestRunner.run( suite() );
70 }
71
72 public static Test suite() {
73 return new TestSuite( TestValueOf.class );
74 }
75
76 public TestValueOf(String name) {
77 super(name);
78 }
79
80
81
82 public void testXPaths() throws Exception {
83 Element root = document.getRootElement();
84 List children = root.elements( "author" );
85 Element child1 = (Element) children.get(0);
86
87 testXPath( document );
88 testXPath( root );
89 testXPath( child1 );
90 }
91
92 protected void testXPath(Node node) throws Exception {
93 log( "Testing XPath on: " + node );
94 log( "===============================" );
95
96 int size = paths.length;
97 for ( int i = 0; i < size; i++ ) {
98 testXPath( node, paths[i] );
99 }
100 }
101
102 protected void testXPath(Node node, String xpathExpr) throws Exception {
103 try {
104 XPath xpath = node.createXPath( xpathExpr );
105 String value = xpath.valueOf(node);
106
107 log( "valueOf: " + xpathExpr + " is: " + value );
108
109 if ( VERBOSE ) {
110 log( "xpath object: " + xpath );
111 log( "===============================" );
112 }
113 }
114 catch (Throwable e) {
115 e.printStackTrace();
116 assertTrue( "Failed with exception: " + e, false );
117 }
118 }
119
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168