1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jexl3.junit;
18
19 import java.lang.reflect.Array;
20 import java.math.BigDecimal;
21 import java.math.BigInteger;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.function.BiPredicate;
25
26
27 import org.apache.commons.jexl3.JexlEvalContext;
28 import org.apache.commons.jexl3.JexlArithmetic;
29 import org.apache.commons.jexl3.JexlContext;
30 import org.apache.commons.jexl3.JexlEngine;
31 import org.apache.commons.jexl3.JexlException;
32 import org.apache.commons.jexl3.JexlScript;
33 import org.junit.Assert;
34
35
36
37
38
39
40
41
42 public class Asserter extends Assert {
43
44 private final Map<String, Object> variables = new HashMap<>();
45
46 private final JexlEvalContext context = new JexlEvalContext(variables);
47
48 private final JexlEngine engine;
49
50
51
52
53
54
55 public Asserter(final JexlEngine jexl) {
56 engine = jexl;
57 }
58
59
60
61
62
63 public JexlEngine getEngine() {
64 return engine;
65 }
66
67
68
69
70
71 public JexlContext getContext() {
72 return context;
73 }
74
75 public void setStrict(final boolean s) {
76 context.getEngineOptions().setStrict(s);
77 }
78
79 public void setStrict(final boolean es, final boolean as) {
80 context.getEngineOptions().setStrict(es);
81 context.getEngineOptions().setStrictArithmetic(as);
82 }
83
84 public void setSilent(final boolean silent) {
85 context.getEngineOptions().setSilent(silent);
86 }
87
88
89
90
91
92
93
94
95
96
97 public void assertExpression(final String expression, final Object expected, Object... args) throws Exception {
98 final JexlScript exp = engine.createScript(expression);
99 final Object value = exp.execute(context, args);
100 if (expected instanceof BigDecimal) {
101 final JexlArithmetic jexla = engine.getArithmetic();
102 Assert.assertEquals("expression: " + expression, 0,
103 ((BigDecimal) expected).compareTo(jexla.toBigDecimal(value)));
104 } else if (expected instanceof BigInteger) {
105 final JexlArithmetic jexla = engine.getArithmetic();
106 Assert.assertEquals("expression: " + expression, 0,
107 ((BigInteger) expected).compareTo(jexla.toBigInteger(value)));
108 } else if (expected != null && value != null) {
109 if (expected.getClass().isArray() && value.getClass().isArray()) {
110 final int esz = Array.getLength(expected);
111 final int vsz = Array.getLength(value);
112 final String report = "expression: " + expression;
113 Assert.assertEquals(report + ", array size", esz, vsz);
114 for (int i = 0; i < vsz; ++i) {
115 Assert.assertEquals(report + ", value@[]" + i, Array.get(expected, i), Array.get(value, i));
116 }
117 } else {
118 Assert.assertEquals("expression: " + expression + ", "
119 + expected.getClass().getSimpleName()
120 + " ?= "
121 + value.getClass().getSimpleName(),
122 expected, value);
123 }
124 } else {
125 Assert.assertEquals("expression: " + expression, expected, value);
126 }
127 }
128
129
130
131
132
133
134
135
136
137 public void failExpression(final String expression, final String matchException) throws Exception {
138 failExpression(expression, matchException, String::matches);
139 }
140 public void failExpression(final String expression, final String matchException, BiPredicate<String,String> predicate) throws Exception {
141 try {
142 final JexlScript exp = engine.createScript(expression);
143 exp.execute(context);
144 fail("expression: " + expression);
145 } catch (final JexlException xjexl) {
146 if (matchException != null && !predicate.test(xjexl.getMessage(), matchException)) {
147 fail("expression: " + expression + ", expected: " + matchException + ", got " + xjexl.getMessage());
148 }
149 }
150 }
151
152
153
154
155
156
157
158
159 public void setVariable(final String name, final Object value) {
160 variables.put(name, value);
161 }
162
163
164
165
166
167
168 public Object removeVariable(final String name) {
169 return variables.remove(name);
170 }
171
172
173
174
175
176
177
178 public Object getVariable(final String name) {
179 return variables.get(name);
180 }
181
182
183
184
185 public Map<String, Object> getVariables() {
186 return variables;
187 }
188 }