Skip to content

Commit ec9b605

Browse files
author
Daniel Behrwind
committed
made TestDataLoader cope with JTA transactions
1 parent bd9e727 commit ec9b605

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,27 @@
3131

3232
<groupId>triology-blog</groupId>
3333
<artifactId>test-data-loader</artifactId>
34-
<version>v0.1</version>
34+
<version>v0.1.1</version>
3535

3636
<dependencies>
3737
<dependency>
3838
<groupId>org.codehaus.groovy</groupId>
3939
<artifactId>groovy-all</artifactId>
4040
<version>2.4.7</version>
4141
</dependency>
42+
43+
<dependency>
44+
<groupId>org.slf4j</groupId>
45+
<artifactId>slf4j-api</artifactId>
46+
<version>1.7.21</version>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>ch.qos.logback</groupId>
51+
<artifactId>logback-classic</artifactId>
52+
<version>1.1.7</version>
53+
</dependency>
54+
4255
<dependency>
4356
<groupId>org.eclipse.persistence</groupId>
4457
<artifactId>javax.persistence</artifactId>

src/main/groovy/de/triology/blog/testdataloader/TestDataLoader.groovy

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
*/
2424
package de.triology.blog.testdataloader
2525

26+
import org.slf4j.Logger
27+
import org.slf4j.LoggerFactory
28+
2629
import javax.persistence.EntityManager
2730

2831
/**
@@ -31,6 +34,8 @@ import javax.persistence.EntityManager
3134
*/
3235
class TestDataLoader {
3336

37+
private static final Logger LOG = LoggerFactory.getLogger(TestDataLoader)
38+
3439
private EntityManager entityManager
3540
private EntityBuilder entityBuilder
3641
private EntityDeleter entityDeleter
@@ -100,16 +105,21 @@ class TestDataLoader {
100105
}
101106

102107
private void withTransaction(Closure doWithinTransaction) {
103-
if (!transactionIsActive()) {
108+
if (newTransactionRequired()) {
104109
withNewTransaction(doWithinTransaction)
105110
} else {
106111
// Someone else is taking care of transaction handling
107112
doWithinTransaction();
108113
}
109114
}
110115

111-
private boolean transactionIsActive() {
112-
return entityManager.getTransaction().isActive()
116+
private boolean newTransactionRequired() {
117+
try {
118+
return !entityManager.getTransaction().isActive()
119+
} catch (IllegalStateException e) {
120+
LOG.info('Caught IllegalStateException while accessing entityManager.getTransaction(). Assuming JTA transaction management', e)
121+
return false;
122+
}
113123
}
114124

115125
private void withNewTransaction(Closure doWithinTransaction) {

src/test/java/de/triology/blog/testdataloader/TestDataLoaderTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/*
22
* The MIT License (MIT)
33
*
44
* Copyright (c) 2016 TRIOLOGY GmbH
@@ -56,6 +56,12 @@ public void setUp() throws Exception {
5656
testDataLoader = new TestDataLoader(entityManagerMock);
5757
}
5858

59+
private EntityManager createTransactionalEntityManagerMock() {
60+
EntityManager entityManagerMock = mock(EntityManager.class);
61+
when(entityManagerMock.getTransaction()).thenReturn(mock(EntityTransaction.class));
62+
return entityManagerMock;
63+
}
64+
5965
@Test
6066
public void getsCreatedEntityByName() throws Exception {
6167
BasicTestEntity entity = loadDefaultTestDataAndCallGetEntityByName("basicEntity", BasicTestEntity.class);
@@ -98,9 +104,10 @@ public void clearsEntitiesFromDatabase() throws Exception {
98104
verify(entityManagerMock, times(12)).remove(any());
99105
}
100106

101-
private EntityManager createTransactionalEntityManagerMock() {
102-
EntityManager entityManagerMock = mock(EntityManager.class);
103-
when(entityManagerMock.getTransaction()).thenReturn(mock(EntityTransaction.class));
104-
return entityManagerMock;
107+
@Test
108+
public void copesWithIllegalStateExceptionWhenTryingToAccessAJTATransaction() throws Exception {
109+
//noinspection unchecked
110+
when(entityManagerMock.getTransaction()).thenThrow(IllegalStateException.class);
111+
testDataLoader.loadTestData(Collections.singletonList("tests/testEntityDefinitions.groovy"));
105112
}
106113
}

0 commit comments

Comments
 (0)