File tree Expand file tree Collapse file tree 3 files changed +39
-9
lines changed
main/groovy/de/triology/blog/testdataloader
test/java/de/triology/blog/testdataloader Expand file tree Collapse file tree 3 files changed +39
-9
lines changed Original file line number Diff line number Diff line change 31
31
32
32
<groupId >triology-blog</groupId >
33
33
<artifactId >test-data-loader</artifactId >
34
- <version >v0.1</version >
34
+ <version >v0.1.1 </version >
35
35
36
36
<dependencies >
37
37
<dependency >
38
38
<groupId >org.codehaus.groovy</groupId >
39
39
<artifactId >groovy-all</artifactId >
40
40
<version >2.4.7</version >
41
41
</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
+
42
55
<dependency >
43
56
<groupId >org.eclipse.persistence</groupId >
44
57
<artifactId >javax.persistence</artifactId >
Original file line number Diff line number Diff line change 23
23
*/
24
24
package de.triology.blog.testdataloader
25
25
26
+ import org.slf4j.Logger
27
+ import org.slf4j.LoggerFactory
28
+
26
29
import javax.persistence.EntityManager
27
30
28
31
/**
@@ -31,6 +34,8 @@ import javax.persistence.EntityManager
31
34
*/
32
35
class TestDataLoader {
33
36
37
+ private static final Logger LOG = LoggerFactory . getLogger(TestDataLoader )
38
+
34
39
private EntityManager entityManager
35
40
private EntityBuilder entityBuilder
36
41
private EntityDeleter entityDeleter
@@ -100,16 +105,21 @@ class TestDataLoader {
100
105
}
101
106
102
107
private void withTransaction (Closure doWithinTransaction ) {
103
- if (! transactionIsActive ()) {
108
+ if (newTransactionRequired ()) {
104
109
withNewTransaction(doWithinTransaction)
105
110
} else {
106
111
// Someone else is taking care of transaction handling
107
112
doWithinTransaction();
108
113
}
109
114
}
110
115
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
+ }
113
123
}
114
124
115
125
private void withNewTransaction (Closure doWithinTransaction ) {
Original file line number Diff line number Diff line change 1
- /**
1
+ /*
2
2
* The MIT License (MIT)
3
3
*
4
4
* Copyright (c) 2016 TRIOLOGY GmbH
@@ -56,6 +56,12 @@ public void setUp() throws Exception {
56
56
testDataLoader = new TestDataLoader (entityManagerMock );
57
57
}
58
58
59
+ private EntityManager createTransactionalEntityManagerMock () {
60
+ EntityManager entityManagerMock = mock (EntityManager .class );
61
+ when (entityManagerMock .getTransaction ()).thenReturn (mock (EntityTransaction .class ));
62
+ return entityManagerMock ;
63
+ }
64
+
59
65
@ Test
60
66
public void getsCreatedEntityByName () throws Exception {
61
67
BasicTestEntity entity = loadDefaultTestDataAndCallGetEntityByName ("basicEntity" , BasicTestEntity .class );
@@ -98,9 +104,10 @@ public void clearsEntitiesFromDatabase() throws Exception {
98
104
verify (entityManagerMock , times (12 )).remove (any ());
99
105
}
100
106
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" ));
105
112
}
106
113
}
You can’t perform that action at this time.
0 commit comments