Integration of unified EL into spring-binding. See SWF-287.
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.el.BeanELResolver;
|
||||
import javax.el.ELContext;
|
||||
import javax.el.ELResolver;
|
||||
import javax.el.FunctionMapper;
|
||||
import javax.el.VariableMapper;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
|
||||
/**
|
||||
* Tests to verify the delegation behavior of ELContextImpl.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public FunctionMapper getFunctionMapper() {
|
||||
return null;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Tests to exercise the extended method invoking expression extensions of JBoss-el.
|
||||
*
|
||||
* @author Jeremy Grelle
|
||||
*
|
||||
*/
|
||||
public class ELMethodExpressionTests extends TestCase {
|
||||
|
||||
ELExpressionParser parser = new ELExpressionParser();
|
||||
|
||||
Map context;
|
||||
|
||||
Map container;
|
||||
|
||||
TestMethods target;
|
||||
MockControl targetMockControl;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
context = new HashMap();
|
||||
container = new HashMap();
|
||||
targetMockControl = MockControl.createControl(TestMethods.class);
|
||||
target = (TestMethods) targetMockControl.getMock();
|
||||
context.put("container", container);
|
||||
container.put("myObject", target);
|
||||
}
|
||||
|
||||
public void testWithIntParam() {
|
||||
String expression = "#{container.myObject.doSomethingWithInt(container.param1)}";
|
||||
int param = 5;
|
||||
container.put("param1", new Integer(param));
|
||||
target.doSomethingWithInt(param);
|
||||
targetMockControl.replay();
|
||||
|
||||
parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
|
||||
}
|
||||
|
||||
public void testReturnWithIntParam() {
|
||||
String expected = "sucess";
|
||||
String expression = "#{container.myObject.returnStringFromInt(container.param1)}";
|
||||
int param = 5;
|
||||
container.put("param1", new Integer(param));
|
||||
targetMockControl.expectAndReturn(target.returnStringFromInt(param), expected);
|
||||
targetMockControl.replay();
|
||||
|
||||
String result = (String) parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
public void testReturnWithIntAndObject() {
|
||||
String expected = "success";
|
||||
String expression = "#{container.myObject.returnStringFromIntAndObject(container.param1, container.param2)}";
|
||||
int param1 = 5;
|
||||
container.put("param1", new Integer(param1));
|
||||
TestBean param2 = new TestBean();
|
||||
container.put("param2", param2);
|
||||
targetMockControl.expectAndReturn(target.returnStringFromIntAndObject(param1, param2), expected);
|
||||
targetMockControl.replay();
|
||||
|
||||
String result = (String) parser.parseExpression(expression).evaluate(context, null);
|
||||
targetMockControl.verify();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.binding.expression.Expression;
|
||||
|
||||
public class ELMethodParsingTests extends TestCase {
|
||||
|
||||
ELExpressionParser parser;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
parser = new ELExpressionParser();
|
||||
}
|
||||
|
||||
public void testEmptyMethod() {
|
||||
|
||||
String expStr1 = "#{foo.bar()}";
|
||||
Expression result1 = parser.parseExpression(expStr1);
|
||||
assertNotNull(result1);
|
||||
assertEquals(expStr1, result1.toString());
|
||||
|
||||
String expStr2 = "foo.bar()";
|
||||
Expression result2 = parser.parseExpression(expStr2);
|
||||
assertNotNull(result2);
|
||||
assertEquals(expStr1, result2.toString());
|
||||
}
|
||||
|
||||
public void testMethodWithParams() {
|
||||
|
||||
String expStr1 = "#{foo.bar(moe.curly, groucho.harpo)}";
|
||||
Expression result1 = parser.parseExpression(expStr1);
|
||||
assertNotNull(result1);
|
||||
assertEquals(expStr1, result1.toString());
|
||||
|
||||
String expStr2 = "foo.bar(moe.curly, groucho.harpo)";
|
||||
Expression result2 = parser.parseExpression(expStr2);
|
||||
assertNotNull(result2);
|
||||
assertEquals(expStr1, result2.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +1,100 @@
|
||||
/*
|
||||
* Copyright 2004-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
|
||||
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.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
*/
|
||||
public class SimpleExpressionTests extends TestCase {
|
||||
|
||||
private ExpressionParser expressionParser;
|
||||
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()));
|
||||
return suite;
|
||||
}
|
||||
|
||||
public SimpleExpressionTests(String name, ExpressionParser expressionParser) {
|
||||
super(name);
|
||||
this.expressionParser = expressionParser;
|
||||
}
|
||||
|
||||
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("${flag}").evaluate(bean, null));
|
||||
assertEquals(Boolean.TRUE, expressionParser.parseExpression("flag").evaluate(bean, null));
|
||||
assertSame(bean.getList(), expressionParser.parseExpression("${list}").evaluate(bean, null));
|
||||
assertEquals("foo", expressionParser.parseExpression("${list[0]}").evaluate(bean, null));
|
||||
}
|
||||
|
||||
public void testSetValue() {
|
||||
expressionParser.parseSettableExpression("${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("${list}").evaluateToSet(bean, newList, null);
|
||||
assertSame(newList, bean.getList());
|
||||
expressionParser.parseSettableExpression("${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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright 2004-2007 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.binding.expression.EvaluationException;
|
||||
import org.springframework.binding.expression.ExpressionParser;
|
||||
import org.springframework.binding.expression.ParserException;
|
||||
|
||||
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.
|
||||
*
|
||||
* @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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.springframework.binding.expression.support;
|
||||
|
||||
public interface TestMethods {
|
||||
|
||||
public void doSomethingWithInt(int arg);
|
||||
|
||||
public String returnStringFromInt(int arg);
|
||||
|
||||
public String returnStringFromIntAndObject(int arg, TestBean bean);
|
||||
}
|
||||
Reference in New Issue
Block a user