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());
}
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);
}
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);
}
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());
}
}
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);
}
Aggregations