use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class EvaluationTests method operatorVariants.
@Test
public void operatorVariants() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("#a < #b");
EvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("a", (short) 3);
ctx.setVariable("b", (short) 6);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("b", (byte) 6);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", (byte) 9);
ctx.setVariable("b", (byte) 6);
assertFalse(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", 10L);
ctx.setVariable("b", (short) 30);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", (byte) 3);
ctx.setVariable("b", (short) 30);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", (byte) 3);
ctx.setVariable("b", 30L);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", (byte) 3);
ctx.setVariable("b", 30f);
assertTrue(expr.getValue(ctx, Boolean.class));
ctx.setVariable("a", new BigInteger("10"));
ctx.setVariable("b", new BigInteger("20"));
assertTrue(expr.getValue(ctx, Boolean.class));
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class EvaluationTests method testComparison.
@Test
public void testComparison() throws Exception {
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
boolean trueValue = parser.parseExpression("T(java.util.Date) == Birthdate.Class").getValue(context, Boolean.class);
assertTrue(trueValue);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class PropertyAccessTests method testAccessingOnNullObject.
/**
* The standard reflection resolver cannot find properties on null objects but some
* supplied resolver might be able to - so null shouldn't crash the reflection resolver.
*/
@Test
public void testAccessingOnNullObject() throws Exception {
SpelExpression expr = (SpelExpression) parser.parseExpression("madeup");
EvaluationContext context = new StandardEvaluationContext(null);
try {
expr.getValue(context);
fail("Should have failed - default property resolver cannot resolve on null");
} catch (Exception ex) {
checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL);
}
assertFalse(expr.isWritable(context));
try {
expr.setValue(context, "abc");
fail("Should have failed - default property resolver cannot resolve on null");
} catch (Exception ex) {
checkException(ex, SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL);
}
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectionWithSet.
@Test
public void selectionWithSet() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new SetTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof List);
List<?> list = (List<?>) value;
assertEquals(5, list.size());
assertEquals(0, list.get(0));
assertEquals(1, list.get(1));
assertEquals(2, list.get(2));
assertEquals(3, list.get(3));
assertEquals(4, list.get(4));
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectFirstItemInPrimitiveArray.
@Test
public void selectFirstItemInPrimitiveArray() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("ints.^[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
Object value = expression.getValue(context);
assertTrue(value instanceof Integer);
assertEquals(0, value);
}
Aggregations