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