|
3 | 3 | import static org.junit.Assert.assertEquals; |
4 | 4 | import static org.junit.Assert.assertNull; |
5 | 5 | import static org.junit.Assert.assertTrue; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
6 | 7 | import static org.mockito.Mockito.when; |
7 | 8 |
|
8 | 9 | import com.bugsnag.testapp.springboot.TestSpringBootApplication; |
9 | 10 |
|
| 11 | +import org.aopalliance.intercept.MethodInterceptor; |
10 | 12 | import org.junit.Before; |
11 | 13 | import org.junit.Test; |
12 | 14 | import org.junit.runner.RunWith; |
13 | 15 |
|
14 | 16 | import org.mockito.Mock; |
15 | | - |
| 17 | +import org.springframework.aop.framework.ProxyFactory; |
16 | 18 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException; |
17 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
18 | 20 | import org.springframework.boot.test.context.SpringBootTest; |
@@ -113,6 +115,31 @@ public void findExecutorByName() throws NoSuchFieldException, IllegalAccessExcep |
113 | 115 | assertEquals(expected, accessField(scheduler, "scheduledExecutor")); |
114 | 116 | } |
115 | 117 |
|
| 118 | + @Test |
| 119 | + public void configureTasks_withProxyWrappedRegistrar() throws NoSuchFieldException, IllegalAccessException { |
| 120 | + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); |
| 121 | + when(context.getBean(TaskScheduler.class)).thenReturn(scheduler); |
| 122 | + TaskScheduler proxyScheduler = createProxy(scheduler); |
| 123 | + registrar.setScheduler(proxyScheduler); |
| 124 | + Object errorHandler = accessField(scheduler, "errorHandler"); |
| 125 | + assertFalse( |
| 126 | + errorHandler instanceof BugsnagScheduledTaskExceptionHandler, |
| 127 | + "errorHandler should not be BugsnagScheduledTaskExceptionHandler" |
| 128 | + ); |
| 129 | + configuration.configureTasks(registrar); |
| 130 | + errorHandler = accessField(scheduler, "errorHandler"); |
| 131 | + assertTrue( |
| 132 | + "errorHandler should be BugsnagScheduledTaskExceptionHandler", |
| 133 | + errorHandler instanceof BugsnagScheduledTaskExceptionHandler |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + private TaskScheduler createProxy(TaskScheduler target) { |
| 138 | + ProxyFactory factory = new ProxyFactory(target); |
| 139 | + factory.addAdvice((MethodInterceptor) invocation -> invocation.proceed()); |
| 140 | + return (TaskScheduler) factory.getProxy(); |
| 141 | + } |
| 142 | + |
116 | 143 | private Object accessField(Object object, String fieldName) |
117 | 144 | throws NoSuchFieldException, IllegalAccessException { |
118 | 145 | Field field = object.getClass().getDeclaredField(fieldName); |
|
0 commit comments