@ -20,7 +20,6 @@ import org.springframework.expression.EvaluationContext;
@@ -20,7 +20,6 @@ import org.springframework.expression.EvaluationContext;
import org.springframework.expression.EvaluationException ;
import org.springframework.expression.Expression ;
import org.springframework.expression.PropertyAccessor ;
import org.springframework.expression.spel.reflection.ReflectionPropertyResolver ;
import org.springframework.expression.spel.standard.StandardEvaluationContext ;
/ * *
@ -38,24 +37,6 @@ public class PropertyAccessTests extends ExpressionTestCase {
@@ -38,24 +37,6 @@ public class PropertyAccessTests extends ExpressionTestCase {
evaluate ( "placeOfBirth.city" , "SmilJan" , String . class ) ;
}
public void testSimpleAccess03 ( ) {
try {
ReflectionPropertyResolver . useResolverExecutorModel = false ;
evaluate ( "name" , "Nikola Tesla" , String . class ) ;
} finally {
ReflectionPropertyResolver . useResolverExecutorModel = true ;
}
}
public void testSimpleAccess04 ( ) {
try {
ReflectionPropertyResolver . useResolverExecutorModel = false ;
evaluate ( "placeOfBirth.city" , "SmilJan" , String . class ) ;
} finally {
ReflectionPropertyResolver . useResolverExecutorModel = true ;
}
}
public void testNonExistentPropertiesAndMethods ( ) {
// madeup does not exist as a property
evaluateAndCheckError ( "madeup" , SpelMessages . PROPERTY_OR_FIELD_NOT_FOUND , 0 ) ;
@ -68,39 +49,42 @@ public class PropertyAccessTests extends ExpressionTestCase {
@@ -68,39 +49,42 @@ public class PropertyAccessTests extends ExpressionTestCase {
static class StringyPropertyAccessor implements PropertyAccessor {
int flibbles = 7 ;
public Class < ? > [ ] getSpecificTargetClasses ( ) {
return new Class [ ] { String . class } ;
return new Class [ ] { String . class } ;
}
public boolean canRead ( EvaluationContext context , Object target , Object name ) throws AccessException {
if ( ! ( target instanceof String ) ) throw new RuntimeException ( "Assertion Failed! target should be String" ) ;
if ( ! ( target instanceof String ) )
throw new RuntimeException ( "Assertion Failed! target should be String" ) ;
return ( name . equals ( "flibbles" ) ) ;
}
public boolean canWrite ( EvaluationContext context , Object target , Object name ) throws AccessException {
if ( ! ( target instanceof String ) ) throw new RuntimeException ( "Assertion Failed! target should be String" ) ;
if ( ! ( target instanceof String ) )
throw new RuntimeException ( "Assertion Failed! target should be String" ) ;
return ( name . equals ( "flibbles" ) ) ;
}
public Object read ( EvaluationContext context , Object target , Object name ) throws AccessException {
if ( ! name . equals ( "flibbles" ) ) throw new RuntimeException ( "Assertion Failed! name should be flibbles" ) ;
if ( ! name . equals ( "flibbles" ) )
throw new RuntimeException ( "Assertion Failed! name should be flibbles" ) ;
return flibbles ;
}
public void write ( EvaluationContext context , Object target , Object name , Object newValue )
throws AccessException {
if ( ! name . equals ( "flibbles" ) ) throw new RuntimeException ( "Assertion Failed! name should be flibbles" ) ;
if ( ! name . equals ( "flibbles" ) )
throw new RuntimeException ( "Assertion Failed! name should be flibbles" ) ;
try {
flibbles = ( Integer ) context . getTypeUtils ( ) . getTypeConverter ( ) . convertValue ( newValue , Integer . class ) ;
flibbles = ( Integer ) context . getTypeUtils ( ) . getTypeConverter ( ) . convertValue ( newValue , Integer . class ) ;
} catch ( EvaluationException e ) {
throw new AccessException ( "Cannot set flibbles to an object of type '" + newValue . getClass ( ) + "'" ) ;
throw new AccessException ( "Cannot set flibbles to an object of type '" + newValue . getClass ( ) + "'" ) ;
}
}
}
// Adding a new property accessor just for a particular type
public void testAddingSpecificPropertyAccessor ( ) throws Exception {
SpelExpressionParser parser = new SpelExpressionParser ( ) ;
@ -111,26 +95,26 @@ public class PropertyAccessTests extends ExpressionTestCase {
@@ -111,26 +95,26 @@ public class PropertyAccessTests extends ExpressionTestCase {
// any 'default' ones
ctx . addPropertyAccessor ( new StringyPropertyAccessor ( ) ) ;
Expression expr = parser . parseExpression ( "new String('hello').flibbles" ) ;
Integer i = ( Integer ) expr . getValue ( ctx , Integer . class ) ;
assertEquals ( ( int ) i , 7 ) ;
Integer i = ( Integer ) expr . getValue ( ctx , Integer . class ) ;
assertEquals ( ( int ) i , 7 ) ;
// The reflection one will be used for other properties...
expr = parser . parseExpression ( "new String('hello').CASE_INSENSITIVE_ORDER" ) ;
Object o = expr . getValue ( ctx ) ;
assertNotNull ( o ) ;
expr = parser . parseExpression ( "new String('hello').flibbles" ) ;
expr . setValue ( ctx , 99 ) ;
i = ( Integer ) expr . getValue ( ctx , Integer . class ) ;
assertEquals ( ( int ) i , 99 ) ;
expr . setValue ( ctx , 99 ) ;
i = ( Integer ) expr . getValue ( ctx , Integer . class ) ;
assertEquals ( ( int ) i , 99 ) ;
// Cannot set it to a string value
try {
expr . setValue ( ctx , "not allowed" ) ;
expr . setValue ( ctx , "not allowed" ) ;
fail ( "Should not have been allowed" ) ;
} catch ( EvaluationException e ) {
// success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
// success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
System . out . println ( e . getMessage ( ) ) ;
}
}