use of org.springframework.expression.spel.support.StandardEvaluationContext 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());
}
use of org.springframework.expression.spel.support.StandardEvaluationContext 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);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext 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);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR10417.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417() {
List list1 = new ArrayList();
list1.add("a");
list1.add("b");
list1.add("x");
List list2 = new ArrayList();
list2.add("c");
list2.add("x");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("list1", list1);
context.setVariable("list2", list2);
// #this should be the element from list1
Expression ex = parser.parseExpression("#list1.?[#list2.contains(#this)]");
Object result = ex.getValue(context);
assertEquals("[x]", result.toString());
// toString() should be called on the element from list1
ex = parser.parseExpression("#list1.?[#list2.contains(toString())]");
result = ex.getValue(context);
assertEquals("[x]", result.toString());
List list3 = new ArrayList();
list3.add(1);
list3.add(2);
list3.add(3);
list3.add(4);
context = new StandardEvaluationContext();
context.setVariable("list3", list3);
ex = parser.parseExpression("#list3.?[#this > 2]");
result = ex.getValue(context);
assertEquals("[3, 4]", result.toString());
ex = parser.parseExpression("#list3.?[#this >= T(java.lang.Math).abs(T(java.lang.Math).abs(#this))]");
result = ex.getValue(context);
assertEquals("[1, 2, 3, 4]", result.toString());
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method beanResolution.
// bean resolver tests
@Test
public void beanResolution() {
StandardEvaluationContext eContext = new StandardEvaluationContext(new XX());
Expression expr = null;
// no resolver registered == exception
try {
expr = new SpelExpressionParser().parseRaw("@foo");
assertEquals("custard", expr.getValue(eContext, String.class));
} catch (SpelEvaluationException see) {
assertEquals(SpelMessage.NO_BEAN_RESOLVER_REGISTERED, see.getMessageCode());
assertEquals("foo", see.getInserts()[0]);
}
eContext.setBeanResolver(new MyBeanResolver());
// bean exists
expr = new SpelExpressionParser().parseRaw("@foo");
assertEquals("custard", expr.getValue(eContext, String.class));
// bean does not exist
expr = new SpelExpressionParser().parseRaw("@bar");
assertEquals(null, expr.getValue(eContext, String.class));
// bean name will cause AccessException
expr = new SpelExpressionParser().parseRaw("@goo");
try {
assertEquals(null, expr.getValue(eContext, String.class));
} catch (SpelEvaluationException see) {
assertEquals(SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, see.getMessageCode());
assertEquals("goo", see.getInserts()[0]);
assertTrue(see.getCause() instanceof AccessException);
assertTrue(see.getCause().getMessage().startsWith("DONT"));
}
// bean exists
expr = new SpelExpressionParser().parseRaw("@'foo.bar'");
assertEquals("trouble", expr.getValue(eContext, String.class));
// bean exists
try {
expr = new SpelExpressionParser().parseRaw("@378");
assertEquals("trouble", expr.getValue(eContext, String.class));
} catch (SpelParseException spe) {
assertEquals(SpelMessage.INVALID_BEAN_REFERENCE, spe.getMessageCode());
}
}
Aggregations