001 package com.mockrunner.mock.web; 002 003 import java.util.HashMap; 004 import java.util.Map; 005 006 import javax.servlet.jsp.el.ELException; 007 import javax.servlet.jsp.el.Expression; 008 import javax.servlet.jsp.el.ExpressionEvaluator; 009 import javax.servlet.jsp.el.FunctionMapper; 010 import javax.servlet.jsp.el.VariableResolver; 011 012 /** 013 * Mock implementation of <code>ExpressionEvaluator</code>. 014 * This implementation cannot be used for real EL 015 * expressions. Real EL expression support is only 016 * available for the <b>Unified Expression Language</b> API 017 * using the {@link JasperJspFactory}. 018 */ 019 public class MockExpressionEvaluator extends ExpressionEvaluator 020 { 021 private Map expressions = new HashMap(); 022 023 /** 024 * Adds an object as a result for the specified expression. 025 * @param expression the expression 026 * @param object the object 027 */ 028 public void addObject(String expression, Object object) 029 { 030 expressions.put(expression, object); 031 } 032 033 /** 034 * Clears all expressions and corresponding objects. 035 */ 036 public void clearObjects() 037 { 038 expressions.clear(); 039 } 040 041 public Object evaluate(String expression, Class expectedType, VariableResolver resolver, FunctionMapper mapper) throws ELException 042 { 043 Object object = expressions.get(expression); 044 if(null == object) 045 { 046 throw new ELException("No object for expression " + expression + " defined."); 047 } 048 if(!object.getClass().equals(expectedType)) 049 { 050 throw new ELException("Expected type " + expectedType + " for expression " + expression + " but actual type is " + object.getClass()); 051 } 052 return object; 053 } 054 055 public Expression parseExpression(String expression, Class expectedType, FunctionMapper mapper) throws ELException 056 { 057 return new MockExpression(this, expression, expectedType, mapper); 058 } 059 }