001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath.ri.compiler;
017:
018: import org.apache.commons.jxpath.JXPathContext;
019: import org.apache.commons.jxpath.JXPathTestCase;
020: import org.apache.commons.jxpath.Variables;
021:
022: /**
023: * Test basic functionality of JXPath - infoset types,
024: * operations.
025: *
026: * @author Dmitri Plotnikov
027: * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
028: */
029:
030: public class VariableTest extends JXPathTestCase {
031: private JXPathContext context;
032:
033: /**
034: * Construct a new instance of this test case.
035: *
036: * @param name Name of the test case
037: */
038: public VariableTest(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: if (context == null) {
044: context = JXPathContext.newContext(null);
045: context.setFactory(new VariableFactory());
046:
047: Variables vars = context.getVariables();
048: vars.declareVariable("a", new Double(1));
049: vars.declareVariable("b", new Double(1));
050: vars.declareVariable("c", null);
051: vars.declareVariable("d", new String[] { "a", "b" });
052: vars.declareVariable("integer", new Integer(1));
053: vars.declareVariable("nan", new Double(Double.NaN));
054: vars.declareVariable("x", null);
055: }
056: }
057:
058: public void testVariables() {
059: // Variables
060: assertXPathValueAndPointer(context, "$a", new Double(1), "$a");
061: }
062:
063: public void testVariablesInExpressions() {
064: assertXPathValue(context, "$a = $b", Boolean.TRUE);
065:
066: assertXPathValue(context, "$a = $nan", Boolean.FALSE);
067:
068: assertXPathValue(context, "$a + 1", new Double(2));
069:
070: assertXPathValue(context, "$c", null);
071:
072: assertXPathValue(context, "$d[2]", "b");
073: }
074:
075: public void testInvalidVariableName() {
076: boolean exception = false;
077: try {
078: context.getValue("$none");
079: } catch (Exception ex) {
080: exception = true;
081: }
082: assertTrue(
083: "Evaluating '$none', expected exception - did not get it",
084: exception);
085:
086: exception = false;
087: try {
088: context.setValue("$none", new Integer(1));
089: } catch (Exception ex) {
090: exception = true;
091: }
092: assertTrue(
093: "Setting '$none = 1', expected exception - did not get it",
094: exception);
095: }
096:
097: public void testNestedContext() {
098: JXPathContext nestedContext = JXPathContext.newContext(context,
099: null);
100:
101: assertXPathValue(nestedContext, "$a", new Double(1));
102: }
103:
104: public void testSetValue() {
105: assertXPathSetValue(context, "$x", new Integer(1));
106: }
107:
108: public void testCreatePathDeclareVariable() {
109: // Calls factory.declareVariable("string")
110: assertXPathCreatePath(context, "$string", null, "$string");
111: }
112:
113: public void testCreatePathAndSetValueDeclareVariable() {
114: // Calls factory.declareVariable("string")
115: assertXPathCreatePathAndSetValue(context, "$string", "Value",
116: "$string");
117: }
118:
119: public void testCreatePathDeclareVariableSetCollectionElement() {
120: // Calls factory.declareVariable("stringArray").
121: // The factory needs to create a collection
122: assertXPathCreatePath(context, "$stringArray[2]", "",
123: "$stringArray[2]");
124:
125: // See if the factory populated the first element as well
126: assertEquals("Created <" + "$stringArray[1]" + ">", "Value1",
127: context.getValue("$stringArray[1]"));
128: }
129:
130: public void testCreateAndSetValuePathDeclareVariableSetCollectionElement() {
131: // Calls factory.declareVariable("stringArray").
132: // The factory needs to create a collection
133: assertXPathCreatePathAndSetValue(context, "$stringArray[2]",
134: "Value2", "$stringArray[2]");
135:
136: // See if the factory populated the first element as well
137: assertEquals("Created <" + "$stringArray[1]" + ">", "Value1",
138: context.getValue("$stringArray[1]"));
139: }
140:
141: public void testCreatePathExpandCollection() {
142: context.getVariables().declareVariable("array",
143: new String[] { "Value1" });
144:
145: // Does not involve factory at all - just expands the collection
146: assertXPathCreatePath(context, "$array[2]", "", "$array[2]");
147:
148: // Make sure it is still the same array
149: assertEquals("Created <" + "$array[1]" + ">", "Value1", context
150: .getValue("$array[1]"));
151: }
152:
153: public void testCreatePathAndSetValueExpandCollection() {
154: context.getVariables().declareVariable("array",
155: new String[] { "Value1" });
156:
157: // Does not involve factory at all - just expands the collection
158: assertXPathCreatePathAndSetValue(context, "$array[2]",
159: "Value2", "$array[2]");
160:
161: // Make sure it is still the same array
162: assertEquals("Created <" + "$array[1]" + ">", "Value1", context
163: .getValue("$array[1]"));
164: }
165:
166: public void testCreatePathDeclareVariableSetProperty() {
167: // Calls factory.declareVariable("test").
168: // The factory should create a TestBean
169: assertXPathCreatePath(context, "$test/boolean", Boolean.FALSE,
170: "$test/boolean");
171:
172: }
173:
174: public void testCreatePathAndSetValueDeclareVariableSetProperty() {
175: // Calls factory.declareVariable("test").
176: // The factory should create a TestBean
177: assertXPathCreatePathAndSetValue(context, "$test/boolean",
178: Boolean.TRUE, "$test/boolean");
179:
180: }
181:
182: public void testCreatePathDeclareVariableSetCollectionElementProperty() {
183: // Calls factory.declareVariable("testArray").
184: // The factory should create a collection of TestBeans.
185: // Then calls factory.createObject(..., collection, "testArray", 1).
186: // That one should produce an instance of TestBean and
187: // put it in the collection at index 1.
188: assertXPathCreatePath(context, "$testArray[2]/boolean",
189: Boolean.FALSE, "$testArray[2]/boolean");
190: }
191:
192: public void testCreatePathAndSetValueDeclVarSetCollectionElementProperty() {
193: // Calls factory.declareVariable("testArray").
194: // The factory should create a collection of TestBeans.
195: // Then calls factory.createObject(..., collection, "testArray", 1).
196: // That one should produce an instance of TestBean and
197: // put it in the collection at index 1.
198: assertXPathCreatePathAndSetValue(context,
199: "$testArray[2]/boolean", Boolean.TRUE,
200: "$testArray[2]/boolean");
201: }
202:
203: public void testRemovePathUndeclareVariable() {
204: // Undeclare variable
205: context.getVariables().declareVariable("temp", "temp");
206: context.removePath("$temp");
207: assertTrue("Undeclare variable", !context.getVariables()
208: .isDeclaredVariable("temp"));
209:
210: }
211:
212: public void testRemovePathArrayElement() {
213: // Remove array element - reassigns the new array to the var
214: context.getVariables().declareVariable("temp",
215: new String[] { "temp1", "temp2" });
216: context.removePath("$temp[1]");
217: assertEquals("Remove array element", "temp2", context
218: .getValue("$temp[1]"));
219: }
220:
221: public void testRemovePathCollectionElement() {
222: // Remove list element - does not create a new list
223: context.getVariables().declareVariable("temp",
224: list("temp1", "temp2"));
225: context.removePath("$temp[1]");
226: assertEquals("Remove collection element", "temp2", context
227: .getValue("$temp[1]"));
228: }
229: }
|