Search in sources :

Example 26 with Expression

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);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 27 with Expression

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));
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 28 with Expression

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();
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 29 with Expression

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);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 30 with Expression

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();
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Aggregations

Expression (cn.taketoday.expression.Expression)418 Test (org.junit.jupiter.api.Test)394 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)302 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)268 SpelExpression (cn.taketoday.expression.spel.standard.SpelExpression)206 ExpressionParser (cn.taketoday.expression.ExpressionParser)112 EvaluationContext (cn.taketoday.expression.EvaluationContext)70 ArrayList (java.util.ArrayList)58 HashMap (java.util.HashMap)38 CompoundExpression (cn.taketoday.expression.spel.ast.CompoundExpression)36 EvaluationException (cn.taketoday.expression.EvaluationException)28 List (java.util.List)24 Map (java.util.Map)20 CompositeStringExpression (cn.taketoday.expression.common.CompositeStringExpression)16 LinkedHashMap (java.util.LinkedHashMap)14 TypedValue (cn.taketoday.expression.TypedValue)12 SimpleEvaluationContext (cn.taketoday.expression.spel.support.SimpleEvaluationContext)10 Method (java.lang.reflect.Method)10 AccessException (cn.taketoday.expression.AccessException)8 MethodResolver (cn.taketoday.expression.MethodResolver)8