Search in sources :

Example 51 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method failsWhenSettingContextForExpression_SPR12326.

@Test
public void failsWhenSettingContextForExpression_SPR12326() {
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader()));
    Person3 person = new Person3("foo", 1);
    SpelExpression expression = parser.parseRaw("#it?.age?.equals([0])");
    StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
    context.setVariable("it", person);
    expression.setEvaluationContext(context);
    assertThat(expression.getValue(Boolean.class)).isTrue();
    // This will trigger compilation (second usage)
    assertThat(expression.getValue(Boolean.class)).isTrue();
    context.setVariable("it", null);
    assertThat(expression.getValue(Boolean.class)).isNull();
    assertCanCompile(expression);
    context.setVariable("it", person);
    assertThat(expression.getValue(Boolean.class)).isTrue();
    context.setVariable("it", null);
    assertThat(expression.getValue(Boolean.class)).isNull();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 52 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method getAst.

// Helper methods
private SpelNodeImpl getAst() {
    SpelExpression spelExpression = (SpelExpression) expression;
    SpelNode ast = spelExpression.getAST();
    return (SpelNodeImpl) ast;
}
Also used : SpelNodeImpl(org.springframework.expression.spel.ast.SpelNodeImpl) SpelExpression(org.springframework.expression.spel.standard.SpelExpression)

Example 53 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method compilationOfBasicNullSafeMethodReference.

@Test
public void compilationOfBasicNullSafeMethodReference() {
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader()));
    SpelExpression expression = parser.parseRaw("#it?.equals(3)");
    StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
    context.setVariable("it", 3);
    expression.setEvaluationContext(context);
    assertThat(expression.getValue(Boolean.class)).isTrue();
    context.setVariable("it", null);
    assertThat(expression.getValue(Boolean.class)).isNull();
    assertCanCompile(expression);
    context.setVariable("it", 3);
    assertThat(expression.getValue(Boolean.class)).isTrue();
    context.setVariable("it", null);
    assertThat(expression.getValue(Boolean.class)).isNull();
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 54 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method verifyCompilationAndBehaviourWithNull2.

private void verifyCompilationAndBehaviourWithNull2(String expressionText, SpelExpressionParser parser, StandardEvaluationContext ctx) {
    SpelExpression fast = (SpelExpression) parser.parseExpression(expressionText);
    SpelExpression slow = (SpelExpression) parser.parseExpression(expressionText);
    fast.getValue(ctx);
    assertThat(fast.compileExpression()).isTrue();
    Reg r = (Reg) ctx.getRootObject().getValue();
    // try the numbers 0,1,2,null
    for (int i = 0; i < 4; i++) {
        r.setValue(i < 3 ? i : null);
        boolean slowResult = (Boolean) slow.getValue(ctx);
        boolean fastResult = (Boolean) fast.getValue(ctx);
        assertThat(fastResult).as("Differing results: expression=" + expressionText + " value=" + r.getValue() + " slow=" + slowResult + " fast=" + fastResult).isEqualTo(slowResult);
    }
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression)

Example 55 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.

the class SpelCompilationCoverageTests method verifyCompilationAndBehaviourWithNull.

private void verifyCompilationAndBehaviourWithNull(String expressionText, SpelExpressionParser parser, StandardEvaluationContext ctx) {
    Reg r = (Reg) ctx.getRootObject().getValue();
    // having a value in value2 fields will enable compilation to succeed, then can switch it to null
    r.setValue2(1);
    SpelExpression fast = (SpelExpression) parser.parseExpression(expressionText);
    SpelExpression slow = (SpelExpression) parser.parseExpression(expressionText);
    fast.getValue(ctx);
    assertThat(fast.compileExpression()).isTrue();
    r.setValue2(null);
    // try the numbers 0,1,2,null
    for (int i = 0; i < 4; i++) {
        r.setValue(i < 3 ? i : null);
        boolean slowResult = (Boolean) slow.getValue(ctx);
        boolean fastResult = (Boolean) fast.getValue(ctx);
        assertThat(fastResult).as("Differing results: expression=" + expressionText + " value=" + r.getValue() + " slow=" + slowResult + " fast=" + fastResult).isEqualTo(slowResult);
    }
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression)

Aggregations

SpelExpression (org.springframework.expression.spel.standard.SpelExpression)56 Test (org.junit.jupiter.api.Test)30 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)27 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)18 Test (org.junit.Test)15 Expression (org.springframework.expression.Expression)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)3 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)3 HttpRequestExecutingMessageHandler (org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2