@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); Assert.assertTrue(value); ctx.setRootObject(new Person("Christian")); value = expr.getValue(ctx, Boolean.class); Assert.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); Assert.assertTrue(value); pAccessor.setPerson(new Person("Christian")); value = expr.getValue(ctx, Boolean.class); Assert.assertFalse(value); }
@Test public void testScenario03_Arithmetic() throws Exception { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); // Might be better with a as a variable although it would work as a property too... // Variable references using a '#' Expression expr = parser.parseRaw( "(hasRole('SUPERVISOR') or (#a < 1.042)) and hasIpAddress('10.10.0.0/16')"); Boolean value = null; ctx.setVariable("a", 1.0d); // referenced as #a in the expression ctx.setRootObject( new Supervisor( "Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against // it value = expr.getValue(ctx, Boolean.class); Assert.assertTrue(value); ctx.setRootObject(new Manager("Luke")); ctx.setVariable("a", 1.043d); value = expr.getValue(ctx, Boolean.class); Assert.assertFalse(value); }
// 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(); ctx.setRootObject( new Supervisor( "Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against // it); ctx.addMethodResolver( new MyMethodResolver()); // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM // 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; ctx.setVariable("a", 1.0d); // referenced as #a in the expression value = expr.getValue(ctx, Boolean.class); Assert.assertTrue(value); // ctx.setRootObject(new Manager("Luke")); // ctx.setVariable("a",1.043d); // value = (Boolean)expr.getValue(ctx,Boolean.class); // assertFalse(value); }
/** * Parse values to {@link SpelExpression} * * @return */ protected SpelExpression getExpression() { if (this.expression == null) { this.expression = parser.parseRaw(buildExpressionForPath()); } return this.expression; }
@Test public void testScenario01_Roles() throws Exception { try { SpelExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = new StandardEvaluationContext(); Expression expr = parser.parseRaw("hasAnyRole('MANAGER','TELLER')"); ctx.setRootObject(new Person("Ben")); Boolean value = expr.getValue(ctx, Boolean.class); Assert.assertFalse(value); ctx.setRootObject(new Manager("Luke")); value = expr.getValue(ctx, Boolean.class); Assert.assertTrue(value); } catch (EvaluationException ee) { ee.printStackTrace(); Assert.fail("Unexpected SpelException: " + ee.getMessage()); } }