1
1
/*
2
- * Copyright 2002-2007 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .dao .annotation ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .List ;
21
+
22
+ import org .aopalliance .intercept .MethodInvocation ;
23
+ import org .junit .jupiter .api .Test ;
24
+
19
25
import org .springframework .aop .framework .ProxyFactory ;
20
26
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
21
27
import org .springframework .beans .factory .support .RootBeanDefinition ;
28
+ import org .springframework .core .Ordered ;
29
+ import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
22
30
import org .springframework .core .annotation .AnnotationUtils ;
31
+ import org .springframework .dao .DataAccessException ;
23
32
import org .springframework .dao .support .PersistenceExceptionTranslationInterceptor ;
24
33
import org .springframework .dao .support .PersistenceExceptionTranslator ;
25
34
import org .springframework .stereotype .Repository ;
26
35
36
+ import static org .assertj .core .api .Assertions .assertThat ;
37
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
38
+ import static org .mockito .BDDMockito .given ;
39
+ import static org .mockito .Mockito .mock ;
40
+
27
41
/**
28
42
* Tests for standalone usage of a PersistenceExceptionTranslationInterceptor, as explicit advice bean in a BeanFactory
29
43
* rather than applied as part of a PersistenceExceptionTranslationAdvisor.
30
44
*
31
45
* @author Juergen Hoeller
46
+ * @author Tadaya Tsuyukubo
32
47
*/
33
48
public class PersistenceExceptionTranslationInterceptorTests extends PersistenceExceptionTranslationAdvisorTests {
34
49
@@ -42,4 +57,50 @@ protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceEx
42
57
}
43
58
}
44
59
60
+ @ Test
61
+ void detectPersistenceExceptionTranslators () throws Throwable {
62
+ DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
63
+ bf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
64
+ bf .registerBeanDefinition ("peti" , new RootBeanDefinition (PersistenceExceptionTranslationInterceptor .class ));
65
+
66
+ List <Integer > callOrder = new ArrayList <>();
67
+ bf .registerSingleton ("pet20" , new CallOrderAwareExceptionTranslator (20 , callOrder ));
68
+ bf .registerSingleton ("pet10" , new CallOrderAwareExceptionTranslator (10 , callOrder ));
69
+ bf .registerSingleton ("pet30" , new CallOrderAwareExceptionTranslator (30 , callOrder ));
70
+
71
+ PersistenceExceptionTranslationInterceptor interceptor = bf .getBean ("peti" , PersistenceExceptionTranslationInterceptor .class );
72
+ interceptor .setAlwaysTranslate (true );
73
+
74
+ RuntimeException exception = new RuntimeException ();
75
+ MethodInvocation invocation = mock (MethodInvocation .class );
76
+ given (invocation .proceed ()).willThrow (exception );
77
+
78
+ assertThatThrownBy (() -> interceptor .invoke (invocation )).isSameAs (exception );
79
+
80
+ assertThat (callOrder ).containsExactly (10 , 20 , 30 );
81
+ }
82
+
83
+ private static class CallOrderAwareExceptionTranslator implements PersistenceExceptionTranslator , Ordered {
84
+
85
+ private final int order ;
86
+
87
+ private final List <Integer > callOrder ;
88
+
89
+ public CallOrderAwareExceptionTranslator (int order , List <Integer > callOrder ) {
90
+ this .order = order ;
91
+ this .callOrder = callOrder ;
92
+ }
93
+
94
+ @ Override
95
+ public DataAccessException translateExceptionIfPossible (RuntimeException ex ) {
96
+ callOrder .add (this .order );
97
+ return null ;
98
+ }
99
+
100
+ @ Override
101
+ public int getOrder () {
102
+ return this .order ;
103
+ }
104
+ }
105
+
45
106
}
0 commit comments