use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class ScenariosForSpringSecurity method testScenario04_ControllingWhichMethodsRun.
// Here i'm going to change which hasRole() executes and make it one of my own Java methods
@Test
public void testScenario04_ControllingWhichMethodsRun() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);
ctx.setRootObject(new Supervisor("Ben"));
// NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
ctx.addMethodResolver(new MyMethodResolver());
// Might be better with a as a variable although it would work as a property too...
// Variable references using a '#'
// SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Expression expr = parser.parseRaw("(hasRole(3) or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')");
Boolean value = null;
// referenced as #a in the expression
ctx.setVariable("a", 1.0d);
value = expr.getValue(ctx, Boolean.class);
assertTrue(value);
// ctx.setRootObject(new Manager("Luke"));
// ctx.setVariable("a",1.043d);
// value = (Boolean)expr.getValue(ctx,Boolean.class);
// assertFalse(value);
}
use of org.springframework.expression.spel.standard.SpelExpressionParser 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.spel.standard.SpelExpressionParser 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);
}
use of org.springframework.expression.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method projectionWithSet.
@Test
public void projectionWithSet() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("#testList.![wrapper.value]");
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("testList", IntegerTestBean.createSet());
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.spel.standard.SpelExpressionParser in project spring-framework by spring-projects.
the class SelectionAndProjectionTests method selectionWithList.
@Test
public void selectionWithList() throws Exception {
Expression expression = new SpelExpressionParser().parseRaw("integers.?[#this<5]");
EvaluationContext context = new StandardEvaluationContext(new ListTestBean());
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));
}
Aggregations