01: package com.mockrunner.mock.web;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import javax.servlet.jsp.el.ELException;
07: import javax.servlet.jsp.el.Expression;
08: import javax.servlet.jsp.el.ExpressionEvaluator;
09: import javax.servlet.jsp.el.FunctionMapper;
10: import javax.servlet.jsp.el.VariableResolver;
11:
12: /**
13: * Mock implementation of <code>ExpressionEvaluator</code>.
14: * This implementation cannot be used for real EL
15: * expressions. Real EL expression support is only
16: * available for the <b>Unified Expression Language</b> API
17: * using the {@link JasperJspFactory}.
18: */
19: public class MockExpressionEvaluator extends ExpressionEvaluator {
20: private Map expressions = new HashMap();
21:
22: /**
23: * Adds an object as a result for the specified expression.
24: * @param expression the expression
25: * @param object the object
26: */
27: public void addObject(String expression, Object object) {
28: expressions.put(expression, object);
29: }
30:
31: /**
32: * Clears all expressions and corresponding objects.
33: */
34: public void clearObjects() {
35: expressions.clear();
36: }
37:
38: public Object evaluate(String expression, Class expectedType,
39: VariableResolver resolver, FunctionMapper mapper)
40: throws ELException {
41: Object object = expressions.get(expression);
42: if (null == object) {
43: throw new ELException("No object for expression "
44: + expression + " defined.");
45: }
46: if (!object.getClass().equals(expectedType)) {
47: throw new ELException("Expected type " + expectedType
48: + " for expression " + expression
49: + " but actual type is " + object.getClass());
50: }
51: return object;
52: }
53:
54: public Expression parseExpression(String expression,
55: Class expectedType, FunctionMapper mapper)
56: throws ELException {
57: return new MockExpression(this, expression, expectedType,
58: mapper);
59: }
60: }
|