26
26
import java .util .Date ;
27
27
import java .util .List ;
28
28
import javax .persistence .EntityManager ;
29
+ import javax .persistence .EntityManagerFactory ;
29
30
import junit .framework .TestCase ;
30
31
31
32
/**
@@ -48,7 +49,7 @@ public class ClassLevelManagedLocalTransactionsTest extends TestCase {
48
49
public void setUp () {
49
50
injector = Guice .createInjector (new JpaPersistModule ("testUnit" ));
50
51
51
- //startup persistence
52
+ // startup persistence
52
53
injector .getInstance (PersistService .class ).start ();
53
54
}
54
55
@@ -66,7 +67,7 @@ public void testSimpleTransaction() {
66
67
"EntityManager was not closed by transactional service" ,
67
68
session .getTransaction ().isActive ());
68
69
69
- //test that the data has been stored
70
+ // test that the data has been stored
70
71
session .getTransaction ().begin ();
71
72
Object result =
72
73
session
@@ -88,15 +89,15 @@ public void testSimpleTransactionRollbackOnChecked() {
88
89
try {
89
90
injector .getInstance (TransactionalObject2 .class ).runOperationInTxnThrowingChecked ();
90
91
} catch (IOException e ) {
91
- //ignore
92
+ // ignore
92
93
}
93
94
94
95
EntityManager session = injector .getInstance (EntityManager .class );
95
96
assertFalse (
96
97
"EntityManager was not closed by transactional service (rollback didnt happen?)" ,
97
98
session .getTransaction ().isActive ());
98
99
99
- //test that the data has been stored
100
+ // test that the data has been stored
100
101
session .getTransaction ().begin ();
101
102
List <?> result =
102
103
session
@@ -115,15 +116,15 @@ public void testSimpleTransactionRollbackOnCheckedExcepting() {
115
116
injector .getInstance (TransactionalObject3 .class ).runOperationInTxnThrowingCheckedExcepting ();
116
117
fail ("Exception was not thrown by test txn-al method!" );
117
118
} catch (IOException e ) {
118
- //ignored
119
+ // ignored
119
120
}
120
121
121
122
EntityManager session = injector .getInstance (EntityManager .class );
122
123
assertFalse (
123
124
"Txn was not closed by transactional service (commit didnt happen?)" ,
124
125
session .getTransaction ().isActive ());
125
126
126
- //test that the data has been stored
127
+ // test that the data has been stored
127
128
session .getTransaction ().begin ();
128
129
Object result =
129
130
session
@@ -140,15 +141,15 @@ public void testSimpleTransactionRollbackOnUnchecked() {
140
141
try {
141
142
injector .getInstance (TransactionalObject4 .class ).runOperationInTxnThrowingUnchecked ();
142
143
} catch (RuntimeException re ) {
143
- //ignore
144
+ // ignore
144
145
}
145
146
146
147
EntityManager session = injector .getInstance (EntityManager .class );
147
148
assertFalse (
148
149
"EntityManager was not closed by transactional service (rollback didnt happen?)" ,
149
150
session .getTransaction ().isActive ());
150
151
151
- //test that the data has been stored
152
+ // test that the data has been stored
152
153
session .getTransaction ().begin ();
153
154
List <?> result =
154
155
session
@@ -161,6 +162,26 @@ public void testSimpleTransactionRollbackOnUnchecked() {
161
162
assertTrue ("a result was returned! rollback sure didnt happen!!!" , result .isEmpty ());
162
163
}
163
164
165
+ public void testTransactionalDoesntAffectObjectMethods () {
166
+ // Given a persist service that tracks when it's called
167
+ JpaPersistService persistService = injector .getInstance (JpaPersistService .class );
168
+ EntityManagerFactory originalEMF = injector .getInstance (EntityManagerFactory .class );
169
+ TrackedEntityManagerFactory trackingEMF = new TrackedEntityManagerFactory (originalEMF );
170
+ persistService .start (trackingEMF );
171
+
172
+ FakeTransactionalObject txnObj = injector .getInstance (FakeTransactionalObject .class );
173
+
174
+ String unused = txnObj .toString ();
175
+ assertFalse (
176
+ "Should not have created a transaction for toString method" ,
177
+ trackingEMF .hasCreatedSomeEntityManager ());
178
+
179
+ txnObj .fakeTransactionalMethod ();
180
+ assertTrue (
181
+ "Transaction should have been created for normal method" ,
182
+ trackingEMF .hasCreatedSomeEntityManager ());
183
+ }
184
+
164
185
@ Transactional
165
186
public static class TransactionalObject {
166
187
@ Inject EntityManager session ;
@@ -215,4 +236,9 @@ public void runOperationInTxnThrowingChecked() throws IOException {
215
236
throw new IOException ();
216
237
}
217
238
}
239
+
240
+ @ Transactional
241
+ public static class FakeTransactionalObject {
242
+ public void fakeTransactionalMethod () {}
243
+ }
218
244
}
0 commit comments