SPR-6763: more methods on StandardEvaluationContext supporting add/remove from resolver/accessor strategies

This commit is contained in:
Andy Clement
2010-02-02 21:38:33 +00:00
parent 66f708392e
commit 5ca1f11ce2
4 changed files with 125 additions and 0 deletions

View File

@@ -16,9 +16,16 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.ConstructorExecutor;
import org.springframework.expression.ConstructorResolver;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -140,6 +147,37 @@ public class ConstructorInvocationTests extends ExpressionTestCase {
Assert.assertEquals(4,parser.parseExpression("counter").getValue(eContext));
}
@Test
public void testAddingConstructorResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective constructor accessor is the only one by default
List<ConstructorResolver> constructorResolvers = ctx.getConstructorResolvers();
Assert.assertEquals(1,constructorResolvers.size());
ConstructorResolver dummy = new DummyConstructorResolver();
ctx.addConstructorResolver(dummy);
Assert.assertEquals(2,ctx.getConstructorResolvers().size());
List<ConstructorResolver> copy = new ArrayList<ConstructorResolver>();
copy.addAll(ctx.getConstructorResolvers());
Assert.assertTrue(ctx.removeConstructorResolver(dummy));
Assert.assertFalse(ctx.removeConstructorResolver(dummy));
Assert.assertEquals(1,ctx.getConstructorResolvers().size());
ctx.setConstructorResolvers(copy);
Assert.assertEquals(2,ctx.getConstructorResolvers().size());
}
static class DummyConstructorResolver implements ConstructorResolver {
public ConstructorExecutor resolve(EvaluationContext context, String typeName, Class<?>[] argumentTypes)
throws AccessException {
throw new UnsupportedOperationException("Auto-generated method stub");
}
}
@Test
public void testVarargsInvocation01() {
// Calling 'Fruit(String... strings)'

View File

@@ -25,8 +25,12 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodFilter;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -243,6 +247,38 @@ public class MethodInvocationTests extends ExpressionTestCase {
}
}
@Test
public void testAddingMethodResolvers() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective method accessor is the only one by default
List<MethodResolver> methodResolvers = ctx.getMethodResolvers();
Assert.assertEquals(1,methodResolvers.size());
MethodResolver dummy = new DummyMethodResolver();
ctx.addMethodResolver(dummy);
Assert.assertEquals(2,ctx.getMethodResolvers().size());
List<MethodResolver> copy = new ArrayList<MethodResolver>();
copy.addAll(ctx.getMethodResolvers());
Assert.assertTrue(ctx.removeMethodResolver(dummy));
Assert.assertFalse(ctx.removeMethodResolver(dummy));
Assert.assertEquals(1,ctx.getMethodResolvers().size());
ctx.setMethodResolvers(copy);
Assert.assertEquals(2,ctx.getMethodResolvers().size());
}
static class DummyMethodResolver implements MethodResolver {
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
Class<?>[] argumentTypes) throws AccessException {
throw new UnsupportedOperationException("Auto-generated method stub");
}
}
@Test
public void testVarargsInvocation01() {

View File

@@ -16,6 +16,9 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
@@ -129,6 +132,28 @@ public class PropertyAccessTests extends ExpressionTestCase {
// System.out.println(e.getMessage());
}
}
@Test
public void testAddingRemovingAccessors() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective property accessor is the only one by default
List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
Assert.assertEquals(1,propertyAccessors.size());
StringyPropertyAccessor spa = new StringyPropertyAccessor();
ctx.addPropertyAccessor(spa);
Assert.assertEquals(2,ctx.getPropertyAccessors().size());
List<PropertyAccessor> copy = new ArrayList<PropertyAccessor>();
copy.addAll(ctx.getPropertyAccessors());
Assert.assertTrue(ctx.removePropertyAccessor(spa));
Assert.assertFalse(ctx.removePropertyAccessor(spa));
Assert.assertEquals(1,ctx.getPropertyAccessors().size());
ctx.setPropertyAccessors(copy);
Assert.assertEquals(2,ctx.getPropertyAccessors().size());
}
// This can resolve the property 'flibbles' on any String (very useful...)