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
- * Tests for standalone usage of a PersistenceExceptionTranslationInterceptor, as explicit advice bean in a BeanFactory
29
- * rather than applied as part of a PersistenceExceptionTranslationAdvisor.
42
+ * Tests for standalone usage of a PersistenceExceptionTranslationInterceptor,
43
+ * as explicit advice bean in a BeanFactory rather than applied as part of a
44
+ * PersistenceExceptionTranslationAdvisor.
30
45
*
31
46
* @author Juergen Hoeller
47
+ * @author Tadaya Tsuyukubo
32
48
*/
33
49
public class PersistenceExceptionTranslationInterceptorTests extends PersistenceExceptionTranslationAdvisorTests {
34
50
@@ -42,4 +58,52 @@ protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceEx
42
58
}
43
59
}
44
60
61
+ @ Test
62
+ void detectPersistenceExceptionTranslators () throws Throwable {
63
+ DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
64
+ bf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
65
+ bf .registerBeanDefinition ("peti" , new RootBeanDefinition (PersistenceExceptionTranslationInterceptor .class ));
66
+
67
+ List <Integer > callOrder = new ArrayList <>();
68
+ bf .registerSingleton ("pet20" , new CallOrderAwareExceptionTranslator (20 , callOrder ));
69
+ bf .registerSingleton ("pet10" , new CallOrderAwareExceptionTranslator (10 , callOrder ));
70
+ bf .registerSingleton ("pet30" , new CallOrderAwareExceptionTranslator (30 , callOrder ));
71
+
72
+ PersistenceExceptionTranslationInterceptor interceptor =
73
+ bf .getBean ("peti" , PersistenceExceptionTranslationInterceptor .class );
74
+ interceptor .setAlwaysTranslate (true );
75
+
76
+ RuntimeException exception = new RuntimeException ();
77
+ MethodInvocation invocation = mock (MethodInvocation .class );
78
+ given (invocation .proceed ()).willThrow (exception );
79
+
80
+ assertThatThrownBy (() -> interceptor .invoke (invocation )).isSameAs (exception );
81
+
82
+ assertThat (callOrder ).containsExactly (10 , 20 , 30 );
83
+ }
84
+
85
+
86
+ private static class CallOrderAwareExceptionTranslator implements PersistenceExceptionTranslator , Ordered {
87
+
88
+ private final int order ;
89
+
90
+ private final List <Integer > callOrder ;
91
+
92
+ public CallOrderAwareExceptionTranslator (int order , List <Integer > callOrder ) {
93
+ this .order = order ;
94
+ this .callOrder = callOrder ;
95
+ }
96
+
97
+ @ Override
98
+ public DataAccessException translateExceptionIfPossible (RuntimeException ex ) {
99
+ callOrder .add (this .order );
100
+ return null ;
101
+ }
102
+
103
+ @ Override
104
+ public int getOrder () {
105
+ return this .order ;
106
+ }
107
+ }
108
+
45
109
}
0 commit comments