Refactoring of EL support in spring-binding (SWF-287) to no longer use a ThreadLocal for storing the ELContext. ELContext creation and configuration is now handled by an ELContextFactory.
This commit is contained in:
@@ -20,87 +20,92 @@ import org.springframework.binding.expression.ExpressionParser;
|
||||
*/
|
||||
public class ELContextDelegationTests extends TestCase {
|
||||
|
||||
private ELContext context = new MockELContext();
|
||||
private ExpressionParser expressionParser = new ELExpressionParser();
|
||||
private TestBean bean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
bean = new TestBean();
|
||||
bean.setFlag(true);
|
||||
private ELContext mockContext = new MockELContext();
|
||||
private ExpressionParser expressionParser = new DelegatingELExpressionParser();
|
||||
private TestBean bean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
bean = new TestBean();
|
||||
bean.setFlag(true);
|
||||
}
|
||||
|
||||
public final void testGetValueByContextDelegation() {
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("#{bean.flag}").evaluate(bean, null));
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("#{bean.flag}").evaluate(null, null));
|
||||
}
|
||||
|
||||
private class DelegatingELExpressionParser extends ELExpressionParser {
|
||||
|
||||
protected ELContextFactory getELContextFactory() {
|
||||
ELContextFactory stubFactory = new ELContextFactory() {
|
||||
|
||||
public ELContext getELContext(Object target) {
|
||||
DelegatingELContext ctx = new DelegatingELContext();
|
||||
ctx.addDelegate(mockContext);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
};
|
||||
return stubFactory;
|
||||
}
|
||||
|
||||
public final void testGetValueByContextDelegation()
|
||||
{
|
||||
DelegatingELContext internalContext = DelegatingELContext.getCurrentInstance();
|
||||
internalContext.addDelegate(context);
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("#{bean.flag}").evaluate(bean, null));
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("#{bean.flag}").evaluate(null, null));
|
||||
assertFalse(internalContext.getELResolver() instanceof DefaultELResolver);
|
||||
}
|
||||
|
||||
private class MockELContext extends ELContext {
|
||||
|
||||
ELResolver resolver = new MockELResolver();
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private class MockELContext extends ELContext
|
||||
{
|
||||
|
||||
ELResolver resolver = new MockELResolver();
|
||||
|
||||
public ELResolver getELResolver() {
|
||||
return resolver;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private class MockELResolver extends ELResolver
|
||||
{
|
||||
|
||||
public Class getCommonPropertyType(ELContext context, Object base) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Iterator getFeatureDescriptors(ELContext context, Object base) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class getType(ELContext context, Object base, Object property) {
|
||||
if (base != null)
|
||||
return base.getClass();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getValue(ELContext context, Object base, Object property) {
|
||||
if (base == null && "bean".equals(property))
|
||||
{
|
||||
context.setPropertyResolved(true);
|
||||
return bean;
|
||||
}
|
||||
if (base == bean)
|
||||
{
|
||||
return new BeanELResolver().getValue(context, base, property);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isReadOnly(ELContext context, Object base, Object property) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setValue(ELContext context, Object base, Object property, Object value) {
|
||||
if (base == null && "bean".equals(property))
|
||||
{
|
||||
context.setPropertyResolved(true);
|
||||
}
|
||||
else if (base == bean)
|
||||
{
|
||||
new BeanELResolver().setValue(context, base, property, value);
|
||||
}
|
||||
}
|
||||
|
||||
public VariableMapper getVariableMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class MockELResolver extends ELResolver {
|
||||
|
||||
public Class getCommonPropertyType(ELContext context, Object base) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Iterator getFeatureDescriptors(ELContext context, Object base) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Class getType(ELContext context, Object base, Object property) {
|
||||
if (base != null)
|
||||
return base.getClass();
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getValue(ELContext context, Object base, Object property) {
|
||||
if (base == null && "bean".equals(property)) {
|
||||
context.setPropertyResolved(true);
|
||||
return bean;
|
||||
}
|
||||
if (base == bean) {
|
||||
return new BeanELResolver().getValue(context, base, property);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isReadOnly(ELContext context, Object base, Object property) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setValue(ELContext context, Object base, Object property, Object value) {
|
||||
if (base == null && "bean".equals(property)) {
|
||||
context.setPropertyResolved(true);
|
||||
} else if (base == bean) {
|
||||
new BeanELResolver().setValue(context, base, property, value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,75 +26,73 @@ import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Tests simple expressions. Any expression language capable enough for
|
||||
* real life usage should be able to pass these tests.
|
||||
*
|
||||
* Tests simple expressions. Any expression language capable enough for real life usage should be able to pass these
|
||||
* tests.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
* @author Jeremy Grelle
|
||||
*/
|
||||
public class SimpleExpressionTests extends TestCase {
|
||||
|
||||
private ExpressionParser expressionParser;
|
||||
private String expressionPrefix;
|
||||
private TestBean bean;
|
||||
|
||||
public static TestSuite suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new ELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new ELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new ELExpressionParser(), "#"));
|
||||
return suite;
|
||||
}
|
||||
|
||||
public SimpleExpressionTests(String name, ExpressionParser expressionParser, String expressionPrefix) {
|
||||
super(name);
|
||||
this.expressionParser = expressionParser;
|
||||
this.expressionPrefix = expressionPrefix;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
bean = new TestBean();
|
||||
bean.setFlag(true);
|
||||
List list = new ArrayList();
|
||||
list.add("foo");
|
||||
list.add("bar");
|
||||
bean.setList(list);
|
||||
}
|
||||
|
||||
public void testGetValue() {
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression(expressionPrefix+"{flag}").evaluate(bean, null));
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("flag").evaluate(bean, null));
|
||||
assertSame(bean.getList(), expressionParser.parseExpression(expressionPrefix+"{list}").evaluate(bean, null));
|
||||
assertEquals("foo", expressionParser.parseExpression(expressionPrefix+"{list[0]}").evaluate(bean, null));
|
||||
}
|
||||
|
||||
public void testSetValue() {
|
||||
expressionParser.parseSettableExpression(expressionPrefix+"{flag}").evaluateToSet(bean, Boolean.FALSE, null);
|
||||
assertFalse(bean.isFlag());
|
||||
expressionParser.parseSettableExpression("flag").evaluateToSet(bean, Boolean.TRUE, null);
|
||||
assertTrue(bean.isFlag());
|
||||
List newList = new ArrayList();
|
||||
newList.add("boo");
|
||||
expressionParser.parseSettableExpression(expressionPrefix+"{list}").evaluateToSet(bean, newList, null);
|
||||
assertSame(newList, bean.getList());
|
||||
expressionParser.parseSettableExpression(expressionPrefix+"{list[0]}").evaluateToSet(bean, "baa", null);
|
||||
assertEquals("baa", bean.getList().get(0));
|
||||
}
|
||||
|
||||
public void testSyntaxError() {
|
||||
try {
|
||||
expressionParser.parseExpression("foo(").evaluate(bean, null);
|
||||
fail("should have failed");
|
||||
}
|
||||
catch (ParserException e) {
|
||||
}
|
||||
catch (EvaluationException e) {
|
||||
}
|
||||
private ExpressionParser expressionParser;
|
||||
private String expressionPrefix;
|
||||
private TestBean bean;
|
||||
|
||||
public static TestSuite suite() {
|
||||
TestSuite suite = new TestSuite();
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new OgnlExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new BeanWrapperExpressionParser(), "$"));
|
||||
suite.addTest(new SimpleExpressionTests("testGetValue", new ELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSetValue", new ELExpressionParser(), "#"));
|
||||
suite.addTest(new SimpleExpressionTests("testSyntaxError", new ELExpressionParser(), "#"));
|
||||
return suite;
|
||||
}
|
||||
|
||||
public SimpleExpressionTests(String name, ExpressionParser expressionParser, String expressionPrefix) {
|
||||
super(name);
|
||||
this.expressionParser = expressionParser;
|
||||
this.expressionPrefix = expressionPrefix;
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
bean = new TestBean();
|
||||
bean.setFlag(true);
|
||||
List list = new ArrayList();
|
||||
list.add("foo");
|
||||
list.add("bar");
|
||||
bean.setList(list);
|
||||
}
|
||||
|
||||
public void testGetValue() {
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression(expressionPrefix + "{flag}").evaluate(bean, null));
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("flag").evaluate(bean, null));
|
||||
assertSame(bean.getList(), expressionParser.parseExpression(expressionPrefix + "{list}").evaluate(bean, null));
|
||||
assertEquals("foo", expressionParser.parseExpression(expressionPrefix + "{list[0]}").evaluate(bean, null));
|
||||
}
|
||||
|
||||
public void testSetValue() {
|
||||
expressionParser.parseSettableExpression(expressionPrefix + "{flag}").evaluateToSet(bean, Boolean.FALSE, null);
|
||||
assertFalse(bean.isFlag());
|
||||
expressionParser.parseSettableExpression("flag").evaluateToSet(bean, Boolean.TRUE, null);
|
||||
assertTrue(bean.isFlag());
|
||||
List newList = new ArrayList();
|
||||
newList.add("boo");
|
||||
expressionParser.parseSettableExpression(expressionPrefix + "{list}").evaluateToSet(bean, newList, null);
|
||||
assertSame(newList, bean.getList());
|
||||
expressionParser.parseSettableExpression(expressionPrefix + "{list[0]}").evaluateToSet(bean, "baa", null);
|
||||
assertEquals("baa", bean.getList().get(0));
|
||||
}
|
||||
|
||||
public void testSyntaxError() {
|
||||
try {
|
||||
expressionParser.parseExpression("foo(").evaluate(bean, null);
|
||||
fail("should have failed");
|
||||
} catch (ParserException e) {
|
||||
} catch (EvaluationException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user