/extensions/persist/test/com/google/inject/persist/jpa/JpaWorkManagerTest.java

https://gitlab.com/metamorphiccode/guice · Java · 123 lines · 77 code · 25 blank · 21 comment · 0 complexity · bfff72d94dadae55681ceeef5faf0139 MD5 · raw file

  1. /**
  2. * Copyright (C) 2010 Google, Inc.
  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.google.inject.persist.jpa;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Inject;
  19. import com.google.inject.Injector;
  20. import com.google.inject.persist.PersistService;
  21. import com.google.inject.persist.Transactional;
  22. import com.google.inject.persist.UnitOfWork;
  23. import junit.framework.TestCase;
  24. import org.hibernate.HibernateException;
  25. import java.util.Date;
  26. import javax.persistence.EntityManager;
  27. import javax.persistence.EntityManagerFactory;
  28. import javax.persistence.Query;
  29. /**
  30. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  31. */
  32. public class JpaWorkManagerTest extends TestCase {
  33. private Injector injector;
  34. private static final String UNIQUE_TEXT_3 = JpaWorkManagerTest.class.getSimpleName()
  35. + "CONSTRAINT_VIOLATING some other unique text" + new Date();
  36. @Override
  37. public void setUp() {
  38. injector = Guice.createInjector(new JpaPersistModule("testUnit"));
  39. //startup persistence
  40. injector.getInstance(PersistService.class).start();
  41. }
  42. @Override
  43. public void tearDown() {
  44. try {
  45. injector.getInstance(EntityManagerFactory.class).close();
  46. } catch(HibernateException ex) {
  47. // Expected if the persist service has already been stopped.
  48. }
  49. }
  50. public void testWorkManagerInSession() {
  51. injector.getInstance(UnitOfWork.class).begin();
  52. try {
  53. injector.getInstance(TransactionalObject.class).runOperationInTxn();
  54. } finally {
  55. injector.getInstance(UnitOfWork.class).end();
  56. }
  57. injector.getInstance(UnitOfWork.class).begin();
  58. injector.getInstance(EntityManager.class).getTransaction().begin();
  59. try {
  60. final Query query = injector.getInstance(EntityManager.class)
  61. .createQuery("select e from JpaTestEntity as e where text = :text");
  62. query.setParameter("text", UNIQUE_TEXT_3);
  63. final Object o = query.getSingleResult();
  64. assertNotNull("no result!!", o);
  65. assertTrue("Unknown type returned " + o.getClass(), o instanceof JpaTestEntity);
  66. JpaTestEntity ent = (JpaTestEntity) o;
  67. assertEquals("Incorrect result returned or not persisted properly" + ent.getText(),
  68. UNIQUE_TEXT_3, ent.getText());
  69. } finally {
  70. injector.getInstance(EntityManager.class).getTransaction().commit();
  71. injector.getInstance(UnitOfWork.class).end();
  72. }
  73. }
  74. public void testCloseMoreThanOnce() {
  75. injector.getInstance(PersistService.class).stop();
  76. try {
  77. injector.getInstance(PersistService.class).stop();
  78. fail();
  79. } catch (IllegalStateException e) {
  80. // Ignored.
  81. }
  82. }
  83. public static class TransactionalObject {
  84. @Inject EntityManager em;
  85. @Transactional
  86. public void runOperationInTxn() {
  87. JpaTestEntity testEntity = new JpaTestEntity();
  88. testEntity.setText(UNIQUE_TEXT_3);
  89. em.persist(testEntity);
  90. }
  91. @Transactional
  92. public void runOperationInTxnError() {
  93. JpaTestEntity testEntity = new JpaTestEntity();
  94. testEntity.setText(UNIQUE_TEXT_3 + "transient never in db!" + hashCode());
  95. em.persist(testEntity);
  96. }
  97. }
  98. }