KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > HibernateTemplateTests


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.hibernate3;
18
19 import java.lang.reflect.Proxy JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29 import org.hibernate.Criteria;
30 import org.hibernate.FlushMode;
31 import org.hibernate.HibernateException;
32 import org.hibernate.Interceptor;
33 import org.hibernate.JDBCException;
34 import org.hibernate.LockMode;
35 import org.hibernate.ObjectDeletedException;
36 import org.hibernate.ObjectNotFoundException;
37 import org.hibernate.PersistentObjectException;
38 import org.hibernate.Query;
39 import org.hibernate.QueryException;
40 import org.hibernate.SessionFactory;
41 import org.hibernate.StaleObjectStateException;
42 import org.hibernate.TransientObjectException;
43 import org.hibernate.WrongClassException;
44 import org.hibernate.classic.Session;
45
46 import org.springframework.beans.TestBean;
47 import org.springframework.dao.DataIntegrityViolationException;
48 import org.springframework.dao.InvalidDataAccessApiUsageException;
49 import org.springframework.transaction.support.TransactionSynchronizationManager;
50
51 /**
52  * @author Juergen Hoeller
53  * @since 05.03.2005
54  */

55 public class HibernateTemplateTests extends TestCase {
56
57     private MockControl sfControl;
58     private SessionFactory sf;
59     private MockControl sessionControl;
60     private Session session;
61
62     protected void setUp() {
63         sfControl = MockControl.createControl(SessionFactory.class);
64         sf = (SessionFactory) sfControl.getMock();
65         sessionControl = MockControl.createControl(Session.class);
66         session = (Session) sessionControl.getMock();
67     }
68
69     public void testExecuteWithNewSession() throws HibernateException {
70         sf.openSession();
71         sfControl.setReturnValue(session, 1);
72         session.getSessionFactory();
73         sessionControl.setReturnValue(sf, 1);
74         session.flush();
75         sessionControl.setVoidCallable(1);
76         session.close();
77         sessionControl.setReturnValue(null, 1);
78         sfControl.replay();
79         sessionControl.replay();
80
81         HibernateTemplate ht = new HibernateTemplate(sf);
82         assertTrue("Correct allowCreate default", ht.isAllowCreate());
83         assertTrue("Correct flushMode default", ht.getFlushMode() == HibernateTemplate.FLUSH_AUTO);
84         final List JavaDoc l = new ArrayList JavaDoc();
85         l.add("test");
86         List JavaDoc result = ht.executeFind(new HibernateCallback() {
87             public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
88                 return l;
89             }
90         });
91         assertTrue("Correct result list", result == l);
92     }
93
94     public void testExecuteWithNewSessionAndFlushNever() throws HibernateException {
95         sf.openSession();
96         sfControl.setReturnValue(session, 1);
97         session.getSessionFactory();
98         sessionControl.setReturnValue(sf, 1);
99         session.setFlushMode(FlushMode.NEVER);
100         sessionControl.setVoidCallable(1);
101         session.close();
102         sessionControl.setReturnValue(null, 1);
103         sfControl.replay();
104         sessionControl.replay();
105
106         HibernateTemplate ht = new HibernateTemplate(sf);
107         ht.setFlushMode(HibernateTemplate.FLUSH_NEVER);
108         final List JavaDoc l = new ArrayList JavaDoc();
109         l.add("test");
110         List JavaDoc result = ht.executeFind(new HibernateCallback() {
111             public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
112                 return l;
113             }
114         });
115         assertTrue("Correct result list", result == l);
116     }
117
118     public void testExecuteWithNewSessionAndFilter() throws HibernateException {
119         sf.openSession();
120         sfControl.setReturnValue(session, 1);
121         session.getSessionFactory();
122         sessionControl.setReturnValue(sf, 1);
123         session.enableFilter("myFilter");
124         sessionControl.setReturnValue(null, 1);
125         session.disableFilter("myFilter");
126         sessionControl.setVoidCallable(1);
127         session.flush();
128         sessionControl.setVoidCallable(1);
129         session.close();
130         sessionControl.setReturnValue(null, 1);
131         sfControl.replay();
132         sessionControl.replay();
133
134         HibernateTemplate ht = new HibernateTemplate(sf);
135         ht.setFilterName("myFilter");
136         final List JavaDoc l = new ArrayList JavaDoc();
137         l.add("test");
138         List JavaDoc result = ht.executeFind(new HibernateCallback() {
139             public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
140                 return l;
141             }
142         });
143         assertTrue("Correct result list", result == l);
144     }
145
146     public void testExecuteWithNewSessionAndFilters() throws HibernateException {
147         sf.openSession();
148         sfControl.setReturnValue(session, 1);
149         session.getSessionFactory();
150         sessionControl.setReturnValue(sf, 1);
151         session.enableFilter("myFilter");
152         sessionControl.setReturnValue(null, 1);
153         session.enableFilter("yourFilter");
154         sessionControl.setReturnValue(null, 1);
155         session.disableFilter("myFilter");
156         sessionControl.setVoidCallable(1);
157         session.disableFilter("yourFilter");
158         sessionControl.setVoidCallable(1);
159         session.flush();
160         sessionControl.setVoidCallable(1);
161         session.close();
162         sessionControl.setReturnValue(null, 1);
163         sfControl.replay();
164         sessionControl.replay();
165
166         HibernateTemplate ht = new HibernateTemplate(sf);
167         ht.setFilterNames(new String JavaDoc[] {"myFilter", "yourFilter"});
168         final List JavaDoc l = new ArrayList JavaDoc();
169         l.add("test");
170         List JavaDoc result = ht.executeFind(new HibernateCallback() {
171             public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
172                 return l;
173             }
174         });
175         assertTrue("Correct result list", result == l);
176     }
177
178     public void testExecuteWithNotAllowCreate() {
179         HibernateTemplate ht = new HibernateTemplate();
180         ht.setSessionFactory(sf);
181         ht.setAllowCreate(false);
182         try {
183             ht.execute(new HibernateCallback() {
184                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException{
185                     return null;
186                 }
187             });
188             fail("Should have thrown IllegalStateException");
189         }
190         catch (IllegalStateException JavaDoc ex) {
191             // expected
192
}
193     }
194
195     public void testExecuteWithNotAllowCreateAndThreadBound() {
196         session.getSessionFactory();
197         sessionControl.setReturnValue(sf, 1);
198         sfControl.replay();
199         sessionControl.replay();
200
201         HibernateTemplate ht = new HibernateTemplate(sf);
202         ht.setAllowCreate(false);
203
204         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
205         try {
206             final List JavaDoc l = new ArrayList JavaDoc();
207             l.add("test");
208             List JavaDoc result = ht.executeFind(new HibernateCallback() {
209                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
210                     return l;
211                 }
212             });
213             assertTrue("Correct result list", result == l);
214         }
215         finally {
216             TransactionSynchronizationManager.unbindResource(sf);
217         }
218     }
219
220     public void testExecuteWithThreadBoundAndFlushEager() throws HibernateException {
221         session.getSessionFactory();
222         sessionControl.setReturnValue(sf, 1);
223         session.flush();
224         sessionControl.setVoidCallable(1);
225         sfControl.replay();
226         sessionControl.replay();
227
228         HibernateTemplate ht = new HibernateTemplate(sf);
229         ht.setFlushModeName("FLUSH_EAGER");
230         ht.setAllowCreate(false);
231
232         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
233         try {
234             final List JavaDoc l = new ArrayList JavaDoc();
235             l.add("test");
236             List JavaDoc result = ht.executeFind(new HibernateCallback() {
237                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
238                     return l;
239                 }
240             });
241             assertTrue("Correct result list", result == l);
242         }
243         finally {
244             TransactionSynchronizationManager.unbindResource(sf);
245         }
246     }
247
248     public void testExecuteWithThreadBoundAndFilter() {
249         session.getSessionFactory();
250         sessionControl.setReturnValue(sf, 1);
251         session.enableFilter("myFilter");
252         sessionControl.setReturnValue(null, 1);
253         session.disableFilter("myFilter");
254         sessionControl.setVoidCallable(1);
255         sfControl.replay();
256         sessionControl.replay();
257
258         HibernateTemplate ht = new HibernateTemplate(sf);
259         ht.setAllowCreate(false);
260         ht.setFilterName("myFilter");
261
262         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
263         try {
264             final List JavaDoc l = new ArrayList JavaDoc();
265             l.add("test");
266             List JavaDoc result = ht.executeFind(new HibernateCallback() {
267                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
268                     return l;
269                 }
270             });
271             assertTrue("Correct result list", result == l);
272         }
273         finally {
274             TransactionSynchronizationManager.unbindResource(sf);
275         }
276     }
277
278     public void testExecuteWithThreadBoundAndNewSession() throws HibernateException {
279         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
280         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
281         MockControl session2Control = MockControl.createControl(Session.class);
282         Session session2 = (Session) session2Control.getMock();
283
284         session2.connection();
285         session2Control.setReturnValue(con, 1);
286         sf.openSession(con);
287         sfControl.setReturnValue(session, 1);
288         session.flush();
289         sessionControl.setVoidCallable(1);
290         session.close();
291         sessionControl.setReturnValue(null, 1);
292         sfControl.replay();
293         sessionControl.replay();
294         session2Control.replay();
295
296         HibernateTemplate ht = new HibernateTemplate(sf);
297         ht.setAlwaysUseNewSession(true);
298
299         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session2));
300         try {
301             final List JavaDoc l = new ArrayList JavaDoc();
302             l.add("test");
303             List JavaDoc result = ht.executeFind(new HibernateCallback() {
304                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
305                     return l;
306                 }
307             });
308             assertTrue("Correct result list", result == l);
309         }
310         finally {
311             TransactionSynchronizationManager.unbindResource(sf);
312         }
313     }
314
315     public void testExecuteWithThreadBoundAndNewSessionAndEntityInterceptor() throws HibernateException {
316         MockControl interceptorControl = MockControl.createControl(org.hibernate.Interceptor.class);
317         Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
318
319         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
320         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
321         MockControl session2Control = MockControl.createControl(Session.class);
322         Session session2 = (Session) session2Control.getMock();
323
324         session2.connection();
325         session2Control.setReturnValue(con, 1);
326         sf.openSession(con, entityInterceptor);
327         sfControl.setReturnValue(session, 1);
328         session.flush();
329         sessionControl.setVoidCallable(1);
330         session.close();
331         sessionControl.setReturnValue(null, 1);
332         sfControl.replay();
333         sessionControl.replay();
334         session2Control.replay();
335
336         HibernateTemplate ht = new HibernateTemplate(sf);
337         ht.setAlwaysUseNewSession(true);
338         ht.setEntityInterceptor(entityInterceptor);
339
340         TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session2));
341         try {
342             final List JavaDoc l = new ArrayList JavaDoc();
343             l.add("test");
344             List JavaDoc result = ht.executeFind(new HibernateCallback() {
345                 public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
346                     return l;
347                 }
348             });
349             assertTrue("Correct result list", result == l);
350         }
351         finally {
352             TransactionSynchronizationManager.unbindResource(sf);
353         }
354     }
355
356     public void testExecuteWithEntityInterceptor() throws HibernateException {
357         MockControl interceptorControl = MockControl.createControl(org.hibernate.Interceptor.class);
358         Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
359
360         sf.openSession(entityInterceptor);
361         sfControl.setReturnValue(session, 1);
362         session.getSessionFactory();
363         sessionControl.setReturnValue(sf, 1);
364         session.flush();
365         sessionControl.setVoidCallable(1);
366         session.close();
367         sessionControl.setReturnValue(null, 1);
368         sfControl.replay();
369         sessionControl.replay();
370
371         HibernateTemplate ht = new HibernateTemplate(sf);
372         ht.setEntityInterceptor(entityInterceptor);
373         final List JavaDoc l = new ArrayList JavaDoc();
374         l.add("test");
375         List JavaDoc result = ht.executeFind(new HibernateCallback() {
376             public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
377                 return l;
378             }
379         });
380         assertTrue("Correct result list", result == l);
381     }
382
383     public void testExecuteWithCacheQueries() throws HibernateException {
384         MockControl query1Control = MockControl.createControl(Query.class);
385         Query query1 = (Query) query1Control.getMock();
386         MockControl query2Control = MockControl.createControl(Query.class);
387         Query query2 = (Query) query2Control.getMock();
388         MockControl criteriaControl = MockControl.createControl(Criteria.class);
389         Criteria criteria = (Criteria) criteriaControl.getMock();
390
391         sf.openSession();
392         sfControl.setReturnValue(session, 1);
393         session.getSessionFactory();
394         sessionControl.setReturnValue(sf, 1);
395         session.createQuery("some query");
396         sessionControl.setReturnValue(query1);
397         query1.setCacheable(true);
398         query1Control.setReturnValue(query1, 1);
399         session.getNamedQuery("some query name");
400         sessionControl.setReturnValue(query2);
401         query2.setCacheable(true);
402         query2Control.setReturnValue(query2, 1);
403         session.createCriteria(TestBean.class);
404         sessionControl.setReturnValue(criteria, 1);
405         criteria.setCacheable(true);
406         criteriaControl.setReturnValue(criteria, 1);
407         session.flush();
408         sessionControl.setVoidCallable(1);
409         session.close();
410         sessionControl.setReturnValue(null, 1);
411         sfControl.replay();
412         sessionControl.replay();
413         query1Control.replay();
414         query2Control.replay();
415         criteriaControl.replay();
416
417         HibernateTemplate ht = new HibernateTemplate(sf);
418         ht.setCacheQueries(true);
419         ht.execute(new HibernateCallback() {
420             public Object JavaDoc doInHibernate(org.hibernate.Session sess) throws HibernateException {
421                 assertNotSame(session, sess);
422                 assertTrue(Proxy.isProxyClass(sess.getClass()));
423                 sess.createQuery("some query");
424                 sess.getNamedQuery("some query name");
425                 sess.createCriteria(TestBean.class);
426                 // should be ignored
427
sess.close();
428                 return null;
429             }
430         });
431
432         query1Control.verify();
433         query2Control.verify();
434         criteriaControl.verify();
435     }
436
437     public void testExecuteWithCacheQueriesAndCacheRegion() throws HibernateException {
438         MockControl query1Control = MockControl.createControl(Query.class);
439         Query query1 = (Query) query1Control.getMock();
440         MockControl query2Control = MockControl.createControl(Query.class);
441         Query query2 = (Query) query2Control.getMock();
442         MockControl criteriaControl = MockControl.createControl(Criteria.class);
443         Criteria criteria = (Criteria) criteriaControl.getMock();
444
445         sf.openSession();
446         sfControl.setReturnValue(session, 1);
447         session.getSessionFactory();
448         sessionControl.setReturnValue(sf, 1);
449         session.createQuery("some query");
450         sessionControl.setReturnValue(query1);
451         query1.setCacheable(true);
452         query1Control.setReturnValue(query1, 1);
453         query1.setCacheRegion("myRegion");
454         query1Control.setReturnValue(query1, 1);
455         session.getNamedQuery("some query name");
456         sessionControl.setReturnValue(query2);
457         query2.setCacheable(true);
458         query2Control.setReturnValue(query2, 1);
459         query2.setCacheRegion("myRegion");
460         query2Control.setReturnValue(query2, 1);
461         session.createCriteria(TestBean.class);
462         sessionControl.setReturnValue(criteria, 1);
463         criteria.setCacheable(true);
464         criteriaControl.setReturnValue(criteria, 1);
465         criteria.setCacheRegion("myRegion");
466         criteriaControl.setReturnValue(criteria, 1);
467         session.flush();
468         sessionControl.setVoidCallable(1);
469         session.close();
470         sessionControl.setReturnValue(null, 1);
471         sfControl.replay();
472         sessionControl.replay();
473         query1Control.replay();
474         query2Control.replay();
475         criteriaControl.replay();
476
477         HibernateTemplate ht = new HibernateTemplate(sf);
478         ht.setCacheQueries(true);
479         ht.setQueryCacheRegion("myRegion");
480         ht.execute(new HibernateCallback() {
481             public Object JavaDoc doInHibernate(org.hibernate.Session sess) throws HibernateException {
482                 assertNotSame(session, sess);
483                 assertTrue(Proxy.isProxyClass(sess.getClass()));
484                 sess.createQuery("some query");
485                 sess.getNamedQuery("some query name");
486                 sess.createCriteria(TestBean.class);
487                 // should be ignored
488
sess.close();
489                 return null;
490             }
491         });
492
493         query1Control.verify();
494         query2Control.verify();
495         criteriaControl.verify();
496     }
497
498     public void testExecuteWithCacheQueriesAndCacheRegionAndNativeSession() throws HibernateException {
499         MockControl query1Control = MockControl.createControl(Query.class);
500         Query query1 = (Query) query1Control.getMock();
501         MockControl query2Control = MockControl.createControl(Query.class);
502         Query query2 = (Query) query2Control.getMock();
503         MockControl criteriaControl = MockControl.createControl(Criteria.class);
504         Criteria criteria = (Criteria) criteriaControl.getMock();
505
506         sf.openSession();
507         sfControl.setReturnValue(session, 1);
508         session.getSessionFactory();
509         sessionControl.setReturnValue(sf, 1);
510         session.createQuery("some query");
511         sessionControl.setReturnValue(query1);
512         session.getNamedQuery("some query name");
513         sessionControl.setReturnValue(query2);
514         session.createCriteria(TestBean.class);
515         sessionControl.setReturnValue(criteria, 1);
516         session.flush();
517         sessionControl.setVoidCallable(1);
518         session.close();
519         sessionControl.setReturnValue(null, 1);
520         sfControl.replay();
521         sessionControl.replay();
522         query1Control.replay();
523         query2Control.replay();
524         criteriaControl.replay();
525
526         HibernateTemplate ht = new HibernateTemplate(sf);
527         ht.setExposeNativeSession(true);
528         ht.setCacheQueries(true);
529         ht.setQueryCacheRegion("myRegion");
530         ht.execute(new HibernateCallback() {
531             public Object JavaDoc doInHibernate(org.hibernate.Session sess) throws HibernateException {
532                 assertSame(session, sess);
533                 sess.createQuery("some query");
534                 sess.getNamedQuery("some query name");
535                 sess.createCriteria(TestBean.class);
536                 return null;
537             }
538         });
539
540         query1Control.verify();
541         query2Control.verify();
542         criteriaControl.verify();
543     }
544
545     public void testGet() throws HibernateException {
546         TestBean tb = new TestBean();
547         sf.openSession();
548         sfControl.setReturnValue(session, 1);
549         session.getSessionFactory();
550         sessionControl.setReturnValue(sf, 1);
551         session.get(TestBean.class, "");
552         sessionControl.setReturnValue(tb, 1);
553         session.flush();
554         sessionControl.setVoidCallable(1);
555         session.close();
556         sessionControl.setReturnValue(null, 1);
557         sfControl.replay();
558         sessionControl.replay();
559
560         HibernateTemplate ht = new HibernateTemplate(sf);
561         Object JavaDoc result = ht.get(TestBean.class, "");
562         assertTrue("Correct result", result == tb);
563     }
564
565     public void testGetWithLockMode() throws HibernateException {
566         TestBean tb = new TestBean();
567         sf.openSession();
568         sfControl.setReturnValue(session, 1);
569         session.getSessionFactory();
570         sessionControl.setReturnValue(sf, 1);
571         session.get(TestBean.class, "", LockMode.UPGRADE_NOWAIT);
572         sessionControl.setReturnValue(tb, 1);
573         session.flush();
574         sessionControl.setVoidCallable(1);
575         session.close();
576         sessionControl.setReturnValue(null, 1);
577         sfControl.replay();
578         sessionControl.replay();
579
580         HibernateTemplate ht = new HibernateTemplate(sf);
581         Object JavaDoc result = ht.get(TestBean.class, "", LockMode.UPGRADE_NOWAIT);
582         assertTrue("Correct result", result == tb);
583     }
584
585     public void testGetWithEntityName() throws HibernateException {
586         TestBean tb = new TestBean();
587         sf.openSession();
588         sfControl.setReturnValue(session, 1);
589         session.getSessionFactory();
590         sessionControl.setReturnValue(sf, 1);
591         session.get("myEntity", "");
592         sessionControl.setReturnValue(tb, 1);
593         session.flush();
594         sessionControl.setVoidCallable(1);
595         session.close();
596         sessionControl.setReturnValue(null, 1);
597         sfControl.replay();
598         sessionControl.replay();
599
600         HibernateTemplate ht = new HibernateTemplate(sf);
601         Object JavaDoc result = ht.get("myEntity", "");
602         assertTrue("Correct result", result == tb);
603     }
604
605     public void testGetWithEntityNameAndLockMode() throws HibernateException {
606         TestBean tb = new TestBean();
607         sf.openSession();
608         sfControl.setReturnValue(session, 1);
609         session.getSessionFactory();
610         sessionControl.setReturnValue(sf, 1);
611         session.get("myEntity", "", LockMode.UPGRADE_NOWAIT);
612         sessionControl.setReturnValue(tb, 1);
613         session.flush();
614         sessionControl.setVoidCallable(1);
615         session.close();
616         sessionControl.setReturnValue(null, 1);
617         sfControl.replay();
618         sessionControl.replay();
619
620         HibernateTemplate ht = new HibernateTemplate(sf);
621         Object JavaDoc result = ht.get("myEntity", "", LockMode.UPGRADE_NOWAIT);
622         assertTrue("Correct result", result == tb);
623     }
624
625     public void testLoad() throws HibernateException {
626         TestBean tb = new TestBean();
627
628         sf.openSession();
629         sfControl.setReturnValue(session, 1);
630         session.getSessionFactory();
631         sessionControl.setReturnValue(sf, 1);
632         session.load(TestBean.class, "");
633         sessionControl.setReturnValue(tb, 1);
634         session.flush();
635         sessionControl.setVoidCallable(1);
636         session.close();
637         sessionControl.setReturnValue(null, 1);
638         sfControl.replay();
639         sessionControl.replay();
640
641         HibernateTemplate ht = new HibernateTemplate(sf);
642         Object JavaDoc result = ht.load(TestBean.class, "");
643         assertTrue("Correct result", result == tb);
644     }
645
646     public void testLoadWithNotFound() throws HibernateException {
647         sf.openSession();
648         sfControl.setReturnValue(session, 1);
649         session.getSessionFactory();
650         sessionControl.setReturnValue(sf, 1);
651         session.load(TestBean.class, "id");
652         ObjectNotFoundException onfex = new ObjectNotFoundException("id", TestBean.class.getName());
653         sessionControl.setThrowable(onfex);
654         session.close();
655         sessionControl.setReturnValue(null, 1);
656         sfControl.replay();
657         sessionControl.replay();
658
659         HibernateTemplate ht = new HibernateTemplate(sf);
660         try {
661             ht.load(TestBean.class, "id");
662             fail("Should have thrown HibernateObjectRetrievalFailureException");
663         }
664         catch (HibernateObjectRetrievalFailureException ex) {
665             // expected
666
assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
667             assertEquals("id", ex.getIdentifier());
668             assertEquals(onfex, ex.getCause());
669         }
670     }
671
672     public void testLoadWithLockMode() throws HibernateException {
673         TestBean tb = new TestBean();
674
675         sf.openSession();
676         sfControl.setReturnValue(session, 1);
677         session.getSessionFactory();
678         sessionControl.setReturnValue(sf, 1);
679         session.load(TestBean.class, "", LockMode.UPGRADE);
680         sessionControl.setReturnValue(tb, 1);
681         session.flush();
682         sessionControl.setVoidCallable(1);
683         session.close();
684         sessionControl.setReturnValue(null, 1);
685         sfControl.replay();
686         sessionControl.replay();
687
688         HibernateTemplate ht = new HibernateTemplate(sf);
689         Object JavaDoc result = ht.load(TestBean.class, "", LockMode.UPGRADE);
690         assertTrue("Correct result", result == tb);
691     }
692
693     public void testLoadWithEntityName() throws HibernateException {
694         TestBean tb = new TestBean();
695
696         sf.openSession();
697         sfControl.setReturnValue(session, 1);
698         session.getSessionFactory();
699         sessionControl.setReturnValue(sf, 1);
700         session.load("myEntity", "");
701         sessionControl.setReturnValue(tb, 1);
702         session.flush();
703         sessionControl.setVoidCallable(1);
704         session.close();
705         sessionControl.setReturnValue(null, 1);
706         sfControl.replay();
707         sessionControl.replay();
708
709         HibernateTemplate ht = new HibernateTemplate(sf);
710         Object JavaDoc result = ht.load("myEntity", "");
711         assertTrue("Correct result", result == tb);
712     }
713
714     public void testLoadWithEntityNameLockMode() throws HibernateException {
715         TestBean tb = new TestBean();
716
717         sf.openSession();
718         sfControl.setReturnValue(session, 1);
719         session.getSessionFactory();
720         sessionControl.setReturnValue(sf, 1);
721         session.load("myEntity", "", LockMode.UPGRADE);
722         sessionControl.setReturnValue(tb, 1);
723         session.flush();
724         sessionControl.setVoidCallable(1);
725         session.close();
726         sessionControl.setReturnValue(null, 1);
727         sfControl.replay();
728         sessionControl.replay();
729
730         HibernateTemplate ht = new HibernateTemplate(sf);
731         Object JavaDoc result = ht.load("myEntity", "", LockMode.UPGRADE);
732         assertTrue("Correct result", result == tb);
733     }
734
735     public void testLoadWithObject() throws HibernateException {
736         TestBean tb = new TestBean();
737         sf.openSession();
738         sfControl.setReturnValue(session, 1);
739         session.getSessionFactory();
740         sessionControl.setReturnValue(sf, 1);
741         session.load(tb, "");
742         sessionControl.setVoidCallable(1);
743         session.flush();
744         sessionControl.setVoidCallable(1);
745         session.close();
746         sessionControl.setReturnValue(null, 1);
747         sfControl.replay();
748         sessionControl.replay();
749
750         HibernateTemplate ht = new HibernateTemplate(sf);
751         ht.load(tb, "");
752     }
753
754     public void testLoadAll() throws HibernateException {
755         MockControl criteriaControl = MockControl.createControl(Criteria.class);
756         Criteria criteria = (Criteria) criteriaControl.getMock();
757         List JavaDoc list = new ArrayList JavaDoc();
758
759         sf.openSession();
760         sfControl.setReturnValue(session, 1);
761         session.getSessionFactory();
762         sessionControl.setReturnValue(sf, 1);
763         session.createCriteria(TestBean.class);
764         sessionControl.setReturnValue(criteria, 1);
765         criteria.list();
766         criteriaControl.setReturnValue(list, 1);
767         session.flush();
768         sessionControl.setVoidCallable(1);
769         session.close();
770         sessionControl.setReturnValue(null, 1);
771         sfControl.replay();
772         sessionControl.replay();
773         criteriaControl.replay();
774
775         HibernateTemplate ht = new HibernateTemplate(sf);
776         List JavaDoc result = ht.loadAll(TestBean.class);
777         assertTrue("Correct result", result == list);
778
779         criteriaControl.verify();
780     }
781
782     public void testLoadAllWithCacheable() throws HibernateException {
783         MockControl criteriaControl = MockControl.createControl(Criteria.class);
784         Criteria criteria = (Criteria) criteriaControl.getMock();
785         List JavaDoc list = new ArrayList JavaDoc();
786
787         sf.openSession();
788         sfControl.setReturnValue(session, 1);
789         session.getSessionFactory();
790         sessionControl.setReturnValue(sf, 1);
791         session.createCriteria(TestBean.class);
792         sessionControl.setReturnValue(criteria, 1);
793         criteria.setCacheable(true);
794         criteriaControl.setReturnValue(criteria, 1);
795         criteria.list();
796         criteriaControl.setReturnValue(list, 1);
797         session.flush();
798         sessionControl.setVoidCallable(1);
799         session.close();
800         sessionControl.setReturnValue(null, 1);
801         sfControl.replay();
802         sessionControl.replay();
803         criteriaControl.replay();
804
805         HibernateTemplate ht = new HibernateTemplate(sf);
806         ht.setCacheQueries(true);
807         List JavaDoc result = ht.loadAll(TestBean.class);
808         assertTrue("Correct result", result == list);
809
810         criteriaControl.verify();
811     }
812
813     public void testLoadAllWithCacheableAndCacheRegion() throws HibernateException {
814         MockControl criteriaControl = MockControl.createControl(Criteria.class);
815         Criteria criteria = (Criteria) criteriaControl.getMock();
816         List JavaDoc list = new ArrayList JavaDoc();
817
818         sf.openSession();
819         sfControl.setReturnValue(session, 1);
820         session.getSessionFactory();
821         sessionControl.setReturnValue(sf, 1);
822         session.createCriteria(TestBean.class);
823         sessionControl.setReturnValue(criteria, 1);
824         criteria.setCacheable(true);
825         criteriaControl.setReturnValue(criteria, 1);
826         criteria.setCacheRegion("myCacheRegion");
827         criteriaControl.setReturnValue(criteria, 1);
828         criteria.list();
829         criteriaControl.setReturnValue(list, 1);
830         session.flush();
831         sessionControl.setVoidCallable(1);
832         session.close();
833         sessionControl.setReturnValue(null, 1);
834         sfControl.replay();
835         sessionControl.replay();
836         criteriaControl.replay();
837
838         HibernateTemplate ht = new HibernateTemplate(sf);
839         ht.setCacheQueries(true);
840         ht.setQueryCacheRegion("myCacheRegion");
841         List JavaDoc result = ht.loadAll(TestBean.class);
842         assertTrue("Correct result", result == list);
843
844         criteriaControl.verify();
845     }
846
847     public void testRefresh() throws HibernateException {
848         TestBean tb = new TestBean();
849         sf.openSession();
850         sfControl.setReturnValue(session, 1);
851         session.getSessionFactory();
852         sessionControl.setReturnValue(sf, 1);
853         session.refresh(tb);
854         sessionControl.setVoidCallable(1);
855         session.flush();
856         sessionControl.setVoidCallable(1);
857         session.close();
858         sessionControl.setReturnValue(null, 1);
859         sfControl.replay();
860         sessionControl.replay();
861
862         HibernateTemplate ht = new HibernateTemplate(sf);
863         ht.refresh(tb);
864     }
865
866     public void testContains() throws HibernateException {
867         TestBean tb = new TestBean();
868         sf.openSession();
869         sfControl.setReturnValue(session, 1);
870         session.getSessionFactory();
871         sessionControl.setReturnValue(sf, 1);
872         session.contains(tb);
873         sessionControl.setReturnValue(true, 1);
874         session.flush();
875         sessionControl.setVoidCallable(1);
876         session.close();
877         sessionControl.setReturnValue(null, 1);
878         sfControl.replay();
879         sessionControl.replay();
880
881         HibernateTemplate ht = new HibernateTemplate(sf);
882         assertTrue(ht.contains(tb));
883     }
884
885     public void testEvict() throws HibernateException {
886         TestBean tb = new TestBean();
887         sf.openSession();
888         sfControl.setReturnValue(session, 1);
889         session.getSessionFactory();
890         sessionControl.setReturnValue(sf, 1);
891         session.evict(tb);
892         sessionControl.setVoidCallable(1);
893         session.flush();
894         sessionControl.setVoidCallable(1);
895         session.close();
896         sessionControl.setReturnValue(null, 1);
897         sfControl.replay();
898         sessionControl.replay();
899
900         HibernateTemplate ht = new HibernateTemplate(sf);
901         ht.evict(tb);
902     }
903
904     public void testLock() throws HibernateException {
905         TestBean tb = new TestBean();
906         sf.openSession();
907         sfControl.setReturnValue(session, 1);
908         session.getSessionFactory();
909         sessionControl.setReturnValue(sf, 1);
910         session.lock(tb, LockMode.WRITE);
911         sessionControl.setVoidCallable(1);
912         session.flush();
913         sessionControl.setVoidCallable(1);
914         session.close();
915             sessionControl.setReturnValue(null, 1);
916         sfControl.replay();
917         sessionControl.replay();
918
919         HibernateTemplate ht = new HibernateTemplate(sf);
920         ht.lock(tb, LockMode.WRITE);
921     }
922
923     public void testLockWithEntityName() throws HibernateException {
924         TestBean tb = new TestBean();
925         sf.openSession();
926         sfControl.setReturnValue(session, 1);
927         session.getSessionFactory();
928         sessionControl.setReturnValue(sf, 1);
929         session.lock("myEntity", tb, LockMode.WRITE);
930         sessionControl.setVoidCallable(1);
931         session.flush();
932         sessionControl.setVoidCallable(1);
933         session.close();
934             sessionControl.setReturnValue(null, 1);
935         sfControl.replay();
936         sessionControl.replay();
937
938         HibernateTemplate ht = new HibernateTemplate(sf);
939         ht.lock("myEntity", tb, LockMode.WRITE);
940     }
941
942     public void testSave() throws HibernateException {
943         TestBean tb = new TestBean();
944         sf.openSession();
945         sfControl.setReturnValue(session, 1);
946         session.getSessionFactory();
947         sessionControl.setReturnValue(sf, 1);
948         session.getFlushMode();
949         sessionControl.setReturnValue(FlushMode.AUTO);
950         session.save(tb);
951         sessionControl.setReturnValue(new Integer JavaDoc(0), 1);
952         session.flush();
953         sessionControl.setVoidCallable(1);
954         session.close();
955         sessionControl.setReturnValue(null, 1);
956         sfControl.replay();
957         sessionControl.replay();
958
959         HibernateTemplate ht = new HibernateTemplate(sf);
960         assertEquals("Correct return value", ht.save(tb), new Integer JavaDoc(0));
961     }
962
963     public void testSaveWithId() throws HibernateException {
964         TestBean tb = new TestBean();
965         sf.openSession();
966         sfControl.setReturnValue(session, 1);
967         session.getSessionFactory();
968         sessionControl.setReturnValue(sf, 1);
969         session.getFlushMode();
970         sessionControl.setReturnValue(FlushMode.AUTO);
971         session.save(tb, "id");
972         sessionControl.setVoidCallable(1);
973         session.flush();
974         sessionControl.setVoidCallable(1);
975         session.close();
976         sessionControl.setReturnValue(null, 1);
977         sfControl.replay();
978         sessionControl.replay();
979
980         HibernateTemplate ht = new HibernateTemplate(sf);
981         ht.save(tb, "id");
982     }
983
984     public void testSaveWithEntityName() throws HibernateException {
985         TestBean tb = new TestBean();
986         sf.openSession();
987         sfControl.setReturnValue(session, 1);
988         session.getSessionFactory();
989         sessionControl.setReturnValue(sf, 1);
990         session.getFlushMode();
991         sessionControl.setReturnValue(FlushMode.AUTO);
992         session.save("myEntity", tb);
993         sessionControl.setReturnValue(new Integer JavaDoc(0), 1);
994         session.flush();
995         sessionControl.setVoidCallable(1);
996         session.close();
997         sessionControl.setReturnValue(null, 1);
998         sfControl.replay();
999         sessionControl.replay();
1000
1001        HibernateTemplate ht = new HibernateTemplate(sf);
1002        assertEquals("Correct return value", ht.save("myEntity", tb), new Integer JavaDoc(0));
1003    }
1004
1005    public void testSaveWithEntityNameAndId() throws HibernateException {
1006        TestBean tb = new TestBean();
1007        sf.openSession();
1008        sfControl.setReturnValue(session, 1);
1009        session.getSessionFactory();
1010        sessionControl.setReturnValue(sf, 1);
1011        session.getFlushMode();
1012        sessionControl.setReturnValue(FlushMode.AUTO);
1013        session.save("myEntity", tb, "id");
1014        sessionControl.setVoidCallable(1);
1015        session.flush();
1016        sessionControl.setVoidCallable(1);
1017        session.close();
1018        sessionControl.setReturnValue(null, 1);
1019        sfControl.replay();
1020        sessionControl.replay();
1021
1022        HibernateTemplate ht = new HibernateTemplate(sf);
1023        ht.save("myEntity", tb, "id");
1024    }
1025
1026    public void testUpdate() throws HibernateException {
1027        TestBean tb = new TestBean();
1028        sf.openSession();
1029        sfControl.setReturnValue(session, 1);
1030        session.getSessionFactory();
1031        sessionControl.setReturnValue(sf, 1);
1032        session.getFlushMode();
1033        sessionControl.setReturnValue(FlushMode.AUTO);
1034        session.update(tb);
1035        sessionControl.setVoidCallable(1);
1036        session.flush();
1037        sessionControl.setVoidCallable(1);
1038        session.close();
1039        sessionControl.setReturnValue(null, 1);
1040        sfControl.replay();
1041        sessionControl.replay();
1042
1043        HibernateTemplate ht = new HibernateTemplate(sf);
1044        ht.update(tb);
1045    }
1046
1047    public void testUpdateWithLockMode() throws HibernateException {
1048        TestBean tb = new TestBean();
1049        sf.openSession();
1050        sfControl.setReturnValue(session, 1);
1051        session.getSessionFactory();
1052        sessionControl.setReturnValue(sf, 1);
1053        session.getFlushMode();
1054        sessionControl.setReturnValue(FlushMode.AUTO);
1055        session.update(tb);
1056        sessionControl.setVoidCallable(1);
1057        session.lock(tb, LockMode.UPGRADE);
1058        sessionControl.setVoidCallable(1);
1059        session.flush();
1060        sessionControl.setVoidCallable(1);
1061        session.close();
1062        sessionControl.setReturnValue(null, 1);
1063        sfControl.replay();
1064        sessionControl.replay();
1065
1066        HibernateTemplate ht = new HibernateTemplate(sf);
1067        ht.update(tb, LockMode.UPGRADE);
1068    }
1069
1070    public void testUpdateWithEntityName() throws HibernateException {
1071        TestBean tb = new TestBean();
1072        sf.openSession();
1073        sfControl.setReturnValue(session, 1);
1074        session.getSessionFactory();
1075        sessionControl.setReturnValue(sf, 1);
1076        session.getFlushMode();
1077        sessionControl.setReturnValue(FlushMode.AUTO);
1078        session.update("myEntity", tb);
1079        sessionControl.setVoidCallable(1);
1080        session.flush();
1081        sessionControl.setVoidCallable(1);
1082        session.close();
1083        sessionControl.setReturnValue(null, 1);
1084        sfControl.replay();
1085        sessionControl.replay();
1086
1087        HibernateTemplate ht = new HibernateTemplate(sf);
1088        ht.update("myEntity", tb);
1089    }
1090
1091    public void testUpdateWithEntityNameAndLockMode() throws HibernateException {
1092        TestBean tb = new TestBean();
1093        sf.openSession();
1094        sfControl.setReturnValue(session, 1);
1095        session.getSessionFactory();
1096        sessionControl.setReturnValue(sf, 1);
1097        session.getFlushMode();
1098        sessionControl.setReturnValue(FlushMode.AUTO);
1099        session.update("myEntity", tb);
1100        sessionControl.setVoidCallable(1);
1101        session.lock(tb, LockMode.UPGRADE);
1102        sessionControl.setVoidCallable(1);
1103        session.flush();
1104        sessionControl.setVoidCallable(1);
1105        session.close();
1106        sessionControl.setReturnValue(null, 1);
1107        sfControl.replay();
1108        sessionControl.replay();
1109
1110        HibernateTemplate ht = new HibernateTemplate(sf);
1111        ht.update("myEntity", tb, LockMode.UPGRADE);
1112    }
1113
1114    public void testSaveOrUpdate() throws HibernateException {
1115        TestBean tb = new TestBean();
1116        sf.openSession();
1117        sfControl.setReturnValue(session, 1);
1118        session.getSessionFactory();
1119        sessionControl.setReturnValue(sf, 1);
1120        session.getFlushMode();
1121        sessionControl.setReturnValue(FlushMode.AUTO);
1122        session.saveOrUpdate(tb);
1123        sessionControl.setVoidCallable(1);
1124        session.flush();
1125        sessionControl.setVoidCallable(1);
1126        session.close();
1127        sessionControl.setReturnValue(null, 1);
1128        sfControl.replay();
1129        sessionControl.replay();
1130
1131        HibernateTemplate ht = new HibernateTemplate(sf);
1132        ht.saveOrUpdate(tb);
1133    }
1134
1135    public void testSaveOrUpdateWithFlushModeNever() throws HibernateException {
1136        TestBean tb = new TestBean();
1137        sf.openSession();
1138        sfControl.setReturnValue(session, 1);
1139        session.getSessionFactory();
1140        sessionControl.setReturnValue(sf, 1);
1141        session.getFlushMode();
1142        sessionControl.setReturnValue(FlushMode.NEVER);
1143        session.close();
1144        sessionControl.setReturnValue(null, 1);
1145        sfControl.replay();
1146        sessionControl.replay();
1147
1148        HibernateTemplate ht = new HibernateTemplate(sf);
1149        try {
1150            ht.saveOrUpdate(tb);
1151            fail("Should have thrown InvalidDataAccessApiUsageException");
1152        }
1153        catch (InvalidDataAccessApiUsageException ex) {
1154            // expected
1155
}
1156    }
1157
1158    public void testSaveOrUpdateWithEntityName() throws HibernateException {
1159        TestBean tb = new TestBean();
1160        sf.openSession();
1161        sfControl.setReturnValue(session, 1);
1162        session.getSessionFactory();
1163        sessionControl.setReturnValue(sf, 1);
1164        session.getFlushMode();
1165        sessionControl.setReturnValue(FlushMode.AUTO);
1166        session.saveOrUpdate("myEntity", tb);
1167        sessionControl.setVoidCallable(1);
1168        session.flush();
1169        sessionControl.setVoidCallable(1);
1170        session.close();
1171        sessionControl.setReturnValue(null, 1);
1172        sfControl.replay();
1173        sessionControl.replay();
1174
1175        HibernateTemplate ht = new HibernateTemplate(sf);
1176        ht.saveOrUpdate("myEntity", tb);
1177    }
1178
1179    public void testSaveOrUpdateAll() throws HibernateException {
1180        TestBean tb1 = new TestBean();
1181        TestBean tb2 = new TestBean();
1182        sf.openSession();
1183        sfControl.setReturnValue(session, 1);
1184        session.getSessionFactory();
1185        sessionControl.setReturnValue(sf, 1);
1186        session.getFlushMode();
1187        sessionControl.setReturnValue(FlushMode.AUTO);
1188        session.saveOrUpdate(tb1);
1189        sessionControl.setVoidCallable(1);
1190        session.saveOrUpdate(tb2);
1191        sessionControl.setVoidCallable(1);
1192        session.flush();
1193        sessionControl.setVoidCallable(1);
1194        session.close();
1195        sessionControl.setReturnValue(null, 1);
1196        sfControl.replay();
1197        sessionControl.replay();
1198
1199        HibernateTemplate ht = new HibernateTemplate(sf);
1200        List JavaDoc tbs = new ArrayList JavaDoc();
1201        tbs.add(tb1);
1202        tbs.add(tb2);
1203        ht.saveOrUpdateAll(tbs);
1204    }
1205
1206    public void testPersist() throws HibernateException {
1207        TestBean tb = new TestBean();
1208        sf.openSession();
1209        sfControl.setReturnValue(session, 1);
1210        session.getSessionFactory();
1211        sessionControl.setReturnValue(sf, 1);
1212        session.getFlushMode();
1213        sessionControl.setReturnValue(FlushMode.AUTO);
1214        session.persist(tb);
1215        sessionControl.setVoidCallable(1);
1216        session.flush();
1217        sessionControl.setVoidCallable(1);
1218        session.close();
1219        sessionControl.setReturnValue(null, 1);
1220        sfControl.replay();
1221        sessionControl.replay();
1222
1223        HibernateTemplate ht = new HibernateTemplate(sf);
1224        ht.persist(tb);
1225    }
1226
1227    public void testPersistWithEntityName() throws HibernateException {
1228        TestBean tb = new TestBean();
1229        sf.openSession();
1230        sfControl.setReturnValue(session, 1);
1231        session.getSessionFactory();
1232        sessionControl.setReturnValue(sf, 1);
1233        session.getFlushMode();
1234        sessionControl.setReturnValue(FlushMode.AUTO);
1235        session.persist("myEntity", tb);
1236        sessionControl.setVoidCallable(1);
1237        session.flush();
1238        sessionControl.setVoidCallable(1);
1239        session.close();
1240        sessionControl.setReturnValue(null, 1);
1241        sfControl.replay();
1242        sessionControl.replay();
1243
1244        HibernateTemplate ht = new HibernateTemplate(sf);
1245        ht.persist("myEntity", tb);
1246    }
1247
1248    public void testMerge() throws HibernateException {
1249        TestBean tb = new TestBean();
1250        TestBean tbMerged = new TestBean();
1251        sf.openSession();
1252        sfControl.setReturnValue(session, 1);
1253        session.getSessionFactory();
1254        sessionControl.setReturnValue(sf, 1);
1255        session.getFlushMode();
1256        sessionControl.setReturnValue(FlushMode.AUTO);
1257        session.merge(tb);
1258        sessionControl.setReturnValue(tbMerged, 1);
1259        session.flush();
1260        sessionControl.setVoidCallable(1);
1261        session.close();
1262        sessionControl.setReturnValue(null, 1);
1263        sfControl.replay();
1264        sessionControl.replay();
1265
1266        HibernateTemplate ht = new HibernateTemplate(sf);
1267        assertSame(tbMerged, ht.merge(tb));
1268    }
1269
1270    public void testMergeWithEntityName() throws HibernateException {
1271        TestBean tb = new TestBean();
1272        TestBean tbMerged = new TestBean();
1273        sf.openSession();
1274        sfControl.setReturnValue(session, 1);
1275        session.getSessionFactory();
1276        sessionControl.setReturnValue(sf, 1);
1277        session.getFlushMode();
1278        sessionControl.setReturnValue(FlushMode.AUTO);
1279        session.merge("myEntity", tb);
1280        sessionControl.setReturnValue(tbMerged, 1);
1281        session.flush();
1282        sessionControl.setVoidCallable(1);
1283        session.close();
1284        sessionControl.setReturnValue(null, 1);
1285        sfControl.replay();
1286        sessionControl.replay();
1287
1288        HibernateTemplate ht = new HibernateTemplate(sf);
1289        assertSame(tbMerged, ht.merge("myEntity", tb));
1290    }
1291
1292    public void testDelete() throws HibernateException {
1293        TestBean tb = new TestBean();
1294        sf.openSession();
1295        sfControl.setReturnValue(session, 1);
1296        session.getSessionFactory();
1297        sessionControl.setReturnValue(sf, 1);
1298        session.getFlushMode();
1299        sessionControl.setReturnValue(FlushMode.AUTO);
1300        session.delete(tb);
1301        sessionControl.setVoidCallable(1);
1302        session.flush();
1303        sessionControl.setVoidCallable(1);
1304        session.close();
1305        sessionControl.setReturnValue(null, 1);
1306        sfControl.replay();
1307        sessionControl.replay();
1308
1309        HibernateTemplate ht = new HibernateTemplate(sf);
1310        ht.delete(tb);
1311    }
1312
1313    public void testDeleteWithLock() throws HibernateException {
1314        TestBean tb = new TestBean();
1315        sf.openSession();
1316        sfControl.setReturnValue(session, 1);
1317        session.getSessionFactory();
1318        sessionControl.setReturnValue(sf, 1);
1319        session.getFlushMode();
1320        sessionControl.setReturnValue(FlushMode.AUTO);
1321        session.lock(tb, LockMode.UPGRADE);
1322        sessionControl.setVoidCallable(1);
1323        session.delete(tb);
1324        sessionControl.setVoidCallable(1);
1325        session.flush();
1326        sessionControl.setVoidCallable(1);
1327        session.close();
1328        sessionControl.setReturnValue(null, 1);
1329        sfControl.replay();
1330        sessionControl.replay();
1331
1332        HibernateTemplate ht = new HibernateTemplate(sf);
1333        ht.delete(tb, LockMode.UPGRADE);
1334    }
1335
1336    public void testDeleteAll() throws HibernateException {
1337        TestBean tb1 = new TestBean();
1338        TestBean tb2 = new TestBean();
1339        sf.openSession();
1340        sfControl.setReturnValue(session, 1);
1341        session.getSessionFactory();
1342        sessionControl.setReturnValue(sf, 1);
1343        session.getFlushMode();
1344        sessionControl.setReturnValue(FlushMode.AUTO);
1345        session.delete(tb1);
1346        sessionControl.setVoidCallable(1);
1347        session.delete(tb2);
1348        sessionControl.setVoidCallable(1);
1349        session.flush();
1350        sessionControl.setVoidCallable(1);
1351        session.close();
1352        sessionControl.setReturnValue(null, 1);
1353        sfControl.replay();
1354        sessionControl.replay();
1355
1356        HibernateTemplate ht = new HibernateTemplate(sf);
1357        List JavaDoc tbs = new ArrayList JavaDoc();
1358        tbs.add(tb1);
1359        tbs.add(tb2);
1360        ht.deleteAll(tbs);
1361    }
1362
1363    public void testFlush() throws HibernateException {
1364        sf.openSession();
1365        sfControl.setReturnValue(session, 1);
1366        session.getSessionFactory();
1367        sessionControl.setReturnValue(sf, 1);
1368        session.setFlushMode(FlushMode.NEVER);
1369        sessionControl.setVoidCallable(1);
1370        session.flush();
1371        sessionControl.setVoidCallable(1);
1372        session.close();
1373        sessionControl.setReturnValue(null, 1);
1374        sfControl.replay();
1375        sessionControl.replay();
1376
1377        HibernateTemplate ht = new HibernateTemplate(sf);
1378        ht.setFlushMode(HibernateTemplate.FLUSH_NEVER);
1379        ht.flush();
1380    }
1381
1382    public void testClear() throws HibernateException {
1383        sf.openSession();
1384        sfControl.setReturnValue(session, 1);
1385        session.getSessionFactory();
1386        sessionControl.setReturnValue(sf, 1);
1387        session.clear();
1388        sessionControl.setVoidCallable(1);
1389        session.flush();
1390        sessionControl.setVoidCallable(1);
1391        session.close();
1392        sessionControl.setReturnValue(null, 1);
1393        sfControl.replay();
1394        sessionControl.replay();
1395
1396        HibernateTemplate ht = new HibernateTemplate(sf);
1397        ht.clear();
1398    }
1399
1400    public void testFind() throws HibernateException {
1401        MockControl queryControl = MockControl.createControl(Query.class);
1402        Query query = (Query) queryControl.getMock();
1403
1404        List JavaDoc list = new ArrayList JavaDoc();
1405        sf.openSession();
1406        sfControl.setReturnValue(session, 1);
1407        session.getSessionFactory();
1408        sessionControl.setReturnValue(sf, 1);
1409        session.createQuery("some query string");
1410        sessionControl.setReturnValue(query, 1);
1411        query.list();
1412        queryControl.setReturnValue(list, 1);
1413        session.flush();
1414        sessionControl.setVoidCallable(1);
1415        session.close();
1416        sessionControl.setReturnValue(null, 1);
1417        sfControl.replay();
1418        sessionControl.replay();
1419        queryControl.replay();
1420
1421        HibernateTemplate ht = new HibernateTemplate(sf);
1422        List JavaDoc result = ht.find("some query string");
1423        assertTrue("Correct list", result == list);
1424        queryControl.verify();
1425    }
1426
1427    public void testFindWithParameter() throws HibernateException {
1428        MockControl queryControl = MockControl.createControl(Query.class);
1429        Query query = (Query) queryControl.getMock();
1430
1431        List JavaDoc list = new ArrayList JavaDoc();
1432        sf.openSession();
1433        sfControl.setReturnValue(session, 1);
1434        session.getSessionFactory();
1435        sessionControl.setReturnValue(sf, 1);
1436        session.createQuery("some query string");
1437        sessionControl.setReturnValue(query, 1);
1438        query.setParameter(0, "myvalue");
1439        queryControl.setReturnValue(query, 1);
1440        query.list();
1441        queryControl.setReturnValue(list, 1);
1442        session.flush();
1443        sessionControl.setVoidCallable(1);
1444        session.close();
1445        sessionControl.setReturnValue(null, 1);
1446        sfControl.replay();
1447        sessionControl.replay();
1448        queryControl.replay();
1449
1450        HibernateTemplate ht = new HibernateTemplate(sf);
1451        List JavaDoc result = ht.find("some query string", "myvalue");
1452        assertTrue("Correct list", result == list);
1453        queryControl.verify();
1454    }
1455
1456    public void testFindWithParameters() throws HibernateException {
1457        MockControl queryControl = MockControl.createControl(Query.class);
1458        Query query = (Query) queryControl.getMock();
1459
1460        List JavaDoc list = new ArrayList JavaDoc();
1461        sf.openSession();
1462        sfControl.setReturnValue(session, 1);
1463        session.getSessionFactory();
1464        sessionControl.setReturnValue(sf, 1);
1465        session.createQuery("some query string");
1466        sessionControl.setReturnValue(query, 1);
1467        query.setParameter(0, "myvalue1");
1468        queryControl.setReturnValue(query, 1);
1469        query.setParameter(1, new Integer JavaDoc(2));
1470        queryControl.setReturnValue(query, 1);
1471        query.list();
1472        queryControl.setReturnValue(list, 1);
1473        session.flush();
1474        sessionControl.setVoidCallable(1);
1475        session.close();
1476        sessionControl.setReturnValue(null, 1);
1477        sfControl.replay();
1478        sessionControl.replay();
1479        queryControl.replay();
1480
1481        HibernateTemplate ht = new HibernateTemplate(sf);
1482        List JavaDoc result = ht.find("some query string", new Object JavaDoc[] {"myvalue1", new Integer JavaDoc(2)});
1483        assertTrue("Correct list", result == list);
1484        queryControl.verify();
1485    }
1486
1487    public void testFindWithNamedParameter() throws HibernateException {
1488        MockControl queryControl = MockControl.createControl(Query.class);
1489        Query query = (Query) queryControl.getMock();
1490
1491        List JavaDoc list = new ArrayList JavaDoc();
1492        sf.openSession();
1493        sfControl.setReturnValue(session, 1);
1494        session.getSessionFactory();
1495        sessionControl.setReturnValue(sf, 1);
1496        session.createQuery("some query string");
1497        sessionControl.setReturnValue(query, 1);
1498        query.setParameter("myparam", "myvalue");
1499        queryControl.setReturnValue(query, 1);
1500        query.list();
1501        queryControl.setReturnValue(list, 1);
1502        session.flush();
1503        sessionControl.setVoidCallable(1);
1504        session.close();
1505        sessionControl.setReturnValue(null, 1);
1506        sfControl.replay();
1507        sessionControl.replay();
1508        queryControl.replay();
1509
1510        HibernateTemplate ht = new HibernateTemplate(sf);
1511        List JavaDoc result = ht.findByNamedParam("some query string", "myparam", "myvalue");
1512        assertTrue("Correct list", result == list);
1513        queryControl.verify();
1514    }
1515
1516    public void testFindWithNamedParameters() throws HibernateException {
1517        MockControl queryControl = MockControl.createControl(Query.class);
1518        Query query = (Query) queryControl.getMock();
1519
1520        List JavaDoc list = new ArrayList JavaDoc();
1521        sf.openSession();
1522        sfControl.setReturnValue(session, 1);
1523        session.getSessionFactory();
1524        sessionControl.setReturnValue(sf, 1);
1525        session.createQuery("some query string");
1526        sessionControl.setReturnValue(query, 1);
1527        query.setParameter("myparam1", "myvalue1");
1528        queryControl.setReturnValue(query, 1);
1529        query.setParameter("myparam2", new Integer JavaDoc(2));
1530        queryControl.setReturnValue(query, 1);
1531        query.list();
1532        queryControl.setReturnValue(list, 1);
1533        session.flush();
1534        sessionControl.setVoidCallable(1);
1535        session.close();
1536        sessionControl.setReturnValue(null, 1);
1537        sfControl.replay();
1538        sessionControl.replay();
1539        queryControl.replay();
1540
1541        HibernateTemplate ht = new HibernateTemplate(sf);
1542        List JavaDoc result = ht.findByNamedParam("some query string",
1543                new String JavaDoc[] {"myparam1", "myparam2"},
1544                new Object JavaDoc[] {"myvalue1", new Integer JavaDoc(2)});
1545        assertTrue("Correct list", result == list);
1546        queryControl.verify();
1547    }
1548
1549    public void testFindByValueBean() throws HibernateException {
1550        MockControl queryControl = MockControl.createControl(Query.class);
1551        Query query = (Query) queryControl.getMock();
1552
1553        TestBean tb = new TestBean();
1554        List JavaDoc list = new ArrayList JavaDoc();
1555        sf.openSession();
1556        sfControl.setReturnValue(session, 1);
1557        session.getSessionFactory();
1558        sessionControl.setReturnValue(sf, 1);
1559        session.createQuery("some query string");
1560        sessionControl.setReturnValue(query, 1);
1561        query.setProperties(tb);
1562        queryControl.setReturnValue(query, 1);
1563        query.list();
1564        queryControl.setReturnValue(list, 1);
1565        session.flush();
1566        sessionControl.setVoidCallable(1);
1567        session.close();
1568        sessionControl.setReturnValue(null, 1);
1569        sfControl.replay();
1570        sessionControl.replay();
1571        queryControl.replay();
1572
1573        HibernateTemplate ht = new HibernateTemplate(sf);
1574        List JavaDoc result = ht.findByValueBean("some query string", tb);
1575        assertTrue("Correct list", result == list);
1576        queryControl.verify();
1577    }
1578
1579    public void testFindByNamedQuery() throws HibernateException {
1580        MockControl queryControl = MockControl.createControl(Query.class);
1581        Query query = (Query) queryControl.getMock();
1582
1583        List JavaDoc list = new ArrayList JavaDoc();
1584        sf.openSession();
1585        sfControl.setReturnValue(session, 1);
1586        session.getSessionFactory();
1587        sessionControl.setReturnValue(sf, 1);
1588        session.getNamedQuery("some query name");
1589        sessionControl.setReturnValue(query, 1);
1590        query.list();
1591        queryControl.setReturnValue(list, 1);
1592        session.flush();
1593        sessionControl.setVoidCallable(1);
1594        session.close();
1595        sessionControl.setReturnValue(null, 1);
1596        sfControl.replay();
1597        sessionControl.replay();
1598        queryControl.replay();
1599
1600        HibernateTemplate ht = new HibernateTemplate(sf);
1601        List JavaDoc result = ht.findByNamedQuery("some query name");
1602        assertTrue("Correct list", result == list);
1603        queryControl.verify();
1604    }
1605
1606    public void testFindByNamedQueryWithParameter() throws HibernateException {
1607        MockControl queryControl = MockControl.createControl(Query.class);
1608        Query query = (Query) queryControl.getMock();
1609
1610        List JavaDoc list = new ArrayList JavaDoc();
1611        sf.openSession();
1612        sfControl.setReturnValue(session, 1);
1613        session.getSessionFactory();
1614        sessionControl.setReturnValue(sf, 1);
1615        session.getNamedQuery("some query name");
1616        sessionControl.setReturnValue(query, 1);
1617        query.setParameter(0, "myvalue");
1618        queryControl.setReturnValue(query, 1);
1619        query.list();
1620        queryControl.setReturnValue(list, 1);
1621        session.flush();
1622        sessionControl.setVoidCallable(1);
1623        session.close();
1624        sessionControl.setReturnValue(null, 1);
1625        sfControl.replay();
1626        sessionControl.replay();
1627        queryControl.replay();
1628
1629        HibernateTemplate ht = new HibernateTemplate(sf);
1630        List JavaDoc result = ht.findByNamedQuery("some query name", "myvalue");
1631        assertTrue("Correct list", result == list);
1632        queryControl.verify();
1633    }
1634
1635    public void testFindByNamedQueryWithParameters() throws HibernateException {
1636        MockControl queryControl = MockControl.createControl(Query.class);
1637        Query query = (Query) queryControl.getMock();
1638
1639        List JavaDoc list = new ArrayList JavaDoc();
1640        sf.openSession();
1641        sfControl.setReturnValue(session, 1);
1642        session.getSessionFactory();
1643        sessionControl.setReturnValue(sf, 1);
1644        session.getNamedQuery("some query name");
1645        sessionControl.setReturnValue(query, 1);
1646        query.setParameter(0, "myvalue1");
1647        queryControl.setReturnValue(query, 1);
1648        query.setParameter(1, new Integer JavaDoc(2));
1649        queryControl.setReturnValue(query, 1);
1650        query.list();
1651        queryControl.setReturnValue(list, 1);
1652        session.flush();
1653        sessionControl.setVoidCallable(1);
1654        session.close();
1655        sessionControl.setReturnValue(null, 1);
1656        sfControl.replay();
1657        sessionControl.replay();
1658        queryControl.replay();
1659
1660        HibernateTemplate ht = new HibernateTemplate(sf);
1661        List JavaDoc result = ht.findByNamedQuery("some query name", new Object JavaDoc[] {"myvalue1", new Integer JavaDoc(2)});
1662        assertTrue("Correct list", result == list);
1663        queryControl.verify();
1664    }
1665
1666    public void testFindByNamedQueryWithNamedParameter() throws HibernateException {
1667        MockControl queryControl = MockControl.createControl(Query.class);
1668        Query query = (Query) queryControl.getMock();
1669
1670        List JavaDoc list = new ArrayList JavaDoc();
1671        sf.openSession();
1672        sfControl.setReturnValue(session, 1);
1673        session.getSessionFactory();
1674        sessionControl.setReturnValue(sf, 1);
1675        session.getNamedQuery("some query name");
1676        sessionControl.setReturnValue(query, 1);
1677        query.setParameter("myparam", "myvalue");
1678        queryControl.setReturnValue(query, 1);
1679        query.list();
1680        queryControl.setReturnValue(list, 1);
1681        session.flush();
1682        sessionControl.setVoidCallable(1);
1683        session.close();
1684        sessionControl.setReturnValue(null, 1);
1685        sfControl.replay();
1686        sessionControl.replay();
1687        queryControl.replay();
1688
1689        HibernateTemplate ht = new HibernateTemplate(sf);
1690        List JavaDoc result = ht.findByNamedQueryAndNamedParam("some query name", "myparam", "myvalue");
1691        assertTrue("Correct list", result == list);
1692        queryControl.verify();
1693    }
1694
1695    public void testFindByNamedQueryWithNamedParameters() throws HibernateException {
1696        MockControl queryControl = MockControl.createControl(Query.class);
1697        Query query = (Query) queryControl.getMock();
1698
1699        List JavaDoc list = new ArrayList JavaDoc();
1700        sf.openSession();
1701        sfControl.setReturnValue(session, 1);
1702        session.getSessionFactory();
1703        sessionControl.setReturnValue(sf, 1);
1704        session.getNamedQuery("some query name");
1705        sessionControl.setReturnValue(query, 1);
1706        query.setParameter("myparam1", "myvalue1");
1707        queryControl.setReturnValue(query, 1);
1708        query.setParameter("myparam2", new Integer JavaDoc(2));
1709        queryControl.setReturnValue(query, 1);
1710        query.list();
1711        queryControl.setReturnValue(list, 1);
1712        session.flush();
1713        sessionControl.setVoidCallable(1);
1714        session.close();
1715        sessionControl.setReturnValue(null, 1);
1716        sfControl.replay();
1717        sessionControl.replay();
1718        queryControl.replay();
1719
1720        HibernateTemplate ht = new HibernateTemplate(sf);
1721        List JavaDoc result = ht.findByNamedQueryAndNamedParam("some query name",
1722                new String JavaDoc[] {"myparam1", "myparam2"},
1723                new Object JavaDoc[] {"myvalue1", new Integer JavaDoc(2)});
1724        assertTrue("Correct list", result == list);
1725        queryControl.verify();
1726    }
1727
1728    public void testFindByNamedQueryAndValueBean() throws HibernateException {
1729        MockControl queryControl = MockControl.createControl(Query.class);
1730        Query query = (Query) queryControl.getMock();
1731
1732        TestBean tb = new TestBean();
1733        List JavaDoc list = new ArrayList JavaDoc();
1734        sf.openSession();
1735        sfControl.setReturnValue(session, 1);
1736        session.getSessionFactory();
1737        sessionControl.setReturnValue(sf, 1);
1738        session.getNamedQuery("some query name");
1739        sessionControl.setReturnValue(query, 1);
1740        query.setProperties(tb);
1741        queryControl.setReturnValue(query, 1);
1742        query.list();
1743        queryControl.setReturnValue(list, 1);
1744        session.flush();
1745        sessionControl.setVoidCallable(1);
1746        session.close();
1747        sessionControl.setReturnValue(null, 1);
1748        sfControl.replay();
1749        sessionControl.replay();
1750        queryControl.replay();
1751
1752        HibernateTemplate ht = new HibernateTemplate(sf);
1753        List JavaDoc result = ht.findByNamedQueryAndValueBean("some query name", tb);
1754        assertTrue("Correct list", result == list);
1755        queryControl.verify();
1756    }
1757
1758    public void testFindWithCacheable() throws HibernateException {
1759        MockControl queryControl = MockControl.createControl(Query.class);
1760        Query query = (Query) queryControl.getMock();
1761
1762        List JavaDoc list = new ArrayList JavaDoc();
1763        sf.openSession();
1764        sfControl.setReturnValue(session, 1);
1765        session.getSessionFactory();
1766        sessionControl.setReturnValue(sf, 1);
1767        session.createQuery("some query string");
1768        sessionControl.setReturnValue(query, 1);
1769        query.setCacheable(true);
1770        queryControl.setReturnValue(query, 1);
1771        query.list();
1772        queryControl.setReturnValue(list, 1);
1773        session.flush();
1774        sessionControl.setVoidCallable(1);
1775        session.close();
1776        sessionControl.setReturnValue(null, 1);
1777        sfControl.replay();
1778        sessionControl.replay();
1779        queryControl.replay();
1780
1781        HibernateTemplate ht = new HibernateTemplate(sf);
1782        ht.setCacheQueries(true);
1783        List JavaDoc result = ht.find("some query string");
1784        assertTrue("Correct list", result == list);
1785        sfControl.verify();
1786    }
1787
1788    public void testFindWithCacheableAndCacheRegion() throws HibernateException {
1789        MockControl queryControl = MockControl.createControl(Query.class);
1790        Query query = (Query) queryControl.getMock();
1791
1792        List JavaDoc list = new ArrayList JavaDoc();
1793        sf.openSession();
1794        sfControl.setReturnValue(session, 1);
1795        session.getSessionFactory();
1796        sessionControl.setReturnValue(sf, 1);
1797        session.createQuery("some query string");
1798        sessionControl.setReturnValue(query, 1);
1799        query.setCacheable(true);
1800        queryControl.setReturnValue(query, 1);
1801        query.setCacheRegion("myCacheRegion");
1802        queryControl.setReturnValue(query, 1);
1803        query.list();
1804        queryControl.setReturnValue(list, 1);
1805        session.flush();
1806        sessionControl.setVoidCallable(1);
1807        session.close();
1808        sessionControl.setReturnValue(null, 1);
1809        sfControl.replay();
1810        sessionControl.replay();
1811        queryControl.replay();
1812
1813        HibernateTemplate ht = new HibernateTemplate(sf);
1814        ht.setCacheQueries(true);
1815        ht.setQueryCacheRegion("myCacheRegion");
1816        List JavaDoc result = ht.find("some query string");
1817        assertTrue("Correct list", result == list);
1818        sfControl.verify();
1819    }
1820
1821    public void testFindByNamedQueryWithCacheable() throws HibernateException {
1822        MockControl queryControl = MockControl.createControl(Query.class);
1823        Query query = (Query) queryControl.getMock();
1824
1825        List JavaDoc list = new ArrayList JavaDoc();
1826        sf.openSession();
1827        sfControl.setReturnValue(session, 1);
1828        session.getSessionFactory();
1829        sessionControl.setReturnValue(sf, 1);
1830        session.getNamedQuery("some query name");
1831        sessionControl.setReturnValue(query, 1);
1832        query.setCacheable(true);
1833        queryControl.setReturnValue(query, 1);
1834        query.list();
1835        queryControl.setReturnValue(list, 1);
1836        session.flush();
1837        sessionControl.setVoidCallable(1);
1838        session.close();
1839        sessionControl.setReturnValue(null, 1);
1840        sfControl.replay();
1841        sessionControl.replay();
1842        queryControl.replay();
1843
1844        HibernateTemplate ht = new HibernateTemplate(sf);
1845        ht.setCacheQueries(true);
1846        List JavaDoc result = ht.findByNamedQuery("some query name");
1847        assertTrue("Correct list", result == list);
1848        queryControl.verify();
1849    }
1850
1851    public void testFindByNamedQueryWithCacheableAndCacheRegion() throws HibernateException {
1852        MockControl queryControl = MockControl.createControl(Query.class);
1853        Query query = (Query) queryControl.getMock();
1854
1855        List JavaDoc list = new ArrayList JavaDoc();
1856        sf.openSession();
1857        sfControl.setReturnValue(session, 1);
1858        session.getSessionFactory();
1859        sessionControl.setReturnValue(sf, 1);
1860        session.getNamedQuery("some query name");
1861        sessionControl.setReturnValue(query, 1);
1862        query.setCacheable(true);
1863        queryControl.setReturnValue(query, 1);
1864        query.setCacheRegion("myCacheRegion");
1865        queryControl.setReturnValue(query, 1);
1866        query.list();
1867        queryControl.setReturnValue(list, 1);
1868        session.flush();
1869        sessionControl.setVoidCallable(1);
1870        session.close();
1871        sessionControl.setReturnValue(null, 1);
1872        sfControl.replay();
1873        sessionControl.replay();
1874        queryControl.replay();
1875
1876        HibernateTemplate ht = new HibernateTemplate(sf);
1877        ht.setCacheQueries(true);
1878        ht.setQueryCacheRegion("myCacheRegion");
1879        List JavaDoc result = ht.findByNamedQuery("some query name");
1880        assertTrue("Correct list", result == list);
1881        queryControl.verify();
1882    }
1883
1884    public void testIterate() throws HibernateException {
1885        MockControl queryControl = MockControl.createControl(Query.class);
1886        Query query = (Query) queryControl.getMock();
1887
1888        Iterator JavaDoc it = Collections.EMPTY_LIST.iterator();
1889        sf.openSession();
1890        sfControl.setReturnValue(session, 1);
1891        session.getSessionFactory();
1892        sessionControl.setReturnValue(sf, 1);
1893        session.createQuery("some query string");
1894        sessionControl.setReturnValue(query, 1);
1895        query.iterate();
1896        queryControl.setReturnValue(it, 1);
1897        session.flush();
1898        sessionControl.setVoidCallable(1);
1899        session.close();
1900        sessionControl.setReturnValue(null, 1);
1901        sfControl.replay();
1902        sessionControl.replay();
1903        queryControl.replay();
1904
1905        HibernateTemplate ht = new HibernateTemplate(sf);
1906        Iterator JavaDoc result = ht.iterate("some query string");
1907        assertTrue("Correct list", result == it);
1908        queryControl.verify();
1909    }
1910
1911    public void testIterateWithParameter() throws HibernateException {
1912        MockControl queryControl = MockControl.createControl(Query.class);
1913        Query query = (Query) queryControl.getMock();
1914
1915        Iterator JavaDoc it = Collections.EMPTY_LIST.iterator();
1916        sf.openSession();
1917        sfControl.setReturnValue(session, 1);
1918        session.getSessionFactory();
1919        sessionControl.setReturnValue(sf, 1);
1920        session.createQuery("some query string");
1921        sessionControl.setReturnValue(query, 1);
1922        query.setParameter(0, "myvalue");
1923        queryControl.setReturnValue(query, 1);
1924        query.iterate();
1925        queryControl.setReturnValue(it, 1);
1926        session.flush();
1927        sessionControl.setVoidCallable(1);
1928        session.close();
1929        sessionControl.setReturnValue(null, 1);
1930        sfControl.replay();
1931        sessionControl.replay();
1932        queryControl.replay();
1933
1934        HibernateTemplate ht = new HibernateTemplate(sf);
1935        Iterator JavaDoc result = ht.iterate("some query string", "myvalue");
1936        assertTrue("Correct list", result == it);
1937        queryControl.verify();
1938    }
1939
1940    public void testIterateWithParameters() throws HibernateException {
1941        MockControl queryControl = MockControl.createControl(Query.class);
1942        Query query = (Query) queryControl.getMock();
1943
1944        Iterator JavaDoc it = Collections.EMPTY_LIST.iterator();
1945        sf.openSession();
1946        sfControl.setReturnValue(session, 1);
1947        session.getSessionFactory();
1948        sessionControl.setReturnValue(sf, 1);
1949        session.createQuery("some query string");
1950        sessionControl.setReturnValue(query, 1);
1951        query.setParameter(0, "myvalue1");
1952        queryControl.setReturnValue(query, 1);
1953        query.setParameter(1, new Integer JavaDoc(2));
1954        queryControl.setReturnValue(query, 1);
1955        query.iterate();
1956        queryControl.setReturnValue(it, 1);
1957        session.flush();
1958        sessionControl.setVoidCallable(1);
1959        session.close();
1960        sessionControl.setReturnValue(null, 1);
1961        sfControl.replay();
1962        sessionControl.replay();
1963        queryControl.replay();
1964
1965        HibernateTemplate ht = new HibernateTemplate(sf);
1966        Iterator JavaDoc result = ht.iterate("some query string",
1967                new Object JavaDoc[] {"myvalue1", new Integer JavaDoc(2)});
1968        assertTrue("Correct list", result == it);
1969        sfControl.verify();
1970    }
1971
1972    public void testIterateWithParameterAndType() throws HibernateException {
1973        MockControl queryControl = MockControl.createControl(Query.class);
1974        Query query = (Query) queryControl.getMock();
1975
1976        Iterator JavaDoc it = Collections.EMPTY_LIST.iterator();
1977        sf.openSession();
1978        sfControl.setReturnValue(session, 1);
1979        session.getSessionFactory();
1980        sessionControl.setReturnValue(sf, 1);
1981        session.createQuery("some query string");
1982        sessionControl.setReturnValue(query, 1);
1983        query.setParameter(0, "myvalue");
1984        queryControl.setReturnValue(query, 1);
1985        query.iterate();
1986        queryControl.setReturnValue(it, 1);
1987        session.flush();
1988        sessionControl.setVoidCallable(1);
1989        session.close();
1990        sessionControl.setReturnValue(null, 1);
1991        sfControl.replay();
1992        sessionControl.replay();
1993        queryControl.replay();
1994
1995        HibernateTemplate ht = new HibernateTemplate(sf);
1996        Iterator JavaDoc result = ht.iterate("some query string", "myvalue");
1997        assertTrue("Correct list", result == it);
1998        queryControl.verify();
1999    }
2000
2001    public void testExceptions() throws HibernateException {
2002        final SQLException JavaDoc sqlex = new SQLException JavaDoc("argh", "27");
2003        try {
2004            createTemplate().execute(new HibernateCallback() {
2005                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2006                    throw new JDBCException("mymsg", sqlex);
2007                }
2008            });
2009            fail("Should have thrown DataIntegrityViolationException");
2010        }
2011        catch (DataIntegrityViolationException ex) {
2012            // expected
2013
assertEquals(sqlex, ex.getCause());
2014            assertTrue(ex.getMessage().indexOf("mymsg") != -1);
2015        }
2016
2017        final ObjectDeletedException odex = new ObjectDeletedException("msg", "id", TestBean.class.getName());
2018        try {
2019            createTemplate().execute(new HibernateCallback() {
2020                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2021                    throw odex;
2022                }
2023            });
2024            fail("Should have thrown HibernateObjectRetrievalFailureException");
2025        }
2026        catch (HibernateObjectRetrievalFailureException ex) {
2027            // expected
2028
assertEquals(odex, ex.getCause());
2029        }
2030
2031        final WrongClassException wcex = new WrongClassException("msg", "id", TestBean.class.getName());
2032        try {
2033            createTemplate().execute(new HibernateCallback() {
2034                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2035                    throw wcex;
2036                }
2037            });
2038            fail("Should have thrown HibernateObjectRetrievalFailureException");
2039        }
2040        catch (HibernateObjectRetrievalFailureException ex) {
2041            // expected
2042
assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
2043            assertEquals("id", ex.getIdentifier());
2044            assertEquals(wcex, ex.getCause());
2045        }
2046
2047        final StaleObjectStateException sosex = new StaleObjectStateException(TestBean.class.getName(), "id");
2048        try {
2049            createTemplate().execute(new HibernateCallback() {
2050                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2051                    throw sosex;
2052                }
2053            });
2054            fail("Should have thrown HibernateOptimisticLockingFailureException");
2055        }
2056        catch (HibernateOptimisticLockingFailureException ex) {
2057            // expected
2058
assertEquals(TestBean.class.getName(), ex.getPersistentClassName());
2059            assertEquals("id", ex.getIdentifier());
2060            assertEquals(sosex, ex.getCause());
2061        }
2062
2063        final QueryException qex = new QueryException("msg");
2064        qex.setQueryString("query");
2065        try {
2066            createTemplate().execute(new HibernateCallback() {
2067                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2068                    throw qex;
2069                }
2070            });
2071            fail("Should have thrown InvalidDataAccessResourceUsageException");
2072        }
2073        catch (HibernateQueryException ex) {
2074            // expected
2075
assertEquals(qex, ex.getCause());
2076            assertEquals("query", ex.getQueryString());
2077        }
2078
2079        try {
2080            createTemplate().execute(new HibernateCallback() {
2081                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2082                    throw new PersistentObjectException("");
2083                }
2084            });
2085            fail("Should have thrown InvalidDataAccessApiUsageException");
2086        }
2087        catch (InvalidDataAccessApiUsageException ex) {
2088            // expected
2089
}
2090
2091        try {
2092            createTemplate().execute(new HibernateCallback() {
2093                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2094                    throw new TransientObjectException("");
2095                }
2096            });
2097            fail("Should have thrown InvalidDataAccessApiUsageException");
2098        }
2099        catch (InvalidDataAccessApiUsageException ex) {
2100            // expected
2101
}
2102
2103        final HibernateException hex = new HibernateException("msg");
2104        try {
2105            createTemplate().execute(new HibernateCallback() {
2106                public Object JavaDoc doInHibernate(org.hibernate.Session session) throws HibernateException {
2107                    throw hex;
2108                }
2109            });
2110            fail("Should have thrown HibernateSystemException");
2111        }
2112        catch (HibernateSystemException ex) {
2113            // expected
2114
assertEquals(hex, ex.getCause());
2115        }
2116    }
2117
2118    private HibernateTemplate createTemplate() throws HibernateException {
2119        sfControl.reset();
2120        sessionControl.reset();
2121        sf.openSession();
2122        sfControl.setReturnValue(session);
2123        session.getSessionFactory();
2124        sessionControl.setReturnValue(sf, 1);
2125        session.close();
2126        sessionControl.setReturnValue(null, 1);
2127        sfControl.replay();
2128        sessionControl.replay();
2129        return new HibernateTemplate(sf);
2130    }
2131
2132    protected void tearDown() {
2133        try {
2134            sfControl.verify();
2135            sessionControl.verify();
2136        }
2137        catch (IllegalStateException JavaDoc ex) {
2138            // ignore: test method didn't call replay
2139
}
2140        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
2141        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
2142    }
2143
2144}
2145
Popular Tags