com.mockrunner.mock.web.MockExpressionEvaluator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.mock.web;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.ExpressionEvaluator;
import javax.servlet.jsp.el.FunctionMapper;
import javax.servlet.jsp.el.VariableResolver;
/**
* Mock implementation of ExpressionEvaluator
.
* This implementation cannot be used for real EL
* expressions. Real EL expression support is only
* available for the Unified Expression Language API
* using the {@link JasperJspFactory}.
*/
public class MockExpressionEvaluator extends ExpressionEvaluator
{
private Map expressions = new HashMap();
/**
* Adds an object as a result for the specified expression.
* @param expression the expression
* @param object the object
*/
public void addObject(String expression, Object object)
{
expressions.put(expression, object);
}
/**
* Clears all expressions and corresponding objects.
*/
public void clearObjects()
{
expressions.clear();
}
public Object evaluate(String expression, Class expectedType, VariableResolver resolver, FunctionMapper mapper) throws ELException
{
Object object = expressions.get(expression);
if(null == object)
{
throw new ELException("No object for expression " + expression + " defined.");
}
if(!object.getClass().equals(expectedType))
{
throw new ELException("Expected type " + expectedType + " for expression " + expression + " but actual type is " + object.getClass());
}
return object;
}
public Expression parseExpression(String expression, Class expectedType, FunctionMapper mapper) throws ELException
{
return new MockExpression(this, expression, expectedType, mapper);
}
}