PageRenderTime 3248ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/warp-persistWithJpaAndHibernate/test/com/wideplay/warp/hibernate/SessionProvisionTest.java

http://warp-persist.googlecode.com/
Java | 85 lines | 53 code | 17 blank | 15 comment | 0 complexity | ea9d0e014f948f17c054f7a790a399d5 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.wideplay.warp.hibernate;
  2. import com.google.inject.Injector;
  3. import com.google.inject.Guice;
  4. import com.google.inject.AbstractModule;
  5. import com.google.inject.matcher.Matchers;
  6. import com.wideplay.codemonkey.web.startup.Initializer;
  7. import com.wideplay.warp.hibernate.HibernatePersistenceService;
  8. import com.wideplay.warp.persist.UnitOfWork;
  9. import com.wideplay.warp.persist.TransactionStrategy;
  10. import com.wideplay.warp.persist.PersistenceService;
  11. import org.testng.annotations.Test;
  12. import org.testng.annotations.BeforeClass;
  13. import org.hibernate.cfg.AnnotationConfiguration;
  14. import org.hibernate.cfg.Configuration;
  15. import org.hibernate.Session;
  16. /**
  17. * Created by IntelliJ IDEA.
  18. * User: dprasanna
  19. * Date: 1/06/2007
  20. * Time: 11:40:36
  21. *
  22. * A test around providing sessions (starting, closing etc.)
  23. *
  24. * @author dprasanna
  25. * @since 1.0
  26. */
  27. public class SessionProvisionTest {
  28. private Injector injector;
  29. @BeforeClass
  30. public void pre() {
  31. injector = Guice.createInjector(PersistenceService.usingHibernate()
  32. .across(UnitOfWork.TRANSACTION)
  33. .transactedWith(TransactionStrategy.LOCAL)
  34. .forAll(Matchers.any())
  35. .buildModule(),
  36. new AbstractModule() {
  37. protected void configure() {
  38. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  39. .addAnnotatedClass(HibernateTestEntity.class)
  40. .setProperties(Initializer.loadProperties("spt-persistence.properties")));
  41. }
  42. });
  43. //startup persistence
  44. injector.getInstance(PersistenceService.class)
  45. .start();
  46. }
  47. @Test
  48. public void testSessionLifecyclePerTxn() {
  49. //obtain session
  50. Session session = injector.getInstance(Session.class);
  51. assert session.isOpen() : "session is not open!";
  52. session.beginTransaction();
  53. assert session.getTransaction().isActive() : "no active txn!";
  54. //obtain same session again (bound to txn)
  55. HibernateTestEntity te = new HibernateTestEntity();
  56. session.persist(te);
  57. assert session.contains(te) : "Persisting object failed";
  58. assert injector.getInstance(Session.class).contains(te) : "Duplicate sessions crossing-scope";
  59. session.getTransaction().commit();
  60. assert !session.isOpen() : "Session did not close on txn commit--current_session_context_class=thread may not be set";
  61. //try to start a new session in a new txn
  62. session = injector.getInstance(Session.class);
  63. session.beginTransaction();
  64. assert !session.contains(te) : "Session wasnt closed and reopened properly (persistent object persists)";
  65. session.getTransaction().rollback();
  66. assert !session.isOpen() : "Session not closed on rollback!!";
  67. }
  68. }