Search in sources :

Example 36 with SpelExpressionParser

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

the class MapAccessTests method testGetValue.

@Test
public void testGetValue() {
    Map<String, String> props1 = new HashMap<>();
    props1.put("key1", "value1");
    props1.put("key2", "value2");
    props1.put("key3", "value3");
    Object bean = new TestBean("name1", new TestBean("name2", null, "Description 2", 15, props1), "description 1", 6, props1);
    ExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("testBean.properties['key2']");
    assertEquals("value2", expr.getValue(bean));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) HashMap(java.util.HashMap) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 37 with SpelExpressionParser

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

the class MapAccessTests method testCustomMapAccessor.

@Test
public void testCustomMapAccessor() throws Exception {
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext();
    ctx.addPropertyAccessor(new MapAccessor());
    Expression expr = parser.parseExpression("testMap.monday");
    Object value = expr.getValue(ctx, String.class);
    assertEquals("montag", value);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ExpressionParser(org.springframework.expression.ExpressionParser) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Test(org.junit.Test)

Example 38 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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());
    assertEquals("TV", o);
    expression = (SpelExpression) parser.parseExpression("foo[t]");
    o = expression.getValue(new MapHolder());
    assertEquals("tv", o);
    expression = (SpelExpression) parser.parseExpression("foo[NEW]");
    o = expression.getValue(new MapHolder());
    assertEquals("VALUE", o);
    expression = (SpelExpression) parser.parseExpression("foo[new]");
    o = expression.getValue(new MapHolder());
    assertEquals("value", o);
    expression = (SpelExpression) parser.parseExpression("foo['abc.def']");
    o = expression.getValue(new MapHolder());
    assertEquals("value", o);
    expression = (SpelExpression) parser.parseExpression("foo[foo[NEW]]");
    o = expression.getValue(new MapHolder());
    assertEquals("37", o);
    expression = (SpelExpression) parser.parseExpression("foo[foo[new]]");
    o = expression.getValue(new MapHolder());
    assertEquals("38", o);
    expression = (SpelExpression) parser.parseExpression("foo[foo[foo[T]]]");
    o = expression.getValue(new MapHolder());
    assertEquals("value", o);
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 39 with SpelExpressionParser

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

the class MethodInvocationTests method testMethodThrowingException_SPR6941_2.

@Test
public void testMethodThrowingException_SPR6941_2() {
    // Test method on inventor: throwException()
    // On 1 it will throw an IllegalArgumentException
    // On 2 it will throw a RuntimeException
    // On 3 it will exit normally
    // In each case it increments the Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    eContext.setVariable("bar", 4);
    try {
        expr.getValue(eContext);
        fail();
    } catch (ExpressionInvocationTargetException ex) {
        Throwable cause = ex.getCause();
        assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", cause.getClass().getName());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) Test(org.junit.Test)

Example 40 with SpelExpressionParser

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

the class SelectionAndProjectionTests method projectionWithIterable.

@Test
public void projectionWithIterable() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("testList", IntegerTestBean.createIterable());
    Object value = expression.getValue(context);
    assertTrue(value instanceof List);
    List<?> list = (List<?>) value;
    assertEquals(3, list.size());
    assertEquals(5, list.get(0));
    assertEquals(6, list.get(1));
    assertEquals(7, list.get(2));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) 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