Search in sources :

Example 71 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method SPR13055_maps.

@Test
public void SPR13055_maps() {
    EvaluationContext context = new StandardEvaluationContext();
    ExpressionParser parser = new SpelExpressionParser();
    Expression ex = parser.parseExpression("{'a':'y','b':'n'}.![value=='y'?key:null]");
    assertEquals("[a, null]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.key) + 5]");
    assertEquals("[7, 8]", ex.getValue(context).toString());
    ex = parser.parseExpression("{2:4,3:6}.![T(java.lang.Math).abs(#this.value) + 5]");
    assertEquals("[9, 11]", ex.getValue(context).toString());
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 72 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method SPR9486_multiplyFloatWithFloat.

@Test
public void SPR9486_multiplyFloatWithFloat() {
    Number expectedNumber = 10.21f * 10.2f;
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Expression expression = parser.parseExpression("10.21f * 10.2f");
    Number result = expression.getValue(context, null, Number.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 73 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method mapOfMap_SPR7244.

@Test
public void mapOfMap_SPR7244() throws Exception {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("uri", "http:");
    Map<String, String> nameMap = new LinkedHashMap<>();
    nameMap.put("givenName", "Arthur");
    map.put("value", nameMap);
    StandardEvaluationContext ctx = new StandardEvaluationContext(map);
    ExpressionParser parser = new SpelExpressionParser();
    String el1 = "#root['value'].get('givenName')";
    Expression exp = parser.parseExpression(el1);
    Object evaluated = exp.getValue(ctx);
    assertEquals("Arthur", evaluated);
    String el2 = "#root['value']['givenName']";
    exp = parser.parseExpression(el2);
    evaluated = exp.getValue(ctx);
    assertEquals("Arthur", evaluated);
}
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) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 74 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method SPR10417.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417() {
    List list1 = new ArrayList();
    list1.add("a");
    list1.add("b");
    list1.add("x");
    List list2 = new ArrayList();
    list2.add("c");
    list2.add("x");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("list1", list1);
    context.setVariable("list2", list2);
    // #this should be the element from list1
    Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
    Object result = ex.getValue(context);
    assertEquals("[x]", result.toString());
    // toString() should be called on the element from list1
    ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
    result = ex.getValue(context);
    assertEquals("[x]", result.toString());
    List list3 = new ArrayList();
    list3.add(1);
    list3.add(2);
    list3.add(3);
    list3.add(4);
    context = new StandardEvaluationContext();
    context.setVariable("list3", list3);
    ex = parser.parseExpression("#list3.?[#this > 2]");
    result = ex.getValue(context);
    assertEquals("[3, 4]", result.toString());
    ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
    result = ex.getValue(context);
    assertEquals("[1, 2, 3, 4]", result.toString());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 75 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.

the class SpelReproTests method beanResolution.

// bean resolver tests
@Test
public void beanResolution() {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
    Expression expr = null;
    // no resolver registered == exception
    try {
        expr = new SpelExpressionParser().parseRaw("@foo");
        assertEquals("custard", expr.getValue(eContext, String.class));
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.NO_BEAN_RESOLVER_REGISTERED, see.getMessageCode());
        assertEquals("foo", see.getInserts()[0]);
    }
    eContext.setBeanResolver(new MyBeanResolver());
    // bean exists
    expr = new SpelExpressionParser().parseRaw("@foo");
    assertEquals("custard", expr.getValue(eContext, String.class));
    // bean does not exist
    expr = new SpelExpressionParser().parseRaw("@bar");
    assertEquals(null, expr.getValue(eContext, String.class));
    // bean name will cause AccessException
    expr = new SpelExpressionParser().parseRaw("@goo");
    try {
        assertEquals(null, expr.getValue(eContext, String.class));
    } catch (SpelEvaluationException see) {
        assertEquals(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, see.getMessageCode());
        assertEquals("goo", see.getInserts()[0]);
        assertTrue(see.getCause() instanceof AccessException);
        assertTrue(see.getCause().getMessage().startsWith("DONT"));
    }
    // bean exists
    expr = new SpelExpressionParser().parseRaw("@'foo.bar'");
    assertEquals("trouble", expr.getValue(eContext, String.class));
    // bean exists
    try {
        expr = new SpelExpressionParser().parseRaw("@378");
        assertEquals("trouble", expr.getValue(eContext, String.class));
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
    }
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) AccessException(org.springframework.expression.AccessException) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) Test(org.junit.Test)

Aggregations

StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)251 Test (org.junit.Test)211 Expression (org.springframework.expression.Expression)157 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)155 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)104 ExpressionParser (org.springframework.expression.ExpressionParser)81 EvaluationContext (org.springframework.expression.EvaluationContext)43 ArrayList (java.util.ArrayList)27 List (java.util.List)14 HashMap (java.util.HashMap)12 EvaluationException (org.springframework.expression.EvaluationException)12 TypedValue (org.springframework.expression.TypedValue)12 Map (java.util.Map)11 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)8 BigInteger (java.math.BigInteger)7 LinkedHashMap (java.util.LinkedHashMap)7 ExpressionState (org.springframework.expression.spel.ExpressionState)7 AccessException (org.springframework.expression.AccessException)6 Inventor (org.springframework.expression.spel.testresources.Inventor)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5