PageRenderTime 3610ms CodeModel.GetById 17ms RepoModel.GetById 2ms app.codeStats 0ms

/warp-persist/test/com/wideplay/warp/persist/jpa/JoiningLocalTransactionsTest.java

http://warp-persist.googlecode.com/
Java | 187 lines | 121 code | 36 blank | 30 comment | 2 complexity | dd69664be8fc385be5f0ac4189c73513 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (C) 2008 Wideplay Interactive.
  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. package com.wideplay.warp.persist.jpa;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Inject;
  20. import com.google.inject.Injector;
  21. import com.google.inject.matcher.Matchers;
  22. import com.wideplay.warp.persist.*;
  23. import org.testng.annotations.AfterClass;
  24. import org.testng.annotations.AfterTest;
  25. import org.testng.annotations.BeforeClass;
  26. import org.testng.annotations.Test;
  27. import javax.persistence.EntityManager;
  28. import javax.persistence.EntityManagerFactory;
  29. import javax.persistence.NoResultException;
  30. import java.io.IOException;
  31. import java.util.Date;
  32. /**
  33. * Created with IntelliJ IDEA.
  34. * On: 2/06/2007
  35. *
  36. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  37. * @since 1.0
  38. */
  39. @Test(suiteName = "jpa")
  40. public class JoiningLocalTransactionsTest {
  41. private Injector injector;
  42. private static final String UNIQUE_TEXT = JoiningLocalTransactionsTest.class + "some unique text" + new Date();
  43. private static final String TRANSIENT_UNIQUE_TEXT = JoiningLocalTransactionsTest.class + "some other unique text" + new Date();
  44. @BeforeClass
  45. public void pre() {
  46. injector = Guice.createInjector(PersistenceService.usingJpa()
  47. .across(UnitOfWork.TRANSACTION)
  48. .forAll(Matchers.any())
  49. .buildModule(),
  50. new AbstractModule() {
  51. protected void configure() {
  52. //tell Warp the name of the jpa persistence unit
  53. bindConstant().annotatedWith(JpaUnit.class).to("testUnit");
  54. }
  55. });
  56. //startup persistence
  57. injector.getInstance(PersistenceService.class)
  58. .start();
  59. }
  60. @AfterTest
  61. //cleanup entitymanager in case some of the rollback tests left it in an open state
  62. public final void post() {
  63. injector.getInstance(WorkManager.class).endWork();
  64. }
  65. @AfterClass
  66. public final void postClass() {
  67. injector.getInstance(EntityManagerFactory.class).close();
  68. }
  69. @Test
  70. public void testSimpleTransaction() {
  71. injector.getInstance(JoiningLocalTransactionsTest.TransactionalObject.class).runOperationInTxn();
  72. EntityManager em = injector.getInstance(EntityManager.class);
  73. assert !em.getTransaction().isActive() : "txn was not closed by transactional service";
  74. //test that the data has been stored
  75. Object result = em.createQuery("from JpaTestEntity where text = :text").setParameter("text", UNIQUE_TEXT).getSingleResult();
  76. injector.getInstance(WorkManager.class).endWork();
  77. assert result instanceof JpaTestEntity : "odd result returned fatal";
  78. assert UNIQUE_TEXT.equals(((JpaTestEntity)result).getText()) : "queried entity did not match--did automatic txn fail?";
  79. }
  80. @Test(expectedExceptions = NoResultException.class)
  81. public void testSimpleTransactionRollbackOnChecked() {
  82. try {
  83. injector.getInstance(JoiningLocalTransactionsTest.TransactionalObject.class).runOperationInTxnThrowingChecked();
  84. } catch(IOException e) {
  85. //ignore
  86. System.out.println("caught (expecting rollback) " + e);
  87. injector.getInstance(WorkManager.class).endWork();
  88. }
  89. EntityManager em = injector.getInstance(EntityManager.class);
  90. assert !em.getTransaction().isActive() : "EM was not closed by transactional service (rollback didnt happen?)";
  91. //test that the data has been stored
  92. Object result = em.createQuery("from JpaTestEntity where text = :text").setParameter("text", TRANSIENT_UNIQUE_TEXT).getSingleResult();
  93. injector.getInstance(WorkManager.class).endWork();
  94. assert null == result : "a result was returned! rollback sure didnt happen!!!";
  95. }
  96. @Test(expectedExceptions = NoResultException.class)
  97. public void testSimpleTransactionRollbackOnUnchecked() {
  98. try {
  99. injector.getInstance(JoiningLocalTransactionsTest.TransactionalObject.class).runOperationInTxnThrowingUnchecked();
  100. } catch(RuntimeException re) {
  101. //ignore
  102. System.out.println("caught (expecting rollback) " + re);
  103. injector.getInstance(WorkManager.class).endWork();
  104. }
  105. EntityManager em = injector.getInstance(EntityManager.class);
  106. assert !em.getTransaction().isActive() : "Session was not closed by transactional service (rollback didnt happen?)";
  107. //test that the data has been stored
  108. Object result = em.createQuery("from JpaTestEntity where text = :text").setParameter("text", TRANSIENT_UNIQUE_TEXT).getSingleResult();
  109. injector.getInstance(WorkManager.class).endWork();
  110. assert null == result : "a result was returned! rollback sure didnt happen!!!";
  111. }
  112. public static class TransactionalObject {
  113. private final EntityManager em;
  114. @Inject
  115. public TransactionalObject(EntityManager em) {
  116. this.em = em;
  117. }
  118. @Transactional
  119. public void runOperationInTxn() {
  120. runOperationInTxnInternal();
  121. }
  122. @Transactional(rollbackOn = IOException.class)
  123. public void runOperationInTxnInternal() {
  124. JpaTestEntity entity = new JpaTestEntity();
  125. entity.setText(UNIQUE_TEXT);
  126. em.persist(entity);
  127. }
  128. @Transactional(rollbackOn = IOException.class)
  129. public void runOperationInTxnThrowingChecked() throws IOException {
  130. runOperationInTxnThrowingCheckedInternal();
  131. }
  132. @Transactional
  133. private void runOperationInTxnThrowingCheckedInternal() throws IOException {
  134. JpaTestEntity entity = new JpaTestEntity();
  135. entity.setText(TRANSIENT_UNIQUE_TEXT);
  136. em.persist(entity);
  137. throw new IOException();
  138. }
  139. @Transactional
  140. public void runOperationInTxnThrowingUnchecked() {
  141. runOperationInTxnThrowingUncheckedInternal();
  142. }
  143. @Transactional(rollbackOn = IOException.class)
  144. public void runOperationInTxnThrowingUncheckedInternal() {
  145. JpaTestEntity entity = new JpaTestEntity();
  146. entity.setText(TRANSIENT_UNIQUE_TEXT);
  147. em.persist(entity);
  148. throw new IllegalStateException();
  149. }
  150. }
  151. }