Search in sources :

Example 41 with EvaluationContext

use of org.springframework.expression.EvaluationContext 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 42 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class SpelDocumentationTests method testPropertyAccess.

@Test
public void testPropertyAccess() throws Exception {
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    // 1856
    int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);
    assertEquals(1856, year);
    String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
    assertEquals("SmilJan", city);
}
Also used : EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Test(org.junit.Test)

Example 43 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class ExpressionEvaluatorTests method testMultipleCachingEval.

@Test
public void testMultipleCachingEval() throws Exception {
    AnnotatedClass target = new AnnotatedClass();
    Method method = ReflectionUtils.findMethod(AnnotatedClass.class, "multipleCaching", Object.class, Object.class);
    Object[] args = new Object[] { new Object(), new Object() };
    Collection<ConcurrentMapCache> caches = Collections.singleton(new ConcurrentMapCache("test"));
    EvaluationContext evalCtx = this.eval.createEvaluationContext(caches, method, args, target, target.getClass(), null);
    Collection<CacheOperation> ops = getOps("multipleCaching");
    Iterator<CacheOperation> it = ops.iterator();
    AnnotatedElementKey key = new AnnotatedElementKey(method, AnnotatedClass.class);
    Object keyA = this.eval.key(it.next().getKey(), key, evalCtx);
    Object keyB = this.eval.key(it.next().getKey(), key, evalCtx);
    assertEquals(args[0], keyA);
    assertEquals(args[1], keyB);
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Method(java.lang.reflect.Method) EvaluationContext(org.springframework.expression.EvaluationContext) AnnotatedElementKey(org.springframework.context.expression.AnnotatedElementKey) Test(org.junit.Test)

Example 44 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.

the class ExpressionEvaluatorTests method unavailableReturnValue.

@Test
public void unavailableReturnValue() throws Exception {
    EvaluationContext context = createEvaluationContext(CacheOperationExpressionEvaluator.RESULT_UNAVAILABLE);
    try {
        new SpelExpressionParser().parseExpression("#result").getValue(context);
        fail("Should have failed to parse expression, result not available");
    } catch (VariableNotAvailableException e) {
        assertEquals("wrong variable name", "result", e.getName());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) EvaluationContext(org.springframework.expression.EvaluationContext) Test(org.junit.Test)

Example 45 with EvaluationContext

use of org.springframework.expression.EvaluationContext in project spring-data-mongodb by spring-projects.

the class ExpressionEvaluatingParameterBinder method evaluateExpression.

/**
 * Evaluates the given {@code expressionString}.
 *
 * @param expressionString must not be {@literal null} or empty.
 * @param parameters must not be {@literal null}.
 * @param parameterValues must not be {@literal null}.
 * @return
 */
@Nullable
private Object evaluateExpression(String expressionString, MongoParameters parameters, Object[] parameterValues) {
    EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, parameterValues);
    Expression expression = expressionParser.parseExpression(expressionString);
    return expression.getValue(evaluationContext, Object.class);
}
Also used : Expression(org.springframework.expression.Expression) EvaluationContext(org.springframework.expression.EvaluationContext) Nullable(org.springframework.lang.Nullable)

Aggregations

EvaluationContext (org.springframework.expression.EvaluationContext)86 Test (org.junit.Test)72 Expression (org.springframework.expression.Expression)53 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)52 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)32 ArrayList (java.util.ArrayList)12 Authentication (org.springframework.security.core.Authentication)11 ExpressionParser (org.springframework.expression.ExpressionParser)10 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)10 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9 List (java.util.List)8 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)8 TypedValue (org.springframework.expression.TypedValue)6 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)6 Map (java.util.Map)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)5 AccessException (org.springframework.expression.AccessException)5 Method (java.lang.reflect.Method)4