KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.jdo.JDODataStoreException;
27 import javax.jdo.JDOException;
28 import javax.jdo.JDOFatalDataStoreException;
29 import javax.jdo.JDOFatalUserException;
30 import javax.jdo.JDOObjectNotFoundException;
31 import javax.jdo.JDOOptimisticVerificationException;
32 import javax.jdo.JDOUserException;
33 import javax.jdo.PersistenceManager;
34 import javax.jdo.PersistenceManagerFactory;
35 import javax.jdo.Query;
36
37 import junit.framework.TestCase;
38 import org.easymock.MockControl;
39
40 import org.springframework.dao.DataIntegrityViolationException;
41 import org.springframework.transaction.support.TransactionSynchronizationManager;
42
43 /**
44  * @author Juergen Hoeller
45  * @since 03.06.2003
46  */

47 public class JdoTemplateTests extends TestCase {
48
49     private MockControl pmfControl;
50     private PersistenceManagerFactory pmf;
51     private MockControl pmControl;
52     private PersistenceManager pm;
53
54     protected void setUp() {
55         pmfControl = MockControl.createControl(PersistenceManagerFactory.class);
56         pmf = (PersistenceManagerFactory) pmfControl.getMock();
57         pmControl = MockControl.createControl(PersistenceManager.class);
58         pm = (PersistenceManager) pmControl.getMock();
59     }
60
61     protected void tearDown() {
62         try {
63             pmfControl.verify();
64             pmControl.verify();
65         }
66         catch (IllegalStateException JavaDoc ex) {
67             // ignore: test method didn't call replay
68
}
69     }
70
71     public void testTemplateExecuteWithNotAllowCreate() {
72         JdoTemplate jt = new JdoTemplate();
73         jt.setPersistenceManagerFactory(pmf);
74         jt.setAllowCreate(false);
75         try {
76             jt.execute(new JdoCallback() {
77                 public Object JavaDoc doInJdo(PersistenceManager pm) {
78                     return null;
79                 }
80             });
81             fail("Should have thrown IllegalStateException");
82         }
83         catch (IllegalStateException JavaDoc ex) {
84             // expected
85
}
86     }
87
88     public void testTemplateExecuteWithNotAllowCreateAndThreadBound() {
89         pmfControl.replay();
90         pmControl.replay();
91
92         JdoTemplate jt = new JdoTemplate(pmf);
93         jt.setAllowCreate(false);
94         TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
95         final List JavaDoc l = new ArrayList JavaDoc();
96         l.add("test");
97         List JavaDoc result = (List JavaDoc) jt.execute(new JdoCallback() {
98             public Object JavaDoc doInJdo(PersistenceManager pm) {
99                 return l;
100             }
101         });
102         assertTrue("Correct result list", result == l);
103         TransactionSynchronizationManager.unbindResource(pmf);
104     }
105
106     public void testTemplateExecuteWithNewPersistenceManager() {
107         pmf.getPersistenceManager();
108         pmfControl.setReturnValue(pm, 1);
109         pm.close();
110         pmControl.setVoidCallable(1);
111         pmfControl.replay();
112         pmControl.replay();
113
114         JdoTemplate jt = new JdoTemplate(pmf);
115         final List JavaDoc l = new ArrayList JavaDoc();
116         l.add("test");
117         List JavaDoc result = (List JavaDoc) jt.execute(new JdoCallback() {
118             public Object JavaDoc doInJdo(PersistenceManager pm) {
119                 return l;
120             }
121         });
122         assertTrue("Correct result list", result == l);
123     }
124
125     public void testTemplateExecuteWithThreadBoundAndFlushEager() {
126         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
127         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
128
129         dialect.flush(pm);
130         dialectControl.setVoidCallable(1);
131         pmfControl.replay();
132         pmControl.replay();
133         dialectControl.replay();
134
135         JdoTemplate jt = new JdoTemplate(pmf);
136         jt.setJdoDialect(dialect);
137         jt.setFlushEager(true);
138         jt.setAllowCreate(false);
139         TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
140         final List JavaDoc l = new ArrayList JavaDoc();
141         l.add("test");
142         List JavaDoc result = (List JavaDoc) jt.execute(new JdoCallback() {
143             public Object JavaDoc doInJdo(PersistenceManager pm) {
144                 return l;
145             }
146         });
147         assertTrue("Correct result list", result == l);
148         TransactionSynchronizationManager.unbindResource(pmf);
149         dialectControl.verify();
150     }
151
152     public void testGetObjectById() {
153         pmf.getPersistenceManager();
154         pmfControl.setReturnValue(pm);
155         pm.getObjectById("0", true);
156         pmControl.setReturnValue("A");
157         pm.close();
158         pmControl.setVoidCallable(1);
159         pmfControl.replay();
160         pmControl.replay();
161
162         JdoTemplate jt = new JdoTemplate(pmf);
163         assertEquals("A", jt.getObjectById("0"));
164     }
165
166     public void testGetObjectByIdWithClassAndValue() {
167         pmf.getPersistenceManager();
168         pmfControl.setReturnValue(pm);
169         pm.getObjectById(String JavaDoc.class, "0");
170         pmControl.setReturnValue("A");
171         pm.close();
172         pmControl.setVoidCallable(1);
173         pmfControl.replay();
174         pmControl.replay();
175
176         JdoTemplate jt = new JdoTemplate(pmf);
177         assertEquals("A", jt.getObjectById(String JavaDoc.class, "0"));
178     }
179
180     public void testEvict() {
181         pmf.getPersistenceManager();
182         pmfControl.setReturnValue(pm);
183         pm.evict("0");
184         pmControl.setVoidCallable(1);
185         pm.close();
186         pmControl.setVoidCallable(1);
187         pmfControl.replay();
188         pmControl.replay();
189
190         JdoTemplate jt = new JdoTemplate(pmf);
191         jt.evict("0");
192     }
193
194     public void testEvictAllWithCollection() {
195         Collection JavaDoc coll = new HashSet JavaDoc();
196
197         pmf.getPersistenceManager();
198         pmfControl.setReturnValue(pm);
199         pm.evictAll(coll);
200         pmControl.setVoidCallable(1);
201         pm.close();
202         pmControl.setVoidCallable(1);
203         pmfControl.replay();
204         pmControl.replay();
205
206         JdoTemplate jt = new JdoTemplate(pmf);
207         jt.evictAll(coll);
208     }
209
210     public void testEvictAll() {
211         pmf.getPersistenceManager();
212         pmfControl.setReturnValue(pm);
213         pm.evictAll();
214         pmControl.setVoidCallable(1);
215         pm.close();
216         pmControl.setVoidCallable(1);
217         pmfControl.replay();
218         pmControl.replay();
219
220         JdoTemplate jt = new JdoTemplate(pmf);
221         jt.evictAll();
222     }
223
224     public void testRefresh() {
225         pmf.getPersistenceManager();
226         pmfControl.setReturnValue(pm);
227         pm.refresh("0");
228         pmControl.setVoidCallable(1);
229         pm.close();
230         pmControl.setVoidCallable(1);
231         pmfControl.replay();
232         pmControl.replay();
233
234         JdoTemplate jt = new JdoTemplate(pmf);
235         jt.refresh("0");
236     }
237
238     public void testRefreshAllWithCollection() {
239         Collection JavaDoc coll = new HashSet JavaDoc();
240
241         pmf.getPersistenceManager();
242         pmfControl.setReturnValue(pm);
243         pm.refreshAll(coll);
244         pmControl.setVoidCallable(1);
245         pm.close();
246         pmControl.setVoidCallable(1);
247         pmfControl.replay();
248         pmControl.replay();
249
250         JdoTemplate jt = new JdoTemplate(pmf);
251         jt.refreshAll(coll);
252     }
253
254     public void testRefreshAll() {
255         pmf.getPersistenceManager();
256         pmfControl.setReturnValue(pm);
257         pm.refreshAll();
258         pmControl.setVoidCallable(1);
259         pm.close();
260         pmControl.setVoidCallable(1);
261         pmfControl.replay();
262         pmControl.replay();
263
264         JdoTemplate jt = new JdoTemplate(pmf);
265         jt.refreshAll();
266     }
267
268     public void testMakePersistent() {
269         pmf.getPersistenceManager();
270         pmfControl.setReturnValue(pm);
271         pm.makePersistent("0");
272         pmControl.setVoidCallable(1);
273         pm.close();
274         pmControl.setVoidCallable(1);
275         pmfControl.replay();
276         pmControl.replay();
277
278         JdoTemplate jt = new JdoTemplate(pmf);
279         jt.makePersistent("0");
280     }
281
282     public void testMakePersistentAll() {
283         Collection JavaDoc coll = new HashSet JavaDoc();
284
285         pmf.getPersistenceManager();
286         pmfControl.setReturnValue(pm);
287         pm.makePersistentAll(coll);
288         pmControl.setVoidCallable(1);
289         pm.close();
290         pmControl.setVoidCallable(1);
291         pmfControl.replay();
292         pmControl.replay();
293
294         JdoTemplate jt = new JdoTemplate(pmf);
295         jt.makePersistentAll(coll);
296     }
297
298     public void testDeletePersistent() {
299         pmf.getPersistenceManager();
300         pmfControl.setReturnValue(pm);
301         pm.deletePersistent("0");
302         pmControl.setVoidCallable(1);
303         pm.close();
304         pmControl.setVoidCallable(1);
305         pmfControl.replay();
306         pmControl.replay();
307
308         JdoTemplate jt = new JdoTemplate(pmf);
309         jt.deletePersistent("0");
310     }
311
312     public void testDeletePersistentAll() {
313         Collection JavaDoc coll = new HashSet JavaDoc();
314
315         pmf.getPersistenceManager();
316         pmfControl.setReturnValue(pm);
317         pm.deletePersistentAll(coll);
318         pmControl.setVoidCallable(1);
319         pm.close();
320         pmControl.setVoidCallable(1);
321         pmfControl.replay();
322         pmControl.replay();
323
324         JdoTemplate jt = new JdoTemplate(pmf);
325         jt.deletePersistentAll(coll);
326     }
327
328     public void testDetachCopy() {
329         pmf.getPersistenceManager();
330         pmfControl.setReturnValue(pm);
331         pm.detachCopy("0");
332         pmControl.setReturnValue("0x", 1);
333         pm.close();
334         pmControl.setVoidCallable(1);
335         pmfControl.replay();
336         pmControl.replay();
337
338         JdoTemplate jt = new JdoTemplate(pmf);
339         assertEquals("0x", jt.detachCopy("0"));
340     }
341
342     public void testDetachCopyAll() {
343         Collection JavaDoc attached = new HashSet JavaDoc();
344         Collection JavaDoc detached = new HashSet JavaDoc();
345
346         pmf.getPersistenceManager();
347         pmfControl.setReturnValue(pm);
348         pm.detachCopyAll(attached);
349         pmControl.setReturnValue(detached, 1);
350         pm.close();
351         pmControl.setVoidCallable(1);
352         pmfControl.replay();
353         pmControl.replay();
354
355         JdoTemplate jt = new JdoTemplate(pmf);
356         assertEquals(detached, jt.detachCopyAll(attached));
357     }
358
359     public void testAttachCopy() {
360         pmf.getPersistenceManager();
361         pmfControl.setReturnValue(pm);
362         pm.attachCopy("0x", true);
363         pmControl.setReturnValue("0", 1);
364         pm.close();
365         pmControl.setVoidCallable(1);
366         pmfControl.replay();
367         pmControl.replay();
368
369         JdoTemplate jt = new JdoTemplate(pmf);
370         assertEquals("0", jt.attachCopy("0x"));
371     }
372
373     public void testAttachCopyAll() {
374         Collection JavaDoc detached = new HashSet JavaDoc();
375         Collection JavaDoc attached = new HashSet JavaDoc();
376
377         pmf.getPersistenceManager();
378         pmfControl.setReturnValue(pm);
379         pm.attachCopyAll(detached, true);
380         pmControl.setReturnValue(attached, 1);
381         pm.close();
382         pmControl.setVoidCallable(1);
383         pmfControl.replay();
384         pmControl.replay();
385
386         JdoTemplate jt = new JdoTemplate(pmf);
387         assertEquals(attached, jt.attachCopyAll(detached));
388     }
389
390     public void testFlush() {
391         pmf.getPersistenceManager();
392         pmfControl.setReturnValue(pm);
393         pm.flush();
394         pmControl.setVoidCallable(1);
395         pm.close();
396         pmControl.setVoidCallable(1);
397         pmfControl.replay();
398         pmControl.replay();
399
400         JdoTemplate jt = new JdoTemplate(pmf);
401         jt.flush();
402     }
403
404     public void testFlushWithDialect() {
405         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
406         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
407
408         pmf.getPersistenceManager();
409         pmfControl.setReturnValue(pm);
410         dialect.flush(pm);
411         dialectControl.setVoidCallable(1);
412         pm.close();
413         pmControl.setVoidCallable(1);
414         pmfControl.replay();
415         pmControl.replay();
416         dialectControl.replay();
417
418         JdoTemplate jt = new JdoTemplate(pmf);
419         jt.setJdoDialect(dialect);
420         jt.flush();
421         dialectControl.verify();
422     }
423
424     public void testFind() {
425         MockControl queryControl = MockControl.createControl(Query.class);
426         Query query = (Query) queryControl.getMock();
427
428         pmf.getPersistenceManager();
429         pmfControl.setReturnValue(pm);
430         pm.newQuery(String JavaDoc.class);
431         pmControl.setReturnValue(query);
432         Collection JavaDoc coll = new HashSet JavaDoc();
433         query.execute();
434         queryControl.setReturnValue(coll);
435         pm.close();
436         pmControl.setVoidCallable(1);
437         pmfControl.replay();
438         pmControl.replay();
439         queryControl.replay();
440
441         JdoTemplate jt = new JdoTemplate(pmf);
442         assertEquals(coll, jt.find(String JavaDoc.class));
443         queryControl.verify();
444     }
445
446     public void testFindWithFilter() {
447         MockControl queryControl = MockControl.createControl(Query.class);
448         Query query = (Query) queryControl.getMock();
449
450         pmf.getPersistenceManager();
451         pmfControl.setReturnValue(pm);
452         pm.newQuery(String JavaDoc.class, "a == b");
453         pmControl.setReturnValue(query);
454         Collection JavaDoc coll = new HashSet JavaDoc();
455         query.execute();
456         queryControl.setReturnValue(coll);
457         pm.close();
458         pmControl.setVoidCallable(1);
459         pmfControl.replay();
460         pmControl.replay();
461         queryControl.replay();
462
463         JdoTemplate jt = new JdoTemplate(pmf);
464         assertEquals(coll, jt.find(String JavaDoc.class, "a == b"));
465         queryControl.verify();
466     }
467
468     public void testFindWithFilterAndOrdering() {
469         MockControl queryControl = MockControl.createControl(Query.class);
470         Query query = (Query) queryControl.getMock();
471
472         pmf.getPersistenceManager();
473         pmfControl.setReturnValue(pm);
474         pm.newQuery(String JavaDoc.class, "a == b");
475         pmControl.setReturnValue(query);
476         query.setOrdering("c asc");
477         queryControl.setVoidCallable(1);
478         Collection JavaDoc coll = new HashSet JavaDoc();
479         query.execute();
480         queryControl.setReturnValue(coll);
481         pm.close();
482         pmControl.setVoidCallable(1);
483         pmfControl.replay();
484         pmControl.replay();
485         queryControl.replay();
486
487         JdoTemplate jt = new JdoTemplate(pmf);
488         assertEquals(coll, jt.find(String JavaDoc.class, "a == b", "c asc"));
489         queryControl.verify();
490     }
491
492     public void testFindWithParameterArray() {
493         MockControl queryControl = MockControl.createControl(Query.class);
494         Query query = (Query) queryControl.getMock();
495
496         pmf.getPersistenceManager();
497         pmfControl.setReturnValue(pm);
498         pm.newQuery(String JavaDoc.class, "a == b");
499         pmControl.setReturnValue(query);
500         query.declareParameters("params");
501         queryControl.setVoidCallable(1);
502         Object JavaDoc[] values = new Object JavaDoc[0];
503         Collection JavaDoc coll = new HashSet JavaDoc();
504         query.executeWithArray(values);
505         queryControl.setReturnValue(coll);
506         pm.close();
507         pmControl.setVoidCallable(1);
508         pmfControl.replay();
509         pmControl.replay();
510         queryControl.replay();
511
512         JdoTemplate jt = new JdoTemplate(pmf);
513         assertEquals(coll, jt.find(String JavaDoc.class, "a == b", "params", values));
514         queryControl.verify();
515     }
516
517     public void testFindWithParameterArrayAndOrdering() {
518         MockControl queryControl = MockControl.createControl(Query.class);
519         Query query = (Query) queryControl.getMock();
520
521         pmf.getPersistenceManager();
522         pmfControl.setReturnValue(pm);
523         pm.newQuery(String JavaDoc.class, "a == b");
524         pmControl.setReturnValue(query);
525         query.declareParameters("params");
526         queryControl.setVoidCallable(1);
527         query.setOrdering("c asc");
528         queryControl.setVoidCallable(1);
529         Object JavaDoc[] values = new Object JavaDoc[0];
530         Collection JavaDoc coll = new HashSet JavaDoc();
531         query.executeWithArray(values);
532         queryControl.setReturnValue(coll);
533         pm.close();
534         pmControl.setVoidCallable(1);
535         pmfControl.replay();
536         pmControl.replay();
537         queryControl.replay();
538
539         JdoTemplate jt = new JdoTemplate(pmf);
540         assertEquals(coll, jt.find(String JavaDoc.class, "a == b", "params", values, "c asc"));
541         queryControl.verify();
542     }
543
544     public void testFindWithParameterMap() {
545         MockControl queryControl = MockControl.createControl(Query.class);
546         Query query = (Query) queryControl.getMock();
547
548         pmf.getPersistenceManager();
549         pmfControl.setReturnValue(pm);
550         pm.newQuery(String JavaDoc.class, "a == b");
551         pmControl.setReturnValue(query);
552         query.declareParameters("params");
553         queryControl.setVoidCallable(1);
554         Map JavaDoc values = new HashMap JavaDoc();
555         Collection JavaDoc coll = new HashSet JavaDoc();
556         query.executeWithMap(values);
557         queryControl.setReturnValue(coll);
558         pm.close();
559         pmControl.setVoidCallable(1);
560         pmfControl.replay();
561         pmControl.replay();
562         queryControl.replay();
563
564         JdoTemplate jt = new JdoTemplate(pmf);
565         assertEquals(coll, jt.find(String JavaDoc.class, "a == b", "params", values));
566         queryControl.verify();
567     }
568
569     public void testFindWithParameterMapAndOrdering() {
570         MockControl queryControl = MockControl.createControl(Query.class);
571         Query query = (Query) queryControl.getMock();
572
573         pmf.getPersistenceManager();
574         pmfControl.setReturnValue(pm);
575         pm.newQuery(String JavaDoc.class, "a == b");
576         pmControl.setReturnValue(query);
577         query.declareParameters("params");
578         queryControl.setVoidCallable(1);
579         query.setOrdering("c asc");
580         queryControl.setVoidCallable(1);
581         Map JavaDoc values = new HashMap JavaDoc();
582         Collection JavaDoc coll = new HashSet JavaDoc();
583         query.executeWithMap(values);
584         queryControl.setReturnValue(coll);
585         pm.close();
586         pmControl.setVoidCallable(1);
587         pmfControl.replay();
588         pmControl.replay();
589         queryControl.replay();
590
591         JdoTemplate jt = new JdoTemplate(pmf);
592         assertEquals(coll, jt.find(String JavaDoc.class, "a == b", "params", values, "c asc"));
593         queryControl.verify();
594     }
595
596     public void testFindWithLanguageAndQueryObject() {
597         MockControl queryControl = MockControl.createControl(Query.class);
598         Query query = (Query) queryControl.getMock();
599
600         pmf.getPersistenceManager();
601         pmfControl.setReturnValue(pm);
602         pm.newQuery(Query.SQL, "some SQL");
603         pmControl.setReturnValue(query);
604         Collection JavaDoc coll = new HashSet JavaDoc();
605         query.execute();
606         queryControl.setReturnValue(coll);
607         pm.close();
608         pmControl.setVoidCallable(1);
609         pmfControl.replay();
610         pmControl.replay();
611         queryControl.replay();
612
613         JdoTemplate jt = new JdoTemplate(pmf);
614         assertEquals(coll, jt.find(Query.SQL, "some SQL"));
615         queryControl.verify();
616     }
617
618     public void testFindWithQueryString() {
619         MockControl queryControl = MockControl.createControl(Query.class);
620         Query query = (Query) queryControl.getMock();
621
622         pmf.getPersistenceManager();
623         pmfControl.setReturnValue(pm);
624         pm.newQuery("single string query");
625         pmControl.setReturnValue(query);
626         Collection JavaDoc coll = new HashSet JavaDoc();
627         query.execute();
628         queryControl.setReturnValue(coll);
629         pm.close();
630         pmControl.setVoidCallable(1);
631         pmfControl.replay();
632         pmControl.replay();
633         queryControl.replay();
634
635         JdoTemplate jt = new JdoTemplate(pmf);
636         assertEquals(coll, jt.find("single string query"));
637         queryControl.verify();
638     }
639
640     public void testFindByNamedQuery() {
641         MockControl queryControl = MockControl.createControl(Query.class);
642         Query query = (Query) queryControl.getMock();
643
644         pmf.getPersistenceManager();
645         pmfControl.setReturnValue(pm);
646         pm.newNamedQuery(String JavaDoc.class, "some query name");
647         pmControl.setReturnValue(query);
648         Collection JavaDoc coll = new HashSet JavaDoc();
649         query.execute();
650         queryControl.setReturnValue(coll);
651         pm.close();
652         pmControl.setVoidCallable(1);
653         pmfControl.replay();
654         pmControl.replay();
655         queryControl.replay();
656
657         JdoTemplate jt = new JdoTemplate(pmf);
658         assertEquals(coll, jt.findByNamedQuery(String JavaDoc.class, "some query name"));
659         queryControl.verify();
660     }
661
662     public void testTemplateExceptions() {
663         try {
664             createTemplate().execute(new JdoCallback() {
665                 public Object JavaDoc doInJdo(PersistenceManager pm) {
666                     throw new JDOObjectNotFoundException();
667                 }
668             });
669             fail("Should have thrown JdoObjectRetrievalFailureException");
670         }
671         catch (JdoObjectRetrievalFailureException ex) {
672             // expected
673
}
674
675         try {
676             createTemplate().execute(new JdoCallback() {
677                 public Object JavaDoc doInJdo(PersistenceManager pm) {
678                     throw new JDOOptimisticVerificationException();
679                 }
680             });
681             fail("Should have thrown JdoOptimisticLockingFailureException");
682         }
683         catch (JdoOptimisticLockingFailureException ex) {
684             // expected
685
}
686
687         try {
688             createTemplate().execute(new JdoCallback() {
689                 public Object JavaDoc doInJdo(PersistenceManager pm) {
690                     throw new JDODataStoreException();
691                 }
692             });
693             fail("Should have thrown JdoResourceFailureException");
694         }
695         catch (JdoResourceFailureException ex) {
696             // expected
697
}
698
699         try {
700             createTemplate().execute(new JdoCallback() {
701                 public Object JavaDoc doInJdo(PersistenceManager pm) {
702                     throw new JDOFatalDataStoreException();
703                 }
704             });
705             fail("Should have thrown JdoResourceFailureException");
706         }
707         catch (JdoResourceFailureException ex) {
708             // expected
709
}
710
711         try {
712             createTemplate().execute(new JdoCallback() {
713                 public Object JavaDoc doInJdo(PersistenceManager pm) {
714                     throw new JDOUserException();
715                 }
716             });
717             fail("Should have thrown JdoUsageException");
718         }
719         catch (JdoUsageException ex) {
720             // expected
721
}
722
723         try {
724             createTemplate().execute(new JdoCallback() {
725                 public Object JavaDoc doInJdo(PersistenceManager pm) {
726                     throw new JDOFatalUserException();
727                 }
728             });
729             fail("Should have thrown JdoUsageException");
730         }
731         catch (JdoUsageException ex) {
732             // expected
733
}
734
735         try {
736             createTemplate().execute(new JdoCallback() {
737                 public Object JavaDoc doInJdo(PersistenceManager pm) {
738                     throw new JDOException();
739                 }
740             });
741             fail("Should have thrown JdoSystemException");
742         }
743         catch (JdoSystemException ex) {
744             // expected
745
}
746     }
747
748     public void testTranslateException() {
749         MockControl dialectControl = MockControl.createControl(JdoDialect.class);
750         JdoDialect dialect = (JdoDialect) dialectControl.getMock();
751         final JDOException ex = new JDOException();
752         dialect.translateException(ex);
753         dialectControl.setReturnValue(new DataIntegrityViolationException("test", ex));
754         dialectControl.replay();
755         try {
756             JdoTemplate template = createTemplate();
757             template.setJdoDialect(dialect);
758             template.execute(new JdoCallback() {
759                 public Object JavaDoc doInJdo(PersistenceManager pm) {
760                     throw ex;
761                 }
762             });
763             fail("Should have thrown DataIntegrityViolationException");
764         }
765         catch (DataIntegrityViolationException dive) {
766             // expected
767
}
768         dialectControl.verify();
769     }
770
771     private JdoTemplate createTemplate() {
772         pmfControl.reset();
773         pmControl.reset();
774         pmf.getPersistenceManager();
775         pmfControl.setReturnValue(pm, 1);
776         pm.close();
777         pmControl.setVoidCallable(1);
778         pmfControl.replay();
779         pmControl.replay();
780         return new JdoTemplate(pmf);
781     }
782
783 }
784
Popular Tags