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