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.DocumentHelper;
20 import org.dom4j.Node;
21 import org.dom4j.XPath;
22 import org.jaxen.SimpleVariableContext;
23
24 /*** Test harness for the valueOf() function
25 *
26 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
27 * @version $Revision: 1.13 $
28 */
29 public class TestVariable extends AbstractTestCase {
30
31 protected static boolean VERBOSE = true;
32
33 protected static String[] paths = {
34 "$author",
35 "$author/@name",
36 "$root/author",
37 "$root/author[1]",
38 "$root/author[1]/@name",
39 "$author/@name"
40 };
41
42 private SimpleVariableContext variableContext = new SimpleVariableContext();
43 private Node rootNode;
44 private Node authorNode;
45
46
47 public static void main( String[] args ) {
48 TestRunner.run( suite() );
49 }
50
51 public static Test suite() {
52 return new TestSuite( TestVariable.class );
53 }
54
55 public TestVariable(String name) {
56 super(name);
57 }
58
59
60
61 public void testXPaths() throws Exception {
62 int size = paths.length;
63 for ( int i = 0; i < size; i++ ) {
64 testXPath( paths[i] );
65 }
66 }
67
68 protected void testXPath(String xpathText) {
69 XPath xpath = createXPath( xpathText );
70 List list = xpath.selectNodes( document );
71
72 log( "Searched path: " + xpathText + " found: " + list.size() + " result(s)" );
73
74 if ( VERBOSE ) {
75 log( "" );
76 log( "xpath: " + xpath );
77 log( "" );
78 log( "results: " + list );
79 }
80
81 assertTrue( "Results should not contain the root node", ! list.contains( rootNode ) );
82 }
83
84 protected XPath createXPath( String xpath ) {
85 return DocumentHelper.createXPath( xpath, variableContext );
86 }
87
88 protected void setUp() throws Exception {
89 super.setUp();
90
91 rootNode = document.selectSingleNode( "/root" );
92 authorNode = document.selectSingleNode( "/root/author[1]" );
93
94 variableContext.setVariableValue( "root", rootNode );
95 variableContext.setVariableValue( "author", authorNode );
96 }
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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