Search in sources :

Example 11 with SpelExpression

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

the class LiteralTests method testNotWritable.

@Test
public void testNotWritable() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("37");
    assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
    expr = (SpelExpression) parser.parseExpression("37L");
    assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
    expr = (SpelExpression) parser.parseExpression("true");
    assertThat(expr.isWritable(new StandardEvaluationContext())).isFalse();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 12 with SpelExpression

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

the class MapTests method testMapKeysThatAreAlsoSpELKeywords.

@Test
public void testMapKeysThatAreAlsoSpELKeywords() {
    SpelExpressionParser parser = new SpelExpressionParser();
    SpelExpression expression = null;
    Object o = null;
    // expression = (SpelExpression) parser.parseExpression("foo['NEW']");
    // o = expression.getValue(new MapHolder());
    // assertEquals("VALUE",o);
    expression = (SpelExpression) parser.parseExpression("foo[T]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("TV");
    expression = (SpelExpression) parser.parseExpression("foo[t]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("tv");
    expression = (SpelExpression) parser.parseExpression("foo[NEW]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("VALUE");
    expression = (SpelExpression) parser.parseExpression("foo[new]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
    expression = (SpelExpression) parser.parseExpression("foo['abc.def']");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
    expression = (SpelExpression) parser.parseExpression("foo[foo[NEW]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("37");
    expression = (SpelExpression) parser.parseExpression("foo[foo[new]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("38");
    expression = (SpelExpression) parser.parseExpression("foo[foo[foo[T]]]");
    o = expression.getValue(new MapHolder());
    assertThat(o).isEqualTo("value");
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 13 with SpelExpression

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

the class OperatorOverloaderTests method testSimpleOperations.

@Test
public void testSimpleOperations() throws Exception {
    // no built in support for this:
    evaluateAndCheckError("'abc'-true", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    eContext.setOperatorOverloader(new StringAndBooleanAddition());
    SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'+true");
    assertThat(expr.getValue(eContext)).isEqualTo("abctrue");
    expr = (SpelExpression) parser.parseExpression("'abc'-true");
    assertThat(expr.getValue(eContext)).isEqualTo("abc");
    expr = (SpelExpression) parser.parseExpression("'abc'+null");
    assertThat(expr.getValue(eContext)).isEqualTo("abcnull");
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 14 with SpelExpression

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

the class SpelCompilationCoverageTests method mixingItUp_propertyAccessIndexerOpLtTernaryRootNull.

@Test
void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() {
    Payload payload = new Payload();
    expression = parser.parseExpression("DR[0].three");
    Object v = expression.getValue(payload);
    assertThat(getAst().getExitDescriptor()).isEqualTo("Lorg/springframework/expression/spel/SpelCompilationCoverageTests$Three");
    Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null");
    v = expression.getValue(payload);
    SpelExpression sExpr = (SpelExpression) expression;
    Ternary ternary = (Ternary) sExpr.getAST();
    OpLT oplt = (OpLT) ternary.getChild(0);
    CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand();
    String cExprExitDescriptor = cExpr.getExitDescriptor();
    assertThat(cExprExitDescriptor).isEqualTo("D");
    assertThat(oplt.getExitDescriptor()).isEqualTo("Z");
    assertCanCompile(expression);
    Object vc = expression.getValue(payload);
    assertThat(v).isEqualTo(payload);
    assertThat(vc).isEqualTo(payload);
    payload.DR[0].three.four = 0.13d;
    vc = expression.getValue(payload);
    assertThat(vc).isNull();
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression) Ternary(org.springframework.expression.spel.ast.Ternary) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) CompoundExpression(org.springframework.expression.spel.ast.CompoundExpression) OpLT(org.springframework.expression.spel.ast.OpLT) Test(org.junit.jupiter.api.Test)

Example 15 with SpelExpression

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

the class SpelCompilationCoverageTests method compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs.

@Test
public void compiledExpressionShouldWorkWhenUsingCustomFunctionWithVarargs() throws Exception {
    StandardEvaluationContext context = null;
    // Here the target method takes Object... and we are passing a string
    expression = parser.parseExpression("#doFormat('hey %s', 'there')");
    context = new StandardEvaluationContext();
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
    expression = parser.parseExpression("#doFormat([0], 'there')");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    ((SpelExpression) expression).setEvaluationContext(context);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
    expression = parser.parseExpression("#doFormat([0], #arg)");
    context = new StandardEvaluationContext(new Object[] { "hey %s" });
    context.registerFunction("doFormat", DelegatingStringFormat.class.getDeclaredMethod("format", String.class, Object[].class));
    context.setVariable("arg", "there");
    ((SpelExpression) expression).setEvaluationContext(context);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
    assertThat(((SpelNodeImpl) ((SpelExpression) expression).getAST()).isCompilable()).isTrue();
    assertCanCompile(expression);
    assertThat(expression.getValue(String.class)).isEqualTo("hey there");
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelNodeImpl(org.springframework.expression.spel.ast.SpelNodeImpl) 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