Search in sources :

Example 16 with SpelExpression

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

the class SpelCompilationCoverageTests method nullsafeFieldPropertyDereferencing_SPR16489.

@Test
public void nullsafeFieldPropertyDereferencing_SPR16489() throws Exception {
    FooObjectHolder foh = new FooObjectHolder();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(foh);
    // First non compiled:
    SpelExpression expression = (SpelExpression) parser.parseExpression("foo?.object");
    assertThat(expression.getValue(context)).isEqualTo("hello");
    foh.foo = null;
    assertThat(expression.getValue(context)).isNull();
    // Now revert state of foh and try compiling it:
    foh.foo = new FooObject();
    assertThat(expression.getValue(context)).isEqualTo("hello");
    assertCanCompile(expression);
    assertThat(expression.getValue(context)).isEqualTo("hello");
    foh.foo = null;
    assertThat(expression.getValue(context)).isNull();
    // Static references
    expression = (SpelExpression) parser.parseExpression("#var?.propertya");
    context.setVariable("var", StaticsHelper.class);
    assertThat(expression.getValue(context).toString()).isEqualTo("sh");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", StaticsHelper.class);
    assertThat(expression.getValue(context).toString()).isEqualTo("sh");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Single size primitive (boolean)
    expression = (SpelExpression) parser.parseExpression("#var?.a");
    context.setVariable("var", new TestClass4());
    assertThat((boolean) (Boolean) expression.getValue(context)).isFalse();
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", new TestClass4());
    assertThat((boolean) (Boolean) expression.getValue(context)).isFalse();
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    // Double slot primitives
    expression = (SpelExpression) parser.parseExpression("#var?.four");
    context.setVariable("var", new Three());
    assertThat(expression.getValue(context).toString()).isEqualTo("0.04");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
    assertCanCompile(expression);
    context.setVariable("var", new Three());
    assertThat(expression.getValue(context).toString()).isEqualTo("0.04");
    context.setVariable("var", null);
    assertThat(expression.getValue(context)).isNull();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 17 with SpelExpression

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

the class SpelReproTests method reservedWords_SPR8228.

@Test
void reservedWords_SPR8228() {
    // "DIV","EQ","GE","GT","LE","LT","MOD","NE","NOT"
    @SuppressWarnings("unused")
    class Reserver {

        public Reserver getReserver() {
            return this;
        }

        public String NE = "abc";

        public String ne = "def";

        public int DIV = 1;

        public int div = 3;

        public Map<String, String> m = new HashMap<>();

        Reserver() {
            m.put("NE", "xyz");
        }
    }
    StandardEvaluationContext context = new StandardEvaluationContext(new Reserver());
    SpelExpressionParser parser = new SpelExpressionParser();
    String ex = "getReserver().NE";
    SpelExpression exp = parser.parseRaw(ex);
    String value = (String) exp.getValue(context);
    assertThat(value).isEqualTo("abc");
    ex = "getReserver().ne";
    exp = parser.parseRaw(ex);
    value = (String) exp.getValue(context);
    assertThat(value).isEqualTo("def");
    ex = "getReserver().m[NE]";
    exp = parser.parseRaw(ex);
    value = (String) exp.getValue(context);
    assertThat(value).isEqualTo("xyz");
    ex = "getReserver().DIV";
    exp = parser.parseRaw(ex);
    assertThat(exp.getValue(context)).isEqualTo(1);
    ex = "getReserver().div";
    exp = parser.parseRaw(ex);
    assertThat(exp.getValue(context)).isEqualTo(3);
    exp = parser.parseRaw("NE");
    assertThat(exp.getValue(context)).isEqualTo("abc");
}
Also used : Reserver(org.springframework.expression.spel.testresources.le.div.mod.reserved.Reserver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 18 with SpelExpression

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

the class SpelReproTests method array.

@Test
void array() {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = null;
    Object result = null;
    expression = parser.parseExpression("new java.lang.Long[0].class");
    result = expression.getValue(context, "");
    assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
    expression = parser.parseExpression("T(java.lang.Long[])");
    result = expression.getValue(context, "");
    assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [Ljava.lang.Long;");
    expression = parser.parseExpression("T(java.lang.String[][][])");
    result = expression.getValue(context, "");
    assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [[[Ljava.lang.String;");
    assertThat(((SpelExpression) expression).toStringAST()).isEqualTo("T(java.lang.String[][][])");
    expression = parser.parseExpression("new int[0].class");
    result = expression.getValue(context, "");
    assertThat(result.toString()).as("Equal assertion failed: ").isEqualTo("class [I");
    expression = parser.parseExpression("T(int[][])");
    result = expression.getValue(context, "");
    assertThat(result.toString()).isEqualTo("class [[I");
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 19 with SpelExpression

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

the class SpelReproTests method projectionTypeDescriptors_3.

@Test
void projectionTypeDescriptors_3() {
    StandardEvaluationContext context = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "ms.![key.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    List<?> value = (List<?>) exp.getValue(context);
    // value is list containing [true,false]
    assertThat(value.get(0).getClass()).isEqualTo(Boolean.class);
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
    assertThat(evaluated.getElementTypeDescriptor()).isNull();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 20 with SpelExpression

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

the class SpelReproTests method projectionTypeDescriptors_2.

@Test
void projectionTypeDescriptors_2() {
    StandardEvaluationContext context = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "as.![#this.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    Object[] value = (Object[]) exp.getValue(context);
    // value is array containing [true,false]
    assertThat(value[0].getClass()).isEqualTo(Boolean.class);
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
    assertThat(evaluated.getElementTypeDescriptor().getType()).isEqualTo(Boolean.class);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

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