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