Search in sources :

Example 56 with SpelExpressionParser

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

the class SpelReproTests method AccessingFactoryBean_spr9511.

@Test
public void AccessingFactoryBean_spr9511() {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new MyBeanResolver());
    Expression expr = new SpelExpressionParser().parseRaw("@foo");
    assertEquals("custard", expr.getValue(context));
    expr = new SpelExpressionParser().parseRaw("&foo");
    assertEquals("foo factory", expr.getValue(context));
    try {
        expr = new SpelExpressionParser().parseRaw("&@foo");
        fail("Illegal syntax, error expected");
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
        assertEquals(0, spe.getPosition());
    }
    try {
        expr = new SpelExpressionParser().parseRaw("@&foo");
        fail("Illegal syntax, error expected");
    } catch (SpelParseException spe) {
        assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
        assertEquals(0, spe.getPosition());
    }
}
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 57 with SpelExpressionParser

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

the class SpelReproTests method projectionTypeDescriptors_2.

@Test
public void projectionTypeDescriptors_2() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "as.![#this.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    Object[] value = (Object[]) exp.getValue(ctx);
    // value is array containing [true,false]
    assertEquals(Boolean.class, value[0].getClass());
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
    assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.Test)

Example 58 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 59 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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 60 with SpelExpressionParser

use of org.springframework.expression.spel.standard.SpelExpressionParser 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)

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