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
75 private static final Class<?>[] NO_PARMS = {};
76
77
78 private static final Class<?>[] STRING_PARM = {String.class};
79
80
81 static final JexlOptions MODE_PRO50 = new JexlOptions();
82
83 static {
84 MODE_PRO50.setFlags("+strict +cancellable +lexical +lexicalShade -safe".split(" "));
85 }
86
87
88
89
90 public static final JexlPermissions SECURE = JexlPermissions.RESTRICTED;
91
92 static JexlEngine createEngine() {
93 return new JexlBuilder().create();
94 }
95
96 public static JexlEngine createEngine(final boolean lenient) {
97 return createEngine(lenient, SECURE);
98 }
99 public static JexlEngine createEngine(final boolean lenient, final JexlPermissions permissions) {
100 return new JexlBuilder()
101 .uberspect(new Uberspect(null, null, permissions))
102 .arithmetic(new JexlArithmetic(!lenient)).cache(128).create();
103 }
104
105 static JexlEngine createEngine(final JexlFeatures features) {
106 return new JexlBuilder().features(features).create();
107 }
108
109
110
111
112
113
114
115 public static void debuggerCheck(final JexlEngine ijexl) throws Exception {
116 Util.debuggerCheck(ijexl);
117 }
118
119
120
121
122
123
124
125
126
127 public static boolean equalsIgnoreWhiteSpace(final String lhs, final String rhs) {
128 final String lhsw = lhs.trim().replaceAll("\\s+", "");
129 final String rhsw = rhs.trim().replaceAll("\\s+", "");
130 return lhsw.equals(rhsw);
131 }
132
133
134
135
136
137
138 public static void main(final String[] args) throws Exception {
139 runTest(args[0], args[1]);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public static void runTest(final String tname, final String mname) throws Exception {
155 final String testClassName = "org.apache.commons.jexl3." + tname;
156 final Class<JexlTestCase> clazz = null;
157 JexlTestCase test = null;
158
159 assertThrows(ClassNotFoundException.class, () -> Class.forName(testClassName), () -> "no such class: " + testClassName);
160
161 Constructor<JexlTestCase> ctor = null;
162 try {
163 ctor = clazz.getConstructor(STRING_PARM);
164 test = ctor.newInstance("debug");
165 } catch (final NoSuchMethodException xctor) {
166
167 try {
168 test = clazz.getConstructor().newInstance();
169 } catch (final Exception xany) {
170 fail("cant instantiate test: " + xany);
171 return;
172 }
173 } catch (final Exception xany) {
174 fail("cant instantiate test: " + xany);
175 return;
176 }
177
178 test.runTest(mname);
179 }
180
181 public static String toString(final JexlScript script) {
182 final Debugger d = new Debugger().lineFeed("").indentation(0);
183 d.debug(script);
184 return d.toString();
185 }
186
187
188 protected final JexlEngine JEXL;
189
190 public JexlTestCase(final String name) {
191 this(name, new JexlBuilder().imports(Arrays.asList("java.lang","java.math")).permissions(null).cache(128).create());
192 }
193
194 protected JexlTestCase(final String name, final JexlEngine jexl) {
195
196 JEXL = jexl;
197 }
198
199
200
201
202
203
204 public void runTest(final String name) throws Exception {
205 if ("runTest".equals(name)) {
206 return;
207 }
208 Method method = null;
209 try {
210 method = this.getClass().getDeclaredMethod(name, NO_PARMS);
211 }
212 catch (final Exception xany) {
213 fail("no such test: " + name);
214 return;
215 }
216 try {
217 setUp();
218 method.invoke(this);
219 } finally {
220 tearDown();
221 }
222 }
223
224 public void setUp() throws Exception {
225
226 }
227
228 public String simpleWhitespace(final String arg) {
229 return arg.trim().replaceAll("\\s+", " ");
230 }
231
232 @AfterEach
233 public void tearDown() throws Exception {
234 debuggerCheck(JEXL);
235 }
236 }