1 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 29 30 public class VariableTest extends JXPathTestCase { 31 private JXPathContext context; 32 33 38 public VariableTest(String 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 (1)); 49 vars.declareVariable("b", new Double (1)); 50 vars.declareVariable("c", null); 51 vars.declareVariable("d", new String [] { "a", "b" }); 52 vars.declareVariable("integer", new Integer (1)); 53 vars.declareVariable("nan", new Double (Double.NaN)); 54 vars.declareVariable("x", null); 55 } 56 } 57 58 public void testVariables() { 59 assertXPathValueAndPointer(context, "$a", new Double (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 (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 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 (1)); 90 } 91 catch (Exception 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 (1)); 103 } 104 105 public void testSetValue() { 106 assertXPathSetValue(context, "$x", new Integer (1)); 107 } 108 109 public void testCreatePathDeclareVariable() { 110 assertXPathCreatePath(context, "$string", null, "$string"); 112 } 113 114 public void testCreatePathAndSetValueDeclareVariable() { 115 assertXPathCreatePathAndSetValue( 117 context, 118 "$string", 119 "Value", 120 "$string"); 121 } 122 123 public void testCreatePathDeclareVariableSetCollectionElement() { 124 assertXPathCreatePath( 127 context, 128 "$stringArray[2]", 129 "", 130 "$stringArray[2]"); 131 132 assertEquals( 134 "Created <" + "$stringArray[1]" + ">", 135 "Value1", 136 context.getValue("$stringArray[1]")); 137 } 138 139 public void testCreateAndSetValuePathDeclareVariableSetCollectionElement() { 140 assertXPathCreatePathAndSetValue( 143 context, 144 "$stringArray[2]", 145 "Value2", 146 "$stringArray[2]"); 147 148 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 [] { "Value1" }); 159 160 assertXPathCreatePath(context, "$array[2]", "", "$array[2]"); 162 163 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 [] { "Value1" }); 174 175 assertXPathCreatePathAndSetValue( 177 context, 178 "$array[2]", 179 "Value2", 180 "$array[2]"); 181 182 assertEquals( 184 "Created <" + "$array[1]" + ">", 185 "Value1", 186 context.getValue("$array[1]")); 187 } 188 189 public void testCreatePathDeclareVariableSetProperty() { 190 assertXPathCreatePath( 193 context, 194 "$test/boolean", 195 Boolean.FALSE, 196 "$test/boolean"); 197 198 } 199 200 public void testCreatePathAndSetValueDeclareVariableSetProperty() { 201 assertXPathCreatePathAndSetValue( 204 context, 205 "$test/boolean", 206 Boolean.TRUE, 207 "$test/boolean"); 208 209 } 210 211 public void testCreatePathDeclareVariableSetCollectionElementProperty() { 212 assertXPathCreatePath( 218 context, 219 "$testArray[2]/boolean", 220 Boolean.FALSE, 221 "$testArray[2]/boolean"); 222 } 223 224 public void testCreatePathAndSetValueDeclVarSetCollectionElementProperty() { 225 assertXPathCreatePathAndSetValue( 231 context, 232 "$testArray[2]/boolean", 233 Boolean.TRUE, 234 "$testArray[2]/boolean"); 235 } 236 237 public void testRemovePathUndeclareVariable() { 238 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 context.getVariables().declareVariable( 250 "temp", 251 new String [] { "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 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 |