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