Search in sources :

Example 41 with SpelExpression

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

the class PropertyAccessTests method addingSpecificPropertyAccessor.

@Test
// Adding a new property accessor just for a particular type
void addingSpecificPropertyAccessor() {
    SpelExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    // Even though this property accessor is added after the reflection one, it specifically
    // names the String class as the type it is interested in so is chosen in preference to
    // any 'default' ones
    ctx.addPropertyAccessor(new StringyPropertyAccessor());
    Expression expr = parser.parseRaw("new String('hello').flibbles");
    Integer i = expr.getValue(ctx, Integer.class);
    assertThat((int) i).isEqualTo(7);
    // The reflection one will be used for other properties...
    expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
    Object o = expr.getValue(ctx);
    assertThat(o).isNotNull();
    SpelExpression flibbleexpr = parser.parseRaw("new String('hello').flibbles");
    flibbleexpr.setValue(ctx, 99);
    i = flibbleexpr.getValue(ctx, Integer.class);
    assertThat((int) i).isEqualTo(99);
    // Cannot set it to a string value
    assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> flibbleexpr.setValue(ctx, "not allowed"));
// message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
// System.out.println(e.getMessage());
}
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) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Example 42 with SpelExpression

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

the class PropertyAccessTests method accessingOnNullObject.

/**
 * The standard reflection resolver cannot find properties on null objects but some
 * supplied resolver might be able to - so null shouldn't crash the reflection resolver.
 */
@Test
void accessingOnNullObject() {
    SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
    EvaluationContext context = new StandardEvaluationContext(null);
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.getValue(context)).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
    assertThat(expr.isWritable(context)).isFalse();
    assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.setValue(context, "abc")).extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) EvaluationContext(org.springframework.expression.EvaluationContext) SimpleEvaluationContext(org.springframework.expression.spel.support.SimpleEvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 43 with SpelExpression

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

the class OperatorTests method testMinus.

@Test
void testMinus() {
    evaluate("'c' - 2", "a", String.class);
    evaluate("3.0f - 5.0f", -2.0f, Float.class);
    evaluateAndCheckError("'ab' - 2", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    evaluateAndCheckError("2-'ab'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
    SpelExpression expr = (SpelExpression) parser.parseExpression("-3");
    assertThat(expr.toStringAST()).isEqualTo("-3");
    expr = (SpelExpression) parser.parseExpression("2-3");
    assertThat(expr.toStringAST()).isEqualTo("(2 - 3)");
    evaluate("-5d", -5d, Double.class);
    evaluate("-5L", -5L, Long.class);
    evaluate("-5", -5, Integer.class);
    evaluate("-new java.math.BigDecimal('5')", new BigDecimal("-5"), BigDecimal.class);
    evaluateAndCheckError("-'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 44 with SpelExpression

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

the class InProgressTests method testSelectionAST.

@Test
public void testSelectionAST() throws Exception {
    SpelExpression expr = (SpelExpression) parser.parseExpression("'abc'.^[true]");
    assertThat(expr.toStringAST()).isEqualTo("'abc'.^[true]");
    expr = (SpelExpression) parser.parseExpression("'abc'.?[true]");
    assertThat(expr.toStringAST()).isEqualTo("'abc'.?[true]");
    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)

Example 45 with SpelExpression

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

the class ListTests method checkConstantList.

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

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