use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodOfClass.
@Test
public void testMethodOfClass() throws Exception {
Expression expression = parser.parseExpression("getName()");
Object value = expression.getValue(new StandardEvaluationContext(String.class));
assertEquals(value, "java.lang.String");
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodThrowingException_SPR6941_2.
@Test
public void testMethodThrowingException_SPR6941_2() {
// Test method on inventor: throwException()
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Inventor field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
eContext.setVariable("bar", 4);
try {
expr.getValue(eContext);
fail();
} catch (ExpressionInvocationTargetException ex) {
Throwable cause = ex.getCause();
assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", cause.getClass().getName());
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method projectionWithIterable.
@Test
public void projectionWithIterable() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testList", IntegerTestBean.createIterable());
Object value = expression.getValue(context);
assertTrue(value instanceof List);
List<?> list = (List<?>) value;
assertEquals(3, list.size());
assertEquals(5, list.get(0));
assertEquals(6, list.get(1));
assertEquals(7, list.get(2));
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectLastItemInMap.
@Test
@SuppressWarnings("unchecked")
public void selectLastItemInMap() {
EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
assertEquals(1, colorsMap.size());
assertEquals("brown", colorsMap.keySet().iterator().next());
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method projectionWithArray.
@Test
public void projectionWithArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testArray", IntegerTestBean.createArray());
Object value = expression.getValue(context);
assertTrue(value.getClass().isArray());
TypedValue typedValue = new TypedValue(value);
assertEquals(Number.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType());
Number[] array = (Number[]) value;
assertEquals(3, array.length);
assertEquals(new Integer(5), array[0]);
assertEquals(5.9f, array[1]);
assertEquals(new Integer(7), array[2]);
}
Aggregations