List of usage examples for org.springframework.util ReflectionUtils rethrowException
public static void rethrowException(Throwable ex) throws Exception
From source file:org.springframework.faces.mvc.annotation.support.AnnotatedMethodInvoker.java
/** * Invoke the specified method, ensuring that the method is accessible and that all exceptions are re-thrown * correctly.//from ww w .j a v a 2s.c o m */ private Object doInvokeMethod(Method method, Object target, Object[] args) throws Exception { ReflectionUtils.makeAccessible(method); try { return method.invoke(target, args); } catch (InvocationTargetException ex) { ReflectionUtils.rethrowException(ex.getTargetException()); } throw new IllegalStateException("Should never get here"); }
From source file:org.springframework.test.context.TestContextManager.java
/** * Hook for pre-processing a test class <em>before</em> execution of any * tests within the class. Should be called prior to any framework-specific * <em>before class methods</em> (e.g., methods annotated with JUnit 4's * {@link org.junit.BeforeClass @BeforeClass}). * <p>An attempt will be made to give each registered * {@link TestExecutionListener} a chance to pre-process the test class * execution. If a listener throws an exception, however, the remaining * registered listeners will <strong>not</strong> be called. * @throws Exception if a registered TestExecutionListener throws an * exception//w w w. j av a2 s . c o m * @since 3.0 * @see #getTestExecutionListeners() */ public void beforeTestClass() throws Exception { Class<?> testClass = getTestContext().getTestClass(); if (logger.isTraceEnabled()) { logger.trace("beforeTestClass(): class [" + testClass.getName() + "]"); } getTestContext().updateState(null, null, null); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.beforeTestClass(getTestContext()); } catch (Throwable ex) { logException(ex, "beforeTestClass", testExecutionListener, testClass); ReflectionUtils.rethrowException(ex); } } }
From source file:org.springframework.test.context.TestContextManager.java
/** * Hook for preparing a test instance prior to execution of any individual * test methods, for example for injecting dependencies, etc. Should be * called immediately after instantiation of the test instance. * <p>The managed {@link TestContext} will be updated with the supplied * {@code testInstance}.//from w ww .jav a2s . c o m * <p>An attempt will be made to give each registered * {@link TestExecutionListener} a chance to prepare the test instance. If a * listener throws an exception, however, the remaining registered listeners * will <strong>not</strong> be called. * @param testInstance the test instance to prepare (never {@code null}) * @throws Exception if a registered TestExecutionListener throws an exception * @see #getTestExecutionListeners() */ public void prepareTestInstance(Object testInstance) throws Exception { if (logger.isTraceEnabled()) { logger.trace("prepareTestInstance(): instance [" + testInstance + "]"); } getTestContext().updateState(testInstance, null, null); for (TestExecutionListener testExecutionListener : getTestExecutionListeners()) { try { testExecutionListener.prepareTestInstance(getTestContext()); } catch (Throwable ex) { if (logger.isErrorEnabled()) { logger.error("Caught exception while allowing TestExecutionListener [" + testExecutionListener + "] to prepare test instance [" + testInstance + "]", ex); } ReflectionUtils.rethrowException(ex); } } }
From source file:org.springframework.test.context.TestContextManager.java
/** * Hook for post-processing a test <em>immediately after</em> execution of * the {@linkplain java.lang.reflect.Method test method} in the supplied * {@linkplain TestContext test context} — for example, for timing * or logging purposes./* ww w. j a v a2 s . co m*/ * <p>This method <strong>must</strong> be called before framework-specific * <em>after</em> lifecycle callbacks (e.g., methods annotated with JUnit 4's * {@link org.junit.After @After}). * <p>The managed {@link TestContext} will be updated with the supplied * {@code testInstance}, {@code testMethod}, and {@code exception}. * <p>Each registered {@link TestExecutionListener} will be given a chance * to perform its post-processing. If a listener throws an exception, the * remaining registered listeners will still be called. After all listeners * have executed, the first caught exception will be rethrown with any * subsequent exceptions {@linkplain Throwable#addSuppressed suppressed} in * the first exception. * <p>Note that registered listeners will be executed in the opposite * order in which they were registered. * @param testInstance the current test instance (never {@code null}) * @param testMethod the test method which has just been executed on the * test instance * @param exception the exception that was thrown during execution of the * test method or by a TestExecutionListener, or {@code null} if none * was thrown * @throws Exception if a registered TestExecutionListener throws an exception * @since 5.0 * @see #beforeTestMethod * @see #afterTestMethod * @see #beforeTestExecution * @see #getTestExecutionListeners() * @see Throwable#addSuppressed(Throwable) */ public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { String callbackName = "afterTestExecution"; prepareForAfterCallback(callbackName, testInstance, testMethod, exception); Throwable afterTestExecutionException = null; // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { try { testExecutionListener.afterTestExecution(getTestContext()); } catch (Throwable ex) { logException(ex, callbackName, testExecutionListener, testInstance, testMethod); if (afterTestExecutionException == null) { afterTestExecutionException = ex; } else { afterTestExecutionException.addSuppressed(ex); } } } if (afterTestExecutionException != null) { ReflectionUtils.rethrowException(afterTestExecutionException); } }
From source file:org.springframework.test.context.TestContextManager.java
/** * Hook for post-processing a test <em>after</em> execution of <em>after</em> * lifecycle callbacks of the underlying test framework — for example, * tearing down test fixtures, ending a transaction, etc. * <p>This method <strong>must</strong> be called immediately after * framework-specific <em>after</em> lifecycle callbacks (e.g., methods * annotated with JUnit 4's {@link org.junit.After @After}). For historical * reasons, this method is named {@code afterTestMethod}. Since the * introduction of {@link #afterTestExecution}, a more suitable name for * this method might be something like {@code afterTestTearDown} or * {@code afterEach}; however, it is unfortunately impossible to rename * this method due to backward compatibility concerns. * <p>The managed {@link TestContext} will be updated with the supplied * {@code testInstance}, {@code testMethod}, and {@code exception}. * <p>Each registered {@link TestExecutionListener} will be given a chance * to perform its post-processing. If a listener throws an exception, the * remaining registered listeners will still be called. After all listeners * have executed, the first caught exception will be rethrown with any * subsequent exceptions {@linkplain Throwable#addSuppressed suppressed} in * the first exception.//from w ww .j a v a 2 s.c o m * <p>Note that registered listeners will be executed in the opposite * @param testInstance the current test instance (never {@code null}) * @param testMethod the test method which has just been executed on the * test instance * @param exception the exception that was thrown during execution of the * test method or by a TestExecutionListener, or {@code null} if none * was thrown * @throws Exception if a registered TestExecutionListener throws an exception * @see #beforeTestMethod * @see #beforeTestExecution * @see #afterTestExecution * @see #getTestExecutionListeners() * @see Throwable#addSuppressed(Throwable) */ public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { String callbackName = "afterTestMethod"; prepareForAfterCallback(callbackName, testInstance, testMethod, exception); Throwable afterTestMethodException = null; // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { try { testExecutionListener.afterTestMethod(getTestContext()); } catch (Throwable ex) { logException(ex, callbackName, testExecutionListener, testInstance, testMethod); if (afterTestMethodException == null) { afterTestMethodException = ex; } else { afterTestMethodException.addSuppressed(ex); } } } if (afterTestMethodException != null) { ReflectionUtils.rethrowException(afterTestMethodException); } }
From source file:org.springframework.test.context.TestContextManager.java
/** * Hook for post-processing a test class <em>after</em> execution of all * tests within the class. Should be called after any framework-specific * <em>after class methods</em> (e.g., methods annotated with JUnit 4's * {@link org.junit.AfterClass @AfterClass}). * <p>Each registered {@link TestExecutionListener} will be given a chance * to perform its post-processing. If a listener throws an exception, the * remaining registered listeners will still be called. After all listeners * have executed, the first caught exception will be rethrown with any * subsequent exceptions {@linkplain Throwable#addSuppressed suppressed} in * the first exception./*ww w . j a va2 s .c om*/ * <p>Note that registered listeners will be executed in the opposite * @throws Exception if a registered TestExecutionListener throws an exception * @since 3.0 * @see #getTestExecutionListeners() * @see Throwable#addSuppressed(Throwable) */ public void afterTestClass() throws Exception { Class<?> testClass = getTestContext().getTestClass(); if (logger.isTraceEnabled()) { logger.trace("afterTestClass(): class [" + testClass.getName() + "]"); } getTestContext().updateState(null, null, null); Throwable afterTestClassException = null; // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { try { testExecutionListener.afterTestClass(getTestContext()); } catch (Throwable ex) { logException(ex, "afterTestClass", testExecutionListener, testClass); if (afterTestClassException == null) { afterTestClassException = ex; } else { afterTestClassException.addSuppressed(ex); } } } this.testContextHolder.remove(); if (afterTestClassException != null) { ReflectionUtils.rethrowException(afterTestClassException); } }
From source file:org.springframework.test.context.TestContextManager.java
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) throws Exception { logException(ex, callbackName, testExecutionListener, testInstance, testMethod); ReflectionUtils.rethrowException(ex); }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Run all {@link BeforeTransaction @BeforeTransaction} methods for the * specified {@linkplain TestContext test context}. If one of the methods * fails, however, the caught exception will be rethrown in a wrapped * {@link RuntimeException}, and the remaining methods will <strong>not</strong> * be given a chance to execute.//ww w .j av a 2 s . c om * @param testContext the current test context */ protected void runBeforeTransactionMethods(TestContext testContext) throws Exception { try { List<Method> methods = getAnnotatedMethods(testContext.getTestClass(), BeforeTransaction.class); Collections.reverse(methods); for (Method method : methods) { if (logger.isDebugEnabled()) { logger.debug( "Executing @BeforeTransaction method [" + method + "] for test context " + testContext); } ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } } catch (InvocationTargetException ex) { if (logger.isErrorEnabled()) { logger.error("Exception encountered while executing @BeforeTransaction methods for test context " + testContext + ".", ex.getTargetException()); } ReflectionUtils.rethrowException(ex.getTargetException()); } }
From source file:org.springframework.test.context.transaction.TransactionalTestExecutionListener.java
/** * Run all {@link AfterTransaction @AfterTransaction} methods for the * specified {@linkplain TestContext test context}. If one of the methods * fails, the caught exception will be logged as an error, and the remaining * methods will be given a chance to execute. After all methods have * executed, the first caught exception, if any, will be rethrown. * @param testContext the current test context *//* www . j a v a 2 s . c o m*/ protected void runAfterTransactionMethods(TestContext testContext) throws Exception { Throwable afterTransactionException = null; List<Method> methods = getAnnotatedMethods(testContext.getTestClass(), AfterTransaction.class); for (Method method : methods) { try { if (logger.isDebugEnabled()) { logger.debug( "Executing @AfterTransaction method [" + method + "] for test context " + testContext); } ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } catch (InvocationTargetException ex) { Throwable targetException = ex.getTargetException(); if (afterTransactionException == null) { afterTransactionException = targetException; } logger.error("Exception encountered while executing @AfterTransaction method [" + method + "] for test context " + testContext, targetException); } catch (Exception ex) { if (afterTransactionException == null) { afterTransactionException = ex; } logger.error("Exception encountered while executing @AfterTransaction method [" + method + "] for test context " + testContext, ex); } } if (afterTransactionException != null) { ReflectionUtils.rethrowException(afterTransactionException); } }
From source file:org.springframework.web.bind.annotation.support.HandlerMethodInvoker.java
public final Object invokeHandlerMethod(Method handlerMethod, Object handler, NativeWebRequest webRequest, ExtendedModelMap implicitModel) throws Exception { Method handlerMethodToInvoke = BridgeMethodResolver.findBridgedMethod(handlerMethod); try {/*w ww.jav a 2s .co m*/ boolean debug = logger.isDebugEnabled(); for (String attrName : this.methodResolver.getActualSessionAttributeNames()) { Object attrValue = this.sessionAttributeStore.retrieveAttribute(webRequest, attrName); if (attrValue != null) { implicitModel.addAttribute(attrName, attrValue); } } for (Method attributeMethod : this.methodResolver.getModelAttributeMethods()) { Method attributeMethodToInvoke = BridgeMethodResolver.findBridgedMethod(attributeMethod); Object[] args = resolveHandlerArguments(attributeMethodToInvoke, handler, webRequest, implicitModel); if (debug) { logger.debug("Invoking model attribute method: " + attributeMethodToInvoke); } String attrName = AnnotationUtils.findAnnotation(attributeMethod, ModelAttribute.class).value(); if (!"".equals(attrName) && implicitModel.containsAttribute(attrName)) { continue; } ReflectionUtils.makeAccessible(attributeMethodToInvoke); Object attrValue = attributeMethodToInvoke.invoke(handler, args); if ("".equals(attrName)) { Class<?> resolvedType = GenericTypeResolver.resolveReturnType(attributeMethodToInvoke, handler.getClass()); attrName = Conventions.getVariableNameForReturnType(attributeMethodToInvoke, resolvedType, attrValue); } if (!implicitModel.containsAttribute(attrName)) { implicitModel.addAttribute(attrName, attrValue); } } Object[] args = resolveHandlerArguments(handlerMethodToInvoke, handler, webRequest, implicitModel); if (debug) { logger.debug("Invoking request handler method: " + handlerMethodToInvoke); } ReflectionUtils.makeAccessible(handlerMethodToInvoke); return handlerMethodToInvoke.invoke(handler, args); } catch (IllegalStateException ex) { // Internal assertion failed (e.g. invalid signature): // throw exception with full handler method context... throw new HandlerMethodInvocationException(handlerMethodToInvoke, ex); } catch (InvocationTargetException ex) { // User-defined @ModelAttribute/@InitBinder/@RequestMapping method threw an exception... ReflectionUtils.rethrowException(ex.getTargetException()); return null; } }