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