Search in sources :

Example 91 with StandardEvaluationContext

use of org.springframework.expression.spel.support.StandardEvaluationContext 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 92 with StandardEvaluationContext

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

Example 93 with StandardEvaluationContext

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

the class SpelReproTests method SPR11609.

@Test
public void SPR11609() {
    StandardEvaluationContext sec = new StandardEvaluationContext();
    sec.addPropertyAccessor(new MapAccessor());
    Expression exp = new SpelExpressionParser().parseExpression("T(org.springframework.expression.spel.SpelReproTests$MapWithConstant).X");
    assertEquals(1, exp.getValue(sec));
}
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 94 with StandardEvaluationContext

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

the class SpelReproTests method projectionTypeDescriptors_3.

@Test
public void projectionTypeDescriptors_3() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "ms.![key.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    List<?> value = (List<?>) exp.getValue(ctx);
    // value is list containing [true,false]
    assertEquals(Boolean.class, value.get(0).getClass());
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
    assertEquals(null, evaluated.getElementTypeDescriptor());
}
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) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 95 with StandardEvaluationContext

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

the class SpelReproTests method nestedProperties_SPR6923.

@Test
public void nestedProperties_SPR6923() {
    StandardEvaluationContext eContext = new StandardEvaluationContext(new Foo());
    Expression expr = new SpelExpressionParser().parseRaw("resource.resource.server");
    String name = expr.getValue(eContext, String.class);
    assertEquals("abc", name);
}
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)

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