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 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   * A utility class for performing JUnit based assertions using Jexl
37   * expressions. This class can make it easier to do unit tests using
38   * JEXL navigation expressions.
39   *
40   * @since 1.0
41   */
42  public class Asserter extends Assert {
43      /** variables used during asserts. */
44      private final Map<String, Object> variables = new HashMap<>();
45      /** context to use during asserts. */
46      private final JexlEvalContext context = new JexlEvalContext(variables);
47      /** JEXL engine to use during Asserts. */
48      private final JexlEngine engine;
49  
50      /**
51       *
52       * Create an asserter.
53       * @param jexl the JEXL engine to use
54       */
55      public Asserter(final JexlEngine jexl) {
56          engine = jexl;
57      }
58  
59      /**
60       * Retrieves the underlying JEXL engine.
61       * @return the JEXL engine
62       */
63      public JexlEngine getEngine() {
64          return engine;
65      }
66  
67      /**
68       * Retrieves the underlying JEXL context.
69       * @return the JEXL context
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       * Performs an assertion that the value of the given JEXL expression
90       * evaluates to the given expected value.
91       *
92       * @param expression is the JEXL expression to evaluate
93       * @param expected is the expected value of the expression
94       * @throws Exception if the expression could not be evaluationed or an assertion
95       * fails
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      * Performs an assertion that the expression fails throwing an exception.
131      * If matchException is not null, the exception message is expected to match it as a regexp.
132      * The engine is temporarily switched to strict * verbose to maximize error detection abilities.
133      * @param expression the expression that should fail
134      * @param matchException the exception message pattern
135      * @throws Exception if the expression did not fail or the exception did not match the expected pattern
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      * Puts a variable of a certain name in the context so that it can be used from
154      * assertion expressions.
155      *
156      * @param name variable name
157      * @param value variable value
158      */
159     public void setVariable(final String name, final Object value) {
160         variables.put(name, value);
161     }
162 
163     /**
164      * Removes a variable of a certain name from the context.
165      * @param name variable name
166      * @return variable value
167      */
168     public Object removeVariable(final String name) {
169         return variables.remove(name);
170     }
171 
172     /**
173      * Gets a variable of a certain name.
174      *
175      * @param name variable name
176      * @return value variable value
177      */
178     public Object getVariable(final String name) {
179         return variables.get(name);
180     }
181 
182     /**
183      * @return the variables map
184      */
185     public Map<String, Object> getVariables() {
186         return variables;
187     }
188 }