@Test public void handleExceptionWithListenableFuture() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithExceptionHandler.class); ITestBean testBean = context.getBean("target", ITestBean.class); TestableAsyncUncaughtExceptionHandler exceptionHandler = context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); Future<Object> result = testBean.failWithListenableFuture(); assertFutureWithException(result, exceptionHandler); }
@Test public void proxyCreated() { ConfigurableApplicationContext context = initContext( new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); Object target = context.getBean("target"); assertTrue(AopUtils.isAopProxy(target)); context.close(); }
@Test public void invokedAsynchronously() { ConfigurableApplicationContext context = initContext( new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class)); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); Thread mainThread = Thread.currentThread(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertNotSame(mainThread, asyncThread); context.close(); }
@Test public void handleExceptionWithFuture() { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithExceptionHandler.class); ITestBean testBean = context.getBean("target", ITestBean.class); TestableAsyncUncaughtExceptionHandler exceptionHandler = context.getBean("exceptionHandler", TestableAsyncUncaughtExceptionHandler.class); assertFalse("handler should not have been called yet", exceptionHandler.isCalled()); Future<Object> result = testBean.failWithFuture(); assertFutureWithException(result, exceptionHandler); }
@Test public void threadNamePrefix() { BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setThreadNamePrefix("testExecutor"); executor.afterPropertiesSet(); processorDefinition.getPropertyValues().add("executor", executor); ConfigurableApplicationContext context = initContext(processorDefinition); ITestBean testBean = context.getBean("target", ITestBean.class); testBean.test(); testBean.await(3000); Thread asyncThread = testBean.getThread(); assertTrue(asyncThread.getName().startsWith("testExecutor")); context.close(); }
@Test public void handleExceptionWithCustomExceptionHandler() { Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler); ConfigurableApplicationContext context = initContext(processorDefinition); ITestBean testBean = context.getBean("target", ITestBean.class); assertFalse("Handler should not have been called", exceptionHandler.isCalled()); testBean.failWithVoid(); exceptionHandler.await(3000); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); }
@Test public void exceptionHandlerThrowsUnexpectedException() { Method m = ReflectionUtils.findMethod(TestBean.class, "failWithVoid"); TestableAsyncUncaughtExceptionHandler exceptionHandler = new TestableAsyncUncaughtExceptionHandler(true); BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class); processorDefinition.getPropertyValues().add("exceptionHandler", exceptionHandler); processorDefinition.getPropertyValues().add("executor", new DirectExecutor()); ConfigurableApplicationContext context = initContext(processorDefinition); ITestBean testBean = context.getBean("target", ITestBean.class); assertFalse("Handler should not have been called", exceptionHandler.isCalled()); try { testBean.failWithVoid(); exceptionHandler.assertCalledWith(m, UnsupportedOperationException.class); } catch (Exception e) { fail("No unexpected exception should have been received"); } }