1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.compiler;
18
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.JXPathTestCase;
21 import org.apache.commons.jxpath.TestMixedModelBean;
22 import org.apache.commons.jxpath.Variables;
23
24
25
26
27
28
29
30
31 public class VariableTest extends JXPathTestCase {
32 private JXPathContext context;
33
34 public void setUp() {
35 if (context == null) {
36 context = JXPathContext.newContext(new TestMixedModelBean());
37 context.setFactory(new VariableFactory());
38
39 Variables vars = context.getVariables();
40 vars.declareVariable("a", new Double(1));
41 vars.declareVariable("b", new Double(1));
42 vars.declareVariable("c", null);
43 vars.declareVariable("d", new String[] { "a", "b" });
44 vars.declareVariable("integer", new Integer(1));
45 vars.declareVariable("nan", new Double(Double.NaN));
46 vars.declareVariable("x", null);
47 }
48 }
49
50 public void testVariables() {
51
52 assertXPathValueAndPointer(context, "$a", new Double(1), "$a");
53 }
54
55 public void testVariablesInExpressions() {
56 assertXPathValue(context, "$a = $b", Boolean.TRUE);
57
58 assertXPathValue(context, "$a = $nan", Boolean.FALSE);
59
60 assertXPathValue(context, "$a + 1", new Double(2));
61
62 assertXPathValue(context, "$c", null);
63
64 assertXPathValue(context, "$d[2]", "b");
65 }
66
67 public void testInvalidVariableName() {
68 boolean exception = false;
69 try {
70 context.getValue("$none");
71 }
72 catch (Exception ex) {
73 exception = true;
74 }
75 assertTrue(
76 "Evaluating '$none', expected exception - did not get it",
77 exception);
78
79 exception = false;
80 try {
81 context.setValue("$none", new Integer(1));
82 }
83 catch (Exception ex) {
84 exception = true;
85 }
86 assertTrue(
87 "Setting '$none = 1', expected exception - did not get it",
88 exception);
89 }
90
91 public void testNestedContext() {
92 JXPathContext nestedContext = JXPathContext.newContext(context, null);
93
94 assertXPathValue(nestedContext, "$a", new Double(1));
95 }
96
97 public void testSetValue() {
98 assertXPathSetValue(context, "$x", new Integer(1));
99 }
100
101 public void testCreatePathDeclareVariable() {
102
103 assertXPathCreatePath(context, "$string", null, "$string");
104 }
105
106 public void testCreatePathAndSetValueDeclareVariable() {
107
108 assertXPathCreatePathAndSetValue(
109 context,
110 "$string",
111 "Value",
112 "$string");
113 }
114
115 public void testCreatePathDeclareVariableSetCollectionElement() {
116
117
118 assertXPathCreatePath(
119 context,
120 "$stringArray[2]",
121 "",
122 "$stringArray[2]");
123
124
125 assertEquals(
126 "Created <" + "$stringArray[1]" + ">",
127 "Value1",
128 context.getValue("$stringArray[1]"));
129 }
130
131 public void testCreateAndSetValuePathDeclareVariableSetCollectionElement() {
132
133
134 assertXPathCreatePathAndSetValue(
135 context,
136 "$stringArray[2]",
137 "Value2",
138 "$stringArray[2]");
139
140
141 assertEquals(
142 "Created <" + "$stringArray[1]" + ">",
143 "Value1",
144 context.getValue("$stringArray[1]"));
145 }
146
147 public void testCreatePathExpandCollection() {
148 context.getVariables().declareVariable(
149 "array",
150 new String[] { "Value1" });
151
152
153 assertXPathCreatePath(context, "$array[2]", "", "$array[2]");
154
155
156 assertEquals(
157 "Created <" + "$array[1]" + ">",
158 "Value1",
159 context.getValue("$array[1]"));
160 }
161
162 public void testCreatePathAndSetValueExpandCollection() {
163 context.getVariables().declareVariable(
164 "array",
165 new String[] { "Value1" });
166
167
168 assertXPathCreatePathAndSetValue(
169 context,
170 "$array[2]",
171 "Value2",
172 "$array[2]");
173
174
175 assertEquals(
176 "Created <" + "$array[1]" + ">",
177 "Value1",
178 context.getValue("$array[1]"));
179 }
180
181 public void testCreatePathDeclareVariableSetProperty() {
182
183
184 assertXPathCreatePath(
185 context,
186 "$test/boolean",
187 Boolean.FALSE,
188 "$test/boolean");
189
190 }
191
192 public void testCreatePathAndSetValueDeclareVariableSetProperty() {
193
194
195 assertXPathCreatePathAndSetValue(
196 context,
197 "$test/boolean",
198 Boolean.TRUE,
199 "$test/boolean");
200
201 }
202
203 public void testCreatePathDeclareVariableSetCollectionElementProperty() {
204
205
206
207
208
209 assertXPathCreatePath(
210 context,
211 "$testArray[2]/boolean",
212 Boolean.FALSE,
213 "$testArray[2]/boolean");
214 }
215
216 public void testCreatePathAndSetValueDeclVarSetCollectionElementProperty() {
217
218
219
220
221
222 assertXPathCreatePathAndSetValue(
223 context,
224 "$testArray[2]/boolean",
225 Boolean.TRUE,
226 "$testArray[2]/boolean");
227 }
228
229 public void testRemovePathUndeclareVariable() {
230
231 context.getVariables().declareVariable("temp", "temp");
232 context.removePath("$temp");
233 assertTrue(
234 "Undeclare variable",
235 !context.getVariables().isDeclaredVariable("temp"));
236
237 }
238
239 public void testRemovePathArrayElement() {
240
241 context.getVariables().declareVariable(
242 "temp",
243 new String[] { "temp1", "temp2" });
244 context.removePath("$temp[1]");
245 assertEquals(
246 "Remove array element",
247 "temp2",
248 context.getValue("$temp[1]"));
249 }
250
251 public void testRemovePathCollectionElement() {
252
253 context.getVariables().declareVariable("temp", list("temp1", "temp2"));
254 context.removePath("$temp[1]");
255 assertEquals(
256 "Remove collection element",
257 "temp2",
258 context.getValue("$temp[1]"));
259 }
260
261 public void testUnionOfVariableAndNode() throws Exception {
262 assertXPathValue(context, "count($a | /document/vendor/location)", new Double(3));
263 assertXPathValue(context, "count($a | /list)", new Double(7));
264 }
265
266 public void testIterateVariable() throws Exception {
267 assertXPathValueIterator(context, "$d", list("a", "b"));
268 assertXPathValue(context, "$d = 'a'", Boolean.TRUE);
269 assertXPathValue(context, "$d = 'b'", Boolean.TRUE);
270 }
271 }