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);
}
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"));
}
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);
}
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);
}
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");
}
Aggregations