use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method variantGetter.
@Test
public void variantGetter() throws Exception {
Payload2Holder holder = new Payload2Holder();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new MyAccessor());
expression = parser.parseExpression("payload2.var1");
Object v = expression.getValue(ctx, holder);
assertEquals("abc", v);
// // time it interpreted
// long stime = System.currentTimeMillis();
// for (int i = 0; i < 100000; i++) {
// v = expression.getValue(ctx,holder);
// }
// System.out.println((System.currentTimeMillis() - stime));
assertCanCompile(expression);
v = expression.getValue(ctx, holder);
assertEquals("abc", v);
// // time it compiled
// stime = System.currentTimeMillis();
// for (int i = 0; i < 100000; i++) {
// v = expression.getValue(ctx,holder);
// }
// System.out.println((System.currentTimeMillis() - stime));
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class PropertyAccessTests method testAddingRemovingAccessors.
@Test
public void testAddingRemovingAccessors() {
StandardEvaluationContext ctx = new StandardEvaluationContext();
// reflective property accessor is the only one by default
List<PropertyAccessor> propertyAccessors = ctx.getPropertyAccessors();
assertEquals(1, propertyAccessors.size());
StringyPropertyAccessor spa = new StringyPropertyAccessor();
ctx.addPropertyAccessor(spa);
assertEquals(2, ctx.getPropertyAccessors().size());
List<PropertyAccessor> copy = new ArrayList<>();
copy.addAll(ctx.getPropertyAccessors());
assertTrue(ctx.removePropertyAccessor(spa));
assertFalse(ctx.removePropertyAccessor(spa));
assertEquals(1, ctx.getPropertyAccessors().size());
ctx.setPropertyAccessors(copy);
assertEquals(2, ctx.getPropertyAccessors().size());
}
use of org.springframework.expression.spel.support.StandardEvaluationContext 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.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class PropertyAccessTests method testAddingSpecificPropertyAccessor.
@Test
public // Adding a new property accessor just for a particular type
void testAddingSpecificPropertyAccessor() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// Even though this property accessor is added after the reflection one, it specifically
// names the String class as the type it is interested in so is chosen in preference to
// any 'default' ones
ctx.addPropertyAccessor(new StringyPropertyAccessor());
Expression expr = parser.parseRaw("new String('hello').flibbles");
Integer i = expr.getValue(ctx, Integer.class);
assertEquals((int) i, 7);
// The reflection one will be used for other properties...
expr = parser.parseRaw("new String('hello').CASE_INSENSITIVE_ORDER");
Object o = expr.getValue(ctx);
assertNotNull(o);
expr = parser.parseRaw("new String('hello').flibbles");
expr.setValue(ctx, 99);
i = expr.getValue(ctx, Integer.class);
assertEquals((int) i, 99);
// Cannot set it to a string value
try {
expr.setValue(ctx, "not allowed");
fail("Should not have been allowed");
} catch (EvaluationException ex) {
// success - message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
// 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
// System.out.println(e.getMessage());
}
}
use of org.springframework.expression.spel.support.StandardEvaluationContext in project spring-framework by spring-projects.
the class ScenariosForSpringSecurity method testScenario02_ComparingNames.
@Test
public void testScenario02_ComparingNames() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new SecurityPrincipalAccessor());
// Multiple options for supporting this expression: "p.name == principal.name"
// (1) If the right person is the root context object then "name==principal.name" is good enough
Expression expr = parser.parseRaw("name == principal.name");
ctx.setRootObject(new Person("Andy"));
Boolean value = expr.getValue(ctx, Boolean.class);
assertTrue(value);
ctx.setRootObject(new Person("Christian"));
value = expr.getValue(ctx, Boolean.class);
assertFalse(value);
// (2) Or register an accessor that can understand 'p' and return the right person
expr = parser.parseRaw("p.name == principal.name");
PersonAccessor pAccessor = new PersonAccessor();
ctx.addPropertyAccessor(pAccessor);
ctx.setRootObject(null);
pAccessor.setPerson(new Person("Andy"));
value = expr.getValue(ctx, Boolean.class);
assertTrue(value);
pAccessor.setPerson(new Person("Christian"));
value = expr.getValue(ctx, Boolean.class);
assertFalse(value);
}
Aggregations