use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class ExpressionWithConversionTests method testCoercionToCollectionOfPrimitive.
@Test
public void testCoercionToCollectionOfPrimitive() throws Exception {
class TestTarget {
@SuppressWarnings("unused")
public int sum(Collection<Integer> numbers) {
int total = 0;
for (int i : numbers) {
total += i;
}
return total;
}
}
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
TypeDescriptor collectionType = new TypeDescriptor(new MethodParameter(TestTarget.class.getDeclaredMethod("sum", Collection.class), 0));
// The type conversion is possible
assertTrue(evaluationContext.getTypeConverter().canConvert(TypeDescriptor.valueOf(String.class), collectionType));
// ... and it can be done successfully
assertEquals("[1, 2, 3, 4]", evaluationContext.getTypeConverter().convertValue("1,2,3,4", TypeDescriptor.valueOf(String.class), collectionType).toString());
evaluationContext.setVariable("target", new TestTarget());
// OK up to here, so the evaluation should be fine...
// ... but this fails
int result = (Integer) parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4");
assertEquals("Wrong result: " + result, 10, result);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class ExpressionWithConversionTests method testConvert.
@Test
public void testConvert() {
Foo root = new Foo("bar");
Collection<String> foos = Collections.singletonList("baz");
StandardEvaluationContext context = new StandardEvaluationContext(root);
// property access
Expression expression = parser.parseExpression("foos");
expression.setValue(context, foos);
Foo baz = root.getFoos().iterator().next();
assertEquals("baz", baz.value);
// method call
expression = parser.parseExpression("setFoos(#foos)");
context.setVariable("foos", foos);
expression.getValue(context);
baz = root.getFoos().iterator().next();
assertEquals("baz", baz.value);
// method call with result from method call
expression = parser.parseExpression("setFoos(getFoosAsStrings())");
expression.getValue(context);
baz = root.getFoos().iterator().next();
assertEquals("baz", baz.value);
// method call with result from method call
expression = parser.parseExpression("setFoos(getFoosAsObjects())");
expression.getValue(context);
baz = root.getFoos().iterator().next();
assertEquals("baz", baz.value);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class ExpressionWithConversionTests method testSetParameterizedList.
@Test
public void testSetParameterizedList() throws Exception {
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
Expression e = parser.parseExpression("listOfInteger.size()");
assertEquals(0, e.getValue(context, Integer.class).intValue());
context.setTypeConverter(new TypeConvertorUsingConversionService());
// Assign a List<String> to the List<Integer> field - the component elements should be converted
parser.parseExpression("listOfInteger").setValue(context, listOfString);
// size now 3
assertEquals(3, e.getValue(context, Integer.class).intValue());
// element type correctly Integer
Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class);
assertEquals(Integer.class, clazz);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class IndexingTests method indexIntoGenericPropertyContainingMapObject.
@Test
public void indexIntoGenericPropertyContainingMapObject() {
Map<String, Map<String, String>> property = new HashMap<>();
Map<String, String> map = new HashMap<>();
map.put("foo", "bar");
property.put("property", map);
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new MapAccessor());
context.setRootObject(property);
Expression expression = parser.parseExpression("property");
assertEquals("java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(context).toString());
assertEquals(map, expression.getValue(context));
assertEquals(map, expression.getValue(context, Map.class));
expression = parser.parseExpression("property['foo']");
assertEquals("bar", expression.getValue(context));
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class MethodInvocationTests method invokeMethodWithoutConversion.
@Test
public void invokeMethodWithoutConversion() throws Exception {
final BytesService service = new BytesService();
byte[] bytes = new byte[100];
StandardEvaluationContext context = new StandardEvaluationContext(bytes);
context.setBeanResolver(new BeanResolver() {
@Override
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
if ("service".equals(beanName)) {
return service;
}
return null;
}
});
Expression expression = parser.parseExpression("@service.handleBytes(#root)");
byte[] outBytes = expression.getValue(context, byte[].class);
assertSame(bytes, outBytes);
}
Aggregations