use of org.springframework.expression.spel.support.StandardEvaluationContext 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.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SetValueTests method testSetGenericMapElementRequiresCoercion.
/*
* Testing the coercion of both the keys and the values to the correct type
*/
@Test
public void testSetGenericMapElementRequiresCoercion() throws Exception {
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
Expression e = parse("mapOfStringToBoolean[42]");
assertNull(e.getValue(eContext));
// Key should be coerced to string representation of 42
e.setValue(eContext, "true");
// All keys should be strings
Set<?> ks = parse("mapOfStringToBoolean.keySet()").getValue(eContext, Set.class);
for (Object o : ks) {
assertEquals(String.class, o.getClass());
}
// All values should be booleans
Collection<?> vs = parse("mapOfStringToBoolean.values()").getValue(eContext, Collection.class);
for (Object o : vs) {
assertEquals(Boolean.class, o.getClass());
}
// One final test check coercion on the key for a map lookup
Object o = e.getValue(eContext);
assertEquals(Boolean.TRUE, o);
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SetValueTests method testIsWritableForInvalidExpressions_SPR10610.
@Test
public void testIsWritableForInvalidExpressions_SPR10610() {
Expression e = null;
StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
// PROPERTYORFIELDREFERENCE
// Non existent field (or property):
e = parser.parseExpression("arrayContainer.wibble");
assertFalse("Should not be writable!", e.isWritable(lContext));
e = parser.parseExpression("arrayContainer.wibble.foo");
try {
assertFalse("Should not be writable!", e.isWritable(lContext));
fail("Should have had an error because wibble does not really exist");
} catch (SpelEvaluationException see) {
// org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 15): Property or field 'wibble' cannot be found on object of type 'org.springframework.expression.spel.testresources.ArrayContainer' - maybe not public?
// at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
// success!
}
// VARIABLE
// the variable does not exist (but that is OK, we should be writable)
e = parser.parseExpression("#madeup1");
assertTrue("Should be writable!", e.isWritable(lContext));
// compound expression
e = parser.parseExpression("#madeup2.bar");
assertFalse("Should not be writable!", e.isWritable(lContext));
// INDEXER
// non existent indexer (wibble made up)
e = parser.parseExpression("arrayContainer.wibble[99]");
try {
assertFalse("Should not be writable!", e.isWritable(lContext));
fail("Should have had an error because wibble does not really exist");
} catch (SpelEvaluationException see) {
// success!
}
// non existent indexer (index via a string)
e = parser.parseExpression("arrayContainer.ints['abc']");
try {
assertFalse("Should not be writable!", e.isWritable(lContext));
fail("Should have had an error because wibble does not really exist");
} catch (SpelEvaluationException see) {
// success!
}
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SetValueTests method testAssign.
@Test
public void testAssign() throws Exception {
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
Expression e = parse("publicName='Andy'");
assertFalse(e.isWritable(eContext));
assertEquals("Andy", e.getValue(eContext));
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SetValueTests method setValueExpectError.
/**
* Call setValue() but expect it to fail.
*/
protected void setValueExpectError(String expression, Object value) {
try {
Expression e = parser.parseExpression(expression);
if (e == null) {
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, e);
}
StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
e.setValue(lContext, value);
fail("expected an error");
} catch (ParseException pe) {
pe.printStackTrace();
fail("Unexpected Exception: " + pe.getMessage());
} catch (EvaluationException ee) {
// success!
}
}
Aggregations