Polishing

This commit is contained in:
Juergen Hoeller
2018-03-28 02:03:31 +02:00
parent 59189e5957
commit 9cc03fa8b0
52 changed files with 511 additions and 560 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@@ -17,10 +17,9 @@
package org.springframework.expression;
/**
* A bean resolver can be registered with the evaluation context
* and will kick in for {@code @myBeanName} and {@code &myBeanName} expressions.
* The <tt>&</tt> variant syntax allows access to the factory bean where
* relevant.
* A bean resolver can be registered with the evaluation context and will kick in
* for bean references: {@code @myBeanName} and {@code &myBeanName} expressions.
* The <tt>&</tt> variant syntax allows access to the factory bean where relevant.
*
* @author Andy Clement
* @since 3.0.3
@@ -28,12 +27,12 @@ package org.springframework.expression;
public interface BeanResolver {
/**
* Look up the named bean and return it. If attempting to access a factory
* bean the name will have a <tt>&</tt> prefix.
* Look up a bean by the given name and return a corresponding instance for it.
* For attempting access to a factory bean, the name needs a <tt>&</tt> prefix.
* @param context the current evaluation context
* @param beanName the name of the bean to lookup
* @param beanName the name of the bean to look up
* @return an object representing the bean
* @throws AccessException if there is an unexpected problem resolving the named bean
* @throws AccessException if there is an unexpected problem resolving the bean
*/
Object resolve(EvaluationContext context, String beanName) throws AccessException;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@@ -44,7 +44,7 @@ public abstract class AbstractExpressionTests {
protected final ExpressionParser parser = new SpelExpressionParser();
protected final StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
protected final StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
/**
@@ -63,14 +63,14 @@ public abstract class AbstractExpressionTests {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
Object value = expr.getValue(eContext);
Object value = expr.getValue(context);
// Check the return value
if (value == null) {
if (expectedValue == null) {
return; // no point doing other checks
}
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
assertNull("Expression returned null value, but expected '" + expectedValue + "'", expectedValue);
}
Class<?> resultType = value.getClass();
@@ -95,12 +95,12 @@ public abstract class AbstractExpressionTests {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
Object value = expr.getValue(eContext, expectedResultType);
Object value = expr.getValue(context, expectedResultType);
if (value == null) {
if (expectedValue == null) {
return; // no point doing other checks
}
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
assertNull("Expression returned null value, but expected '" + expectedValue + "'", expectedValue);
}
Class<?> resultType = value.getClass();
@@ -127,12 +127,12 @@ public abstract class AbstractExpressionTests {
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
}
Object value = expr.getValue(eContext);
Object value = expr.getValue(context);
if (value == null) {
if (expectedValue == null) {
return; // no point doing other checks
}
assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
assertNull("Expression returned null value, but expected '" + expectedValue + "'", expectedValue);
}
Class<? extends Object> resultType = value.getClass();
if (expectedValue instanceof String) {
@@ -142,10 +142,10 @@ public abstract class AbstractExpressionTests {
else {
assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
}
assertEquals("Type of the result was not as expected. Expected '" + expectedClassOfResult +
"' but result was of type '" + resultType + "'", expectedClassOfResult.equals(resultType), true);
assertTrue("Type of the result was not as expected. Expected '" + expectedClassOfResult +
"' but result was of type '" + resultType + "'", expectedClassOfResult.equals(resultType));
boolean isWritable = expr.isWritable(eContext);
boolean isWritable = expr.isWritable(context);
if (isWritable != shouldBeWritable) {
if (shouldBeWritable)
fail("Expected the expression to be writable but it is not");
@@ -184,10 +184,10 @@ public abstract class AbstractExpressionTests {
fail("Parser returned null for expression");
}
if (expectedReturnType != null) {
expr.getValue(eContext, expectedReturnType);
expr.getValue(context, expectedReturnType);
}
else {
expr.getValue(eContext);
expr.getValue(context);
}
fail("Should have failed with message " + expectedMessage);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2018 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.
@@ -104,11 +104,12 @@ public class BooleanExpressionTests extends AbstractExpressionTests {
return targetType.getType() == Boolean.class ? false : null;
}
};
eContext.setTypeConverter(new StandardTypeConverter(conversionService));
context.setTypeConverter(new StandardTypeConverter(conversionService));
evaluate("null or true", Boolean.TRUE, Boolean.class, false);
evaluate("null and true", Boolean.FALSE, Boolean.class, false);
evaluate("!null", Boolean.TRUE, Boolean.class, false);
evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
}

View File

@@ -359,7 +359,7 @@ public class EvaluationTests extends AbstractExpressionTests {
@Test
public void testTernaryOperator04() {
Expression e = parser.parseExpression("1>2?3:4");
assertFalse(e.isWritable(eContext));
assertFalse(e.isWritable(context));
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 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.
@@ -98,7 +98,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
eContext.setVariable("bar", 3);
Object o = expr.getValue(eContext);
assertEquals(o, 3);
assertEquals(3, o);
assertEquals(1, parser.parseExpression("counter").getValue(eContext));
// Now the expression has cached that throwException(int) is the right thing to call
@@ -163,9 +163,9 @@ public class MethodInvocationTests extends AbstractExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
eContext.setVariable("bar", 2);
context.setVariable("bar", 2);
try {
expr.getValue(eContext);
expr.getValue(context);
fail();
}
catch (Exception ex) {
@@ -187,9 +187,9 @@ public class MethodInvocationTests extends AbstractExpressionTests {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
eContext.setVariable("bar", 4);
context.setVariable("bar", 4);
try {
expr.getValue(eContext);
expr.getValue(context);
fail();
}
catch (ExpressionInvocationTargetException ex) {
@@ -251,7 +251,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
ctx.addMethodResolver(dummy);
assertEquals(2, ctx.getMethodResolvers().size());
List<MethodResolver> copy = new ArrayList<MethodResolver>();
List<MethodResolver> copy = new ArrayList<>();
copy.addAll(ctx.getMethodResolvers());
assertTrue(ctx.removeMethodResolver(dummy));
assertFalse(ctx.removeMethodResolver(dummy));
@@ -294,7 +294,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
public void testMethodOfClass() throws Exception {
Expression expression = parser.parseExpression("getName()");
Object value = expression.getValue(new StandardEvaluationContext(String.class));
assertEquals(value, "java.lang.String");
assertEquals("java.lang.String", value);
}
@Test
@@ -341,7 +341,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
@Override
public List<Method> filter(List<Method> methods) {
filterCalled = true;
List<Method> forRemoval = new ArrayList<Method>();
List<Method> forRemoval = new ArrayList<>();
for (Method method: methods) {
if (removeIfNotAnnotated && !isAnnotated(method)) {
forRemoval.add(method);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -102,11 +102,11 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void SPR5899() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
StandardEvaluationContext context = new StandardEvaluationContext(new Spr5899Class());
Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
assertEquals(12, expr.getValue(eContext));
assertEquals(12, expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
assertEquals(null, expr.getValue(eContext));
assertEquals(null, expr.getValue(context));
try {
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
expr.getValue();
@@ -115,73 +115,73 @@ public class SpelReproTests extends AbstractExpressionTests {
catch (EvaluationException see) {
// success
}
eContext.setTypeLocator(new MyTypeLocator());
context.setTypeLocator(new MyTypeLocator());
// varargs
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
assertEquals("ab", expr.getValue(eContext));
assertEquals("ab", expr.getValue(context));
// varargs 2 - null is packed into the varargs
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
assertEquals("anullc", expr.getValue(eContext));
assertEquals("anullc", expr.getValue(context));
// check we can find the ctor ok
expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
assertEquals("instance", expr.getValue(eContext));
assertEquals("instance", expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
assertEquals("instance", expr.getValue(eContext));
assertEquals("instance", expr.getValue(context));
// ctor varargs
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
assertEquals("instance", expr.getValue(eContext));
assertEquals("instance", expr.getValue(context));
// ctor varargs 2
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
assertEquals("instance", expr.getValue(eContext));
assertEquals("instance", expr.getValue(context));
}
@Test
public void SPR5905_InnerTypeReferences() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
StandardEvaluationContext context = new StandardEvaluationContext(new Spr5899Class());
Expression expr = new SpelExpressionParser().parseRaw("T(java.util.Map$Entry)");
assertEquals(Map.Entry.class, expr.getValue(eContext));
assertEquals(Map.Entry.class, expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("T(org.springframework.expression.spel.SpelReproTests$Outer$Inner).run()");
assertEquals(12, expr.getValue(eContext));
assertEquals(12, expr.getValue(context));
expr = new SpelExpressionParser().parseRaw("new org.springframework.expression.spel.SpelReproTests$Outer$Inner().run2()");
assertEquals(13, expr.getValue(eContext));
assertEquals(13, expr.getValue(context));
}
@Test
public void SPR5804() {
Map<String, String> m = new HashMap<>();
m.put("foo", "bar");
StandardEvaluationContext eContext = new StandardEvaluationContext(m); // root is a map instance
eContext.addPropertyAccessor(new MapAccessor());
StandardEvaluationContext context = new StandardEvaluationContext(m); // root is a map instance
context.addPropertyAccessor(new MapAccessor());
Expression expr = new SpelExpressionParser().parseRaw("['foo']");
assertEquals("bar", expr.getValue(eContext));
assertEquals("bar", expr.getValue(context));
}
@Test
public void SPR5847() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new TestProperties());
StandardEvaluationContext context = new StandardEvaluationContext(new TestProperties());
String name = null;
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("jdbcProperties['username']");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("Dave", name);
expr = new SpelExpressionParser().parseRaw("jdbcProperties[username]");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("Dave", name);
// MapAccessor required for this to work
expr = new SpelExpressionParser().parseRaw("jdbcProperties.username");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
context.addPropertyAccessor(new MapAccessor());
name = expr.getValue(context, String.class);
assertEquals("Dave", name);
// --- dotted property names
@@ -189,14 +189,14 @@ public class SpelReproTests extends AbstractExpressionTests {
// lookup foo on the root, then bar on that, then use that as the key into
// jdbcProperties
expr = new SpelExpressionParser().parseRaw("jdbcProperties[foo.bar]");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
context.addPropertyAccessor(new MapAccessor());
name = expr.getValue(context, String.class);
assertEquals("Dave2", name);
// key is foo.bar
expr = new SpelExpressionParser().parseRaw("jdbcProperties['foo.bar']");
eContext.addPropertyAccessor(new MapAccessor());
name = expr.getValue(eContext, String.class);
context.addPropertyAccessor(new MapAccessor());
name = expr.getValue(context, String.class);
assertEquals("Elephant", name);
}
@@ -237,60 +237,60 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void accessingNullPropertyViaReflection_SPR5663() throws AccessException {
PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
PropertyAccessor accessor = new ReflectivePropertyAccessor();
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
assertFalse(propertyAccessor.canRead(context, null, "abc"));
assertFalse(propertyAccessor.canWrite(context, null, "abc"));
assertFalse(accessor.canRead(context, null, "abc"));
assertFalse(accessor.canWrite(context, null, "abc"));
try {
propertyAccessor.read(context, null, "abc");
fail("Should have failed with an AccessException");
accessor.read(context, null, "abc");
fail("Should have failed with an IllegalStateException");
}
catch (AccessException ex) {
catch (IllegalStateException ex) {
// expected
}
try {
propertyAccessor.write(context, null, "abc", "foo");
fail("Should have failed with an AccessException");
accessor.write(context, null, "abc", "foo");
fail("Should have failed with an IllegalStateException");
}
catch (AccessException ex) {
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void nestedProperties_SPR6923() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Foo());
StandardEvaluationContext context = new StandardEvaluationContext(new Foo());
Expression expr = new SpelExpressionParser().parseRaw("resource.resource.server");
String name = expr.getValue(eContext, String.class);
String name = expr.getValue(context, String.class);
assertEquals("abc", name);
}
/** Should be accessing Goo.getKey because 'bar' field evaluates to "key" */
@Test
public void indexingAsAPropertyAccess_SPR6968_1() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Goo());
StandardEvaluationContext context = new StandardEvaluationContext(new Goo());
String name = null;
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("instance[bar]");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("hello", name);
name = expr.getValue(eContext, String.class); // will be using the cached accessor this time
name = expr.getValue(context, String.class); // will be using the cached accessor this time
assertEquals("hello", name);
}
/** Should be accessing Goo.getKey because 'bar' variable evaluates to "key" */
@Test
public void indexingAsAPropertyAccess_SPR6968_2() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Goo());
eContext.setVariable("bar", "key");
StandardEvaluationContext context = new StandardEvaluationContext(new Goo());
context.setVariable("bar", "key");
String name = null;
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("instance[#bar]");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("hello", name);
name = expr.getValue(eContext, String.class); // will be using the cached accessor this time
name = expr.getValue(context, String.class); // will be using the cached accessor this time
assertEquals("hello", name);
}
@@ -298,8 +298,8 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void dollarPrefixedIdentifier_SPR7100() {
Holder h = new Holder();
StandardEvaluationContext eContext = new StandardEvaluationContext(h);
eContext.addPropertyAccessor(new MapAccessor());
StandardEvaluationContext context = new StandardEvaluationContext(h);
context.addPropertyAccessor(new MapAccessor());
h.map.put("$foo", "wibble");
h.map.put("foo$bar", "wobble");
h.map.put("foobar$$", "wabble");
@@ -310,42 +310,42 @@ public class SpelReproTests extends AbstractExpressionTests {
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("map.$foo");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("wibble", name);
expr = new SpelExpressionParser().parseRaw("map.foo$bar");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("wobble", name);
expr = new SpelExpressionParser().parseRaw("map.foobar$$");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("wabble", name);
expr = new SpelExpressionParser().parseRaw("map.$");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("wubble", name);
expr = new SpelExpressionParser().parseRaw("map.$$");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("webble", name);
expr = new SpelExpressionParser().parseRaw("map.$_$");
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("tribble", name);
}
/** Should be accessing Goo.wibble field because 'bar' variable evaluates to "wibble" */
@Test
public void indexingAsAPropertyAccess_SPR6968_3() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Goo());
eContext.setVariable("bar", "wibble");
StandardEvaluationContext context = new StandardEvaluationContext(new Goo());
context.setVariable("bar", "wibble");
String name = null;
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("instance[#bar]");
// will access the field 'wibble' and not use a getter
name = expr.getValue(eContext, String.class);
name = expr.getValue(context, String.class);
assertEquals("wobble", name);
name = expr.getValue(eContext, String.class); // will be using the cached accessor this time
name = expr.getValue(context, String.class); // will be using the cached accessor this time
assertEquals("wobble", name);
}
@@ -356,14 +356,14 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void indexingAsAPropertyAccess_SPR6968_4() {
Goo g = Goo.instance;
StandardEvaluationContext eContext = new StandardEvaluationContext(g);
eContext.setVariable("bar", "wibble");
StandardEvaluationContext context = new StandardEvaluationContext(g);
context.setVariable("bar", "wibble");
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("instance[#bar]='world'");
// will access the field 'wibble' and not use a getter
expr.getValue(eContext, String.class);
expr.getValue(context, String.class);
assertEquals("world", g.wibble);
expr.getValue(eContext, String.class); // will be using the cached accessor this time
expr.getValue(context, String.class); // will be using the cached accessor this time
assertEquals("world", g.wibble);
}
@@ -371,31 +371,31 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void indexingAsAPropertyAccess_SPR6968_5() {
Goo g = Goo.instance;
StandardEvaluationContext eContext = new StandardEvaluationContext(g);
StandardEvaluationContext context = new StandardEvaluationContext(g);
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("instance[bar]='world'");
expr.getValue(eContext, String.class);
expr.getValue(context, String.class);
assertEquals("world", g.value);
expr.getValue(eContext, String.class); // will be using the cached accessor this time
expr.getValue(context, String.class); // will be using the cached accessor this time
assertEquals("world", g.value);
}
@Test
public void dollars() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
StandardEvaluationContext context = new StandardEvaluationContext(new XX());
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("m['$foo']");
eContext.setVariable("file_name", "$foo");
assertEquals("wibble", expr.getValue(eContext, String.class));
context.setVariable("file_name", "$foo");
assertEquals("wibble", expr.getValue(context, String.class));
}
@Test
public void dollars2() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
StandardEvaluationContext context = new StandardEvaluationContext(new XX());
Expression expr = null;
expr = new SpelExpressionParser().parseRaw("m[$foo]");
eContext.setVariable("file_name", "$foo");
assertEquals("wibble", expr.getValue(eContext, String.class));
context.setVariable("file_name", "$foo");
assertEquals("wibble", expr.getValue(context, String.class));
}
private void checkTemplateParsing(String expression, String expectedValue) {
@@ -448,33 +448,33 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void beanResolution() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
StandardEvaluationContext context = new StandardEvaluationContext(new XX());
Expression expr = null;
// no resolver registered == exception
try {
expr = new SpelExpressionParser().parseRaw("@foo");
assertEquals("custard", expr.getValue(eContext, String.class));
assertEquals("custard", expr.getValue(context, String.class));
}
catch (SpelEvaluationException see) {
assertEquals(SpelMessage.NO_BEAN_RESOLVER_REGISTERED, see.getMessageCode());
assertEquals("foo", see.getInserts()[0]);
}
eContext.setBeanResolver(new MyBeanResolver());
context.setBeanResolver(new MyBeanResolver());
// bean exists
expr = new SpelExpressionParser().parseRaw("@foo");
assertEquals("custard", expr.getValue(eContext, String.class));
assertEquals("custard", expr.getValue(context, String.class));
// bean does not exist
expr = new SpelExpressionParser().parseRaw("@bar");
assertEquals(null, expr.getValue(eContext, String.class));
assertEquals(null, expr.getValue(context, String.class));
// bean name will cause AccessException
expr = new SpelExpressionParser().parseRaw("@goo");
try {
assertEquals(null, expr.getValue(eContext, String.class));
assertEquals(null, expr.getValue(context, String.class));
}
catch (SpelEvaluationException see) {
assertEquals(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, see.getMessageCode());
@@ -485,12 +485,12 @@ public class SpelReproTests extends AbstractExpressionTests {
// bean exists
expr = new SpelExpressionParser().parseRaw("@'foo.bar'");
assertEquals("trouble", expr.getValue(eContext, String.class));
assertEquals("trouble", expr.getValue(context, String.class));
// bean exists
try {
expr = new SpelExpressionParser().parseRaw("@378");
assertEquals("trouble", expr.getValue(eContext, String.class));
assertEquals("trouble", expr.getValue(context, String.class));
}
catch (SpelParseException spe) {
assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
@@ -499,7 +499,7 @@ public class SpelReproTests extends AbstractExpressionTests {
@Test
public void elvis_SPR7209_1() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
StandardEvaluationContext context = new StandardEvaluationContext(new XX());
Expression expr = null;
// Different parts of elvis expression are null
@@ -513,7 +513,7 @@ public class SpelReproTests extends AbstractExpressionTests {
// Different parts of ternary expression are null
try {
expr = new SpelExpressionParser().parseRaw("(?'abc':'default')");
expr.getValue(eContext);
expr.getValue(context);
fail();
}
catch (SpelEvaluationException see) {
@@ -525,7 +525,7 @@ public class SpelReproTests extends AbstractExpressionTests {
// Assignment
try {
expr = new SpelExpressionParser().parseRaw("(='default')");
expr.getValue(eContext);
expr.getValue(context);
fail();
}
catch (SpelEvaluationException see) {
@@ -553,55 +553,55 @@ public class SpelReproTests extends AbstractExpressionTests {
nameMap.put("givenName", "Arthur");
map.put("value", nameMap);
StandardEvaluationContext ctx = new StandardEvaluationContext(map);
StandardEvaluationContext context = new StandardEvaluationContext(map);
ExpressionParser parser = new SpelExpressionParser();
String el1 = "#root['value'].get('givenName')";
Expression exp = parser.parseExpression(el1);
Object evaluated = exp.getValue(ctx);
Object evaluated = exp.getValue(context);
assertEquals("Arthur", evaluated);
String el2 = "#root['value']['givenName']";
exp = parser.parseExpression(el2);
evaluated = exp.getValue(ctx);
evaluated = exp.getValue(context);
assertEquals("Arthur", evaluated);
}
@Test
public void projectionTypeDescriptors_1() {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ls.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List<?> value = (List<?>) exp.getValue(ctx);
List<?> value = (List<?>) exp.getValue(context);
// value is list containing [true,false]
assertEquals(Boolean.class, value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
assertEquals(null, evaluated.getElementTypeDescriptor());
}
@Test
public void projectionTypeDescriptors_2() {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "as.![#this.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
Object[] value = (Object[]) exp.getValue(ctx);
Object[] value = (Object[]) exp.getValue(context);
// value is array containing [true,false]
assertEquals(Boolean.class, value[0].getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
}
@Test
public void projectionTypeDescriptors_3() {
StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
StandardEvaluationContext context = new StandardEvaluationContext(new C());
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "ms.![key.equals('abc')]";
SpelExpression exp = parser.parseRaw(el1);
List<?> value = (List<?>) exp.getValue(ctx);
List<?> value = (List<?>) exp.getValue(context);
// value is list containing [true,false]
assertEquals(Boolean.class, value.get(0).getClass());
TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
assertEquals(null, evaluated.getElementTypeDescriptor());
}
@@ -615,23 +615,23 @@ public class SpelReproTests extends AbstractExpressionTests {
list.add(new D(null));
list.add(new D("zzz"));
StandardEvaluationContext ctx = new StandardEvaluationContext(list);
StandardEvaluationContext context = new StandardEvaluationContext(list);
SpelExpressionParser parser = new SpelExpressionParser();
String el1 = "#root.?[a < 'hhh']";
SpelExpression exp = parser.parseRaw(el1);
Object value = exp.getValue(ctx);
Object value = exp.getValue(context);
assertEquals("[D(aaa), D(bbb), D(null), D(ccc), D(null)]", value.toString());
String el2 = "#root.?[a > 'hhh']";
SpelExpression exp2 = parser.parseRaw(el2);
Object value2 = exp2.getValue(ctx);
Object value2 = exp2.getValue(context);
assertEquals("[D(zzz)]", value2.toString());
// trim out the nulls first
String el3 = "#root.?[a!=null].?[a < 'hhh']";
SpelExpression exp3 = parser.parseRaw(el3);
Object value3 = exp3.getValue(ctx);
Object value3 = exp3.getValue(context);
assertEquals("[D(aaa), D(bbb), D(ccc)]", value3.toString());
}
@@ -820,41 +820,41 @@ public class SpelReproTests extends AbstractExpressionTests {
}
}
StandardEvaluationContext ctx = new StandardEvaluationContext(new Reserver());
StandardEvaluationContext context = new StandardEvaluationContext(new Reserver());
SpelExpressionParser parser = new SpelExpressionParser();
String ex = "getReserver().NE";
SpelExpression exp = parser.parseRaw(ex);
String value = (String) exp.getValue(ctx);
String value = (String) exp.getValue(context);
assertEquals("abc", value);
ex = "getReserver().ne";
exp = parser.parseRaw(ex);
value = (String) exp.getValue(ctx);
value = (String) exp.getValue(context);
assertEquals("def", value);
ex = "getReserver().m[NE]";
exp = parser.parseRaw(ex);
value = (String) exp.getValue(ctx);
value = (String) exp.getValue(context);
assertEquals("xyz", value);
ex = "getReserver().DIV";
exp = parser.parseRaw(ex);
assertEquals(1, exp.getValue(ctx));
assertEquals(1, exp.getValue(context));
ex = "getReserver().div";
exp = parser.parseRaw(ex);
assertEquals(3, exp.getValue(ctx));
assertEquals(3, exp.getValue(context));
exp = parser.parseRaw("NE");
assertEquals("abc", exp.getValue(ctx));
assertEquals("abc", exp.getValue(context));
}
@Test
public void reservedWordProperties_SPR9862() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
StandardEvaluationContext context = new StandardEvaluationContext();
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = parser.parseRaw("T(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver).CONST");
Object value = expression.getValue(ctx);
Object value = expression.getValue(context);
assertEquals(value, Reserver.CONST);
}

View File

@@ -38,7 +38,7 @@ import org.springframework.expression.spel.support.ReflectionHelper.ArgumentsMat
import static org.junit.Assert.*;
/**
* Tests for any helper code.
* Tests for reflection helper code.
*
* @author Andy Clement
*/
@@ -46,23 +46,14 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
@Test
public void testFormatHelperForClassName() {
assertEquals("java.lang.String",FormatHelper.formatClassNameForMessage(String.class));
assertEquals("java.lang.String[]",FormatHelper.formatClassNameForMessage(new String[1].getClass()));
assertEquals("java.lang.String[][]",FormatHelper.formatClassNameForMessage(new String[1][1].getClass()));
assertEquals("int[]",FormatHelper.formatClassNameForMessage(new int[1].getClass()));
assertEquals("int[][]",FormatHelper.formatClassNameForMessage(new int[1][2].getClass()));
assertEquals("null",FormatHelper.formatClassNameForMessage(null));
assertEquals("java.lang.String", FormatHelper.formatClassNameForMessage(String.class));
assertEquals("java.lang.String[]", FormatHelper.formatClassNameForMessage(String[].class));
assertEquals("java.lang.String[][]", FormatHelper.formatClassNameForMessage(String[][].class));
assertEquals("int[]", FormatHelper.formatClassNameForMessage(int[].class));
assertEquals("int[][]", FormatHelper.formatClassNameForMessage(int[][].class));
assertEquals("null", FormatHelper.formatClassNameForMessage(null));
}
/*
@Test
public void testFormatHelperForMethod() {
assertEquals("foo(java.lang.String)",FormatHelper.formatMethodForMessage("foo", String.class));
assertEquals("goo(java.lang.String,int[])",FormatHelper.formatMethodForMessage("goo", String.class, new int[1].getClass()));
assertEquals("boo()",FormatHelper.formatMethodForMessage("boo"));
}
*/
@Test
public void testUtilities() throws ParseException {
SpelExpression expr = (SpelExpression)parser.parseExpression("3+4+5+6+7-2");
@@ -159,47 +150,45 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
StandardTypeConverter typeConverter = new StandardTypeConverter();
// Passing (Super,String) on call to foo(Sub,String) is not a match
checkMatch(new Class<?>[] {Super.class,String.class}, new Class<?>[] {Sub.class,String.class},typeConverter,null);
checkMatch(new Class<?>[] {Super.class,String.class}, new Class<?>[] {Sub.class,String.class}, typeConverter, null);
}
@Test
public void testReflectionHelperCompareArguments_Varargs_ExactMatching() {
StandardTypeConverter tc = new StandardTypeConverter();
Class<?> stringArrayClass = new String[0].getClass();
Class<?> integerArrayClass = new Integer[0].getClass();
// Passing (String[]) on call to (String[]) is exact match
checkMatch2(new Class<?>[] {stringArrayClass}, new Class<?>[] {stringArrayClass}, tc, ArgumentsMatchKind.EXACT);
checkMatch2(new Class<?>[] {String[].class}, new Class<?>[] {String[].class}, tc, ArgumentsMatchKind.EXACT);
// Passing (Integer, String[]) on call to (Integer, String[]) is exact match
checkMatch2(new Class<?>[] {Integer.class, stringArrayClass}, new Class<?>[] {Integer.class, stringArrayClass}, tc, ArgumentsMatchKind.EXACT);
checkMatch2(new Class<?>[] {Integer.class, String[].class}, new Class<?>[] {Integer.class, String[].class}, tc, ArgumentsMatchKind.EXACT);
// Passing (String, Integer, String[]) on call to (String, String, String[]) is exact match
checkMatch2(new Class<?>[] {String.class, Integer.class, stringArrayClass}, new Class<?>[] {String.class,Integer.class, stringArrayClass}, tc, ArgumentsMatchKind.EXACT);
checkMatch2(new Class<?>[] {String.class, Integer.class, String[].class}, new Class<?>[] {String.class,Integer.class, String[].class}, tc, ArgumentsMatchKind.EXACT);
// Passing (Sub, String[]) on call to (Super, String[]) is exact match
checkMatch2(new Class<?>[] {Sub.class, stringArrayClass}, new Class<?>[] {Super.class,stringArrayClass}, tc, ArgumentsMatchKind.CLOSE);
checkMatch2(new Class<?>[] {Sub.class, String[].class}, new Class<?>[] {Super.class,String[].class}, tc, ArgumentsMatchKind.CLOSE);
// Passing (Integer, String[]) on call to (String, String[]) is exact match
checkMatch2(new Class<?>[] {Integer.class, stringArrayClass}, new Class<?>[] {String.class, stringArrayClass}, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);
checkMatch2(new Class<?>[] {Integer.class, String[].class}, new Class<?>[] {String.class, String[].class}, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);
// Passing (Integer, Sub, String[]) on call to (String, Super, String[]) is exact match
checkMatch2(new Class<?>[] {Integer.class, Sub.class, String[].class}, new Class<?>[] {String.class,Super .class, String[].class}, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);
// Passing (String) on call to (String[]) is exact match
checkMatch2(new Class<?>[] {String.class}, new Class<?>[] {stringArrayClass}, tc, ArgumentsMatchKind.EXACT);
checkMatch2(new Class<?>[] {String.class}, new Class<?>[] {String[].class}, tc, ArgumentsMatchKind.EXACT);
// Passing (Integer,String) on call to (Integer,String[]) is exact match
checkMatch2(new Class<?>[] {Integer.class, String.class}, new Class<?>[] {Integer.class, stringArrayClass}, tc, ArgumentsMatchKind.EXACT);
checkMatch2(new Class<?>[] {Integer.class, String.class}, new Class<?>[] {Integer.class, String[].class}, tc, ArgumentsMatchKind.EXACT);
// Passing (String) on call to (Integer[]) is conversion match (String to Integer)
checkMatch2(new Class<?>[] {String.class}, new Class<?>[] {integerArrayClass}, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);
checkMatch2(new Class<?>[] {String.class}, new Class<?>[] {Integer[].class}, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);
// Passing (Sub) on call to (Super[]) is close match
checkMatch2(new Class<?>[] {Sub.class}, new Class<?>[] {new Super[0].getClass()}, tc, ArgumentsMatchKind.CLOSE);
checkMatch2(new Class<?>[] {Sub.class}, new Class<?>[] {Super[].class}, tc, ArgumentsMatchKind.CLOSE);
// Passing (Super) on call to (Sub[]) is not a match
checkMatch2(new Class<?>[] {Super.class}, new Class<?>[] {new Sub[0].getClass()}, tc, null);
checkMatch2(new Class<?>[] {Super.class}, new Class<?>[] {Sub[].class}, tc, null);
checkMatch2(new Class<?>[] {Unconvertable.class, String.class}, new Class<?>[] {Sub.class, Super[].class}, tc, null);
@@ -234,12 +223,12 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
// varargs with nothing needing conversion
args = new Object[] {3, "abc", "abc"};
ReflectionHelper.convertArguments(tc, args, twoArg, 1);
checkArguments(args, "3","abc","abc");
checkArguments(args, "3", "abc", "abc");
// varargs with conversion required
args = new Object[] {3, false ,3.0d};
ReflectionHelper.convertArguments(tc, args, twoArg, 1);
checkArguments(args, "3","false","3.0");
checkArguments(args, "3", "false", "3.0");
}
@Test
@@ -251,33 +240,33 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
// Simple conversion: int to string
Object[] args = new Object[] {3};
ReflectionHelper.convertAllArguments(tc, args, oneArg);
checkArguments(args,"3");
checkArguments(args, "3");
// varargs conversion
args = new Object[] {3, false, 3.0f};
ReflectionHelper.convertAllArguments(tc, args, twoArg);
checkArguments(args,"3","false","3.0");
checkArguments(args, "3", "false", "3.0");
// varargs conversion but no varargs
args = new Object[] {3};
ReflectionHelper.convertAllArguments(tc, args, twoArg);
checkArguments(args,"3");
checkArguments(args, "3");
// null value
args = new Object[] {3, null, 3.0f};
ReflectionHelper.convertAllArguments(tc, args, twoArg);
checkArguments(args,"3",null,"3.0");
checkArguments(args, "3", null, "3.0");
}
@Test
public void testSetupArguments() {
Object[] newArray = ReflectionHelper.setupArgumentsForVarargsInvocation(
new Class<?>[] {new String[0].getClass()},"a","b","c");
new Class<?>[] {String[].class}, "a", "b", "c");
assertEquals(1, newArray.length);
Object firstParam = newArray[0];
assertEquals(String.class,firstParam.getClass().getComponentType());
Object[] firstParamArray = (Object[])firstParam;
Object[] firstParamArray = (Object[]) firstParam;
assertEquals(3,firstParamArray.length);
assertEquals("a",firstParamArray[0]);
assertEquals("b",firstParamArray[1]);
@@ -285,75 +274,75 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
}
@Test
public void testReflectivePropertyResolver() throws Exception {
ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
public void testReflectivePropertyAccessor() throws Exception {
ReflectivePropertyAccessor rpa = new ReflectivePropertyAccessor();
Tester t = new Tester();
t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t);
assertTrue(rpr.canRead(ctx, t, "property"));
assertEquals("hello",rpr.read(ctx, t, "property").getValue());
assertEquals("hello",rpr.read(ctx, t, "property").getValue()); // cached accessor used
assertTrue(rpa.canRead(ctx, t, "property"));
assertEquals("hello",rpa.read(ctx, t, "property").getValue());
assertEquals("hello",rpa.read(ctx, t, "property").getValue()); // cached accessor used
assertTrue(rpr.canRead(ctx, t, "field"));
assertEquals(3,rpr.read(ctx, t, "field").getValue());
assertEquals(3,rpr.read(ctx, t, "field").getValue()); // cached accessor used
assertTrue(rpa.canRead(ctx, t, "field"));
assertEquals(3,rpa.read(ctx, t, "field").getValue());
assertEquals(3,rpa.read(ctx, t, "field").getValue()); // cached accessor used
assertTrue(rpr.canWrite(ctx, t, "property"));
rpr.write(ctx, t, "property","goodbye");
rpr.write(ctx, t, "property","goodbye"); // cached accessor used
assertTrue(rpa.canWrite(ctx, t, "property"));
rpa.write(ctx, t, "property", "goodbye");
rpa.write(ctx, t, "property", "goodbye"); // cached accessor used
assertTrue(rpr.canWrite(ctx, t, "field"));
rpr.write(ctx, t, "field",12);
rpr.write(ctx, t, "field",12);
assertTrue(rpa.canWrite(ctx, t, "field"));
rpa.write(ctx, t, "field", 12);
rpa.write(ctx, t, "field", 12);
// Attempted write as first activity on this field and property to drive testing
// of populating type descriptor cache
rpr.write(ctx,t,"field2",3);
rpr.write(ctx, t, "property2","doodoo");
assertEquals(3,rpr.read(ctx,t,"field2").getValue());
rpa.write(ctx, t, "field2", 3);
rpa.write(ctx, t, "property2", "doodoo");
assertEquals(3,rpa.read(ctx, t, "field2").getValue());
// Attempted read as first activity on this field and property (no canRead before them)
assertEquals(0,rpr.read(ctx,t,"field3").getValue());
assertEquals("doodoo",rpr.read(ctx,t,"property3").getValue());
assertEquals(0,rpa.read(ctx, t, "field3").getValue());
assertEquals("doodoo",rpa.read(ctx, t, "property3").getValue());
// Access through is method
// assertEquals(0,rpr.read(ctx,t,"field3").getValue());
assertEquals(false,rpr.read(ctx,t,"property4").getValue());
assertTrue(rpr.canRead(ctx,t,"property4"));
assertEquals(0,rpa .read(ctx, t, "field3").getValue());
assertEquals(false,rpa.read(ctx, t, "property4").getValue());
assertTrue(rpa.canRead(ctx, t, "property4"));
// repro SPR-9123, ReflectivePropertyAccessor JavaBean property names compliance tests
assertEquals("iD",rpr.read(ctx,t,"iD").getValue());
assertTrue(rpr.canRead(ctx,t,"iD"));
assertEquals("id",rpr.read(ctx,t,"id").getValue());
assertTrue(rpr.canRead(ctx,t,"id"));
assertEquals("ID",rpr.read(ctx,t,"ID").getValue());
assertTrue(rpr.canRead(ctx,t,"ID"));
assertEquals("iD",rpa.read(ctx, t, "iD").getValue());
assertTrue(rpa.canRead(ctx, t, "iD"));
assertEquals("id",rpa.read(ctx, t, "id").getValue());
assertTrue(rpa.canRead(ctx, t, "id"));
assertEquals("ID",rpa.read(ctx, t, "ID").getValue());
assertTrue(rpa.canRead(ctx, t, "ID"));
// note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
assertEquals("id",rpr.read(ctx,t,"Id").getValue());
assertTrue(rpr.canRead(ctx,t,"Id"));
assertEquals("id",rpa.read(ctx, t, "Id").getValue());
assertTrue(rpa.canRead(ctx, t, "Id"));
// repro SPR-10994
assertEquals("xyZ",rpr.read(ctx,t,"xyZ").getValue());
assertTrue(rpr.canRead(ctx,t,"xyZ"));
assertEquals("xY",rpr.read(ctx,t,"xY").getValue());
assertTrue(rpr.canRead(ctx,t,"xY"));
assertEquals("xyZ",rpa.read(ctx, t, "xyZ").getValue());
assertTrue(rpa.canRead(ctx, t, "xyZ"));
assertEquals("xY",rpa.read(ctx, t, "xY").getValue());
assertTrue(rpa.canRead(ctx, t, "xY"));
// SPR-10122, ReflectivePropertyAccessor JavaBean property names compliance tests - setters
rpr.write(ctx, t, "pEBS","Test String");
assertEquals("Test String",rpr.read(ctx,t,"pEBS").getValue());
rpa.write(ctx, t, "pEBS", "Test String");
assertEquals("Test String",rpa.read(ctx, t, "pEBS").getValue());
}
@Test
public void testOptimalReflectivePropertyResolver() throws Exception {
ReflectivePropertyAccessor rpr = new ReflectivePropertyAccessor();
public void testOptimalReflectivePropertyAccessor() throws Exception {
ReflectivePropertyAccessor rpa = new ReflectivePropertyAccessor();
Tester t = new Tester();
t.setProperty("hello");
EvaluationContext ctx = new StandardEvaluationContext(t);
// assertTrue(rpr.canRead(ctx, t, "property"));
// assertEquals("hello",rpr.read(ctx, t, "property").getValue());
// assertEquals("hello",rpr.read(ctx, t, "property").getValue()); // cached accessor used
assertTrue(rpa.canRead(ctx, t, "property"));
assertEquals("hello", rpa.read(ctx, t, "property").getValue());
assertEquals("hello", rpa.read(ctx, t, "property").getValue()); // cached accessor used
PropertyAccessor optA = rpr.createOptimalAccessor(ctx, t, "property");
PropertyAccessor optA = rpa.createOptimalAccessor(ctx, t, "property");
assertTrue(optA.canRead(ctx, t, "property"));
assertFalse(optA.canRead(ctx, t, "property2"));
try {
@@ -381,14 +370,14 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
// success
}
try {
optA.write(ctx,t,"property",null);
optA.write(ctx, t, "property", null);
fail();
}
catch (UnsupportedOperationException uoe) {
// success
}
optA = rpr.createOptimalAccessor(ctx, t, "field");
optA = rpa.createOptimalAccessor(ctx, t, "field");
assertTrue(optA.canRead(ctx, t, "field"));
assertFalse(optA.canRead(ctx, t, "field2"));
try {
@@ -406,7 +395,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
// success
}
assertEquals(3,optA.read(ctx, t, "field").getValue());
assertEquals(3,optA.read(ctx, t, "field").getValue()); // cached accessor used
assertEquals(3,optA.read(ctx, t, "field").getValue()); // cached accessor used
try {
optA.getSpecificTargetClasses();
@@ -416,7 +405,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
// success
}
try {
optA.write(ctx,t,"field",null);
optA.write(ctx, t, "field", null);
fail();
}
catch (UnsupportedOperationException uoe) {