more tests
This commit is contained in:
@@ -75,7 +75,8 @@ class OgnlExpression implements Expression {
|
||||
|
||||
public Object getValue(Object context) throws EvaluationException {
|
||||
try {
|
||||
return Ognl.getValue(expression, getVariables(context), context, expectedResultType);
|
||||
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
|
||||
return Ognl.getValue(expression, evaluationContext, context, expectedResultType);
|
||||
} catch (OgnlException e) {
|
||||
if (e.getReason() != null && e.getReason() != e) {
|
||||
// unwrap the OgnlException since the actual exception is wrapped inside it
|
||||
@@ -90,7 +91,8 @@ class OgnlExpression implements Expression {
|
||||
public void setValue(Object context, Object value) {
|
||||
Assert.notNull(context, "The context to set the provided value in is required");
|
||||
try {
|
||||
Ognl.setValue(expression, getVariables(context), context, value);
|
||||
Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context));
|
||||
Ognl.setValue(expression, evaluationContext, context, value);
|
||||
} catch (OgnlException e) {
|
||||
throw new EvaluationException(new SetValueAttempt(this, context, value), e);
|
||||
}
|
||||
|
||||
@@ -78,6 +78,23 @@ public class ELExpressionParserTests extends TestCase {
|
||||
assertEquals("foo2", exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testVariablesWithCoersion() {
|
||||
String expressionString = "#{max}";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("max", "#{maximum}", new ParserContextImpl().expect(Long.class))));
|
||||
TestBean target = new TestBean();
|
||||
assertEquals(new Long(2), exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testNestedVariables() {
|
||||
String expressionString = "#{value}#{max}";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("max", "#{maximum}#{var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "bar")))));
|
||||
TestBean target = new TestBean();
|
||||
assertEquals("foo2bar", exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testParseImmediateEvalExpression() {
|
||||
String expressionString = "${3 + 4}";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
|
||||
@@ -166,4 +166,19 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
"var", "${flag}")));
|
||||
assertEquals(false, ((Boolean) exp.getValue(bean)).booleanValue());
|
||||
}
|
||||
|
||||
public void testVariablesWithCoersion() {
|
||||
Expression exp = parser.parseExpression("${#var}", new ParserContextImpl().variable(new ExpressionVariable(
|
||||
"var", "${number}", new ParserContextImpl().expect(Long.class))));
|
||||
assertEquals(new Long(0), exp.getValue(bean));
|
||||
}
|
||||
|
||||
public void testNestedVariables() {
|
||||
Expression exp = parser
|
||||
.parseExpression("${#var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "${flag}${#var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "${number}")))));
|
||||
assertEquals("false0", exp.getValue(bean));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,6 +22,8 @@ public class TestBean {
|
||||
|
||||
private boolean flag;
|
||||
|
||||
private int number;
|
||||
|
||||
private List list = new ArrayList();
|
||||
|
||||
public boolean isFlag() {
|
||||
@@ -32,6 +34,14 @@ public class TestBean {
|
||||
this.flag = flag;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public List getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user