Search in sources :

Example 11 with Expression

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

the class SelectionAndProjectionTests method selectLastItemInPrimitiveArray.

@Test
void selectLastItemInPrimitiveArray() throws Exception {
    Expression expression = new SpelExpressionParser().parseRaw("ints.$[#this<5]");
    EvaluationContext context = new StandardEvaluationContext(new ArrayTestBean());
    Object value = expression.getValue(context);
    assertThat(value).isInstanceOf(Integer.class);
    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) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) EvaluationContext(cn.taketoday.expression.EvaluationContext) Test(org.junit.jupiter.api.Test)

Example 12 with Expression

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

the class MethodInvocationTests method testMethodThrowingException_SPR6941_2.

@Test
public void testMethodThrowingException_SPR6941_2() {
    // Test method on inventor: throwException()
    // 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 Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    context.setVariable("bar", 4);
    assertThatExceptionOfType(ExpressionInvocationTargetException.class).isThrownBy(() -> expr.getValue(context)).satisfies(ex -> assertThat(ex.getCause().getClass().getName()).isEqualTo("cn.taketoday.expression.spel.testresources.Inventor$TestException"));
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 13 with Expression

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

the class MethodInvocationTests method testMethodThrowingException_SPR6760.

@Test
public void testMethodThrowingException_SPR6760() {
    // Test method on inventor: throwException()
    // 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 Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    // Normal exit
    StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
    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("London");
    // 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);
    // Now cause it to throw an exception:
    eContext.setVariable("bar", 1);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext)).isNotInstanceOf(SpelEvaluationException.class);
    // If counter is 4 then the method got called twice!
    assertThat(parser.parseExpression("counter").getValue(eContext)).isEqualTo(3);
    eContext.setVariable("bar", 4);
    assertThatExceptionOfType(ExpressionInvocationTargetException.class).isThrownBy(() -> expr.getValue(eContext));
    // 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) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) ExpressionInvocationTargetException(cn.taketoday.expression.ExpressionInvocationTargetException) PlaceOfBirth(cn.taketoday.expression.spel.testresources.PlaceOfBirth) Test(org.junit.jupiter.api.Test)

Example 14 with Expression

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

the class MethodInvocationTests method testMethodThrowingException_SPR6941.

/**
 * Check on first usage (when the cachedExecutor in MethodReference is null) that the exception is not wrapped.
 */
@Test
public void testMethodThrowingException_SPR6941() {
    // Test method on inventor: throwException()
    // 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 Inventor field 'counter' when invoked
    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression("throwException(#bar)");
    context.setVariable("bar", 2);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(context)).isNotInstanceOf(SpelEvaluationException.class);
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 15 with Expression

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

the class PropertyAccessTests method accessingPropertyOfClass.

@Test
void accessingPropertyOfClass() {
    Expression expression = parser.parseExpression("name");
    Object value = expression.getValue(new StandardEvaluationContext(String.class));
    assertThat(value).isEqualTo("java.lang.String");
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) 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