KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > JdoTransactionManagerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jdo;
18
19 import java.lang.reflect.Proxy JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Savepoint JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.jdo.PersistenceManager;
28 import javax.jdo.PersistenceManagerFactory;
29 import javax.jdo.Query;
30 import javax.jdo.Transaction;
31 import javax.sql.DataSource JavaDoc;
32 import javax.transaction.Status JavaDoc;
33 import javax.transaction.UserTransaction JavaDoc;
34
35 import junit.framework.TestCase;
36 import org.easymock.MockControl;
37
38 import org.springframework.beans.TestBean;
39 import org.springframework.core.JdkVersion;
40 import org.springframework.jdbc.datasource.ConnectionHandle;
41 import org.springframework.jdbc.datasource.SimpleConnectionHandle;
42 import org.springframework.transaction.InvalidIsolationLevelException;
43 import org.springframework.transaction.PlatformTransactionManager;
44 import org.springframework.transaction.TransactionDefinition;
45 import org.springframework.transaction.TransactionStatus;
46 import org.springframework.transaction.UnexpectedRollbackException;
47 import org.springframework.transaction.jta.JtaTransactionManager;
48 import org.springframework.transaction.support.TransactionCallback;
49 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
50 import org.springframework.transaction.support.TransactionSynchronizationManager;
51 import org.springframework.transaction.support.TransactionTemplate;
52
53 /**
54  * @author Juergen Hoeller
55  */

56 public class JdoTransactionManagerTests extends TestCase {
57
58     public void testTransactionCommit() {
59         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
60         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
61         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
62         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
63         MockControl txControl = MockControl.createControl(Transaction.class);
64         Transaction tx = (Transaction) txControl.getMock();
65
66         pmf.getConnectionFactory();
67         pmfControl.setReturnValue(null, 1);
68         pmf.getPersistenceManager();
69         pmfControl.setReturnValue(pm, 1);
70         pm.currentTransaction();
71         pmControl.setReturnValue(tx, 2);
72         pm.flush();
73         pmControl.setVoidCallable(2);
74         pm.close();
75         pmControl.setVoidCallable(1);
76         tx.begin();
77         txControl.setVoidCallable(1);
78         tx.commit();
79         txControl.setVoidCallable(1);
80         pmfControl.replay();
81         pmControl.replay();
82         txControl.replay();
83
84         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
85         TransactionTemplate tt = new TransactionTemplate(tm);
86         final List JavaDoc l = new ArrayList JavaDoc();
87         l.add("test");
88         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
89         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
90
91         Object JavaDoc result = tt.execute(new TransactionCallback() {
92             public Object JavaDoc doInTransaction(TransactionStatus status) {
93                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
94
95                 TransactionAwarePersistenceManagerFactoryProxy proxyFactory =
96                         new TransactionAwarePersistenceManagerFactoryProxy();
97                 proxyFactory.setTargetPersistenceManagerFactory(pmf);
98                 PersistenceManagerFactory proxy = (PersistenceManagerFactory) proxyFactory.getObject();
99                 assertEquals(pm.toString(), proxy.getPersistenceManager().toString());
100                 proxy.getPersistenceManager().flush();
101                 proxy.getPersistenceManager().close();
102
103                 JdoTemplate jt = new JdoTemplate(pmf);
104                 return jt.execute(new JdoCallback() {
105                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
106                         pm2.flush();
107                         return l;
108                     }
109                 });
110             }
111         });
112         assertTrue("Correct result list", result == l);
113
114         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
115         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
116         pmfControl.verify();
117         pmControl.verify();
118         txControl.verify();
119     }
120
121     public void testTransactionRollback() {
122         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
123         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
124         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
125         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
126         MockControl txControl = MockControl.createControl(Transaction.class);
127         Transaction tx = (Transaction) txControl.getMock();
128
129         pmf.getConnectionFactory();
130         pmfControl.setReturnValue(null, 1);
131         pmf.getPersistenceManager();
132         pmfControl.setReturnValue(pm, 1);
133         pm.currentTransaction();
134         pmControl.setReturnValue(tx, 2);
135         pm.close();
136         pmControl.setVoidCallable(1);
137         tx.begin();
138         txControl.setVoidCallable(1);
139         tx.rollback();
140         txControl.setVoidCallable(1);
141         pmfControl.replay();
142         pmControl.replay();
143         txControl.replay();
144
145         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
146         TransactionTemplate tt = new TransactionTemplate(tm);
147         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
148         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
149
150         try {
151             tt.execute(new TransactionCallback() {
152                 public Object JavaDoc doInTransaction(TransactionStatus status) {
153                     assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
154                     JdoTemplate jt = new JdoTemplate(pmf);
155                     return jt.execute(new JdoCallback() {
156                         public Object JavaDoc doInJdo(PersistenceManager pm) {
157                             throw new RuntimeException JavaDoc("application exception");
158                         }
159                     });
160                 }
161             });
162             fail("Should have thrown RuntimeException");
163         }
164         catch (RuntimeException JavaDoc ex) {
165             // expected
166
}
167
168         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
169         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
170         pmfControl.verify();
171         pmControl.verify();
172         txControl.verify();
173     }
174
175     public void testTransactionRollbackOnly() {
176         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
177         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
178         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
179         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
180         MockControl txControl = MockControl.createControl(Transaction.class);
181         Transaction tx = (Transaction) txControl.getMock();
182
183         pmf.getConnectionFactory();
184         pmfControl.setReturnValue(null, 1);
185         pmf.getPersistenceManager();
186         pmfControl.setReturnValue(pm, 1);
187         pm.currentTransaction();
188         pmControl.setReturnValue(tx, 2);
189         pm.flush();
190         pmControl.setVoidCallable(1);
191         pm.close();
192         pmControl.setVoidCallable(1);
193         tx.begin();
194         txControl.setVoidCallable(1);
195         tx.rollback();
196         txControl.setVoidCallable(1);
197         pmfControl.replay();
198         pmControl.replay();
199         txControl.replay();
200
201         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
202         TransactionTemplate tt = new TransactionTemplate(tm);
203         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
204
205         tt.execute(new TransactionCallback() {
206             public Object JavaDoc doInTransaction(TransactionStatus status) {
207                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
208                 JdoTemplate jt = new JdoTemplate(pmf);
209                 jt.execute(new JdoCallback() {
210                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
211                         pm2.flush();
212                         return null;
213                     }
214                 });
215                 status.setRollbackOnly();
216                 return null;
217             }
218         });
219
220         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
221         pmfControl.verify();
222         pmControl.verify();
223         txControl.verify();
224     }
225
226     public void testParticipatingTransactionWithCommit() {
227         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
228         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
229         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
230         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
231         final MockControl txControl = MockControl.createControl(Transaction.class);
232         final Transaction tx = (Transaction) txControl.getMock();
233
234         pmf.getConnectionFactory();
235         pmfControl.setReturnValue(null, 1);
236         pmf.getPersistenceManager();
237         pmfControl.setReturnValue(pm, 1);
238         pm.currentTransaction();
239         pmControl.setReturnValue(tx, 3);
240         pm.flush();
241         pmControl.setVoidCallable(1);
242         pm.close();
243         pmControl.setVoidCallable(1);
244         tx.begin();
245         txControl.setVoidCallable(1);
246         pmfControl.replay();
247         pmControl.replay();
248         txControl.replay();
249
250         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
251         final TransactionTemplate tt = new TransactionTemplate(tm);
252         final List JavaDoc l = new ArrayList JavaDoc();
253         l.add("test");
254
255         Object JavaDoc result = tt.execute(new TransactionCallback() {
256             public Object JavaDoc doInTransaction(TransactionStatus status) {
257                 txControl.reset();
258                 tx.isActive();
259                 txControl.setReturnValue(true, 1);
260                 tx.commit();
261                 txControl.setVoidCallable(1);
262                 txControl.replay();
263
264                 return tt.execute(new TransactionCallback() {
265                     public Object JavaDoc doInTransaction(TransactionStatus status) {
266                         JdoTemplate jt = new JdoTemplate(pmf);
267                         return jt.execute(new JdoCallback() {
268                             public Object JavaDoc doInJdo(PersistenceManager pm2) {
269                                 pm2.flush();
270                                 return l;
271                             }
272                         });
273                     }
274                 });
275             }
276         });
277         assertTrue("Correct result list", result == l);
278
279         pmfControl.verify();
280         pmControl.verify();
281         txControl.verify();
282     }
283
284     public void testParticipatingTransactionWithRollback() {
285         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
286         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
287         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
288         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
289         final MockControl txControl = MockControl.createControl(Transaction.class);
290         final Transaction tx = (Transaction) txControl.getMock();
291
292         pmf.getConnectionFactory();
293         pmfControl.setReturnValue(null, 1);
294         pmf.getPersistenceManager();
295         pmfControl.setReturnValue(pm, 1);
296         pm.currentTransaction();
297         pmControl.setReturnValue(tx, 3);
298         pm.close();
299         pmControl.setVoidCallable(1);
300         tx.isActive();
301         txControl.setReturnValue(false, 1);
302         tx.begin();
303         txControl.setVoidCallable(1);
304         pmfControl.replay();
305         pmControl.replay();
306         txControl.replay();
307
308         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
309         final TransactionTemplate tt = new TransactionTemplate(tm);
310         try {
311             tt.execute(new TransactionCallback() {
312                 public Object JavaDoc doInTransaction(TransactionStatus status) {
313                     txControl.reset();
314                     tx.isActive();
315                     txControl.setReturnValue(true, 1);
316                     tx.rollback();
317                     txControl.setVoidCallable(1);
318                     txControl.replay();
319
320                     return tt.execute(new TransactionCallback() {
321                         public Object JavaDoc doInTransaction(TransactionStatus status) {
322                             JdoTemplate jt = new JdoTemplate(pmf);
323                             return jt.execute(new JdoCallback() {
324                                 public Object JavaDoc doInJdo(PersistenceManager pm) {
325                                     throw new RuntimeException JavaDoc("application exception");
326                                 }
327                             });
328                         }
329                     });
330                 }
331             });
332             fail("Should have thrown RuntimeException");
333         }
334         catch (RuntimeException JavaDoc ex) {
335             // expected
336
}
337
338         pmfControl.verify();
339         pmControl.verify();
340         txControl.verify();
341     }
342
343     public void testParticipatingTransactionWithRollbackOnly() {
344         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
345         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
346         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
347         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
348         final MockControl txControl = MockControl.createControl(Transaction.class);
349         final Transaction tx = (Transaction) txControl.getMock();
350
351         pmf.getConnectionFactory();
352         pmfControl.setReturnValue(null, 1);
353         pmf.getPersistenceManager();
354         pmfControl.setReturnValue(pm, 1);
355         pm.currentTransaction();
356         pmControl.setReturnValue(tx, 3);
357         pm.flush();
358         pmControl.setVoidCallable(1);
359         pm.close();
360         pmControl.setVoidCallable(1);
361         tx.begin();
362         txControl.setVoidCallable(1);
363         pmfControl.replay();
364         pmControl.replay();
365         txControl.replay();
366
367         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
368         final TransactionTemplate tt = new TransactionTemplate(tm);
369         final List JavaDoc l = new ArrayList JavaDoc();
370         l.add("test");
371
372         try {
373             tt.execute(new TransactionCallback() {
374                 public Object JavaDoc doInTransaction(TransactionStatus status) {
375                     txControl.reset();
376                     tx.isActive();
377                     txControl.setReturnValue(true, 1);
378                     tx.rollback();
379                     txControl.setVoidCallable(1);
380                     txControl.replay();
381
382                     return tt.execute(new TransactionCallback() {
383                         public Object JavaDoc doInTransaction(TransactionStatus status) {
384                             JdoTemplate jt = new JdoTemplate(pmf);
385                             jt.execute(new JdoCallback() {
386                                 public Object JavaDoc doInJdo(PersistenceManager pm2) {
387                                     pm2.flush();
388                                     return l;
389                                 }
390                             });
391                             status.setRollbackOnly();
392                             return null;
393                         }
394                     });
395                 }
396             });
397             fail("Should have thrown UnexpectedRollbackException");
398         }
399         catch (UnexpectedRollbackException ex) {
400             // expected
401
}
402
403         pmfControl.verify();
404         pmControl.verify();
405         txControl.verify();
406     }
407
408     public void testParticipatingTransactionWithWithRequiresNew() {
409         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
410         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
411         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
412         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
413         final MockControl txControl = MockControl.createControl(Transaction.class);
414         final Transaction tx = (Transaction) txControl.getMock();
415
416         pmf.getConnectionFactory();
417         pmfControl.setReturnValue(null, 1);
418         pmf.getPersistenceManager();
419         pmfControl.setReturnValue(pm, 2);
420         pm.currentTransaction();
421         pmControl.setReturnValue(tx, 5);
422         tx.begin();
423         txControl.setVoidCallable(1);
424         pm.flush();
425         pmControl.setVoidCallable(1);
426         pm.close();
427         pmControl.setVoidCallable(2);
428         pmfControl.replay();
429         pmControl.replay();
430         txControl.replay();
431
432         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
433         final TransactionTemplate tt = new TransactionTemplate(tm);
434         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
435         final List JavaDoc l = new ArrayList JavaDoc();
436         l.add("test");
437
438         Object JavaDoc result = tt.execute(new TransactionCallback() {
439             public Object JavaDoc doInTransaction(TransactionStatus status) {
440                 txControl.reset();
441                 tx.isActive();
442                 txControl.setReturnValue(true, 1);
443                 tx.begin();
444                 txControl.setVoidCallable(1);
445                 tx.commit();
446                 txControl.setVoidCallable(2);
447                 txControl.replay();
448
449                 return tt.execute(new TransactionCallback() {
450                     public Object JavaDoc doInTransaction(TransactionStatus status) {
451                         JdoTemplate jt = new JdoTemplate(pmf);
452                         return jt.execute(new JdoCallback() {
453                             public Object JavaDoc doInJdo(PersistenceManager pm2) {
454                                 pm2.flush();
455                                 return l;
456                             }
457                         });
458                     }
459                 });
460             }
461         });
462         assertTrue("Correct result list", result == l);
463
464         pmfControl.verify();
465         pmControl.verify();
466         txControl.verify();
467     }
468
469     public void testJtaTransactionCommit() throws Exception JavaDoc {
470         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
471         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
472         ut.getStatus();
473         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
474         ut.getStatus();
475         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
476         ut.begin();
477         utControl.setVoidCallable(1);
478         ut.commit();
479         utControl.setVoidCallable(1);
480         utControl.replay();
481
482         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
483         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
484         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
485         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
486         pmf.getPersistenceManager();
487         pmfControl.setReturnValue(pm, 1);
488         pm.flush();
489         pmControl.setVoidCallable(2);
490         pm.close();
491         pmControl.setVoidCallable(1);
492         pmfControl.replay();
493         pmControl.replay();
494
495         JtaTransactionManager ptm = new JtaTransactionManager(ut);
496         TransactionTemplate tt = new TransactionTemplate(ptm);
497         final List JavaDoc l = new ArrayList JavaDoc();
498         l.add("test");
499         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
500         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
501
502         Object JavaDoc result = tt.execute(new TransactionCallback() {
503             public Object JavaDoc doInTransaction(TransactionStatus status) {
504                 assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
505                 assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
506                 JdoTemplate jt = new JdoTemplate(pmf);
507                 jt.execute(new JdoCallback() {
508                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
509                         assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
510                         pm2.flush();
511                         return l;
512                     }
513                 });
514                 Object JavaDoc result = jt.execute(new JdoCallback() {
515                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
516                         assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
517                         pm2.flush();
518                         return l;
519                     }
520                 });
521                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
522                 return result;
523             }
524         });
525         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
526         assertTrue("Correct result list", result == l);
527
528         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
529         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
530         pmfControl.verify();
531         pmControl.verify();
532     }
533
534     public void testTransactionCommitWithPropagationSupports() {
535         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
536         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
537         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
538         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
539         pmf.getConnectionFactory();
540         pmfControl.setReturnValue(null, 1);
541         pmf.getPersistenceManager();
542         pmfControl.setReturnValue(pm, 1);
543         pm.close();
544         pmControl.setVoidCallable(1);
545         pmfControl.replay();
546         pmControl.replay();
547
548         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
549         TransactionTemplate tt = new TransactionTemplate(tm);
550         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
551         final List JavaDoc l = new ArrayList JavaDoc();
552         l.add("test");
553         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
554
555         Object JavaDoc result = tt.execute(new TransactionCallback() {
556             public Object JavaDoc doInTransaction(TransactionStatus status) {
557                 assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
558                 assertTrue("Is not new transaction", !status.isNewTransaction());
559                 JdoTemplate jt = new JdoTemplate(pmf);
560                 return jt.execute(new JdoCallback() {
561                     public Object JavaDoc doInJdo(PersistenceManager pm) {
562                         return l;
563                     }
564                 });
565             }
566         });
567         assertTrue("Correct result list", result == l);
568
569         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
570         pmfControl.verify();
571         pmControl.verify();
572     }
573
574     public void testInvalidIsolation() {
575         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
576         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
577         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
578         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
579         pmf.getConnectionFactory();
580         pmfControl.setReturnValue(null, 1);
581         pmf.getPersistenceManager();
582         pmfControl.setReturnValue(pm, 1);
583         pm.currentTransaction();
584         pmControl.setReturnValue(null, 1);
585         pm.close();
586         pmControl.setVoidCallable(1);
587         pmfControl.replay();
588         pmControl.replay();
589
590         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
591         TransactionTemplate tt = new TransactionTemplate(tm);
592         tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
593         try {
594             tt.execute(new TransactionCallbackWithoutResult() {
595                 protected void doInTransactionWithoutResult(TransactionStatus status) {
596                 }
597             });
598             fail("Should have thrown InvalidIsolationLevelException");
599         }
600         catch (InvalidIsolationLevelException ex) {
601             // expected
602
}
603
604         pmfControl.verify();
605     }
606
607     public void testTransactionCommitWithPrebound() {
608         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
609         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
610         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
611         PersistenceManager pm = (PersistenceManager) pmControl.getMock();
612         MockControl txControl = MockControl.createControl(Transaction.class);
613         Transaction tx = (Transaction) txControl.getMock();
614         pmf.getConnectionFactory();
615         pmfControl.setReturnValue(null, 1);
616         pm.currentTransaction();
617         pmControl.setReturnValue(tx, 3);
618         tx.isActive();
619         txControl.setReturnValue(false, 1);
620         tx.begin();
621         txControl.setVoidCallable(1);
622         tx.commit();
623         txControl.setVoidCallable(1);
624         pmfControl.replay();
625         pmControl.replay();
626         txControl.replay();
627
628         PlatformTransactionManager tm = new JdoTransactionManager(pmf);
629         TransactionTemplate tt = new TransactionTemplate(tm);
630         final List JavaDoc l = new ArrayList JavaDoc();
631         l.add("test");
632         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
633         TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
634         assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
635
636         Object JavaDoc result = tt.execute(new TransactionCallback() {
637             public Object JavaDoc doInTransaction(TransactionStatus status) {
638                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
639                 JdoTemplate jt = new JdoTemplate(pmf);
640                 return jt.execute(new JdoCallback() {
641                     public Object JavaDoc doInJdo(PersistenceManager pm) {
642                         return l;
643                     }
644                 });
645             }
646         });
647         assertTrue("Correct result list", result == l);
648
649         assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
650         TransactionSynchronizationManager.unbindResource(pmf);
651         assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
652         pmfControl.verify();
653         pmControl.verify();
654         txControl.verify();
655     }
656
657     public void testTransactionCommitWithDataSource() throws SQLException JavaDoc {
658         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
659         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
660         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
661         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
662         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
663         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
664         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
665         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
666         MockControl txControl = MockControl.createControl(Transaction.class);
667         Transaction tx = (Transaction) txControl.getMock();
668         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
669         final Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
670         ConnectionHandle conHandle = new SimpleConnectionHandle(con);
671
672         pmf.getPersistenceManager();
673         pmfControl.setReturnValue(pm, 1);
674         pm.currentTransaction();
675         pmControl.setReturnValue(tx, 2);
676         pm.close();
677         pmControl.setVoidCallable(1);
678         TransactionTemplate tt = new TransactionTemplate();
679         dialect.beginTransaction(tx, tt);
680         dialectControl.setReturnValue(null, 1);
681         dialect.getJdbcConnection(pm, false);
682         dialectControl.setReturnValue(conHandle, 1);
683         dialect.releaseJdbcConnection(conHandle, pm);
684         dialectControl.setVoidCallable(1);
685         dialect.cleanupTransaction(null);
686         dialectControl.setVoidCallable(1);
687         tx.commit();
688         txControl.setVoidCallable(1);
689         pmfControl.replay();
690         dsControl.replay();
691         dialectControl.replay();
692         pmControl.replay();
693         txControl.replay();
694         conControl.replay();
695
696         JdoTransactionManager tm = new JdoTransactionManager();
697         tm.setPersistenceManagerFactory(pmf);
698         tm.setDataSource(ds);
699         tm.setJdoDialect(dialect);
700         tt.setTransactionManager(tm);
701         final List JavaDoc l = new ArrayList JavaDoc();
702         l.add("test");
703         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
704
705         Object JavaDoc result = tt.execute(new TransactionCallback() {
706             public Object JavaDoc doInTransaction(TransactionStatus status) {
707                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
708                 assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
709                 JdoTemplate jt = new JdoTemplate(pmf);
710                 return jt.execute(new JdoCallback() {
711                     public Object JavaDoc doInJdo(PersistenceManager pm) {
712                         return l;
713                     }
714                 });
715             }
716         });
717         assertTrue("Correct result list", result == l);
718
719         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
720         assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
721         pmfControl.verify();
722         dsControl.verify();
723         dialectControl.verify();
724         pmControl.verify();
725         txControl.verify();
726         conControl.verify();
727     }
728
729     public void testTransactionCommitWithAutoDetectedDataSource() throws SQLException JavaDoc {
730         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
731         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
732         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
733         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
734         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
735         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
736         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
737         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
738         MockControl txControl = MockControl.createControl(Transaction.class);
739         Transaction tx = (Transaction) txControl.getMock();
740         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
741         final Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
742         ConnectionHandle conHandle = new SimpleConnectionHandle(con);
743
744         pmf.getConnectionFactory();
745         pmfControl.setReturnValue(ds, 1);
746         pmf.getPersistenceManager();
747         pmfControl.setReturnValue(pm, 1);
748         pm.currentTransaction();
749         pmControl.setReturnValue(tx, 2);
750         pm.close();
751         pmControl.setVoidCallable(1);
752         TransactionTemplate tt = new TransactionTemplate();
753         dialect.beginTransaction(tx, tt);
754         dialectControl.setReturnValue(null, 1);
755         dialect.getJdbcConnection(pm, false);
756         dialectControl.setReturnValue(conHandle, 1);
757         dialect.releaseJdbcConnection(conHandle, pm);
758         dialectControl.setVoidCallable(1);
759         dialect.cleanupTransaction(null);
760         dialectControl.setVoidCallable(1);
761         tx.commit();
762         txControl.setVoidCallable(1);
763         pmfControl.replay();
764         dsControl.replay();
765         dialectControl.replay();
766         pmControl.replay();
767         txControl.replay();
768         conControl.replay();
769
770         JdoTransactionManager tm = new JdoTransactionManager();
771         tm.setPersistenceManagerFactory(pmf);
772         tm.setJdoDialect(dialect);
773         tm.afterPropertiesSet();
774         tt.setTransactionManager(tm);
775         final List JavaDoc l = new ArrayList JavaDoc();
776         l.add("test");
777         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
778
779         Object JavaDoc result = tt.execute(new TransactionCallback() {
780             public Object JavaDoc doInTransaction(TransactionStatus status) {
781                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
782                 assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
783                 JdoTemplate jt = new JdoTemplate(pmf);
784                 return jt.execute(new JdoCallback() {
785                     public Object JavaDoc doInJdo(PersistenceManager pm) {
786                         return l;
787                     }
788                 });
789             }
790         });
791         assertTrue("Correct result list", result == l);
792
793         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
794         assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
795         pmfControl.verify();
796         dsControl.verify();
797         dialectControl.verify();
798         pmControl.verify();
799         txControl.verify();
800         conControl.verify();
801     }
802
803     public void testTransactionCommitWithAutoDetectedDataSourceAndNoConnection() throws SQLException JavaDoc {
804         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
805         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
806         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
807         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
808         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
809         final JdoDialect dialect = (JdoDialect) dialectControl.getMock();
810         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
811         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
812         MockControl txControl = MockControl.createControl(Transaction.class);
813         Transaction tx = (Transaction) txControl.getMock();
814         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
815
816         pmf.getConnectionFactory();
817         pmfControl.setReturnValue(ds, 1);
818         pmf.getPersistenceManager();
819         pmfControl.setReturnValue(pm, 1);
820         pm.currentTransaction();
821         pmControl.setReturnValue(tx, 2);
822         pm.flush();
823         pmControl.setVoidCallable(1);
824         pm.close();
825         pmControl.setVoidCallable(1);
826         TransactionTemplate tt = new TransactionTemplate();
827         dialect.beginTransaction(tx, tt);
828         dialectControl.setReturnValue(null, 1);
829         dialect.getJdbcConnection(pm, false);
830         dialectControl.setReturnValue(null, 1);
831         dialect.cleanupTransaction(null);
832         dialectControl.setVoidCallable(1);
833         tx.commit();
834         txControl.setVoidCallable(1);
835         pmfControl.replay();
836         dsControl.replay();
837         dialectControl.replay();
838         pmControl.replay();
839         txControl.replay();
840         conControl.replay();
841
842         JdoTransactionManager tm = new JdoTransactionManager();
843         tm.setPersistenceManagerFactory(pmf);
844         tm.setJdoDialect(dialect);
845         tm.afterPropertiesSet();
846         tt.setTransactionManager(tm);
847         final List JavaDoc l = new ArrayList JavaDoc();
848         l.add("test");
849         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
850
851         Object JavaDoc result = tt.execute(new TransactionCallback() {
852             public Object JavaDoc doInTransaction(TransactionStatus status) {
853                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
854                 assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
855                 JdoTemplate jt = new JdoTemplate();
856                 jt.setPersistenceManagerFactory(pmf);
857                 jt.setJdoDialect(dialect);
858                 return jt.execute(new JdoCallback() {
859                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
860                         pm2.flush();
861                         return l;
862                     }
863                 });
864             }
865         });
866         assertTrue("Correct result list", result == l);
867
868         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
869         assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
870         pmfControl.verify();
871         dsControl.verify();
872         dialectControl.verify();
873         pmControl.verify();
874         txControl.verify();
875         conControl.verify();
876     }
877
878     public void testExistingTransactionWithPropagationNestedAndRollback() throws SQLException JavaDoc {
879         doTestExistingTransactionWithPropagationNestedAndRollback(false);
880     }
881
882     public void testExistingTransactionWithManualSavepointAndRollback() throws SQLException JavaDoc {
883         doTestExistingTransactionWithPropagationNestedAndRollback(true);
884     }
885
886     private void doTestExistingTransactionWithPropagationNestedAndRollback(final boolean manualSavepoint)
887             throws SQLException JavaDoc {
888
889         if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
890             return;
891         }
892
893         MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
894         final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
895         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
896         final DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
897         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
898         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
899         MockControl pmControl = MockControl.createControl(PersistenceManager.class);
900         final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
901         MockControl txControl = MockControl.createControl(Transaction.class);
902         Transaction tx = (Transaction) txControl.getMock();
903         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
904         final Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
905         MockControl mdControl = MockControl.createControl(DatabaseMetaData JavaDoc.class);
906         DatabaseMetaData JavaDoc md = (DatabaseMetaData JavaDoc) mdControl.getMock();
907         MockControl spControl = MockControl.createControl(Savepoint JavaDoc.class);
908         Savepoint JavaDoc sp = (Savepoint JavaDoc) spControl.getMock();
909
910         pmf.getPersistenceManager();
911         pmfControl.setReturnValue(pm, 1);
912         pm.currentTransaction();
913         pmControl.setReturnValue(tx, manualSavepoint ? 2 : 3);
914         pm.flush();
915         pmControl.setVoidCallable(1);
916         pm.close();
917         pmControl.setVoidCallable(1);
918         md.supportsSavepoints();
919         mdControl.setReturnValue(true, 1);
920         con.getMetaData();
921         conControl.setReturnValue(md, 1);
922         con.setSavepoint();
923         conControl.setReturnValue(sp, 1);
924         con.rollback(sp);
925         conControl.setVoidCallable(1);
926         final TransactionTemplate tt = new TransactionTemplate();
927         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
928         dialect.beginTransaction(tx, tt);
929         dialectControl.setReturnValue(null, 1);
930         ConnectionHandle conHandle = new SimpleConnectionHandle(con);
931         dialect.getJdbcConnection(pm, false);
932         dialectControl.setReturnValue(conHandle, 1);
933         dialect.releaseJdbcConnection(conHandle, pm);
934         dialectControl.setVoidCallable(1);
935         dialect.cleanupTransaction(null);
936         dialectControl.setVoidCallable(1);
937         if (!manualSavepoint) {
938             tx.isActive();
939             txControl.setReturnValue(true, 1);
940         }
941         tx.commit();
942         txControl.setVoidCallable(1);
943         pmfControl.replay();
944         dsControl.replay();
945         dialectControl.replay();
946         pmControl.replay();
947         txControl.replay();
948         conControl.replay();
949         mdControl.replay();
950         spControl.replay();
951
952         JdoTransactionManager tm = new JdoTransactionManager();
953         tm.setNestedTransactionAllowed(true);
954         tm.setPersistenceManagerFactory(pmf);
955         tm.setDataSource(ds);
956         tm.setJdoDialect(dialect);
957         tt.setTransactionManager(tm);
958         final List JavaDoc l = new ArrayList JavaDoc();
959         l.add("test");
960         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
961
962         Object JavaDoc result = tt.execute(new TransactionCallback() {
963             public Object JavaDoc doInTransaction(TransactionStatus status) {
964                 assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
965                 assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
966                 if (manualSavepoint) {
967                     Object JavaDoc savepoint = status.createSavepoint();
968                     status.rollbackToSavepoint(savepoint);
969                 }
970                 else {
971                     tt.execute(new TransactionCallbackWithoutResult() {
972                         protected void doInTransactionWithoutResult(TransactionStatus status) {
973                             assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(pmf));
974                             assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
975                             status.setRollbackOnly();
976                         }
977                     });
978                 }
979                 JdoTemplate jt = new JdoTemplate(pmf);
980                 return jt.execute(new JdoCallback() {
981                     public Object JavaDoc doInJdo(PersistenceManager pm2) {
982                         pm2.flush();
983                         return l;
984                     }
985                 });
986             }
987         });
988         assertTrue("Correct result list", result == l);
989
990         assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
991         assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
992         pmfControl.verify();
993         dsControl.verify();
994         dialectControl.verify();
995         pmControl.verify();
996         txControl.verify();
997         conControl.verify();
998         mdControl.verify();
999         spControl.verify();
1000    }
1001
1002    public void testTransactionTimeoutWithJdoDialect() throws SQLException JavaDoc {
1003        doTestTransactionTimeoutWithJdoDialect(true);
1004    }
1005
1006    public void testTransactionTimeoutWithJdoDialectAndPmProxy() throws SQLException JavaDoc {
1007        doTestTransactionTimeoutWithJdoDialect(false);
1008    }
1009
1010    private void doTestTransactionTimeoutWithJdoDialect(final boolean exposeNativePm) throws SQLException JavaDoc {
1011        MockControl pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
1012        final PersistenceManagerFactory pmf = (PersistenceManagerFactory) pmfControl.getMock();
1013        MockControl pmControl = MockControl.createControl(PersistenceManager.class);
1014        final PersistenceManager pm = (PersistenceManager) pmControl.getMock();
1015        MockControl txControl = MockControl.createControl(Transaction.class);
1016        Transaction tx = (Transaction) txControl.getMock();
1017        MockControl queryControl = MockControl.createControl(Query.class);
1018        Query query = (Query) queryControl.getMock();
1019        MockControl dialectControl = MockControl.createControl(JdoDialect.class);
1020        final JdoDialect dialect = (JdoDialect) dialectControl.getMock();
1021
1022        TransactionTemplate tt = new TransactionTemplate();
1023
1024        pmf.getConnectionFactory();
1025        pmfControl.setReturnValue(null, 1);
1026        pmf.getPersistenceManager();
1027        pmfControl.setReturnValue(pm, 1);
1028        pm.currentTransaction();
1029        pmControl.setReturnValue(tx, 2);
1030        dialect.beginTransaction(tx, tt);
1031        dialectControl.setReturnValue(null, 1);
1032        if (!exposeNativePm) {
1033            dialect.applyQueryTimeout(query, 10);
1034        }
1035        dialect.cleanupTransaction(null);
1036        dialectControl.setVoidCallable(1);
1037        pm.newQuery(TestBean.class);
1038        pmControl.setReturnValue(query, 1);
1039        pm.close();
1040        pmControl.setVoidCallable(1);
1041        tx.commit();
1042        txControl.setVoidCallable(1);
1043
1044        pmfControl.replay();
1045        pmControl.replay();
1046        txControl.replay();
1047        queryControl.replay();
1048        dialectControl.replay();
1049
1050        JdoTransactionManager tm = new JdoTransactionManager(pmf);
1051        tm.setJdoDialect(dialect);
1052        tt.setTransactionManager(tm);
1053        tt.setTimeout(10);
1054
1055        assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
1056        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1057
1058        tt.execute(new TransactionCallback() {
1059            public Object JavaDoc doInTransaction(TransactionStatus status) {
1060                assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
1061                JdoTemplate jt = new JdoTemplate(pmf);
1062                jt.setJdoDialect(dialect);
1063                if (exposeNativePm) {
1064                    jt.setExposeNativePersistenceManager(true);
1065                }
1066                return jt.execute(new JdoCallback() {
1067                    public Object JavaDoc doInJdo(PersistenceManager pm2) {
1068                        if (exposeNativePm) {
1069                            assertSame(pm, pm2);
1070                        }
1071                        else {
1072                            assertTrue(Proxy.isProxyClass(pm2.getClass()));
1073                        }
1074                        pm2.newQuery(TestBean.class);
1075                        return null;
1076                    }
1077                });
1078            }
1079        });
1080
1081        assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
1082        assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
1083
1084        pmfControl.verify();
1085        pmControl.verify();
1086        txControl.verify();
1087        dialectControl.verify();
1088        queryControl.verify();
1089    }
1090
1091    protected void tearDown() {
1092        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
1093        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
1094        assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
1095        assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
1096    }
1097
1098}
1099
Popular Tags