SimpleEvaluationContext.Builder withRootObject/withTypedRootObject

Issue: SPR-16588
This commit is contained in:
Juergen Hoeller
2018-03-27 00:22:54 +02:00
parent 19875d8e3f
commit c60cefa331
2 changed files with 72 additions and 17 deletions

View File

@@ -234,10 +234,29 @@ public class PropertyAccessTests extends AbstractExpressionTests {
assertEquals("p4", expr.getValue(context, target));
}
@Test
public void propertyReadWriteWithRootObject() {
Person target = new Person("p1");
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withRootObject(target).build();
assertSame(target, context.getRootObject().getValue());
Expression expr = parser.parseExpression("name");
assertEquals("p1", expr.getValue(context, target));
target.setName("p2");
assertEquals("p2", expr.getValue(context, target));
parser.parseExpression("name='p3'").getValue(context, target);
assertEquals("p3", target.getName());
assertEquals("p3", expr.getValue(context, target));
expr.setValue(context, target, "p4");
assertEquals("p4", target.getName());
assertEquals("p4", expr.getValue(context, target));
}
@Test
public void propertyAccessWithoutMethodResolver() {
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
Person target = new Person("p1");
try {
parser.parseExpression("name.substring(1)").getValue(context, target);
@@ -251,11 +270,21 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@Test
public void propertyAccessWithInstanceMethodResolver() {
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().withInstanceMethods().build();
Person target = new Person("p1");
assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
}
@Test
public void propertyAccessWithInstanceMethodResolverAndTypedRootObject() {
Person target = new Person("p1");
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().
withInstanceMethods().withTypedRootObject(target, TypeDescriptor.valueOf(Object.class)).build();
assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target));
assertSame(target, context.getRootObject().getValue());
assertSame(Object.class, context.getRootObject().getTypeDescriptor().getType());
}
// This can resolve the property 'flibbles' on any String (very useful...)
private static class StringyPropertyAccessor implements PropertyAccessor {