1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3;
18
19 import org.junit.Assert;
20 import org.junit.Test;
21
22 import java.util.Arrays;
23
24
25
26
27
28
29 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
30 public class AssignTest extends JexlTestCase {
31
32 public static class Froboz {
33 int value;
34 public Froboz(final int v) {
35 value = v;
36 }
37 public void setValue(final int v) {
38 value = v;
39 }
40 public int getValue() {
41 return value;
42 }
43 }
44
45 public static class Quux {
46 String str;
47 Froboz froboz;
48 public Quux(final String str, final int fro) {
49 this.str = str;
50 froboz = new Froboz(fro);
51 }
52
53 public Froboz getFroboz() {
54 return froboz;
55 }
56
57 public void setFroboz(final Froboz froboz) {
58 this.froboz = froboz;
59 }
60
61 public String getStr() {
62 return str;
63 }
64
65 public void setStr(final String str) {
66 this.str = str;
67 }
68 }
69
70 public AssignTest() {
71 super("AssignTest", new JexlBuilder().cache(512).strict(true).silent(false).create());
72 }
73
74
75
76
77
78 @Test
79 public void testAntish() {
80 final JexlExpression assign = JEXL.createExpression("froboz.value = 10");
81 final JexlExpression check = JEXL.createExpression("froboz.value");
82 final JexlContext jc = new MapContext();
83 Object o = assign.evaluate(jc);
84 Assert.assertEquals("Result is not 10", new Integer(10), o);
85 o = check.evaluate(jc);
86 Assert.assertEquals("Result is not 10", new Integer(10), o);
87 }
88
89 @Test
90 public void testAntishInteger() {
91 final JexlExpression assign = JEXL.createExpression("froboz.0 = 10");
92 final JexlExpression check = JEXL.createExpression("froboz.0");
93 final JexlContext jc = new MapContext();
94 Object o = assign.evaluate(jc);
95 Assert.assertEquals("Result is not 10", new Integer(10), o);
96 o = check.evaluate(jc);
97 Assert.assertEquals("Result is not 10", new Integer(10), o);
98 }
99
100 @Test
101 public void testBeanish() {
102 final JexlExpression assign = JEXL.createExpression("froboz.value = 10");
103 final JexlExpression check = JEXL.createExpression("froboz.value");
104 final JexlContext jc = new MapContext();
105 final Froboz froboz = new Froboz(-169);
106 jc.set("froboz", froboz);
107 Object o = assign.evaluate(jc);
108 Assert.assertEquals("Result is not 10", new Integer(10), o);
109 o = check.evaluate(jc);
110 Assert.assertEquals("Result is not 10", new Integer(10), o);
111 }
112
113 @Test
114 public void testAmbiguous() {
115 final JexlExpression assign = JEXL.createExpression("froboz.nosuchbean = 10");
116 final JexlContext jc = new MapContext();
117 final Froboz froboz = new Froboz(-169);
118 jc.set("froboz", froboz);
119 Object o = null;
120 try {
121 o = assign.evaluate(jc);
122 }
123 catch(final RuntimeException xrt) {
124 final String str = xrt.toString();
125 Assert.assertTrue(str.contains("nosuchbean"));
126 }
127 finally {
128 Assert.assertNull("Should have failed", o);
129 }
130 }
131
132 @Test
133 public void testArray() {
134 final JexlExpression assign = JEXL.createExpression("froboz[\"value\"] = 10");
135 final JexlExpression check = JEXL.createExpression("froboz[\"value\"]");
136 final JexlContext jc = new MapContext();
137 final Froboz froboz = new Froboz(0);
138 jc.set("froboz", froboz);
139 Object o = assign.evaluate(jc);
140 Assert.assertEquals("Result is not 10", new Integer(10), o);
141 o = check.evaluate(jc);
142 Assert.assertEquals("Result is not 10", new Integer(10), o);
143 }
144
145 @Test
146 public void testMini() {
147 final JexlContext jc = new MapContext();
148 final JexlExpression assign = JEXL.createExpression("quux = 10");
149 final Object o = assign.evaluate(jc);
150 Assert.assertEquals("Result is not 10", new Integer(10), o);
151
152 }
153
154 @Test
155 public void testMore() {
156 final JexlContext jc = new MapContext();
157 jc.set("quuxClass", Quux.class);
158 final JexlExpression create = JEXL.createExpression("quux = new(quuxClass, 'xuuq', 100)");
159 final JexlExpression assign = JEXL.createExpression("quux.froboz.value = 10");
160 final JexlExpression check = JEXL.createExpression("quux[\"froboz\"].value");
161
162 final Quux quux = (Quux) create.evaluate(jc);
163 Assert.assertNotNull("quux is null", quux);
164 Object o = assign.evaluate(jc);
165 Assert.assertEquals("Result is not 10", new Integer(10), o);
166 o = check.evaluate(jc);
167 Assert.assertEquals("Result is not 10", new Integer(10), o);
168 }
169
170 @Test
171 public void testUtil() {
172 final Quux quux = JEXL.newInstance(Quux.class, "xuuq", Integer.valueOf(100));
173 Assert.assertNotNull(quux);
174 JEXL.setProperty(quux, "froboz.value", Integer.valueOf(100));
175 Object o = JEXL.getProperty(quux, "froboz.value");
176 Assert.assertEquals("Result is not 100", new Integer(100), o);
177 JEXL.setProperty(quux, "['froboz'].value", Integer.valueOf(1000));
178 o = JEXL.getProperty(quux, "['froboz']['value']");
179 Assert.assertEquals("Result is not 1000", new Integer(1000), o);
180 }
181
182 @Test
183 public void testRejectLocal() {
184 final JexlContext jc = new MapContext();
185 JexlScript assign = JEXL.createScript("var quux = null; quux.froboz.value = 10");
186 try {
187 final Object o = assign.execute(jc);
188 Assert.fail("quux is local and null, should fail");
189 } catch (final JexlException xjexl) {
190 final String x = xjexl.toString();
191 final String y = x;
192 }
193
194 assign = JEXL.createScript("quux.froboz.value = 10");
195 final Object o = assign.execute(jc);
196 Assert.assertEquals(10, o);
197 }
198
199 @Test
200 public void testPropertyInError0() {
201 JexlScript script;
202 for(String op : Arrays.asList(
203 " = ", "+= ", " -= "," *= "," /= "," %= ",
204 " &= ", " |= ", " ^= ",
205 " <<= ", " >>= ", " >>>= ")) {
206 script = JEXL.createScript("x -> x.y " +op+ "42");
207 try {
208 script.execute(null, "the_x_value");
209 } catch (final JexlException.Property xprop) {
210 Assert.assertEquals("y", xprop.getProperty());
211 }
212 }
213 script = JEXL.createScript("x -> x.y ");
214 try {
215 script.execute(null, "the_x_value");
216 } catch (final JexlException.Property xprop) {
217 Assert.assertEquals("y", xprop.getProperty());
218 }
219 }
220
221 @Test
222 public void testSetInError1() {
223 try {
224 JEXL.setProperty("the_x_value", "y", 42);
225 } catch (final JexlException.Property xprop) {
226 Assert.assertEquals("y", xprop.getProperty());
227 }
228 try {
229 JEXL.setProperty(null, "y", 42);
230 } catch (final JexlException xprop) {
231
232 }
233 }
234 @Test
235 public void testGetInError1() {
236 try {
237 JEXL.getProperty("the_x_value", "y");
238 } catch (final JexlException.Property xprop) {
239 Assert.assertEquals("y", xprop.getProperty());
240 }
241 try {
242 JEXL.getProperty(null, "y");
243 } catch (final JexlException xprop) {
244
245 }
246 }
247 }