use of org.springframework.expression.EvaluationContext 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]);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectLastItemInSet.
@Test
public void selectLastItemInSet() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.$[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof Integer);
assertEquals(4, value);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectFirstItemInArray.
@Test
public void selectFirstItemInArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof Integer);
assertEquals(0, value);
}
use of org.springframework.expression.EvaluationContext 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.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method accessingNullPropertyViaReflection_SPR5663.
@Test
public void accessingNullPropertyViaReflection_SPR5663() throws AccessException {
PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
assertFalse(propertyAccessor.canRead(context, null, "abc"));
assertFalse(propertyAccessor.canWrite(context, null, "abc"));
try {
propertyAccessor.read(context, null, "abc");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
// success
}
try {
propertyAccessor.write(context, null, "abc", "foo");
fail("Should have failed with an AccessException");
} catch (AccessException ae) {
// success
}
}
Aggregations