Skip to content

Commit 665b284

Browse files
committed
Added some leftover tests
1 parent d752917 commit 665b284

File tree

3 files changed

+377
-0
lines changed

3 files changed

+377
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.dao.annotation;
18+
19+
import javax.persistence.PersistenceException;
20+
21+
import junit.framework.TestCase;
22+
23+
import org.springframework.aop.framework.ProxyFactory;
24+
import org.springframework.dao.DataAccessException;
25+
import org.springframework.dao.InvalidDataAccessApiUsageException;
26+
import org.springframework.dao.support.DataAccessUtilsTests.MapPersistenceExceptionTranslator;
27+
import org.springframework.dao.support.PersistenceExceptionTranslator;
28+
import org.springframework.stereotype.Repository;
29+
30+
/**
31+
* Tests for PersistenceExceptionTranslationAdvisor's exception translation, as applied by
32+
* PersistenceExceptionTranslationPostProcessor.
33+
*
34+
* @author Rod Johnson
35+
* @author Juergen Hoeller
36+
*/
37+
public class PersistenceExceptionTranslationAdvisorTests extends TestCase {
38+
39+
private RuntimeException doNotTranslate = new RuntimeException();
40+
41+
private PersistenceException persistenceException1 = new PersistenceException();
42+
43+
protected RepositoryInterface createProxy(RepositoryInterfaceImpl target) {
44+
MapPersistenceExceptionTranslator mpet = new MapPersistenceExceptionTranslator();
45+
mpet.addTranslation(persistenceException1, new InvalidDataAccessApiUsageException("", persistenceException1));
46+
ProxyFactory pf = new ProxyFactory(target);
47+
pf.addInterface(RepositoryInterface.class);
48+
addPersistenceExceptionTranslation(pf, mpet);
49+
return (RepositoryInterface) pf.getProxy();
50+
}
51+
52+
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
53+
pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
54+
}
55+
56+
public void testNoTranslationNeeded() {
57+
RepositoryInterfaceImpl target = new RepositoryInterfaceImpl();
58+
RepositoryInterface ri = createProxy(target);
59+
60+
ri.noThrowsClause();
61+
ri.throwsPersistenceException();
62+
63+
target.setBehavior(persistenceException1);
64+
try {
65+
ri.noThrowsClause();
66+
fail();
67+
}
68+
catch (RuntimeException ex) {
69+
assertSame(persistenceException1, ex);
70+
}
71+
try {
72+
ri.throwsPersistenceException();
73+
fail();
74+
}
75+
catch (RuntimeException ex) {
76+
assertSame(persistenceException1, ex);
77+
}
78+
}
79+
80+
public void testTranslationNotNeededForTheseExceptions() {
81+
RepositoryInterfaceImpl target = new StereotypedRepositoryInterfaceImpl();
82+
RepositoryInterface ri = createProxy(target);
83+
84+
ri.noThrowsClause();
85+
ri.throwsPersistenceException();
86+
87+
target.setBehavior(doNotTranslate);
88+
try {
89+
ri.noThrowsClause();
90+
fail();
91+
}
92+
catch (RuntimeException ex) {
93+
assertSame(doNotTranslate, ex);
94+
}
95+
try {
96+
ri.throwsPersistenceException();
97+
fail();
98+
}
99+
catch (RuntimeException ex) {
100+
assertSame(doNotTranslate, ex);
101+
}
102+
}
103+
104+
public void testTranslationNeededForTheseExceptions() {
105+
doTestTranslationNeededForTheseExceptions(new StereotypedRepositoryInterfaceImpl());
106+
}
107+
108+
public void testTranslationNeededForTheseExceptionsOnSuperclass() {
109+
doTestTranslationNeededForTheseExceptions(new MyStereotypedRepositoryInterfaceImpl());
110+
}
111+
112+
public void testTranslationNeededForTheseExceptionsOnInterface() {
113+
doTestTranslationNeededForTheseExceptions(new MyInterfaceStereotypedRepositoryInterfaceImpl());
114+
}
115+
116+
public void testTranslationNeededForTheseExceptionsOnInheritedInterface() {
117+
doTestTranslationNeededForTheseExceptions(new MyInterfaceInheritedStereotypedRepositoryInterfaceImpl());
118+
}
119+
120+
private void doTestTranslationNeededForTheseExceptions(RepositoryInterfaceImpl target) {
121+
RepositoryInterface ri = createProxy(target);
122+
123+
target.setBehavior(persistenceException1);
124+
try {
125+
ri.noThrowsClause();
126+
fail();
127+
}
128+
catch (DataAccessException ex) {
129+
// Expected
130+
assertSame(persistenceException1, ex.getCause());
131+
}
132+
catch (PersistenceException ex) {
133+
fail("Should have been translated");
134+
}
135+
136+
try {
137+
ri.throwsPersistenceException();
138+
fail();
139+
}
140+
catch (PersistenceException ex) {
141+
assertSame(persistenceException1, ex);
142+
}
143+
}
144+
145+
public interface RepositoryInterface {
146+
147+
void noThrowsClause();
148+
149+
void throwsPersistenceException() throws PersistenceException;
150+
}
151+
152+
public static class RepositoryInterfaceImpl implements RepositoryInterface {
153+
154+
private RuntimeException runtimeException;
155+
156+
public void setBehavior(RuntimeException rex) {
157+
this.runtimeException = rex;
158+
}
159+
160+
public void noThrowsClause() {
161+
if (runtimeException != null) {
162+
throw runtimeException;
163+
}
164+
}
165+
166+
public void throwsPersistenceException() throws PersistenceException {
167+
if (runtimeException != null) {
168+
throw runtimeException;
169+
}
170+
}
171+
}
172+
173+
@Repository
174+
public static class StereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl {
175+
// Extends above class just to add repository annotation
176+
}
177+
178+
public static class MyStereotypedRepositoryInterfaceImpl extends StereotypedRepositoryInterfaceImpl {
179+
180+
}
181+
182+
@Repository
183+
public interface StereotypedInterface {
184+
185+
}
186+
187+
public static class MyInterfaceStereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl
188+
implements StereotypedInterface {
189+
190+
}
191+
192+
public interface StereotypedInheritingInterface extends StereotypedInterface {
193+
194+
}
195+
196+
public static class MyInterfaceInheritedStereotypedRepositoryInterfaceImpl extends RepositoryInterfaceImpl
197+
implements StereotypedInheritingInterface {
198+
199+
}
200+
201+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.dao.annotation;
18+
19+
import org.springframework.aop.framework.ProxyFactory;
20+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
21+
import org.springframework.beans.factory.support.RootBeanDefinition;
22+
import org.springframework.core.annotation.AnnotationUtils;
23+
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
24+
import org.springframework.dao.support.PersistenceExceptionTranslator;
25+
import org.springframework.stereotype.Repository;
26+
27+
/**
28+
* Tests for standalone usage of a PersistenceExceptionTranslationInterceptor, as explicit advice bean in a BeanFactory
29+
* rather than applied as part of a PersistenceExceptionTranslationAdvisor.
30+
*
31+
* @author Juergen Hoeller
32+
*/
33+
public class PersistenceExceptionTranslationInterceptorTests extends PersistenceExceptionTranslationAdvisorTests {
34+
35+
@Override
36+
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
37+
if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) {
38+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
39+
bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class));
40+
bf.registerSingleton("pet", pet);
41+
pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti"));
42+
}
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2002-2007 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.dao.annotation;
18+
19+
import junit.framework.TestCase;
20+
import org.aspectj.lang.JoinPoint;
21+
import org.aspectj.lang.annotation.Aspect;
22+
import org.aspectj.lang.annotation.Before;
23+
24+
import org.springframework.aop.Advisor;
25+
import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
26+
import org.springframework.aop.framework.Advised;
27+
import org.springframework.aop.support.AopUtils;
28+
import org.springframework.beans.BeansException;
29+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
30+
import org.springframework.beans.factory.support.RootBeanDefinition;
31+
import org.springframework.context.support.GenericApplicationContext;
32+
import org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterface;
33+
import org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.RepositoryInterfaceImpl;
34+
import org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisorTests.StereotypedRepositoryInterfaceImpl;
35+
import org.springframework.dao.support.ChainedPersistenceExceptionTranslator;
36+
import org.springframework.stereotype.Repository;
37+
38+
/**
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+
* @author Rod Johnson
43+
*/
44+
public class PersistenceExceptionTranslationPostProcessorTests extends TestCase {
45+
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+
public void testProxiesCorrectly() {
61+
GenericApplicationContext gac = new GenericApplicationContext();
62+
gac.registerBeanDefinition("translator",
63+
new RootBeanDefinition(PersistenceExceptionTranslationPostProcessor.class));
64+
gac.registerBeanDefinition("notProxied", new RootBeanDefinition(RepositoryInterfaceImpl.class));
65+
gac.registerBeanDefinition("proxied", new RootBeanDefinition(StereotypedRepositoryInterfaceImpl.class));
66+
gac.registerBeanDefinition("classProxied", new RootBeanDefinition(RepositoryWithoutInterface.class));
67+
gac.registerBeanDefinition("classProxiedAndAdvised",
68+
new RootBeanDefinition(RepositoryWithoutInterfaceAndOtherwiseAdvised.class));
69+
gac.registerBeanDefinition("chainedTranslator",
70+
new RootBeanDefinition(ChainedPersistenceExceptionTranslator.class));
71+
gac.registerBeanDefinition("proxyCreator",
72+
BeanDefinitionBuilder.rootBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class).
73+
addPropertyValue("order", 50).getBeanDefinition());
74+
gac.registerBeanDefinition("logger", new RootBeanDefinition(LogAllAspect.class));
75+
gac.refresh();
76+
77+
RepositoryInterface shouldNotBeProxied = (RepositoryInterface) gac.getBean("notProxied");
78+
assertFalse(AopUtils.isAopProxy(shouldNotBeProxied));
79+
RepositoryInterface shouldBeProxied = (RepositoryInterface) gac.getBean("proxied");
80+
assertTrue(AopUtils.isAopProxy(shouldBeProxied));
81+
RepositoryWithoutInterface rwi = (RepositoryWithoutInterface) gac.getBean("classProxied");
82+
assertTrue(AopUtils.isAopProxy(rwi));
83+
checkWillTranslateExceptions(rwi);
84+
85+
Additional rwi2 = (Additional) gac.getBean("classProxiedAndAdvised");
86+
assertTrue(AopUtils.isAopProxy(rwi2));
87+
rwi2.additionalMethod();
88+
checkWillTranslateExceptions(rwi2);
89+
}
90+
91+
protected void checkWillTranslateExceptions(Object o) {
92+
assertTrue(o instanceof Advised);
93+
Advised a = (Advised) o;
94+
for (Advisor advisor : a.getAdvisors()) {
95+
if (advisor instanceof PersistenceExceptionTranslationAdvisor) {
96+
return;
97+
}
98+
}
99+
fail("No translation");
100+
}
101+
102+
@Repository
103+
public static class RepositoryWithoutInterface {
104+
105+
public void nameDoesntMatter() {
106+
}
107+
}
108+
109+
public interface Additional {
110+
111+
void additionalMethod();
112+
}
113+
114+
public static class RepositoryWithoutInterfaceAndOtherwiseAdvised extends StereotypedRepositoryInterfaceImpl
115+
implements Additional {
116+
117+
public void additionalMethod() {
118+
}
119+
}
120+
121+
@Aspect
122+
public static class LogAllAspect {
123+
124+
//@Before("execution(* *())")
125+
@Before("execution(void *.additionalMethod())")
126+
public void log(JoinPoint jp) {
127+
System.out.println("Before " + jp.getSignature().getName());
128+
}
129+
}
130+
131+
}

0 commit comments

Comments
 (0)