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.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 @SuppressWarnings({"UnnecessaryBoxing", "AssertEqualsBetweenInconvertibleTypes"})
30 class PublicFieldsTest extends JexlTestCase {
31 public enum Gender { MALE, FEMALE }
32
33
34
35 public static class Inner {
36 public static double NOT42 = -42.0;
37 public double aDouble = 42.0;
38 }
39
40
41
42 public static class Struct {
43 public Inner inner = new Inner();
44 public int anInt = 42;
45 public String aString = LOWER42;
46 }
47
48
49 private static final String LOWER42 = "fourty-two";
50
51 private static final String UPPER42 = "FOURTY-TWO";
52
53 private Struct pub;
54
55
56 private JexlContext ctxt;
57
58 public PublicFieldsTest() {
59 super("PublicFieldsTest");
60 }
61
62 @BeforeEach
63 @Override
64 public void setUp() {
65 pub = new Struct();
66 ctxt = new MapContext();
67 ctxt.set("pub", pub);
68 }
69
70 @Test
71 void testGetEnum() throws Exception {
72 ctxt.set("com.jexl.gender", Gender.class);
73 final String src = "x = com.jexl.gender.FEMALE";
74 final JexlScript script = JEXL.createScript(src);
75 final Object result = script.execute(ctxt);
76 assertEquals(Gender.FEMALE, result);
77 assertEquals(Gender.FEMALE, ctxt.get("x"));
78 }
79
80 @Test
81 void testGetInnerDouble() throws Exception {
82 final JexlExpression get = JEXL.createExpression("pub.inner.aDouble");
83 assertEquals(42.0, get.evaluate(ctxt));
84 JEXL.setProperty(pub, "inner.aDouble", -42);
85 assertEquals(-42.0, get.evaluate(ctxt));
86 }
87
88 @Test
89 void testGetInt() throws Exception {
90 final JexlExpression get = JEXL.createExpression("pub.anInt");
91 assertEquals(42, get.evaluate(ctxt));
92 JEXL.setProperty(pub, "anInt", -42);
93 assertEquals(-42, get.evaluate(ctxt));
94 }
95
96 @Test
97 void testGetStaticField() throws Exception {
98 ctxt.set("com.jexl", Inner.class);
99 final String src = "x = com.jexl.NOT42";
100 final JexlScript script = JEXL.createScript(src);
101 final Object result = script.execute(ctxt);
102 assertEquals(Inner.NOT42, result);
103 assertEquals(Inner.NOT42, ctxt.get("x"));
104 }
105
106 @Test
107 void testGetString() throws Exception {
108 final JexlExpression get = JEXL.createExpression("pub.aString");
109 assertEquals(LOWER42, get.evaluate(ctxt));
110 JEXL.setProperty(pub, "aString", UPPER42);
111 assertEquals(UPPER42, get.evaluate(ctxt));
112 }
113
114 @Test
115 void testSetInnerDouble() throws Exception {
116 final JexlExpression set = JEXL.createExpression("pub.inner.aDouble = value");
117 ctxt.set("value", -42.0);
118 assertEquals(-42.0, set.evaluate(ctxt));
119 assertEquals(-42.0, JEXL.getProperty(pub, "inner.aDouble"));
120 ctxt.set("value", 42.0);
121 assertEquals(42.0, set.evaluate(ctxt));
122 assertEquals(42.0, JEXL.getProperty(pub, "inner.aDouble"));
123 assertThrows(JexlException.class, () -> {
124 ctxt.set("value", UPPER42);
125 assertNull(set.evaluate(ctxt));
126 });
127 }
128
129 @Test
130 void testSetInt() throws Exception {
131 final JexlExpression set = JEXL.createExpression("pub.anInt = value");
132 ctxt.set("value", -42);
133 assertEquals(-42, set.evaluate(ctxt));
134 assertEquals(-42, JEXL.getProperty(pub, "anInt"));
135 ctxt.set("value", 42);
136 assertEquals(42, set.evaluate(ctxt));
137 assertEquals(42, JEXL.getProperty(pub, "anInt"));
138 assertThrows(JexlException.class, () -> {
139 ctxt.set("value", UPPER42);
140 assertNull(set.evaluate(ctxt));
141 });
142 }
143
144 @Test
145 void testSetString() throws Exception {
146 final JexlExpression set = JEXL.createExpression("pub.aString = value");
147 ctxt.set("value", UPPER42);
148 assertEquals(UPPER42, set.evaluate(ctxt));
149 assertEquals(UPPER42, JEXL.getProperty(pub, "aString"));
150 ctxt.set("value", LOWER42);
151 assertEquals(LOWER42, set.evaluate(ctxt));
152 assertEquals(LOWER42, JEXL.getProperty(pub, "aString"));
153 }
154 }