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 static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.util.Arrays;
25
26 import org.junit.jupiter.api.Test;
27
28
29
30
31 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
32 class AssignTest extends JexlTestCase {
33
34 public static class Froboz {
35 int value;
36 public Froboz(final int v) {
37 value = v;
38 }
39 public int getValue() {
40 return value;
41 }
42 public void setValue(final int v) {
43 value = v;
44 }
45 }
46
47 public static class Quux {
48 String str;
49 Froboz froboz;
50 public Quux(final String str, final int fro) {
51 this.str = str;
52 froboz = new Froboz(fro);
53 }
54
55 public Froboz getFroboz() {
56 return froboz;
57 }
58
59 public String getStr() {
60 return str;
61 }
62
63 public void setFroboz(final Froboz froboz) {
64 this.froboz = froboz;
65 }
66
67 public void setStr(final String str) {
68 this.str = str;
69 }
70 }
71
72 public AssignTest() {
73 super("AssignTest", new JexlBuilder().cache(512).strict(true).silent(false).create());
74 }
75
76 @Test
77 void testAmbiguous() {
78 final JexlExpression assign = JEXL.createExpression("froboz.nosuchbean = 10");
79 final JexlContext jc = new MapContext();
80 final Froboz froboz = new Froboz(-169);
81 jc.set("froboz", froboz);
82 final RuntimeException xrt = assertThrows(RuntimeException.class, () -> assign.evaluate(jc));
83 assertTrue(xrt.toString().contains("nosuchbean"));
84 }
85
86
87
88
89 @Test
90 void testAntish() {
91 final JexlExpression assign = JEXL.createExpression("froboz.value = 10");
92 final JexlExpression check = JEXL.createExpression("froboz.value");
93 final JexlContext jc = new MapContext();
94 Object o = assign.evaluate(jc);
95 assertEquals(Integer.valueOf(10), o);
96 o = check.evaluate(jc);
97 assertEquals(Integer.valueOf(10), o);
98 }
99
100 @Test
101 void testAntishInteger() {
102 final JexlExpression assign = JEXL.createExpression("froboz.0 = 10");
103 final JexlExpression check = JEXL.createExpression("froboz.0");
104 final JexlContext jc = new MapContext();
105 Object o = assign.evaluate(jc);
106 assertEquals(Integer.valueOf(10), o);
107 o = check.evaluate(jc);
108 assertEquals(Integer.valueOf(10), o);
109 }
110
111 @Test
112 void testArray() {
113 final JexlExpression assign = JEXL.createExpression("froboz[\"value\"] = 10");
114 final JexlExpression check = JEXL.createExpression("froboz[\"value\"]");
115 final JexlContext jc = new MapContext();
116 final Froboz froboz = new Froboz(0);
117 jc.set("froboz", froboz);
118 Object o = assign.evaluate(jc);
119 assertEquals(Integer.valueOf(10), o);
120 o = check.evaluate(jc);
121 assertEquals(Integer.valueOf(10), o);
122 }
123
124 @Test
125 void testBeanish() {
126 final JexlExpression assign = JEXL.createExpression("froboz.value = 10");
127 final JexlExpression check = JEXL.createExpression("froboz.value");
128 final JexlContext jc = new MapContext();
129 final Froboz froboz = new Froboz(-169);
130 jc.set("froboz", froboz);
131 Object o = assign.evaluate(jc);
132 assertEquals(Integer.valueOf(10), o);
133 o = check.evaluate(jc);
134 assertEquals(Integer.valueOf(10), o);
135 }
136
137 @Test
138 void testGetInError1() {
139 final JexlException.Property e = assertThrows(JexlException.Property.class, () -> JEXL.getProperty("the_x_value", "y"));
140 assertEquals("y", e.getProperty());
141 assertThrows(JexlException.class, () -> JEXL.getProperty(null, "y"));
142 }
143
144 @Test
145 void testMini() {
146 final JexlContext jc = new MapContext();
147 final JexlExpression assign = JEXL.createExpression("quux = 10");
148 final Object o = assign.evaluate(jc);
149 assertEquals(Integer.valueOf(10), o);
150
151 }
152
153 @Test
154 void testMore() {
155 final JexlContext jc = new MapContext();
156 jc.set("quuxClass", Quux.class);
157 final JexlExpression create = JEXL.createExpression("quux = new(quuxClass, 'xuuq', 100)");
158 final JexlExpression assign = JEXL.createExpression("quux.froboz.value = 10");
159 final JexlExpression check = JEXL.createExpression("quux[\"froboz\"].value");
160
161 final Quux quux = (Quux) create.evaluate(jc);
162 assertNotNull(quux, "quux is null");
163 Object o = assign.evaluate(jc);
164 assertEquals(Integer.valueOf(10), o);
165 o = check.evaluate(jc);
166 assertEquals(Integer.valueOf(10), o);
167 }
168
169 @Test
170 void testPropertyInError0() {
171 for (final String op : Arrays.asList(" = ", "+= ", " -= ", " *= ", " /= ", " %= ", " &= ", " |= ", " ^= ", " <<= ", " >>= ", " >>>= ")) {
172 final JexlScript script = JEXL.createScript("x -> x.y " + op + "42");
173 final JexlException.Property xprop = assertThrows(JexlException.Property.class, () -> script.execute(null, "the_x_value"));
174 assertEquals("y", xprop.getProperty());
175 }
176 final JexlScript script = JEXL.createScript("x -> x.y ");
177 final JexlException.Property xprop = assertThrows(JexlException.Property.class, () -> script.execute(null, "the_x_value"));
178 assertEquals("y", xprop.getProperty());
179 }
180
181 @Test
182 void testRejectLocal() {
183 final JexlContext jc = new MapContext();
184 final JexlScript assign = JEXL.createScript("var quux = null; quux.froboz.value = 10");
185 assertNotNull(assertThrows(JexlException.class, () -> assign.execute(jc)).toString());
186
187 final JexlScript assign2 = JEXL.createScript("quux.froboz.value = 10");
188 final Object o = assign2.execute(jc);
189 assertEquals(10, o);
190 }
191
192 @Test
193 void testSetInError1() {
194 final JexlException.Property xprop = assertThrows(JexlException.Property.class, () -> JEXL.setProperty("the_x_value", "y", 42));
195 assertEquals("y", xprop.getProperty());
196 assertThrows(JexlException.Property.class, () -> JEXL.setProperty(null, "y", 42));
197 }
198 @Test
199 void testUtil() {
200 final Quux quux = JEXL.newInstance(Quux.class, "xuuq", Integer.valueOf(100));
201 assertNotNull(quux);
202 JEXL.setProperty(quux, "froboz.value", Integer.valueOf(100));
203 Object o = JEXL.getProperty(quux, "froboz.value");
204 assertEquals(Integer.valueOf(100), o);
205 JEXL.setProperty(quux, "['froboz'].value", Integer.valueOf(1000));
206 o = JEXL.getProperty(quux, "['froboz']['value']");
207 assertEquals(Integer.valueOf(1000), o);
208 }
209 }