Search in sources :

Example 46 with SpelExpression

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

the class MapTests method checkConstantMap.

private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
    SpelNode node = expression.getAST();
    boolean condition = node instanceof InlineMap;
    assertThat(condition).isTrue();
    InlineMap inlineMap = (InlineMap) node;
    if (expectedToBeConstant) {
        assertThat(inlineMap.isConstant()).isTrue();
    } else {
        assertThat(inlineMap.isConstant()).isFalse();
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) InlineMap(org.springframework.expression.spel.ast.InlineMap)

Example 47 with SpelExpression

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

the class SpelReproTests method greaterThanWithNulls_SPR7840.

@Test
void greaterThanWithNulls_SPR7840() {
    List<D> list = new ArrayList<>();
    list.add(new D("aaa"));
    list.add(new D("bbb"));
    list.add(new D(null));
    list.add(new D("ccc"));
    list.add(new D(null));
    list.add(new D("zzz"));
    StandardEvaluationContext context = new StandardEvaluationContext(list);
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "#root.?[a < 'hhh']";
    SpelExpression exp = parser.parseRaw(el1);
    Object value = exp.getValue(context);
    assertThat(value.toString()).isEqualTo("[D(aaa), D(bbb), D(null), D(ccc), D(null)]");
    String el2 = "#root.?[a > 'hhh']";
    SpelExpression exp2 = parser.parseRaw(el2);
    Object value2 = exp2.getValue(context);
    assertThat(value2.toString()).isEqualTo("[D(zzz)]");
    // trim out the nulls first
    String el3 = "#root.?[a!=null].?[a < 'hhh']";
    SpelExpression exp3 = parser.parseRaw(el3);
    Object value3 = exp3.getValue(context);
    assertThat(value3.toString()).isEqualTo("[D(aaa), D(bbb), D(ccc)]");
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 48 with SpelExpression

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

the class SpelReproTests method projectionTypeDescriptors_1.

@Test
void projectionTypeDescriptors_1() {
    StandardEvaluationContext context = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "ls.![#this.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 49 with SpelExpression

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

the class SpelCompilationCoverageTests method opNe_SPR14863.

@Test
public void opNe_SPR14863() throws Exception {
    SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.MIXED, ClassLoader.getSystemClassLoader());
    SpelExpressionParser parser = new SpelExpressionParser(configuration);
    Expression expression = parser.parseExpression("data['my-key'] != 'my-value'");
    Map<String, String> data = new HashMap<>();
    data.put("my-key", new String("my-value"));
    StandardEvaluationContext context = new StandardEvaluationContext(new MyContext(data));
    assertThat(expression.getValue(context, Boolean.class)).isFalse();
    assertCanCompile(expression);
    ((SpelExpression) expression).compileExpression();
    assertThat(expression.getValue(context, Boolean.class)).isFalse();
    List<String> ls = new ArrayList<String>();
    ls.add(new String("foo"));
    context = new StandardEvaluationContext(ls);
    expression = parse("get(0) != 'foo'");
    assertThat(expression.getValue(context, Boolean.class)).isFalse();
    assertCanCompile(expression);
    assertThat(expression.getValue(context, Boolean.class)).isFalse();
    ls.remove(0);
    ls.add("goo");
    assertThat(expression.getValue(context, Boolean.class)).isTrue();
}
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) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression) HashMap(java.util.HashMap) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 50 with SpelExpression

use of org.springframework.expression.spel.standard.SpelExpression 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

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