@ -52,22 +52,22 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -52,22 +52,22 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class PropertyAccessTests extends AbstractExpressionTests {
@Test
public void te stS impleAccess01( ) {
void simpleAccess01 ( ) {
evaluate ( "name" , "Nikola Tesla" , String . class ) ;
}
@Test
public void te stS impleAccess02( ) {
void simpleAccess02 ( ) {
evaluate ( "placeOfBirth.city" , "SmilJan" , String . class ) ;
}
@Test
public void te stS impleAccess03( ) {
void simpleAccess03 ( ) {
evaluate ( "stringArrayOfThreeItems.length" , "3" , Integer . class ) ;
}
@Test
public void testN onExistentPropertiesAndMethods( ) {
void n onExistentPropertiesAndMethods( ) {
// madeup does not exist as a property
evaluateAndCheckError ( "madeup" , SpelMessage . PROPERTY_OR_FIELD_NOT_READABLE , 0 ) ;
@ -80,21 +80,21 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -80,21 +80,21 @@ public class PropertyAccessTests extends AbstractExpressionTests {
* supplied resolver might be able to - so null shouldn ' t crash the reflection resolver .
* /
@Test
public void testA ccessingOnNullObject( ) {
SpelExpression expr = ( SpelExpression ) parser . parseExpression ( "madeup" ) ;
void a ccessingOnNullObject( ) {
SpelExpression expr = ( SpelExpression ) parser . parseExpression ( "madeup" ) ;
EvaluationContext context = new StandardEvaluationContext ( null ) ;
assertThatExceptionOfType ( SpelEvaluationException . class ) . isThrownBy ( ( ) - >
expr . getValue ( context ) )
. satisfies ( ex - > assertThat ( ex . getMessageCode ( ) ) . isEqualTo ( SpelMessage . PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL ) ) ;
assertThatExceptionOfType ( SpelEvaluationException . class )
. isThrownBy ( ( ) - > expr . getValue ( context ) )
. extracting ( SpelEvaluationException : : getMessageCode ) . isEqualTo ( SpelMessage . PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL ) ;
assertThat ( expr . isWritable ( context ) ) . isFalse ( ) ;
assertThatExceptionOfType ( SpelEvaluationException . class ) . isThrownBy ( ( ) - >
expr . setValue ( context , "abc" ) )
. satisfies ( ex - > assertThat ( ex . getMessageCode ( ) ) . isEqualTo ( SpelMessage . PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL ) ) ;
assertThatExceptionOfType ( SpelEvaluationException . class )
. isThrownBy ( ( ) - > expr . setValue ( context , "abc" ) )
. extracting ( SpelEvaluationException : : getMessageCode ) . isEqualTo ( SpelMessage . PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL ) ;
}
@Test
// Adding a new property accessor just for a particular type
public void testA ddingSpecificPropertyAccessor( ) {
void a ddingSpecificPropertyAccessor( ) {
SpelExpressionParser parser = new SpelExpressionParser ( ) ;
StandardEvaluationContext ctx = new StandardEvaluationContext ( ) ;
@ -125,35 +125,34 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -125,35 +125,34 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void testAdding RemovingAccessors( ) {
void addingAnd RemovingAccessors( ) {
StandardEvaluationContext ctx = new StandardEvaluationContext ( ) ;
// reflective property accessor is the only one by default
List < PropertyAccessor > propertyAccessors = ctx . getPropertyAccessors ( ) ;
assertThat ( propertyAccessors . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( ctx . getPropertyAccessors ( ) ) . hasSize ( 1 ) ;
StringyPropertyAccessor spa = new StringyPropertyAccessor ( ) ;
ctx . addPropertyAccessor ( spa ) ;
assertThat ( ctx . getPropertyAccessors ( ) . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( ctx . getPropertyAccessors ( ) ) . hasSize ( 2 ) ;
List < PropertyAccessor > copy = new ArrayList < > ( ctx . getPropertyAccessors ( ) ) ;
assertThat ( ctx . removePropertyAccessor ( spa ) ) . isTrue ( ) ;
assertThat ( ctx . removePropertyAccessor ( spa ) ) . isFalse ( ) ;
assertThat ( ctx . getPropertyAccessors ( ) . size ( ) ) . isEqualTo ( 1 ) ;
assertThat ( ctx . getPropertyAccessors ( ) ) . hasSize ( 1 ) ;
ctx . setPropertyAccessors ( copy ) ;
assertThat ( ctx . getPropertyAccessors ( ) . size ( ) ) . isEqualTo ( 2 ) ;
assertThat ( ctx . getPropertyAccessors ( ) ) . hasSize ( 2 ) ;
}
@Test
public void testA ccessingPropertyOfClass( ) {
void a ccessingPropertyOfClass( ) {
Expression expression = parser . parseExpression ( "name" ) ;
Object value = expression . getValue ( new StandardEvaluationContext ( String . class ) ) ;
assertThat ( value ) . isEqualTo ( "java.lang.String" ) ;
}
@Test
public void shouldAlwaysUsePropertyAccessorFromEvaluationContext ( ) {
void shouldAlwaysUsePropertyAccessorFromEvaluationContext ( ) {
SpelExpressionParser parser = new SpelExpressionParser ( ) ;
Expression expression = parser . parseExpression ( "name" ) ;
@ -167,19 +166,19 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -167,19 +166,19 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void standardGetClassAccess ( ) {
void standardGetClassAccess ( ) {
assertThat ( parser . parseExpression ( "'a'.class.name" ) . getValue ( ) ) . isEqualTo ( String . class . getName ( ) ) ;
}
@Test
public void noGetClassAccess ( ) {
void noGetClassAccess ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) . build ( ) ;
assertThatExceptionOfType ( SpelEvaluationException . class ) . isThrownBy ( ( ) - >
parser . parseExpression ( "'a'.class.name" ) . getValue ( context ) ) ;
}
@Test
public void propertyReadOnly ( ) {
void propertyReadOnly ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) . build ( ) ;
Expression expr = parser . parseExpression ( "name" ) ;
@ -193,7 +192,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -193,7 +192,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void propertyReadOnlyWithRecordStyle ( ) {
void propertyReadOnlyWithRecordStyle ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) . build ( ) ;
Expression expr = parser . parseExpression ( "name" ) ;
@ -207,7 +206,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -207,7 +206,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void propertyReadWrite ( ) {
void propertyReadWrite ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadWriteDataBinding ( ) . build ( ) ;
Expression expr = parser . parseExpression ( "name" ) ;
@ -226,27 +225,27 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -226,27 +225,27 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void propertyReadWriteWithRootObject ( ) {
Person targe t = new Person ( "p1" ) ;
EvaluationContext context = SimpleEvaluationContext . forReadWriteDataBinding ( ) . withRootObject ( targe t) . build ( ) ;
assertThat ( context . getRootObject ( ) . getValue ( ) ) . isSameAs ( targe t) ;
void propertyReadWriteWithRootObject ( ) {
Person rootObjec t = new Person ( "p1" ) ;
EvaluationContext context = SimpleEvaluationContext . forReadWriteDataBinding ( ) . withRootObject ( rootObjec t) . build ( ) ;
assertThat ( context . getRootObject ( ) . getValue ( ) ) . isSameAs ( rootObjec t) ;
Expression expr = parser . parseExpression ( "name" ) ;
assertThat ( expr . getValue ( context ) ) . isEqualTo ( "p1" ) ;
targe t. setName ( "p2" ) ;
rootObjec t. setName ( "p2" ) ;
assertThat ( expr . getValue ( context ) ) . isEqualTo ( "p2" ) ;
parser . parseExpression ( "name='p3'" ) . getValue ( context , targe t) ;
assertThat ( targe t. getName ( ) ) . isEqualTo ( "p3" ) ;
parser . parseExpression ( "name='p3'" ) . getValue ( context , rootObjec t) ;
assertThat ( rootObjec t. getName ( ) ) . isEqualTo ( "p3" ) ;
assertThat ( expr . getValue ( context ) ) . isEqualTo ( "p3" ) ;
expr . setValue ( context , "p4" ) ;
assertThat ( targe t. getName ( ) ) . isEqualTo ( "p4" ) ;
assertThat ( rootObjec t. getName ( ) ) . isEqualTo ( "p4" ) ;
assertThat ( expr . getValue ( context ) ) . isEqualTo ( "p4" ) ;
}
@Test
public void propertyAccessWithoutMethodResolver ( ) {
void propertyAccessWithoutMethodResolver ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) . build ( ) ;
Person target = new Person ( "p1" ) ;
assertThatExceptionOfType ( SpelEvaluationException . class ) . isThrownBy ( ( ) - >
@ -254,14 +253,14 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -254,14 +253,14 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
@Test
public void propertyAccessWithInstanceMethodResolver ( ) {
void propertyAccessWithInstanceMethodResolver ( ) {
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) . withInstanceMethods ( ) . build ( ) ;
Person target = new Person ( "p1" ) ;
assertThat ( parser . parseExpression ( "name.substring(1)" ) . getValue ( context , target ) ) . isEqualTo ( "1" ) ;
}
@Test
public void propertyAccessWithInstanceMethodResolverAndTypedRootObject ( ) {
void propertyAccessWithInstanceMethodResolverAndTypedRootObject ( ) {
Person rootObject = new Person ( "p1" ) ;
EvaluationContext context = SimpleEvaluationContext . forReadOnlyDataBinding ( ) .
withInstanceMethods ( ) . withTypedRootObject ( rootObject , TypeDescriptor . valueOf ( Object . class ) ) . build ( ) ;
@ -277,7 +276,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -277,7 +276,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
Expression expression = parser . parseExpression ( "stringArrayOfThreeItems[3]" ) ;
assertThatExceptionOfType ( SpelEvaluationException . class )
. isThrownBy ( ( ) - > expression . getValue ( context , new Inventor ( ) ) )
. satisfies ( ex - > assertThat ( ex . getMessageCode ( ) ) . isEqualTo ( SpelMessage . ARRAY_INDEX_OUT_OF_BOUNDS ) ) ;
. extracting ( SpelEvaluationException : : getMessageCode ) . isEqualTo ( SpelMessage . ARRAY_INDEX_OUT_OF_BOUNDS ) ;
}
@ -335,7 +334,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
@@ -335,7 +334,7 @@ public class PropertyAccessTests extends AbstractExpressionTests {
private final Map < String , Object > values ;
public ConfigurablePropertyAccessor ( Map < String , Object > values ) {
ConfigurablePropertyAccessor ( Map < String , Object > values ) {
this . values = values ;
}