Search in sources :

Example 1 with ExpressionInvocationTargetException

use of org.springframework.expression.ExpressionInvocationTargetException in project spring-framework by spring-projects.

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)");
    eContext.setVariable("bar", 4);
    try {
        expr.getValue(eContext);
        fail();
    } catch (ExpressionInvocationTargetException ex) {
        Throwable cause = ex.getCause();
        assertEquals("org.springframework.expression.spel.testresources.Inventor$TestException", cause.getClass().getName());
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) Test(org.junit.Test)

Example 2 with ExpressionInvocationTargetException

use of org.springframework.expression.ExpressionInvocationTargetException in project spring-framework by spring-projects.

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);
    assertEquals(o, 3);
    assertEquals(1, parser.parseExpression("counter").getValue(eContext));
    // 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);
    assertEquals("London", o);
    // 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);
    assertEquals(3, o);
    assertEquals(2, parser.parseExpression("counter").getValue(eContext));
    // Now cause it to throw an exception:
    eContext.setVariable("bar", 1);
    try {
        o = expr.getValue(eContext);
        fail();
    } catch (Exception ex) {
        if (ex instanceof SpelEvaluationException) {
            fail("Should not be a SpelEvaluationException: " + ex);
        }
    // normal
    }
    // If counter is 4 then the method got called twice!
    assertEquals(3, parser.parseExpression("counter").getValue(eContext));
    eContext.setVariable("bar", 4);
    try {
        o = expr.getValue(eContext);
        fail();
    } catch (Exception ex) {
        // 4 means it will throw a checked exception - this will be wrapped
        if (!(ex instanceof ExpressionInvocationTargetException)) {
            fail("Should have been wrapped: " + ex);
        }
    // normal
    }
    // If counter is 5 then the method got called twice!
    assertEquals(4, parser.parseExpression("counter").getValue(eContext));
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Expression(org.springframework.expression.Expression) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) AccessException(org.springframework.expression.AccessException) ExpressionInvocationTargetException(org.springframework.expression.ExpressionInvocationTargetException) PlaceOfBirth(org.springframework.expression.spel.testresources.PlaceOfBirth) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 Expression (org.springframework.expression.Expression)2 ExpressionInvocationTargetException (org.springframework.expression.ExpressionInvocationTargetException)2 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)2 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)2 AccessException (org.springframework.expression.AccessException)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 PlaceOfBirth (org.springframework.expression.spel.testresources.PlaceOfBirth)1