Search in sources :

Example 66 with SpelExpressionParser

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

the class SpelReproTests method SPR9486_floatLessThanDouble.

@Test
public void SPR9486_floatLessThanDouble() {
    Boolean expectedNumber = -10.21f < -10.2;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("-10.21f < -10.2");
    Boolean result = expression.getValue(context, null, Boolean.class);
    assertEquals(expectedNumber, result);
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 67 with SpelExpressionParser

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

the class SpelReproTests method checkTemplateParsingError.

private void checkTemplateParsingError(String expression, ParserContext context, String expectedMessage) throws Exception {
    SpelExpressionParser parser = new SpelExpressionParser();
    try {
        parser.parseExpression(expression, context);
        fail("Should have failed with message: " + expectedMessage);
    } catch (Exception ex) {
        String message = ex.getMessage();
        if (ex instanceof ExpressionException) {
            message = ((ExpressionException) ex).getSimpleMessage();
        }
        if (!message.equals(expectedMessage)) {
            ex.printStackTrace();
        }
        assertThat(expectedMessage, equalTo(message));
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ExpressionException(org.springframework.expression.ExpressionException) EvaluationException(org.springframework.expression.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) AccessException(org.springframework.expression.AccessException) ExpressionException(org.springframework.expression.ExpressionException)

Example 68 with SpelExpressionParser

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

the class SpelReproTests method array.

@Test
public 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, "");
    assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
    expression = parser.parseExpression("T(java.lang.Long[])");
    result = expression.getValue(context, "");
    assertEquals("Equal assertion failed: ", "class [Ljava.lang.Long;", result.toString());
    expression = parser.parseExpression("T(java.lang.String[][][])");
    result = expression.getValue(context, "");
    assertEquals("Equal assertion failed: ", "class [[[Ljava.lang.String;", result.toString());
    assertEquals("T(java.lang.String[][][])", ((SpelExpression) expression).toStringAST());
    expression = parser.parseExpression("new int[0].class");
    result = expression.getValue(context, "");
    assertEquals("Equal assertion failed: ", "class [I", result.toString());
    expression = parser.parseExpression("T(int[][])");
    result = expression.getValue(context, "");
    assertEquals("class [[I", result.toString());
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 69 with SpelExpressionParser

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

the class SpelReproTests method elvis_SPR7209_1.

// end bean resolver tests
@Test
public void elvis_SPR7209_1() {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
    Expression expr = null;
    // Different parts of elvis expression are null
    expr = new SpelExpressionParser().parseRaw("(?:'default')");
    assertEquals("default", expr.getValue());
    expr = new SpelExpressionParser().parseRaw("?:'default'");
    assertEquals("default", expr.getValue());
    expr = new SpelExpressionParser().parseRaw("?:");
    assertEquals(null, expr.getValue());
    // Different parts of ternary expression are null
    try {
        expr = new SpelExpressionParser().parseRaw("(?'abc':'default')");
        expr.getValue(eContext);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.TYPE_CONVERSION_ERROR, see.getMessageCode());
    }
    expr = new SpelExpressionParser().parseRaw("(false?'abc':null)");
    assertEquals(null, expr.getValue());
    // Assignment
    try {
        expr = new SpelExpressionParser().parseRaw("(='default')");
        expr.getValue(eContext);
        fail();
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.SETVALUE_NOT_SUPPORTED, see.getMessageCode());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Example 70 with SpelExpressionParser

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

the class SpelReproTests method SPR13055.

@Test
@SuppressWarnings("rawtypes")
public void SPR13055() throws Exception {
    List<Map<String, Object>> myPayload = new ArrayList<>();
    Map<String, Object> v1 = new HashMap<>();
    Map<String, Object> v2 = new HashMap<>();
    v1.put("test11", "test11");
    v1.put("test12", "test12");
    v2.put("test21", "test21");
    v2.put("test22", "test22");
    myPayload.add(v1);
    myPayload.add(v2);
    EvaluationContext context = new StandardEvaluationContext(myPayload);
    ExpressionParser parser = new SpelExpressionParser();
    String ex = "#root.![T(org.springframework.util.StringUtils).collectionToCommaDelimitedString(#this.values())]";
    List res = parser.parseExpression(ex).getValue(context, List.class);
    assertEquals("[test12,test11, test22,test21]", res.toString());
    res = parser.parseExpression("#root.![#this.values()]").getValue(context, List.class);
    assertEquals("[[test12, test11], [test22, test21]]", res.toString());
    res = parser.parseExpression("#root.![values()]").getValue(context, List.class);
    assertEquals("[[test12, test11], [test22, test21]]", res.toString());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)218 Test (org.junit.Test)190 Expression (org.springframework.expression.Expression)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)101 ExpressionParser (org.springframework.expression.ExpressionParser)89 EvaluationContext (org.springframework.expression.EvaluationContext)32 ArrayList (java.util.ArrayList)31 HashMap (java.util.HashMap)16 List (java.util.List)14 EvaluationException (org.springframework.expression.EvaluationException)13 Map (java.util.Map)11 LinkedHashMap (java.util.LinkedHashMap)8 BigInteger (java.math.BigInteger)6 AccessException (org.springframework.expression.AccessException)6 CompositeStringExpression (org.springframework.expression.common.CompositeStringExpression)6 ParseException (org.springframework.expression.ParseException)5 BigDecimal (java.math.BigDecimal)4 TypedValue (org.springframework.expression.TypedValue)4 TreeMap (java.util.TreeMap)3