Search in sources :

Example 16 with PropertyAccessor

use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.

the class PropertyAccessTests method addingAndRemovingAccessors.

@Test
void addingAndRemovingAccessors() {
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // reflective property accessor is the only one by default
    assertThat(ctx.getPropertyAccessors()).hasSize(1);
    StringyPropertyAccessor spa = new StringyPropertyAccessor();
    ctx.addPropertyAccessor(spa);
    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()).hasSize(1);
    ctx.setPropertyAccessors(copy);
    assertThat(ctx.getPropertyAccessors()).hasSize(2);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) PropertyAccessor(org.springframework.expression.PropertyAccessor) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 17 with PropertyAccessor

use of org.springframework.expression.PropertyAccessor in project spring-framework by spring-projects.

the class SpelReproTests method propertyAccessOnNullTarget_SPR5663.

@Test
void propertyAccessOnNullTarget_SPR5663() throws AccessException {
    PropertyAccessor accessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertThat(accessor.canRead(context, null, "abc")).isFalse();
    assertThat(accessor.canWrite(context, null, "abc")).isFalse();
    assertThatIllegalStateException().isThrownBy(() -> accessor.read(context, null, "abc"));
    assertThatIllegalStateException().isThrownBy(() -> accessor.write(context, null, "abc", "foo"));
}
Also used : ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.jupiter.api.Test)

Aggregations

PropertyAccessor (org.springframework.expression.PropertyAccessor)17 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)7 Test (org.junit.Test)5 CompilablePropertyAccessor (org.springframework.expression.spel.CompilablePropertyAccessor)5 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)5 ArrayList (java.util.ArrayList)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 Method (java.lang.reflect.Method)3 Test (org.junit.jupiter.api.Test)3 AccessException (org.springframework.expression.AccessException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MapAccessor (org.springframework.context.expression.MapAccessor)2 EvaluationException (org.springframework.expression.EvaluationException)2 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)2 List (java.util.List)1 Map (java.util.Map)1 BeansException (org.springframework.beans.BeansException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ApplicationContext (org.springframework.context.ApplicationContext)1