Skip to content

Commit ce3cf32

Browse files
committed
Tests for annotation lookups in interfaces (currently ignored for CGLIB proxies)
Issue: SPR-15271 Issue: SPR-14949 Issue: SPR-14322 (cherry picked from commit d003f66)
1 parent d69fb06 commit ce3cf32

File tree

4 files changed

+356
-75
lines changed

4 files changed

+356
-75
lines changed

spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,97 +18,93 @@
1818

1919
import java.lang.reflect.Method;
2020

21+
import org.junit.Before;
22+
import org.junit.Test;
23+
2124
import org.springframework.tests.transaction.CallCountingTransactionManager;
2225
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
23-
import org.springframework.transaction.interceptor.TransactionAspectSupport;
2426
import org.springframework.transaction.interceptor.TransactionAttribute;
2527

28+
import static org.junit.Assert.*;
29+
2630
/**
2731
* @author Rod Johnson
2832
* @author Ramnivas Laddad
2933
* @author Juergen Hoeller
3034
* @author Sam Brannen
3135
*/
32-
@SuppressWarnings("deprecation")
33-
public class TransactionAspectTests extends org.springframework.test.AbstractDependencyInjectionSpringContextTests {
36+
public class TransactionAspectTests {
3437

35-
private CallCountingTransactionManager txManager;
38+
private final CallCountingTransactionManager txManager = new CallCountingTransactionManager();
3639

37-
private TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface;
40+
private final TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface =
41+
new TransactionalAnnotationOnlyOnClassWithNoInterface();
3842

39-
private ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod;
43+
private final ClassWithProtectedAnnotatedMember beanWithAnnotatedProtectedMethod =
44+
new ClassWithProtectedAnnotatedMember();
4045

41-
private ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod;
46+
private final ClassWithPrivateAnnotatedMember beanWithAnnotatedPrivateMethod =
47+
new ClassWithPrivateAnnotatedMember();
4248

43-
private MethodAnnotationOnClassWithNoInterface methodAnnotationOnly = new MethodAnnotationOnClassWithNoInterface();
49+
private final MethodAnnotationOnClassWithNoInterface methodAnnotationOnly =
50+
new MethodAnnotationOnClassWithNoInterface();
4451

4552

46-
public void setAnnotationOnlyOnClassWithNoInterface(
47-
TransactionalAnnotationOnlyOnClassWithNoInterface annotationOnlyOnClassWithNoInterface) {
48-
this.annotationOnlyOnClassWithNoInterface = annotationOnlyOnClassWithNoInterface;
49-
}
50-
51-
public void setClassWithAnnotatedProtectedMethod(ClassWithProtectedAnnotatedMember aBean) {
52-
this.beanWithAnnotatedProtectedMethod = aBean;
53-
}
54-
55-
public void setClassWithAnnotatedPrivateMethod(ClassWithPrivateAnnotatedMember aBean) {
56-
this.beanWithAnnotatedPrivateMethod = aBean;
57-
}
58-
59-
public void setTransactionAspect(TransactionAspectSupport transactionAspect) {
60-
this.txManager = (CallCountingTransactionManager) transactionAspect.getTransactionManager();
61-
}
62-
63-
64-
@Override
65-
protected String[] getConfigPaths() {
66-
return new String[] { "TransactionAspectTests-context.xml" };
53+
@Before
54+
public void initContext() {
55+
AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
6756
}
6857

6958

59+
@Test
7060
public void testCommitOnAnnotatedClass() throws Throwable {
7161
txManager.clear();
7262
assertEquals(0, txManager.begun);
7363
annotationOnlyOnClassWithNoInterface.echo(null);
7464
assertEquals(1, txManager.commits);
7565
}
7666

67+
@Test
7768
public void testCommitOnAnnotatedProtectedMethod() throws Throwable {
7869
txManager.clear();
7970
assertEquals(0, txManager.begun);
8071
beanWithAnnotatedProtectedMethod.doInTransaction();
8172
assertEquals(1, txManager.commits);
8273
}
8374

75+
@Test
8476
public void testCommitOnAnnotatedPrivateMethod() throws Throwable {
8577
txManager.clear();
8678
assertEquals(0, txManager.begun);
8779
beanWithAnnotatedPrivateMethod.doSomething();
8880
assertEquals(1, txManager.commits);
8981
}
9082

83+
@Test
9184
public void testNoCommitOnNonAnnotatedNonPublicMethodInTransactionalType() throws Throwable {
9285
txManager.clear();
9386
assertEquals(0,txManager.begun);
9487
annotationOnlyOnClassWithNoInterface.nonTransactionalMethod();
9588
assertEquals(0,txManager.begun);
9689
}
9790

91+
@Test
9892
public void testCommitOnAnnotatedMethod() throws Throwable {
9993
txManager.clear();
10094
assertEquals(0, txManager.begun);
10195
methodAnnotationOnly.echo(null);
10296
assertEquals(1, txManager.commits);
10397
}
10498

99+
@Test
105100
public void testNotTransactional() throws Throwable {
106101
txManager.clear();
107102
assertEquals(0, txManager.begun);
108103
new NotTransactional().noop();
109104
assertEquals(0, txManager.begun);
110105
}
111106

107+
@Test
112108
public void testDefaultCommitOnAnnotatedClass() throws Throwable {
113109
final Exception ex = new Exception();
114110
try {
@@ -125,6 +121,7 @@ public Object performTransactionalOperation() throws Throwable {
125121
}
126122
}
127123

124+
@Test
128125
public void testDefaultRollbackOnAnnotatedClass() throws Throwable {
129126
final RuntimeException ex = new RuntimeException();
130127
try {
@@ -141,10 +138,11 @@ public Object performTransactionalOperation() throws Throwable {
141138
}
142139
}
143140

141+
@Test
144142
public void testDefaultCommitOnSubclassOfAnnotatedClass() throws Throwable {
145143
final Exception ex = new Exception();
146144
try {
147-
testRollback(new TransactionOperationCallback() {
145+
testRollback(new TransactionOperationCallback() {
148146
@Override
149147
public Object performTransactionalOperation() throws Throwable {
150148
return new SubclassOfClassWithTransactionalAnnotation().echo(ex);
@@ -157,6 +155,7 @@ public Object performTransactionalOperation() throws Throwable {
157155
}
158156
}
159157

158+
@Test
160159
public void testDefaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated() throws Throwable {
161160
final Exception ex = new Exception();
162161
try {
@@ -173,6 +172,7 @@ public Object performTransactionalOperation() throws Throwable {
173172
}
174173
}
175174

175+
@Test
176176
public void testDefaultCommitOnImplementationOfAnnotatedInterface() throws Throwable {
177177
final Exception ex = new Exception();
178178
testNotTransactional(new TransactionOperationCallback() {
@@ -185,16 +185,19 @@ public Object performTransactionalOperation() throws Throwable {
185185

186186
/**
187187
* Note: resolution does not occur. Thus we can't make a class transactional if
188-
* it implements a transactionally annotated interface. This behaviour could only
188+
* it implements a transactionally annotated interface. This behavior could only
189189
* be changed in AbstractFallbackTransactionAttributeSource in Spring proper.
190+
* See SPR-14322.
190191
*/
192+
@Test
191193
public void testDoesNotResolveTxAnnotationOnMethodFromClassImplementingAnnotatedInterface() throws Exception {
192194
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
193-
Method m = ImplementsAnnotatedInterface.class.getMethod("echo", Throwable.class);
194-
TransactionAttribute ta = atas.getTransactionAttribute(m, ImplementsAnnotatedInterface.class);
195+
Method method = ImplementsAnnotatedInterface.class.getMethod("echo", Throwable.class);
196+
TransactionAttribute ta = atas.getTransactionAttribute(method, ImplementsAnnotatedInterface.class);
195197
assertNull(ta);
196198
}
197199

200+
@Test
198201
public void testDefaultRollbackOnImplementationOfAnnotatedInterface() throws Throwable {
199202
final Exception rollbackProvokingException = new RuntimeException();
200203
testNotTransactional(new TransactionOperationCallback() {
@@ -237,15 +240,19 @@ protected void testNotTransactional(TransactionOperationCallback toc, Throwable
237240

238241

239242
private interface TransactionOperationCallback {
243+
240244
Object performTransactionalOperation() throws Throwable;
241245
}
242246

247+
243248
public static class SubclassOfClassWithTransactionalAnnotation extends TransactionalAnnotationOnlyOnClassWithNoInterface {
244249
}
245250

251+
246252
public static class SubclassOfClassWithTransactionalMethodAnnotation extends MethodAnnotationOnClassWithNoInterface {
247253
}
248254

255+
249256
public static class ImplementsAnnotatedInterface implements ITransactional {
250257

251258
@Override
@@ -257,6 +264,7 @@ public Object echo(Throwable t) throws Throwable {
257264
}
258265
}
259266

267+
260268
public static class NotTransactional {
261269

262270
public void noop() {

spring-context/src/test/java/org/springframework/cache/CacheReproTests.java

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,12 @@
1616

1717
package org.springframework.cache;
1818

19-
import java.util.Arrays;
2019
import java.util.Collection;
2120
import java.util.Collections;
2221
import java.util.List;
2322
import java.util.Optional;
2423

24+
import org.junit.Ignore;
2525
import org.junit.Rule;
2626
import org.junit.Test;
2727
import org.junit.rules.ExpectedException;
@@ -58,8 +58,9 @@ public class CacheReproTests {
5858
@Rule
5959
public final ExpectedException thrown = ExpectedException.none();
6060

61+
6162
@Test
62-
public void spr11124() throws Exception {
63+
public void spr11124MultipleAnnotations() throws Exception {
6364
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr11124Config.class);
6465
Spr11124Service bean = context.getBean(Spr11124Service.class);
6566
bean.single(2);
@@ -70,7 +71,7 @@ public void spr11124() throws Exception {
7071
}
7172

7273
@Test
73-
public void spr11249() throws Exception {
74+
public void spr11249PrimitiveVarargs() throws Exception {
7475
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr11249Config.class);
7576
Spr11249Service bean = context.getBean(Spr11249Service.class);
7677
Object result = bean.doSomething("op", 2, 3);
@@ -128,8 +129,8 @@ public void spr13081ConfigFailIfCacheResolverReturnsNullCacheName() {
128129
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr13081Config.class);
129130
Spr13081Service bean = context.getBean(Spr13081Service.class);
130131

131-
thrown.expect(IllegalStateException.class);
132-
thrown.expectMessage(MyCacheResolver.class.getName());
132+
this.thrown.expect(IllegalStateException.class);
133+
this.thrown.expectMessage(MyCacheResolver.class.getName());
133134
bean.getSimple(null);
134135
}
135136

@@ -167,6 +168,31 @@ public void spr14853AdaptsToOptionalWithSync() {
167168
assertSame(tb2, cache.get("tb1").get());
168169
}
169170

171+
@Test
172+
public void spr15271FindsOnInterfaceWithInterfaceProxy() {
173+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr15271ConfigA.class);
174+
Spr15271Interface bean = context.getBean(Spr15271Interface.class);
175+
Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
176+
177+
TestBean tb = new TestBean("tb1");
178+
bean.insertItem(tb);
179+
assertSame(tb, bean.findById("tb1").get());
180+
assertSame(tb, cache.get("tb1").get());
181+
}
182+
183+
@Test @Ignore // TODO
184+
public void spr15271FindsOnInterfaceWithCglibProxy() {
185+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Spr15271ConfigB.class);
186+
Spr15271Interface bean = context.getBean(Spr15271Interface.class);
187+
Cache cache = context.getBean(CacheManager.class).getCache("itemCache");
188+
189+
TestBean tb = new TestBean("tb1");
190+
bean.insertItem(tb);
191+
assertSame(tb, bean.findById("tb1").get());
192+
assertSame(tb, cache.get("tb1").get());
193+
}
194+
195+
170196
@Configuration
171197
@EnableCaching
172198
public static class Spr11124Config {
@@ -251,7 +277,7 @@ public static class Spr11592Config {
251277
@Bean
252278
public CacheManager cacheManager() {
253279
SimpleCacheManager cacheManager = new SimpleCacheManager();
254-
cacheManager.setCaches(Arrays.asList(cache()));
280+
cacheManager.setCaches(Collections.singletonList(cache()));
255281
return cacheManager;
256282
}
257283

@@ -358,6 +384,7 @@ public Spr14230Service service() {
358384
}
359385
}
360386

387+
361388
public static class Spr14853Service {
362389

363390
@Cacheable(value = "itemCache", sync = true)
@@ -372,6 +399,7 @@ public TestBean insertItem(TestBean item) {
372399

373400
}
374401

402+
375403
@Configuration
376404
@EnableCaching
377405
public static class Spr14853Config {
@@ -387,4 +415,60 @@ public Spr14853Service service() {
387415
}
388416
}
389417

418+
419+
public interface Spr15271Interface {
420+
421+
@Cacheable(value = "itemCache", sync = true)
422+
Optional<TestBean> findById(String id);
423+
424+
@CachePut(cacheNames = "itemCache", key = "#item.name")
425+
TestBean insertItem(TestBean item);
426+
}
427+
428+
429+
public static class Spr15271Service implements Spr15271Interface {
430+
431+
@Override
432+
public Optional<TestBean> findById(String id) {
433+
return Optional.of(new TestBean(id));
434+
}
435+
436+
@Override
437+
public TestBean insertItem(TestBean item) {
438+
return item;
439+
}
440+
}
441+
442+
443+
@Configuration
444+
@EnableCaching
445+
public static class Spr15271ConfigA {
446+
447+
@Bean
448+
public CacheManager cacheManager() {
449+
return new ConcurrentMapCacheManager();
450+
}
451+
452+
@Bean
453+
public Spr15271Interface service() {
454+
return new Spr15271Service();
455+
}
456+
}
457+
458+
459+
@Configuration
460+
@EnableCaching(proxyTargetClass = true)
461+
public static class Spr15271ConfigB {
462+
463+
@Bean
464+
public CacheManager cacheManager() {
465+
return new ConcurrentMapCacheManager();
466+
}
467+
468+
@Bean
469+
public Spr15271Interface service() {
470+
return new Spr15271Service();
471+
}
472+
}
473+
390474
}

0 commit comments

Comments
 (0)