1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jexl3;
19
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.lang.reflect.Constructor;
24 import java.lang.reflect.Method;
25 import java.util.Arrays;
26
27 import org.apache.commons.jexl3.internal.Debugger;
28 import org.apache.commons.jexl3.internal.OptionsContext;
29 import org.apache.commons.jexl3.internal.Util;
30 import org.apache.commons.jexl3.internal.introspection.Uberspect;
31 import org.apache.commons.jexl3.introspection.JexlPermissions;
32 import org.junit.jupiter.api.AfterEach;
33
34
35
36
37
38
39 public class JexlTestCase {
40 public static class PragmaticContext extends OptionsContext implements JexlContext.PragmaProcessor, JexlContext.OptionsHandle {
41 private final JexlOptions options;
42
43 public PragmaticContext() {
44 this(new JexlOptions());
45 }
46
47 public PragmaticContext(final JexlOptions o) {
48 this.options = o;
49 }
50
51 @Override
52 public JexlOptions getEngineOptions() {
53 return options;
54 }
55
56 @Override
57 public void processPragma(final JexlOptions opts, final String key, final Object value) {
58 if ("script.mode".equals(key) && "pro50".equals(value)) {
59 opts.set(MODE_PRO50);
60 }
61 }
62
63 @Override
64 public void processPragma(final String key, final Object value) {
65 processPragma(null, key, value);
66 }
67 }
68
69
70 static {
71 JexlOptions.setDefaultFlags("-safe", "+lexical");
72 }
73
74 private static final Class<?>[] NO_PARMS = {};
75
76
77 private static final Class<?>[] STRING_PARM = {String.class};
78
79
80 static final JexlOptions MODE_PRO50 = new JexlOptions();
81
82 static {
83 MODE_PRO50.setFlags( "+strict +cancellable +lexical +lexicalShade -safe".split(" "));
84 }
85
86
87
88
89 public static final JexlPermissions SECURE = JexlPermissions.RESTRICTED;
90
91 static JexlEngine createEngine() {
92 return new JexlBuilder().create();
93 }
94
95 public static JexlEngine createEngine(final boolean lenient) {
96 return createEngine(lenient, SECURE);
97 }
98 public static JexlEngine createEngine(final boolean lenient, final JexlPermissions permissions) {
99 return new JexlBuilder()
100 .uberspect(new Uberspect(null, null, permissions))
101 .arithmetic(new JexlArithmetic(!lenient)).cache(128).create();
102 }
103
104 static JexlEngine createEngine(final JexlFeatures features) {
105 return new JexlBuilder().features(features).create();
106 }
107
108
109
110
111
112
113 public static void debuggerCheck(final JexlEngine ijexl) throws Exception {
114 Util.debuggerCheck(ijexl);
115 }
116
117
118
119
120
121
122
123
124
125 public static boolean equalsIgnoreWhiteSpace(final String lhs, final String rhs) {
126 final String lhsw = lhs.trim().replaceAll("\\s+", "");
127 final String rhsw = rhs.trim().replaceAll("\\s+", "");
128 return lhsw.equals(rhsw);
129 }
130
131
132
133
134
135
136 public static void main(final String[] args) throws Exception {
137 runTest(args[0], args[1]);
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152 public static void runTest(final String tname, final String mname) throws Exception {
153 final String testClassName = "org.apache.commons.jexl3." + tname;
154 final Class<JexlTestCase> clazz = null;
155 JexlTestCase test = null;
156
157 assertThrows(ClassNotFoundException.class, () -> Class.forName(testClassName), () -> "no such class: " + testClassName);
158
159 Constructor<JexlTestCase> ctor = null;
160 try {
161 ctor = clazz.getConstructor(STRING_PARM);
162 test = ctor.newInstance("debug");
163 } catch (final NoSuchMethodException xctor) {
164
165 try {
166 test = clazz.getConstructor().newInstance();
167 } catch (final Exception xany) {
168 fail("cant instantiate test: " + xany);
169 return;
170 }
171 } catch (final Exception xany) {
172 fail("cant instantiate test: " + xany);
173 return;
174 }
175
176 test.runTest(mname);
177 }
178
179 public static String toString(final JexlScript script) {
180 final Debugger d = new Debugger().lineFeed("").indentation(0);
181 d.debug(script);
182 return d.toString();
183 }
184
185
186 protected final JexlEngine JEXL;
187
188 public JexlTestCase(final String name) {
189 this(name, new JexlBuilder().imports(Arrays.asList("java.lang","java.math")).permissions(null).cache(128).create());
190 }
191
192 protected JexlTestCase(final String name, final JexlEngine jexl) {
193
194 JEXL = jexl;
195 }
196
197
198
199
200
201
202 public void runTest(final String name) throws Exception {
203 if ("runTest".equals(name)) {
204 return;
205 }
206 Method method = null;
207 try {
208 method = this.getClass().getDeclaredMethod(name, NO_PARMS);
209 }
210 catch (final Exception xany) {
211 fail("no such test: " + name);
212 return;
213 }
214 try {
215 setUp();
216 method.invoke(this);
217 } finally {
218 tearDown();
219 }
220 }
221
222 public void setUp() throws Exception {
223
224 }
225
226 public String simpleWhitespace(final String arg) {
227 return arg.trim().replaceAll("\\s+", " ");
228 }
229
230 @AfterEach
231 public void tearDown() throws Exception {
232 debuggerCheck(JEXL);
233 }
234 }