PageRenderTime 4569ms CodeModel.GetById 23ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://warp-persist.googlecode.com/
Java | 148 lines | 76 code | 29 blank | 43 comment | 0 complexity | 1d1ea3257147de50197b097802776a34 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.BeforeClass;
  25. import org.testng.annotations.Test;
  26. import javax.persistence.EntityManager;
  27. import javax.persistence.EntityManagerFactory;
  28. /**
  29. * Created by IntelliJ IDEA.
  30. * User: Dhanji R. Prasanna (dhanji@gmail.com)
  31. * Date: 1/06/2007
  32. * Time: 11:40:36
  33. *
  34. * A test around providing sessions (starting, closing etc.)
  35. *
  36. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  37. * @since 1.0
  38. */
  39. @Test(suiteName = "jpa")
  40. public class EntityManagerPerRequestProvisionTest {
  41. private Injector injector;
  42. @BeforeClass
  43. public void pre() {
  44. injector = Guice.createInjector(PersistenceService.usingJpa()
  45. .across(UnitOfWork.REQUEST)
  46. .forAll(Matchers.any())
  47. .buildModule(),
  48. new AbstractModule() {
  49. protected void configure() {
  50. //tell Warp the name of the jpa persistence unit
  51. bindConstant().annotatedWith(JpaUnit.class).to("testUnit");
  52. }
  53. });
  54. //startup persistence
  55. injector.getInstance(PersistenceService.class)
  56. .start();
  57. injector.getInstance(WorkManager.class).beginWork();
  58. }
  59. @AfterClass
  60. public final void postClass() {
  61. injector.getInstance(WorkManager.class).endWork();
  62. injector.getInstance(EntityManagerFactory.class).close();
  63. }
  64. @Test
  65. public void testEntityManagerLifecyclePerTxn() {
  66. //obtain em
  67. JpaDao dao = injector.getInstance(JpaDao.class);
  68. //obtain same em again (bound to txn)
  69. JpaTestEntity te = new JpaTestEntity();
  70. dao.persist(te);
  71. //im not sure this hack works...
  72. assert JpaDao.em.equals(injector.getInstance(EntityManager.class))
  73. : "Entity managers closed inside same thread-scope";
  74. //try to start a new em in a new txn
  75. dao = injector.getInstance(JpaDao.class);
  76. assert dao.contains(te) : "EntityManager was closed and reopened around txn (persistent object does not persist)";
  77. }
  78. @Test //a duplicate to try and induce static crossovers
  79. public void testEntityManagerLifecyclePerTxn2() {
  80. //obtain em
  81. JpaDao dao = injector.getInstance(JpaDao.class);
  82. //obtain same em again (bound to txn)
  83. JpaTestEntity te = new JpaTestEntity();
  84. dao.persist(te);
  85. //im not sure this hack works...
  86. assert JpaDao.em.equals(injector.getInstance(EntityManager.class))
  87. : "Duplicate entity managers crossing-scope";
  88. assert JpaDao.em.equals(injector.getInstance(EntityManager.class))
  89. : "Duplicate entity managers crossing-scope";
  90. // TODO commented out after multiple modules refactoring
  91. //some fuzz to test no open/close is happening
  92. //assert EntityManagerFactoryHolder.checkCurrentEntityManager() == EntityManagerFactoryHolder.getCurrentEntityManager();
  93. //assert EntityManagerFactoryHolder.checkCurrentEntityManager() == EntityManagerFactoryHolder.getCurrentEntityManager();
  94. //assert EntityManagerFactoryHolder.checkCurrentEntityManager() == EntityManagerFactoryHolder.getCurrentEntityManager();
  95. //assert EntityManagerFactoryHolder.checkCurrentEntityManager() == EntityManagerFactoryHolder.getCurrentEntityManager();
  96. //assert EntityManagerFactoryHolder.checkCurrentEntityManager() == EntityManagerFactoryHolder.getCurrentEntityManager();
  97. //try to start a new em in a new txn
  98. dao = injector.getInstance(JpaDao.class);
  99. assert dao.contains(te) : "EntityManager was closed and reopened around txn (persistent object doesnt persist)";
  100. }
  101. public static class JpaDao {
  102. static EntityManager em;
  103. @Inject
  104. public JpaDao(EntityManager em) {
  105. JpaDao.em = em;
  106. }
  107. @Transactional
  108. public <T> void persist(T t) {
  109. assert em.isOpen() : "em is not open!";
  110. assert em.getTransaction().isActive() : "no active txn!";
  111. em.persist(t);
  112. assert em.contains(t) : "Persisting object failed";
  113. }
  114. @Transactional
  115. public <T> boolean contains(T t) {
  116. return em.contains(t);
  117. }
  118. }
  119. }