1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 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 javax .persistence .PersistenceException ;
20
+
19
21
import junit .framework .TestCase ;
20
22
import org .aspectj .lang .JoinPoint ;
21
23
import org .aspectj .lang .annotation .Aspect ;
25
27
import org .springframework .aop .aspectj .annotation .AnnotationAwareAspectJAutoProxyCreator ;
26
28
import org .springframework .aop .framework .Advised ;
27
29
import org .springframework .aop .support .AopUtils ;
28
- import org .springframework .beans .BeansException ;
29
30
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
30
31
import org .springframework .beans .factory .support .RootBeanDefinition ;
31
32
import org .springframework .context .support .GenericApplicationContext ;
33
+ import org .springframework .dao .DataAccessException ;
34
+ import org .springframework .dao .DataAccessResourceFailureException ;
32
35
import org .springframework .dao .annotation .PersistenceExceptionTranslationAdvisorTests .RepositoryInterface ;
33
36
import org .springframework .dao .annotation .PersistenceExceptionTranslationAdvisorTests .RepositoryInterfaceImpl ;
34
37
import org .springframework .dao .annotation .PersistenceExceptionTranslationAdvisorTests .StereotypedRepositoryInterfaceImpl ;
35
- import org .springframework .dao .support .ChainedPersistenceExceptionTranslator ;
38
+ import org .springframework .dao .support .PersistenceExceptionTranslator ;
36
39
import org .springframework .stereotype .Repository ;
37
40
38
41
/**
39
- * Unit tests for PersistenceExceptionTranslationPostProcessor. Does not test translation; there are separate unit tests
40
- * for the Spring AOP Advisor. Just checks whether proxying occurs correctly, as a unit test should.
41
- *
42
42
* @author Rod Johnson
43
+ * @author Juergen Hoeller
43
44
*/
44
45
public class PersistenceExceptionTranslationPostProcessorTests extends TestCase {
45
46
46
- public void testFailsWithNoPersistenceExceptionTranslators () {
47
- GenericApplicationContext gac = new GenericApplicationContext ();
48
- gac .registerBeanDefinition ("translator" ,
49
- new RootBeanDefinition (PersistenceExceptionTranslationPostProcessor .class ));
50
- gac .registerBeanDefinition ("proxied" , new RootBeanDefinition (StereotypedRepositoryInterfaceImpl .class ));
51
- try {
52
- gac .refresh ();
53
- fail ("Should fail with no translators" );
54
- }
55
- catch (BeansException ex ) {
56
- // Ok
57
- }
58
- }
59
-
60
47
public void testProxiesCorrectly () {
61
48
GenericApplicationContext gac = new GenericApplicationContext ();
62
49
gac .registerBeanDefinition ("translator" ,
@@ -66,8 +53,8 @@ public void testProxiesCorrectly() {
66
53
gac .registerBeanDefinition ("classProxied" , new RootBeanDefinition (RepositoryWithoutInterface .class ));
67
54
gac .registerBeanDefinition ("classProxiedAndAdvised" ,
68
55
new RootBeanDefinition (RepositoryWithoutInterfaceAndOtherwiseAdvised .class ));
69
- gac .registerBeanDefinition ("chainedTranslator " ,
70
- new RootBeanDefinition (ChainedPersistenceExceptionTranslator .class ));
56
+ gac .registerBeanDefinition ("myTranslator " ,
57
+ new RootBeanDefinition (MyPersistenceExceptionTranslator .class ));
71
58
gac .registerBeanDefinition ("proxyCreator" ,
72
59
BeanDefinitionBuilder .rootBeanDefinition (AnnotationAwareAspectJAutoProxyCreator .class ).
73
60
addPropertyValue ("order" , 50 ).getBeanDefinition ());
@@ -84,8 +71,15 @@ public void testProxiesCorrectly() {
84
71
85
72
Additional rwi2 = (Additional ) gac .getBean ("classProxiedAndAdvised" );
86
73
assertTrue (AopUtils .isAopProxy (rwi2 ));
87
- rwi2 .additionalMethod ();
74
+ rwi2 .additionalMethod (false );
88
75
checkWillTranslateExceptions (rwi2 );
76
+ try {
77
+ rwi2 .additionalMethod (true );
78
+ fail ("Should have thrown DataAccessResourceFailureException" );
79
+ }
80
+ catch (DataAccessResourceFailureException ex ) {
81
+ assertEquals ("my failure" , ex .getMessage ());
82
+ }
89
83
}
90
84
91
85
protected void checkWillTranslateExceptions (Object o ) {
@@ -99,26 +93,46 @@ protected void checkWillTranslateExceptions(Object o) {
99
93
fail ("No translation" );
100
94
}
101
95
96
+
102
97
@ Repository
103
98
public static class RepositoryWithoutInterface {
104
99
105
100
public void nameDoesntMatter () {
106
101
}
107
102
}
108
103
104
+
109
105
public interface Additional {
110
106
111
- void additionalMethod ();
107
+ void additionalMethod (boolean fail );
112
108
}
113
109
110
+
114
111
public static class RepositoryWithoutInterfaceAndOtherwiseAdvised extends StereotypedRepositoryInterfaceImpl
115
112
implements Additional {
116
113
117
114
@ Override
118
- public void additionalMethod () {
115
+ public void additionalMethod (boolean fail ) {
116
+ if (fail ) {
117
+ throw new PersistenceException ("my failure" );
118
+ }
119
119
}
120
120
}
121
121
122
+
123
+ public static class MyPersistenceExceptionTranslator implements PersistenceExceptionTranslator {
124
+
125
+
126
+ @ Override
127
+ public DataAccessException translateExceptionIfPossible (RuntimeException ex ) {
128
+ if (ex instanceof PersistenceException ) {
129
+ return new DataAccessResourceFailureException (ex .getMessage ());
130
+ }
131
+ return null ;
132
+ }
133
+ }
134
+
135
+
122
136
@ Aspect
123
137
public static class LogAllAspect {
124
138
0 commit comments