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    *      http://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      /** Variables used during asserts. */
46      private final Map<String, Object> variables = new HashMap<>();
47      /** Context to use during asserts. */
48      private final JexlEvalContext context = new JexlEvalContext(variables);
49      /** JEXL engine to use during Asserts. */
50      private final JexlEngine engine;
51  
52      /**
53       *
54       * Create an asserter.
55       * @param jexl the JEXL engine to use
56       */
57      public Asserter(final JexlEngine jexl) {
58          engine = jexl;
59      }
60  
61      /**
62       * Performs an assertion that the value of the given JEXL expression
63       * evaluates to the given expected value.
64       *
65       * @param expression is the JEXL expression to evaluate
66       * @param expected is the expected value of the expression
67       * @throws Exception if the expression could not be evaluationed or an assertion
68       * fails
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       * Performs an assertion that the expression fails throwing an exception.
99       * If matchException is not null, the exception message is expected to match it as a regexp.
100      * The engine is temporarily switched to strict * verbose to maximize error detection abilities.
101      * @param expression the expression that should fail
102      * @param matchException the exception message pattern
103      * @throws Exception if the expression did not fail or the exception did not match the expected pattern
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      * Retrieves the underlying JEXL context.
118      * @return the JEXL context
119      */
120     public JexlContext getContext() {
121         return context;
122     }
123 
124     /**
125      * Retrieves the underlying JEXL engine.
126      * @return the JEXL engine
127      */
128     public JexlEngine getEngine() {
129         return engine;
130     }
131 
132     /**
133      * Gets a variable of a certain name.
134      *
135      * @param name variable name
136      * @return value variable value
137      */
138     public Object getVariable(final String name) {
139         return variables.get(name);
140     }
141 
142     /**
143      * @return the variables map
144      */
145     public Map<String, Object> getVariables() {
146         return variables;
147     }
148     /**
149      * Removes a variable of a certain name from the context.
150      * @param name variable name
151      * @return variable value
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      * Puts a variable of a certain name in the context so that it can be used from
172      * assertion expressions.
173      *
174      * @param name variable name
175      * @param value variable value
176      */
177     public void setVariable(final String name, final Object value) {
178         variables.put(name, value);
179     }
180 }