Merging of SWF-367 back into trunk. This has all of the SWF 2.0 goodies.

This commit is contained in:
Ben Hale
2007-10-11 01:16:37 +00:00
parent b9513cd42b
commit 58d1ea3acc
984 changed files with 11849 additions and 39596 deletions

View File

@@ -1,92 +1,160 @@
package org.springframework.binding.expression.el;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import org.jboss.el.ExpressionFactoryImpl;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.support.TestBean;
import org.springframework.binding.expression.support.TestMethods;
import org.springframework.binding.expression.ExpressionVariable;
/**
* Tests to exercise the extended method invoking expression extensions of JBoss-el.
* @author Jeremy Grelle
*/
public class ELExpressionParserTests extends TestCase {
ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl());
private ELExpressionParser parser = new ELExpressionParser(new ExpressionFactoryImpl());
Map context;
Map container;
TestMethods target;
protected void setUp() throws Exception {
context = new HashMap();
container = new HashMap();
target = (TestMethods) EasyMock.createMock(TestMethods.class);
context.put("container", container);
container.put("myObject", target);
public void setUp() {
parser.putContextFactory(TestBean.class, new TestELContextFactory());
}
public void testWithIntParam() {
String expression = "#{container.myObject.doSomethingWithInt(container.param1)}";
int param = 5;
container.put("param1", new Integer(param));
target.doSomethingWithInt(param);
EasyMock.replay(new Object[] { target });
private static class TestELContextFactory implements ELContextFactory {
public ELContext getELContext(final Object target, final VariableMapper variableMapper) {
return new ELContext() {
public ELResolver getELResolver() {
return new DefaultELResolver(target, null);
}
parser.parseExpression(expression).evaluate(context, null);
EasyMock.verify(new Object[] { target });
public FunctionMapper getFunctionMapper() {
return null;
}
public VariableMapper getVariableMapper() {
return variableMapper;
}
};
}
}
public void testReturnWithIntParam() {
String expected = "sucess";
String expression = "#{container.myObject.returnStringFromInt(container.param1)}";
int param = 5;
container.put("param1", new Integer(param));
EasyMock.expect(target.returnStringFromInt(param)).andReturn(expected);
EasyMock.replay(new Object[] { target });
String result = (String) parser.parseExpression(expression).evaluate(context, null);
EasyMock.verify(new Object[] { target });
assertEquals(expected, result);
public void testParseEvalExpression() {
String expressionString = "#{value}";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = String.class;
ExpressionVariable[] expressionVariables = null;
Expression exp = parser.parseExpression(expressionString, expressionTargetType, expectedEvaluationResultType,
expressionVariables);
TestBean target = new TestBean();
assertEquals("foo", exp.getValue(target));
}
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);
EasyMock.expect(target.returnStringFromIntAndObject(param1, param2)).andReturn(expected);
EasyMock.replay(new Object[] { target });
String result = (String) parser.parseExpression(expression).evaluate(context, null);
EasyMock.verify(new Object[] { target });
assertEquals(expected, result);
public void testParseLiteralExpressionStringAsEvalExpression() {
String expressionString = "value";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = String.class;
ExpressionVariable[] expressionVariables = null;
Expression exp = parser.parseExpression(parser.parseEvalExpressionString(expressionString),
expressionTargetType, expectedEvaluationResultType, expressionVariables);
TestBean target = new TestBean();
assertEquals("foo", exp.getValue(target));
}
public void testEmptyMethod() {
String expStr1 = "#{foo.bar()}";
Expression result1 = parser.parseExpression(expStr1);
assertNotNull(result1);
assertEquals(expStr1, result1.toString());
public void testParseLiteralExpression() {
String expressionString = "value";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = String.class;
ExpressionVariable[] expressionVariables = null;
Expression exp = parser.parseExpression(expressionString, expressionTargetType, expectedEvaluationResultType,
expressionVariables);
TestBean target = new TestBean();
assertEquals("value", exp.getValue(target));
}
public void testMethodWithParams() {
String expStr1 = "#{foo.bar(moe.curly, groucho.harpo)}";
Expression result1 = parser.parseExpression(expStr1);
assertNotNull(result1);
assertEquals(expStr1, result1.toString());
public void testParseExpressionWithVariables() {
String expressionString = "#{value}#{max}";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = String.class;
ExpressionVariable[] expressionVariables = new ExpressionVariable[] { new ExpressionVariable("max",
"#{maximum}") };
Expression exp = parser.parseExpression(expressionString, expressionTargetType, expectedEvaluationResultType,
expressionVariables);
TestBean target = new TestBean();
assertEquals("foo2", exp.getValue(target));
}
public void testParseExpressionWithVariables2() {
String expressionString = "#{value}#{bean.encode(value)}";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = String.class;
ExpressionVariable[] expressionVariables = null;
Expression exp = parser.parseExpression(expressionString, expressionTargetType, expectedEvaluationResultType,
expressionVariables);
TestBean target = new TestBean(new TestBean());
assertEquals("foo!foo", exp.getValue(target));
}
public void testParseExpressionCoerceToInteger() {
String expressionString = "#{maximum}#{max}";
Class expressionTargetType = TestBean.class;
Class expectedEvaluationResultType = Integer.class;
ExpressionVariable[] expressionVariables = new ExpressionVariable[] { new ExpressionVariable("max",
"#{maximum}") };
Expression exp = parser.parseExpression(expressionString, expressionTargetType, expectedEvaluationResultType,
expressionVariables);
TestBean target = new TestBean();
assertEquals(new Integer(22), exp.getValue(target));
}
public static class TestBean {
private String value = "foo";
private int maximum = 2;
private TestBean bean;
private List list = new ArrayList();
public TestBean() {
initList();
}
public TestBean(TestBean bean) {
this.bean = bean;
initList();
}
private void initList() {
list.add("1");
list.add("2");
list.add("3");
}
public TestBean getBean() {
return bean;
}
public String getValue() {
return value;
}
public String encode(String data) {
return "!" + data;
}
public void setValue(String value) {
}
public int getMaximum() {
return maximum;
}
public void setMaximum(int maximum) {
this.maximum = maximum;
}
}
}

View File

@@ -19,8 +19,6 @@ import junit.framework.TestCase;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.ognl.OgnlExpressionParser;
import org.springframework.binding.expression.support.TestBean;
/**
* Unit tests for {@link org.springframework.binding.expression.ognl.OgnlExpressionParser}.
@@ -33,44 +31,44 @@ public class OgnlExpressionParserTests extends TestCase {
public void testParseSimpleDelimited() {
String exp = "${flag}";
Expression e = parser.parseExpression(exp);
Expression e = parser.parseExpression(exp, null, null, null);
assertNotNull(e);
Boolean b = (Boolean) e.evaluate(bean, null);
Boolean b = (Boolean) e.getValue(bean);
assertFalse(b.booleanValue());
}
public void testParseSimple() {
String exp = "flag";
Expression e = parser.parseExpression(exp);
Expression e = parser.parseExpression(exp, null, null, null);
assertNotNull(e);
Boolean b = (Boolean) e.evaluate(bean, null);
Boolean b = (Boolean) e.getValue(bean);
assertFalse(b.booleanValue());
}
public void testParseNull() {
Expression e = parser.parseExpression(null);
Expression e = parser.parseExpression(null, null, null, null);
assertNotNull(e);
assertNull(e.evaluate(bean, null));
assertNull(e.getValue(bean));
}
public void testParseEmpty() {
Expression e = parser.parseExpression("");
Expression e = parser.parseExpression("", null, null, null);
assertNotNull(e);
assertEquals("", e.evaluate(bean, null));
assertEquals("", e.getValue(bean));
}
public void testParseComposite() {
String exp = "hello ${flag} ${flag} ${flag}";
Expression e = parser.parseExpression(exp);
Expression e = parser.parseExpression(exp, null, null, null);
assertNotNull(e);
String str = (String) e.evaluate(bean, null);
String str = (String) e.getValue(bean);
assertEquals("hello false false false", str);
}
public void testEnclosedCompositeNotSupported() {
String exp = "${hello ${flag} ${flag} ${flag}}";
try {
parser.parseExpression(exp);
parser.parseExpression(exp, null, null, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
@@ -78,14 +76,13 @@ public class OgnlExpressionParserTests extends TestCase {
public void testSyntaxError1() {
try {
parser.parseExpression("${");
parser.parseExpression("${", null, null, null);
fail();
} catch (ParserException e) {
}
try {
String exp = "hello ${flag} ${abcd defg";
parser.parseExpression(exp);
parser.parseExpression(exp, null, null, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
@@ -93,50 +90,38 @@ public class OgnlExpressionParserTests extends TestCase {
public void testSyntaxError2() {
try {
parser.parseExpression("${}");
parser.parseExpression("${}", null, null, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
try {
String exp = "hello ${flag} ${}";
parser.parseExpression(exp);
parser.parseExpression(exp, null, null, null);
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
}
public void testIsDelimitedExpression() {
assertTrue(parser.isDelimitedExpression("${foo}"));
assertTrue(parser.isDelimitedExpression("${foo ${foo}}"));
assertTrue(parser.isDelimitedExpression("foo ${bar}"));
}
public void testIsNotDelimitedExpression() {
assertFalse(parser.isDelimitedExpression("foo"));
assertFalse(parser.isDelimitedExpression("foo ${"));
assertFalse(parser.isDelimitedExpression("$foo}"));
assertFalse(parser.isDelimitedExpression("foo ${}"));
}
public void testCollectionContructionSyntax() {
public void testCollectionConstructionSyntax() {
// lists
parser.parseExpression("name in {null, \"Untitled\"}");
parser.parseExpression("${name in {null, \"Untitled\"}}");
parser.parseExpression("name in {null, \"Untitled\"}", null, null, null);
parser.parseExpression("${name in {null, \"Untitled\"}}", null, null, null);
// native arrays
parser.parseExpression("new int[] {1, 2, 3}");
parser.parseExpression("${new int[] {1, 2, 3}}");
parser.parseExpression("new int[] {1, 2, 3}", null, null, null);
parser.parseExpression("${new int[] {1, 2, 3}}", null, null, null);
// maps
parser.parseExpression("#{ 'foo' : 'foo value', 'bar' : 'bar value' }");
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}");
parser.parseExpression("#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }");
parser.parseExpression("${#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }}");
parser.parseExpression("#{ 'foo' : 'foo value', 'bar' : 'bar value' }", null, null, null);
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", null, null, null);
parser.parseExpression("#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }", null, null,
null);
parser.parseExpression("${#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }}", null, null,
null);
// complex examples
parser.parseExpression("b,#{1:2}");
parser.parseExpression("${b,#{1:2}}");
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k");
parser.parseExpression("b,#{1:2}", null, null, null);
parser.parseExpression("${b,#{1:2}}", null, null, null);
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k", null, null, null);
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.binding.expression.support;
package org.springframework.binding.expression.ognl;
import java.util.ArrayList;
import java.util.List;

View File

@@ -1,67 +0,0 @@
/*
* 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 junit.framework.TestCase;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionParser;
/**
* Unit tests for {@link org.springframework.binding.expression.support.CollectionAddingExpression}.
*/
public class CollectionAddingExpressionTests extends TestCase {
ExpressionParser parser = new BeanWrapperExpressionParser();
TestBean bean = new TestBean();
Expression exp = parser.parseExpression("list");
public void testEvaluation() {
ArrayList list = new ArrayList();
bean.setList(list);
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
assertSame(list, colExp.evaluate(bean, null));
}
public void testAddToCollection() {
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
colExp.evaluateToSet(bean, "1", null);
colExp.evaluateToSet(bean, "2", null);
assertEquals("1", bean.getList().get(0));
assertEquals("2", bean.getList().get(1));
}
public void testNotACollection() {
Expression exp = parser.parseExpression("flag");
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
try {
colExp.evaluateToSet(bean, "1", null);
fail("not a collection");
} catch (IllegalArgumentException e) {
}
}
public void testNoAddOnNullValue() {
CollectionAddingExpression colExp = new CollectionAddingExpression(exp);
colExp.evaluateToSet(bean, null, null);
colExp.evaluateToSet(bean, "2", null);
assertEquals("2", bean.getList().get(0));
}
}

View File

@@ -1,101 +0,0 @@
/*
* 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.jboss.el.ExpressionFactoryImpl;
import org.springframework.binding.expression.EvaluationException;
import org.springframework.binding.expression.ExpressionParser;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.el.ELExpressionParser;
import org.springframework.binding.expression.ognl.OgnlExpressionParser;
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(new ExpressionFactoryImpl()),
"#"));
suite.addTest(new SimpleExpressionTests("testSetValue", new ELExpressionParser(new ExpressionFactoryImpl()),
"#"));
suite.addTest(new SimpleExpressionTests("testSyntaxError", new ELExpressionParser(new ExpressionFactoryImpl()),
"#"));
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));
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());
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(expressionPrefix + "{foo(}").evaluate(bean, null);
fail("should have failed");
} catch (ParserException e) {
} catch (EvaluationException e) {
}
}
}

View File

@@ -1,10 +0,0 @@
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);
}

View File

@@ -103,7 +103,7 @@ public class TextToMethodSignatureTests extends TestCase {
assertEquals("foo", signature.getMethodName());
assertEquals(1, signature.getParameters().size());
assertNull(signature.getParameters().getParameter(0).getType());
assertEquals("{1, 2, 3}", signature.getParameters().getParameter(0).getName().toString());
assertEquals("{ 1, 2, 3 }", signature.getParameters().getParameter(0).getName().toString());
}
public void testCollectionConstructionSyntaxWithType() {
@@ -111,7 +111,7 @@ public class TextToMethodSignatureTests extends TestCase {
assertEquals("foo", signature.getMethodName());
assertEquals(1, signature.getParameters().size());
assertEquals(java.util.List.class, signature.getParameters().getParameter(0).getType());
assertEquals("{1, 2, 3}", signature.getParameters().getParameter(0).getName().toString());
assertEquals("{ 1, 2, 3 }", signature.getParameters().getParameter(0).getName().toString());
signature = (MethodSignature) converter.convert("foo(a${b,#{1:2},e}f${g,#{3:4},j}k)");
}