Skip to content

Commit 697d14a

Browse files
committed
Unit tests for transaction annotations on parent interfaces
Issue: SPR-15833
1 parent dd2bbcb commit 697d14a

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionAttributeSourceTests.java

Lines changed: 31 additions & 42 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.
@@ -55,14 +55,14 @@ public void serializable() throws Exception {
5555
TransactionInterceptor ti = new TransactionInterceptor(ptm, tas);
5656

5757
ProxyFactory proxyFactory = new ProxyFactory();
58-
proxyFactory.setInterfaces(ITestBean.class);
58+
proxyFactory.setInterfaces(ITestBean1.class);
5959
proxyFactory.addAdvice(ti);
6060
proxyFactory.setTarget(tb);
61-
ITestBean proxy = (ITestBean) proxyFactory.getProxy();
61+
ITestBean1 proxy = (ITestBean1) proxyFactory.getProxy();
6262
proxy.getAge();
6363
assertEquals(1, ptm.commits);
6464

65-
ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
65+
ITestBean1 serializedProxy = (ITestBean1) SerializationTestUtils.serializeAndDeserialize(proxy);
6666
serializedProxy.getAge();
6767
Advised advised = (Advised) serializedProxy;
6868
TransactionInterceptor serializedTi = (TransactionInterceptor) advised.getAdvisors()[0].getAdvice();
@@ -88,7 +88,7 @@ public void nullOrEmpty() throws Exception {
8888
*/
8989
@Test
9090
public void transactionAttributeDeclaredOnClassMethod() throws Exception {
91-
Method classMethod = ITestBean.class.getMethod("getAge");
91+
Method classMethod = ITestBean1.class.getMethod("getAge");
9292

9393
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
9494
TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class);
@@ -104,7 +104,7 @@ public void transactionAttributeDeclaredOnClassMethod() throws Exception {
104104
*/
105105
@Test
106106
public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception {
107-
Method classMethod = ITestBean.class.getMethod("getAge");
107+
Method classMethod = ITestBean1.class.getMethod("getAge");
108108
TestBean1 tb = new TestBean1();
109109
ProxyFactory pf = new ProxyFactory(tb);
110110
pf.setProxyTargetClass(true);
@@ -129,7 +129,7 @@ public void transactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception
129129
TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class);
130130

131131
RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
132-
assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
132+
assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
133133
}
134134

135135
/**
@@ -300,8 +300,8 @@ public void customMethodAttributeWithReadOnlyOverrideOnInterface() throws Except
300300

301301
@Test
302302
public void transactionAttributeDeclaredOnClassMethodWithEjb3() throws Exception {
303-
Method getAgeMethod = ITestBean.class.getMethod("getAge");
304-
Method getNameMethod = ITestBean.class.getMethod("getName");
303+
Method getAgeMethod = ITestBean1.class.getMethod("getAge");
304+
Method getNameMethod = ITestBean1.class.getMethod("getName");
305305

306306
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
307307
TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean1.class);
@@ -312,8 +312,8 @@ public void transactionAttributeDeclaredOnClassMethodWithEjb3() throws Exception
312312

313313
@Test
314314
public void transactionAttributeDeclaredOnClassWithEjb3() throws Exception {
315-
Method getAgeMethod = ITestBean.class.getMethod("getAge");
316-
Method getNameMethod = ITestBean.class.getMethod("getName");
315+
Method getAgeMethod = ITestBean1.class.getMethod("getAge");
316+
Method getNameMethod = ITestBean1.class.getMethod("getName");
317317

318318
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
319319
TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, Ejb3AnnotatedBean2.class);
@@ -336,8 +336,8 @@ public void transactionAttributeDeclaredOnInterfaceWithEjb3() throws Exception {
336336

337337
@Test
338338
public void transactionAttributeDeclaredOnClassMethodWithJta() throws Exception {
339-
Method getAgeMethod = ITestBean.class.getMethod("getAge");
340-
Method getNameMethod = ITestBean.class.getMethod("getName");
339+
Method getAgeMethod = ITestBean1.class.getMethod("getAge");
340+
Method getNameMethod = ITestBean1.class.getMethod("getName");
341341

342342
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
343343
TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, JtaAnnotatedBean1.class);
@@ -348,8 +348,8 @@ public void transactionAttributeDeclaredOnClassMethodWithJta() throws Exception
348348

349349
@Test
350350
public void transactionAttributeDeclaredOnClassWithJta() throws Exception {
351-
Method getAgeMethod = ITestBean.class.getMethod("getAge");
352-
Method getNameMethod = ITestBean.class.getMethod("getName");
351+
Method getAgeMethod = ITestBean1.class.getMethod("getAge");
352+
Method getNameMethod = ITestBean1.class.getMethod("getName");
353353

354354
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
355355
TransactionAttribute getAgeAttr = atas.getTransactionAttribute(getAgeMethod, JtaAnnotatedBean2.class);
@@ -372,8 +372,8 @@ public void transactionAttributeDeclaredOnInterfaceWithJta() throws Exception {
372372

373373
@Test
374374
public void transactionAttributeDeclaredOnGroovyClass() throws Exception {
375-
Method getAgeMethod = ITestBean.class.getMethod("getAge");
376-
Method getNameMethod = ITestBean.class.getMethod("getName");
375+
Method getAgeMethod = ITestBean1.class.getMethod("getAge");
376+
Method getNameMethod = ITestBean1.class.getMethod("getName");
377377
Method getMetaClassMethod = GroovyObject.class.getMethod("getMetaClass");
378378

379379
AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
@@ -385,7 +385,7 @@ public void transactionAttributeDeclaredOnGroovyClass() throws Exception {
385385
}
386386

387387

388-
interface ITestBean {
388+
interface ITestBean1 {
389389

390390
int getAge();
391391

@@ -403,6 +403,10 @@ interface ITestBean2 {
403403
int getAge();
404404

405405
void setAge(int age);
406+
}
407+
408+
409+
interface ITestBean2X extends ITestBean2 {
406410

407411
String getName();
408412

@@ -423,7 +427,7 @@ interface ITestBean3 {
423427
}
424428

425429

426-
static class Empty implements ITestBean {
430+
static class Empty implements ITestBean1 {
427431

428432
private String name;
429433

@@ -460,7 +464,7 @@ public void setAge(int age) {
460464

461465

462466
@SuppressWarnings("serial")
463-
static class TestBean1 implements ITestBean, Serializable {
467+
static class TestBean1 implements ITestBean1, Serializable {
464468

465469
private String name;
466470

@@ -497,7 +501,7 @@ public void setAge(int age) {
497501
}
498502

499503

500-
static class TestBean2 implements ITestBean2 {
504+
static class TestBean2 implements ITestBean2X {
501505

502506
private String name;
503507

@@ -635,6 +639,7 @@ public int getAge() {
635639
@Retention(RetentionPolicy.RUNTIME)
636640
@Transactional(rollbackFor = Exception.class, noRollbackFor = IOException.class)
637641
@interface TxWithAttribute {
642+
638643
boolean readOnly();
639644
}
640645

@@ -689,23 +694,7 @@ public int getAge() {
689694
}
690695

691696

692-
interface Foo<T> {
693-
694-
void doSomething(T theArgument);
695-
}
696-
697-
698-
static class MyFoo implements Foo<String> {
699-
700-
@Override
701-
@Transactional
702-
public void doSomething(String theArgument) {
703-
System.out.println(theArgument);
704-
}
705-
}
706-
707-
708-
static class Ejb3AnnotatedBean1 implements ITestBean {
697+
static class Ejb3AnnotatedBean1 implements ITestBean1 {
709698

710699
private String name;
711700

@@ -736,7 +725,7 @@ public void setAge(int age) {
736725

737726

738727
@javax.ejb.TransactionAttribute(TransactionAttributeType.SUPPORTS)
739-
static class Ejb3AnnotatedBean2 implements ITestBean {
728+
static class Ejb3AnnotatedBean2 implements ITestBean1 {
740729

741730
private String name;
742731

@@ -807,7 +796,7 @@ public void setAge(int age) {
807796
}
808797

809798

810-
static class JtaAnnotatedBean1 implements ITestBean {
799+
static class JtaAnnotatedBean1 implements ITestBean1 {
811800

812801
private String name;
813802

@@ -838,7 +827,7 @@ public void setAge(int age) {
838827

839828

840829
@javax.transaction.Transactional(javax.transaction.Transactional.TxType.SUPPORTS)
841-
static class JtaAnnotatedBean2 implements ITestBean {
830+
static class JtaAnnotatedBean2 implements ITestBean1 {
842831

843832
private String name;
844833

@@ -910,7 +899,7 @@ public void setAge(int age) {
910899

911900

912901
@Transactional
913-
static class GroovyTestBean implements ITestBean, GroovyObject {
902+
static class GroovyTestBean implements ITestBean1, GroovyObject {
914903

915904
private String name;
916905

spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ public void spr14322FindsOnInterfaceWithInterfaceProxy() {
158158
CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
159159

160160
bean.saveFoo();
161-
assertThat(txManager.begun, equalTo(1));
162-
assertThat(txManager.commits, equalTo(1));
161+
bean.saveBar();
162+
assertThat(txManager.begun, equalTo(2));
163+
assertThat(txManager.commits, equalTo(2));
163164
assertThat(txManager.rollbacks, equalTo(0));
164165

165166
ctx.close();
@@ -172,8 +173,9 @@ public void spr14322FindsOnInterfaceWithCglibProxy() {
172173
CallCountingTransactionManager txManager = ctx.getBean(CallCountingTransactionManager.class);
173174

174175
bean.saveFoo();
175-
assertThat(txManager.begun, equalTo(1));
176-
assertThat(txManager.commits, equalTo(1));
176+
bean.saveBar();
177+
assertThat(txManager.begun, equalTo(2));
178+
assertThat(txManager.commits, equalTo(2));
177179
assertThat(txManager.rollbacks, equalTo(0));
178180

179181
ctx.close();
@@ -298,7 +300,15 @@ public TransactionalTestBean testBean() {
298300
}
299301

300302

301-
public interface TransactionalTestInterface {
303+
public interface BaseTransactionalInterface {
304+
305+
@Transactional
306+
default void saveBar() {
307+
}
308+
}
309+
310+
311+
public interface TransactionalTestInterface extends BaseTransactionalInterface {
302312

303313
@Transactional
304314
void saveFoo();

0 commit comments

Comments
 (0)