use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ExpressionLanguageScenarioTests method testScenario_UsingADifferentRootContextObject.
/**
* Scenario: using your own root context object
*/
@Test
public void testScenario_UsingADifferentRootContextObject() throws Exception {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Use the standard evaluation context
StandardEvaluationContext ctx = new StandardEvaluationContext();
TestClass tc = new TestClass();
tc.setProperty(42);
tc.str = "wibble";
ctx.setRootObject(tc);
// read it, set it, read it again
Expression expr = parser.parseRaw("str");
Object value = expr.getValue(ctx);
assertThat(value).isEqualTo("wibble");
expr = parser.parseRaw("str");
expr.setValue(ctx, "wobble");
expr = parser.parseRaw("str");
value = expr.getValue(ctx);
assertThat(value).isEqualTo("wobble");
// or using assignment within the expression
expr = parser.parseRaw("str='wabble'");
value = expr.getValue(ctx);
expr = parser.parseRaw("str");
value = expr.getValue(ctx);
assertThat(value).isEqualTo("wabble");
// private property will be accessed through getter()
expr = parser.parseRaw("property");
value = expr.getValue(ctx);
assertThat(value).isEqualTo(42);
// ... and set through setter
expr = parser.parseRaw("property=4");
value = expr.getValue(ctx);
expr = parser.parseRaw("property");
value = expr.getValue(ctx);
assertThat(value).isEqualTo(4);
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ExpressionLanguageScenarioTests method testScenario_AddingYourOwnPropertyResolvers_2.
@Test
public void testScenario_AddingYourOwnPropertyResolvers_2() throws Exception {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Use the standard evaluation context
StandardEvaluationContext ctx = new StandardEvaluationContext();
ctx.addPropertyAccessor(new VegetableColourAccessor());
Expression expr = parser.parseRaw("pea");
Object value = expr.getValue(ctx);
assertThat(value).isEqualTo(Color.green);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> expr.setValue(ctx, Color.blue)).satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL));
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ScenariosForSpringSecurityExpressionTests 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);
assertThat((boolean) value).isTrue();
ctx.setRootObject(new Person("Christian"));
value = expr.getValue(ctx, Boolean.class);
assertThat((boolean) value).isFalse();
// (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);
assertThat((boolean) value).isTrue();
pAccessor.setPerson(new Person("Christian"));
value = expr.getValue(ctx, Boolean.class);
assertThat((boolean) value).isFalse();
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ScenariosForSpringSecurityExpressionTests 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);
assertThat((boolean) value).isTrue();
// ctx.setRootObject(new Manager("Luke"));
// ctx.setVariable("a",1.043d);
// value = (Boolean)expr.getValue(ctx,Boolean.class);
// assertFalse(value);
}
use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.
the class ScenariosForSpringSecurityExpressionTests method testScenario03_Arithmetic.
@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;
// referenced as #a in the expression
ctx.setVariable("a", 1.0d);
// so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it
ctx.setRootObject(new Supervisor("Ben"));
value = expr.getValue(ctx, Boolean.class);
assertThat((boolean) value).isTrue();
ctx.setRootObject(new Manager("Luke"));
ctx.setVariable("a", 1.043d);
value = expr.getValue(ctx, Boolean.class);
assertThat((boolean) value).isFalse();
}
Aggregations