PageRenderTime 465ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://warp-persist.googlecode.com/
Java | 144 lines | 104 code | 27 blank | 13 comment | 2 complexity | b1b70840a1a3a2986ad2a8fe89916cac MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.wideplay.warp.hibernate;
  2. import org.testng.annotations.Test;
  3. import org.testng.annotations.BeforeClass;
  4. import org.testng.annotations.AfterClass;
  5. import org.hibernate.cfg.AnnotationConfiguration;
  6. import org.hibernate.cfg.Configuration;
  7. import org.hibernate.Session;
  8. import org.hibernate.SessionFactory;
  9. import org.hibernate.criterion.Expression;
  10. import com.google.inject.Guice;
  11. import com.google.inject.Injector;
  12. import com.google.inject.Inject;
  13. import com.google.inject.AbstractModule;
  14. import com.google.inject.matcher.Matchers;
  15. import com.wideplay.codemonkey.web.startup.Initializer;
  16. import com.wideplay.warp.persist.*;
  17. import java.util.Date;
  18. import java.io.IOException;
  19. /**
  20. * Created with IntelliJ IDEA.
  21. * On: 2/06/2007
  22. *
  23. * @author Dhanji R. Prasanna
  24. * @since 1.0
  25. */
  26. public class ManagedLocalTransactionsTest {
  27. private Injector injector;
  28. private static final String UNIQUE_TEXT = "some unique text" + new Date();
  29. private static final String TRANSIENT_UNIQUE_TEXT = "some other unique text" + new Date();
  30. @BeforeClass
  31. public void pre() {
  32. injector = Guice.createInjector(PersistenceService.usingHibernate()
  33. .across(UnitOfWork.TRANSACTION)
  34. .transactedWith(TransactionStrategy.LOCAL)
  35. .forAll(Matchers.any())
  36. .buildModule(),
  37. new AbstractModule() {
  38. protected void configure() {
  39. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  40. .addAnnotatedClass(HibernateTestEntity.class)
  41. .setProperties(Initializer.loadProperties("spt-persistence.properties")));
  42. }
  43. }
  44. );
  45. //startup persistence
  46. injector.getInstance(PersistenceService.class)
  47. .start();
  48. }
  49. @AfterClass void post() {
  50. injector.getInstance(SessionFactory.class).close();
  51. }
  52. @Test
  53. public void testSimpleTransaction() {
  54. injector.getInstance(TransactionalObject.class).runOperationInTxn();
  55. Session session = injector.getInstance(Session.class);
  56. assert !session.getTransaction().isActive() : "Session was not closed by transactional service";
  57. //test that the data has been stored
  58. session.beginTransaction();
  59. Object result = session.createCriteria(HibernateTestEntity.class).add(Expression.eq("text", UNIQUE_TEXT)).uniqueResult();
  60. session.getTransaction().commit();
  61. assert result instanceof HibernateTestEntity : "odd result returned fatal";
  62. assert UNIQUE_TEXT.equals(((HibernateTestEntity)result).getText()) : "queried entity did not match--did automatic txn fail?";
  63. }
  64. @Test
  65. public void testSimpleTransactionRollbackOnChecked() {
  66. try {
  67. injector.getInstance(TransactionalObject.class).runOperationInTxnThrowingChecked();
  68. } catch(IOException e) {
  69. //ignore
  70. }
  71. Session session = injector.getInstance(Session.class);
  72. assert !session.getTransaction().isActive() : "Session was not closed by transactional service (rollback didnt happen?)";
  73. //test that the data has been stored
  74. session.beginTransaction();
  75. Object result = session.createCriteria(HibernateTestEntity.class).add(Expression.eq("text", TRANSIENT_UNIQUE_TEXT)).uniqueResult();
  76. session.getTransaction().commit();
  77. assert null == result : "a result was returned! rollback sure didnt happen!!!";
  78. }
  79. @Test
  80. public void testSimpleTransactionRollbackOnUnchecked() {
  81. try {
  82. injector.getInstance(TransactionalObject.class).runOperationInTxnThrowingUnchecked();
  83. } catch(RuntimeException re) {
  84. //ignore
  85. }
  86. Session session = injector.getInstance(Session.class);
  87. assert !session.getTransaction().isActive() : "Session was not closed by transactional service (rollback didnt happen?)";
  88. //test that the data has been stored
  89. session.beginTransaction();
  90. Object result = session.createCriteria(HibernateTestEntity.class).add(Expression.eq("text", TRANSIENT_UNIQUE_TEXT)).uniqueResult();
  91. session.getTransaction().commit();
  92. assert null == result : "a result was returned! rollback sure didnt happen!!!";
  93. }
  94. public static class TransactionalObject {
  95. @Inject Session session;
  96. @Transactional
  97. public void runOperationInTxn() {
  98. HibernateTestEntity entity = new HibernateTestEntity();
  99. entity.setText(UNIQUE_TEXT);
  100. session.persist(entity);
  101. }
  102. @Transactional(rollbackOn = IOException.class)
  103. public void runOperationInTxnThrowingChecked() throws IOException {
  104. HibernateTestEntity entity = new HibernateTestEntity();
  105. entity.setText(TRANSIENT_UNIQUE_TEXT);
  106. session.persist(entity);
  107. throw new IOException();
  108. }
  109. @Transactional
  110. public void runOperationInTxnThrowingUnchecked() {
  111. HibernateTestEntity entity = new HibernateTestEntity();
  112. entity.setText(TRANSIENT_UNIQUE_TEXT);
  113. session.persist(entity);
  114. throw new IllegalStateException();
  115. }
  116. }
  117. }