View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A utility class for performing JUnit based assertions using Jexl
39   * expressions. This class can make it easier to do unit tests using
40   * JEXL navigation expressions.
41   *
42   * @since 1.0
43   */
44  public class Asserter {
45  
46      /** Variables used during asserts. */
47      private final Map<String, Object> variables = new HashMap<>();
48  
49      /** Context to use during asserts. */
50      private final JexlEvalContext context = new JexlEvalContext(variables);
51  
52      /** JEXL engine to use during Asserts. */
53      private final JexlEngine engine;
54  
55      /**
56       *
57       * Create an asserter.
58       * @param jexl the JEXL engine to use
59       */
60      public Asserter(final JexlEngine jexl) {
61          engine = jexl;
62      }
63  
64      /**
65       * Performs an assertion that the value of the given JEXL expression
66       * evaluates to the given expected value.
67       *
68       * @param expression is the JEXL expression to evaluate
69       * @param expected is the expected value of the expression
70       * @throws Exception if the expression could not be evaluationed or an assertion
71       * fails
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      * Performs an assertion that the expression fails throwing an exception.
102      * If matchException is not null, the exception message is expected to match it as a regexp.
103      * The engine is temporarily switched to strict * verbose to maximize error detection abilities.
104      * @param expression the expression that should fail
105      * @param matchException the exception message pattern
106      * @throws Exception if the expression did not fail or the exception did not match the expected pattern
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      * Gets the underlying JEXL context.
121      * @return the JEXL context
122      */
123     public JexlContext getContext() {
124         return context;
125     }
126 
127     /**
128      * Gets the underlying JEXL engine.
129      * @return the JEXL engine
130      */
131     public JexlEngine getEngine() {
132         return engine;
133     }
134 
135     /**
136      * Gets a variable of a certain name.
137      *
138      * @param name variable name
139      * @return value variable value
140      */
141     public Object getVariable(final String name) {
142         return variables.get(name);
143     }
144 
145     /**
146      * @return the variables map
147      */
148     public Map<String, Object> getVariables() {
149         return variables;
150     }
151 
152     /**
153      * Removes a variable of a certain name from the context.
154      * @param name variable name
155      * @return variable value
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      * Puts a variable of a certain name in the context so that it can be used from
176      * assertion expressions.
177      *
178      * @param name variable name
179      * @param value variable value
180      */
181     public void setVariable(final String name, final Object value) {
182         variables.put(name, value);
183     }
184 }