|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
21 | 22 |
|
22 | 23 | import org.aopalliance.intercept.MethodInvocation;
|
23 | 24 | import org.junit.jupiter.api.Test;
|
|
29 | 30 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
30 | 31 | import org.springframework.core.annotation.AnnotationUtils;
|
31 | 32 | import org.springframework.dao.DataAccessException;
|
| 33 | +import org.springframework.dao.support.ChainedPersistenceExceptionTranslator; |
32 | 34 | import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
|
33 | 35 | import org.springframework.dao.support.PersistenceExceptionTranslator;
|
34 | 36 | import org.springframework.stereotype.Repository;
|
@@ -78,10 +80,37 @@ void detectPersistenceExceptionTranslators() throws Throwable {
|
78 | 80 | given(invocation.proceed()).willThrow(exception);
|
79 | 81 |
|
80 | 82 | assertThatThrownBy(() -> interceptor.invoke(invocation)).isSameAs(exception);
|
81 |
| - |
82 | 83 | assertThat(callOrder).containsExactly(10, 20, 30);
|
83 | 84 | }
|
84 | 85 |
|
| 86 | + @Test |
| 87 | + void detectPersistenceExceptionTranslatorsOnShutdown() throws Throwable { |
| 88 | + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); |
| 89 | + bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); |
| 90 | + bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class)); |
| 91 | + bf.registerBeanDefinition("pet", new RootBeanDefinition(ChainedPersistenceExceptionTranslator.class)); |
| 92 | + |
| 93 | + PersistenceExceptionTranslationInterceptor interceptor = |
| 94 | + bf.getBean("peti", PersistenceExceptionTranslationInterceptor.class); |
| 95 | + interceptor.setAlwaysTranslate(true); |
| 96 | + |
| 97 | + RuntimeException exception = new RuntimeException(); |
| 98 | + MethodInvocation invocation = mock(); |
| 99 | + given(invocation.proceed()).willThrow(exception); |
| 100 | + |
| 101 | + AtomicBoolean correctException = new AtomicBoolean(false); |
| 102 | + bf.registerDisposableBean("disposable", () -> { |
| 103 | + try { |
| 104 | + interceptor.invoke(invocation); |
| 105 | + } |
| 106 | + catch (Throwable ex) { |
| 107 | + correctException.set(ex == exception); |
| 108 | + } |
| 109 | + }); |
| 110 | + bf.destroySingletons(); |
| 111 | + assertThat(correctException).isTrue(); |
| 112 | + } |
| 113 | + |
85 | 114 |
|
86 | 115 | private static class CallOrderAwareExceptionTranslator implements PersistenceExceptionTranslator, Ordered {
|
87 | 116 |
|
|
0 commit comments