%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jexl.junit.Asserter |
|
|
1 | /* |
|
2 | * Copyright 2003,2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | ||
17 | package org.apache.commons.jexl.junit; |
|
18 | ||
19 | import java.util.HashMap; |
|
20 | import java.util.Map; |
|
21 | ||
22 | import junit.framework.Assert; |
|
23 | ||
24 | import org.apache.commons.jexl.Expression; |
|
25 | import org.apache.commons.jexl.ExpressionFactory; |
|
26 | import org.apache.commons.jexl.JexlContext; |
|
27 | import org.apache.commons.jexl.JexlHelper; |
|
28 | ||
29 | /** |
|
30 | * A utility class for performing JUnit based assertions using Jexl |
|
31 | * expressions. This class can make it easier to do unit tests using |
|
32 | * Jexl navigation expressions. |
|
33 | * |
|
34 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
35 | * @version $Revision: 1.4 $ |
|
36 | */ |
|
37 | public class Asserter extends Assert { |
|
38 | ||
39 | 30 | private Map variables = new HashMap(); |
40 | 30 | private JexlContext context = JexlHelper.createContext(); |
41 | ||
42 | 30 | public Asserter() {} |
43 | ||
44 | /** |
|
45 | * This constructor will register the given variableValue as the |
|
46 | * "this" variable. |
|
47 | * |
|
48 | * @param variableValue |
|
49 | */ |
|
50 | 15 | public Asserter(Object variableValue) { |
51 | 15 | setVariable("this", variableValue); |
52 | 15 | } |
53 | ||
54 | /** |
|
55 | * Performs an assertion that the value of the given Jexl expression |
|
56 | * evaluates to the given expected value |
|
57 | * |
|
58 | * @param expression is the Jexl expression to evaluate |
|
59 | * @param expected is the expected value of the expression |
|
60 | * @throws Exception if the expression could not be evaluationed or an assertion |
|
61 | * fails |
|
62 | */ |
|
63 | public void assertExpression(String expression, Object expected) throws Exception { |
|
64 | 105 | Expression exp = ExpressionFactory.createExpression(expression); |
65 | ||
66 | 105 | context.setVars(variables); |
67 | 105 | Object value = exp.evaluate(context); |
68 | ||
69 | 105 | assertEquals("expression: " + expression, expected, value); |
70 | 75 | } |
71 | ||
72 | /** |
|
73 | * Puts a variable of a certain name in the context so that it can be used from |
|
74 | * assertion expressions. |
|
75 | * |
|
76 | * @param name |
|
77 | * @param value |
|
78 | */ |
|
79 | public void setVariable(String name, Object value) { |
|
80 | 45 | variables.put(name, value); |
81 | 45 | } |
82 | ||
83 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |