Search in sources :

Example 21 with Expression

use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.

the class ComparatorTests method customComparatorWorksWithEquality.

@Test
public void customComparatorWorksWithEquality() {
    final StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setTypeComparator(customComparator);
    ExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("'1' == 1");
    assertThat(expr.getValue(ctx, Boolean.class)).isTrue();
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ExpressionParser(cn.taketoday.expression.ExpressionParser) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Test(org.junit.jupiter.api.Test)

Example 22 with Expression

use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.

the class CachedMethodExecutorTests method testCachedExecutionForTarget.

@Test
public void testCachedExecutionForTarget() {
    Expression expression = this.parser.parseExpression("#var.echo(42)");
    assertMethodExecution(expression, new RootObject(), "int: 42");
    assertMethodExecution(expression, new RootObject(), "int: 42");
    assertMethodExecution(expression, new BaseObject(), "String: 42");
    assertMethodExecution(expression, new RootObject(), "int: 42");
}
Also used : Expression(cn.taketoday.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 23 with Expression

use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.

the class ConstructorInvocationTests method testConstructorThrowingException_SPR6760.

@Test
public void testConstructorThrowingException_SPR6760() {
    // Test ctor on inventor:
    // On 1 it will throw an IllegalArgumentException
    // On 2 it will throw a RuntimeException
    // On 3 it will exit normally
    // In each case it increments the Tester field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("new cn.taketoday.expression.spel.ConstructorInvocationTests$Tester(#bar).i");
    // Normal exit
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    eContext.setRootObject(new Tester());
    eContext.setVariable("bar", 3);
    Object o = expr.getValue(eContext);
    assertThat(o).isEqualTo(3);
    assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(1);
    // Now the expression has cached that throwException(int) is the right thing to
    // call. Let's change 'bar' to be a PlaceOfBirth which indicates the cached
    // reference is out of date.
    eContext.setVariable("bar", new PlaceOfBirth("London"));
    o = expr.getValue(eContext);
    assertThat(o).isEqualTo(0);
    // That confirms the logic to mark the cached reference stale and retry is working
    // Now let's cause the method to exit via exception and ensure it doesn't cause
    // a retry.
    // First, switch back to throwException(int)
    eContext.setVariable("bar", 3);
    o = expr.getValue(eContext);
    assertThat(o).isEqualTo(3);
    assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(2);
    // 4 will make it throw a checked exception - this will be wrapped by spel on the
    // way out
    eContext.setVariable("bar", 4);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext)).withMessageContaining("Tester");
    // A problem occurred whilst attempting to construct an object of type
    // 'cn.taketoday.expression.spel.ConstructorInvocationTests$Tester'
    // using arguments '(java.lang.Integer)'
    // If counter is 4 then the method got called twice!
    assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(3);
    // 1 will make it throw a RuntimeException - SpEL will let this through
    eContext.setVariable("bar", 1);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext)).isNotInstanceOf(SpelEvaluationException.class);
    // A problem occurred whilst attempting to construct an object of type
    // 'cn.taketoday.expression.spel.ConstructorInvocationTests$Tester'
    // using arguments '(java.lang.Integer)'
    // If counter is 5 then the method got called twice!
    assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(4);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) PlaceOfBirth(cn.taketoday.expression.spel.testresources.PlaceOfBirth) Test(org.junit.jupiter.api.Test)

Example 24 with Expression

use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_AddingYourOwnPropertyResolvers_1.

/**
 * Scenario: add a property resolver that will get called in the resolver chain, this one only supports reading.
 */
@Test
public void testScenario_AddingYourOwnPropertyResolvers_1() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.addPropertyAccessor(new FruitColourAccessor());
    Expression expr = parser.parseRaw("orange");
    Object value = expr.getValue(ctx);
    assertThat(value).isEqualTo(Color.orange);
    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 25 with Expression

use of cn.taketoday.expression.Expression in project today-infrastructure by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_DefiningVariablesThatWillBeAccessibleInExpressions.

/**
 * Scenario: using the standard context but adding your own variables
 */
@Test
public void testScenario_DefiningVariablesThatWillBeAccessibleInExpressions() throws Exception {
    // Create a parser
    SpelExpressionParser parser = new SpelExpressionParser();
    // Use the standard evaluation context
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("favouriteColour", "blue");
    List<Integer> primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17);
    ctx.setVariable("primes", primes);
    Expression expr = parser.parseRaw("#favouriteColour");
    Object value = expr.getValue(ctx);
    assertThat(value).isEqualTo("blue");
    expr = parser.parseRaw("#primes.get(1)");
    value = expr.getValue(ctx);
    assertThat(value).isEqualTo(3);
    // all prime numbers > 10 from the list (using selection ?{...})
    expr = parser.parseRaw("#primes.?[#this>10]");
    value = expr.getValue(ctx);
    assertThat(value.toString()).isEqualTo("[11, 13, 17]");
}
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