KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > ri > compiler > VariableTest


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

16 package org.apache.commons.jxpath.ri.compiler;
17
18 import org.apache.commons.jxpath.JXPathContext;
19 import org.apache.commons.jxpath.JXPathTestCase;
20 import org.apache.commons.jxpath.Variables;
21
22 /**
23  * Test basic functionality of JXPath - infoset types,
24  * operations.
25  *
26  * @author Dmitri Plotnikov
27  * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
28  */

29
30 public class VariableTest extends JXPathTestCase {
31     private JXPathContext context;
32
33     /**
34      * Construct a new instance of this test case.
35      *
36      * @param name Name of the test case
37      */

38     public VariableTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public void setUp() {
43         if (context == null) {
44             context = JXPathContext.newContext(null);
45             context.setFactory(new VariableFactory());
46
47             Variables vars = context.getVariables();
48             vars.declareVariable("a", new Double JavaDoc(1));
49             vars.declareVariable("b", new Double JavaDoc(1));
50             vars.declareVariable("c", null);
51             vars.declareVariable("d", new String JavaDoc[] { "a", "b" });
52             vars.declareVariable("integer", new Integer JavaDoc(1));
53             vars.declareVariable("nan", new Double JavaDoc(Double.NaN));
54             vars.declareVariable("x", null);
55         }
56     }
57
58     public void testVariables() {
59         // Variables
60
assertXPathValueAndPointer(context, "$a", new Double JavaDoc(1), "$a");
61     }
62
63     public void testVariablesInExpressions() {
64         assertXPathValue(context, "$a = $b", Boolean.TRUE);
65
66         assertXPathValue(context, "$a = $nan", Boolean.FALSE);
67
68         assertXPathValue(context, "$a + 1", new Double JavaDoc(2));
69
70         assertXPathValue(context, "$c", null);
71
72         assertXPathValue(context, "$d[2]", "b");
73     }
74
75     public void testInvalidVariableName() {
76         boolean exception = false;
77         try {
78             context.getValue("$none");
79         }
80         catch (Exception JavaDoc ex) {
81             exception = true;
82         }
83         assertTrue(
84             "Evaluating '$none', expected exception - did not get it",
85             exception);
86
87         exception = false;
88         try {
89             context.setValue("$none", new Integer JavaDoc(1));
90         }
91         catch (Exception JavaDoc ex) {
92             exception = true;
93         }
94         assertTrue(
95             "Setting '$none = 1', expected exception - did not get it",
96             exception);
97     }
98
99     public void testNestedContext() {
100         JXPathContext nestedContext = JXPathContext.newContext(context, null);
101
102         assertXPathValue(nestedContext, "$a", new Double JavaDoc(1));
103     }
104
105     public void testSetValue() {
106         assertXPathSetValue(context, "$x", new Integer JavaDoc(1));
107     }
108
109     public void testCreatePathDeclareVariable() {
110         // Calls factory.declareVariable("string")
111
assertXPathCreatePath(context, "$string", null, "$string");
112     }
113
114     public void testCreatePathAndSetValueDeclareVariable() {
115         // Calls factory.declareVariable("string")
116
assertXPathCreatePathAndSetValue(
117             context,
118             "$string",
119             "Value",
120             "$string");
121     }
122
123     public void testCreatePathDeclareVariableSetCollectionElement() {
124         // Calls factory.declareVariable("stringArray").
125
// The factory needs to create a collection
126
assertXPathCreatePath(
127             context,
128             "$stringArray[2]",
129             "",
130             "$stringArray[2]");
131
132         // See if the factory populated the first element as well
133
assertEquals(
134             "Created <" + "$stringArray[1]" + ">",
135             "Value1",
136             context.getValue("$stringArray[1]"));
137     }
138
139     public void testCreateAndSetValuePathDeclareVariableSetCollectionElement() {
140         // Calls factory.declareVariable("stringArray").
141
// The factory needs to create a collection
142
assertXPathCreatePathAndSetValue(
143             context,
144             "$stringArray[2]",
145             "Value2",
146             "$stringArray[2]");
147
148         // See if the factory populated the first element as well
149
assertEquals(
150             "Created <" + "$stringArray[1]" + ">",
151             "Value1",
152             context.getValue("$stringArray[1]"));
153     }
154
155     public void testCreatePathExpandCollection() {
156         context.getVariables().declareVariable(
157             "array",
158             new String JavaDoc[] { "Value1" });
159
160         // Does not involve factory at all - just expands the collection
161
assertXPathCreatePath(context, "$array[2]", "", "$array[2]");
162
163         // Make sure it is still the same array
164
assertEquals(
165             "Created <" + "$array[1]" + ">",
166             "Value1",
167             context.getValue("$array[1]"));
168     }
169
170     public void testCreatePathAndSetValueExpandCollection() {
171         context.getVariables().declareVariable(
172             "array",
173             new String JavaDoc[] { "Value1" });
174
175         // Does not involve factory at all - just expands the collection
176
assertXPathCreatePathAndSetValue(
177             context,
178             "$array[2]",
179             "Value2",
180             "$array[2]");
181
182         // Make sure it is still the same array
183
assertEquals(
184             "Created <" + "$array[1]" + ">",
185             "Value1",
186             context.getValue("$array[1]"));
187     }
188
189     public void testCreatePathDeclareVariableSetProperty() {
190         // Calls factory.declareVariable("test").
191
// The factory should create a TestBean
192
assertXPathCreatePath(
193             context,
194             "$test/boolean",
195             Boolean.FALSE,
196             "$test/boolean");
197
198     }
199
200     public void testCreatePathAndSetValueDeclareVariableSetProperty() {
201         // Calls factory.declareVariable("test").
202
// The factory should create a TestBean
203
assertXPathCreatePathAndSetValue(
204             context,
205             "$test/boolean",
206             Boolean.TRUE,
207             "$test/boolean");
208
209     }
210
211     public void testCreatePathDeclareVariableSetCollectionElementProperty() {
212         // Calls factory.declareVariable("testArray").
213
// The factory should create a collection of TestBeans.
214
// Then calls factory.createObject(..., collection, "testArray", 1).
215
// That one should produce an instance of TestBean and
216
// put it in the collection at index 1.
217
assertXPathCreatePath(
218             context,
219             "$testArray[2]/boolean",
220             Boolean.FALSE,
221             "$testArray[2]/boolean");
222     }
223
224     public void testCreatePathAndSetValueDeclVarSetCollectionElementProperty() {
225         // Calls factory.declareVariable("testArray").
226
// The factory should create a collection of TestBeans.
227
// Then calls factory.createObject(..., collection, "testArray", 1).
228
// That one should produce an instance of TestBean and
229
// put it in the collection at index 1.
230
assertXPathCreatePathAndSetValue(
231             context,
232             "$testArray[2]/boolean",
233             Boolean.TRUE,
234             "$testArray[2]/boolean");
235     }
236
237     public void testRemovePathUndeclareVariable() {
238         // Undeclare variable
239
context.getVariables().declareVariable("temp", "temp");
240         context.removePath("$temp");
241         assertTrue(
242             "Undeclare variable",
243             !context.getVariables().isDeclaredVariable("temp"));
244
245     }
246
247     public void testRemovePathArrayElement() {
248         // Remove array element - reassigns the new array to the var
249
context.getVariables().declareVariable(
250             "temp",
251             new String JavaDoc[] { "temp1", "temp2" });
252         context.removePath("$temp[1]");
253         assertEquals(
254             "Remove array element",
255             "temp2",
256             context.getValue("$temp[1]"));
257     }
258
259     public void testRemovePathCollectionElement() {
260         // Remove list element - does not create a new list
261
context.getVariables().declareVariable("temp", list("temp1", "temp2"));
262         context.removePath("$temp[1]");
263         assertEquals(
264             "Remove collection element",
265             "temp2",
266             context.getValue("$temp[1]"));
267     }
268 }
Popular Tags