Search in sources :

Example 6 with SpelExpression

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

the class ParsingTests method parseCheck.

/**
 * Parse the supplied expression and then create a string representation of the resultant AST, it should be the
 * expected value.
 *
 * @param expression the expression to parse
 * @param expectedStringFormOfAST the expected string form of the AST
 */
public void parseCheck(String expression, String expectedStringFormOfAST) {
    SpelExpression e = parser.parseRaw(expression);
    assertThat(e).isNotNull();
    assertThat(e.toStringAST()).isEqualTo(expectedStringFormOfAST);
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression)

Example 7 with SpelExpression

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

the class EvaluationTests method testConstructorInvocation06.

@Test
public void testConstructorInvocation06() {
    // repeated evaluation to drive use of cached executor
    SpelExpression e = (SpelExpression) parser.parseExpression("new String('wibble')");
    String newString = e.getValue(String.class);
    assertThat(newString).isEqualTo("wibble");
    newString = e.getValue(String.class);
    assertThat(newString).isEqualTo("wibble");
    // not writable
    assertThat(e.isWritable(new StandardEvaluationContext())).isFalse();
    // ast
    assertThat(e.toStringAST()).isEqualTo("new String('wibble')");
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 8 with SpelExpression

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

the class MethodInvocationTests method testMethodFiltering_SPR6764.

@Test
public void testMethodFiltering_SPR6764() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setRootObject(new TestObject());
    LocalFilter filter = new LocalFilter();
    context.registerMethodFilter(TestObject.class, filter);
    // Filter will be called but not do anything, so first doit() will be invoked
    SpelExpression expr = (SpelExpression) parser.parseExpression("doit(1)");
    String result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("1");
    assertThat(filter.filterCalled).isTrue();
    // Filter will now remove non @Anno annotated methods
    filter.removeIfNotAnnotated = true;
    filter.filterCalled = false;
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("double 1.0");
    assertThat(filter.filterCalled).isTrue();
    // check not called for other types
    filter.filterCalled = false;
    context.setRootObject(new String("abc"));
    expr = (SpelExpression) parser.parseExpression("charAt(0)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("a");
    assertThat(filter.filterCalled).isFalse();
    // check de-registration works
    filter.filterCalled = false;
    // clear filter
    context.registerMethodFilter(TestObject.class, null);
    context.setRootObject(new TestObject());
    expr = (SpelExpression) parser.parseExpression("doit(1)");
    result = expr.getValue(context, String.class);
    assertThat(result).isEqualTo("1");
    assertThat(filter.filterCalled).isFalse();
}
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 9 with SpelExpression

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

the class EvaluationTests method operatorVariants.

@Test
public void operatorVariants() {
    SpelExpression e = (SpelExpression) parser.parseExpression("#a < #b");
    EvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("a", (short) 3);
    ctx.setVariable("b", (short) 6);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("b", (byte) 6);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("a", (byte) 9);
    ctx.setVariable("b", (byte) 6);
    assertThat(e.getValue(ctx, Boolean.class)).isFalse();
    ctx.setVariable("a", 10L);
    ctx.setVariable("b", (short) 30);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", (short) 30);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", 30L);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("a", (byte) 3);
    ctx.setVariable("b", 30f);
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
    ctx.setVariable("a", new BigInteger("10"));
    ctx.setVariable("b", new BigInteger("20"));
    assertThat(e.getValue(ctx, Boolean.class)).isTrue();
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) BigInteger(java.math.BigInteger) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 10 with SpelExpression

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

the class InProgressTests method testProjection06.

@Test
public void testProjection06() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.![true]");
    assertThat(expr.toStringAST()).isEqualTo("'abc'.![true]");
}
Also used : 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