Search in sources :

Example 1 with PersonInOtherPackage

use of org.springframework.expression.spel.testdata.PersonInOtherPackage in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method methodReferenceMissingCastAndRootObjectAccessing_SPR12326.

@Test
public void methodReferenceMissingCastAndRootObjectAccessing_SPR12326() {
    // Need boxing code on the 1 so that toString() can be called
    expression = parser.parseExpression("1.toString()");
    assertThat(expression.getValue()).isEqualTo("1");
    assertCanCompile(expression);
    assertThat(expression.getValue()).isEqualTo("1");
    expression = parser.parseExpression("#it?.age.equals([0])");
    Person person = new Person(1);
    StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { person.getAge() });
    context.setVariable("it", person);
    assertThat(expression.getValue(context, Boolean.class)).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(context, Boolean.class)).isTrue();
    // Variant of above more like what was in the bug report:
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader()));
    SpelExpression ex = parser.parseRaw("#it?.age.equals([0])");
    context = new StandardEvaluationContext(new Object[] { person.getAge() });
    context.setVariable("it", person);
    assertThat(ex.getValue(context, Boolean.class)).isTrue();
    assertThat(ex.getValue(context, Boolean.class)).isTrue();
    PersonInOtherPackage person2 = new PersonInOtherPackage(1);
    ex = parser.parseRaw("#it?.age.equals([0])");
    context = new StandardEvaluationContext(new Object[] { person2.getAge() });
    context.setVariable("it", person2);
    assertThat(ex.getValue(context, Boolean.class)).isTrue();
    assertThat(ex.getValue(context, Boolean.class)).isTrue();
    ex = parser.parseRaw("#it?.age.equals([0])");
    context = new StandardEvaluationContext(new Object[] { person2.getAge() });
    context.setVariable("it", person2);
    assertThat((boolean) (Boolean) ex.getValue(context)).isTrue();
    assertThat((boolean) (Boolean) ex.getValue(context)).isTrue();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) PersonInOtherPackage(org.springframework.expression.spel.testdata.PersonInOtherPackage) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 PersonInOtherPackage (org.springframework.expression.spel.testdata.PersonInOtherPackage)1