PageRenderTime 59ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/metamorphiccode/guice
Java | 105 lines | 58 code | 22 blank | 25 comment | 0 complexity | 4842578a6cd32a3e356f55bb584b0978 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 java.util.Date;
  25. import javax.persistence.EntityManager;
  26. import javax.persistence.EntityManagerFactory;
  27. /**
  28. * For instance, a session-per-request strategy will control the opening and closing of the EM at
  29. * its own (manual) discretion. As opposed to a transactional unit of work.
  30. *
  31. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  32. */
  33. public class ManualLocalTransactionsTest extends TestCase {
  34. private Injector injector;
  35. private static final String UNIQUE_TEXT = "some unique text" + new Date();
  36. private static final String UNIQUE_TEXT_2 = "some other unique text" + new Date();
  37. public void setUp() {
  38. injector = Guice.createInjector(new JpaPersistModule("testUnit"));
  39. //startup persistence
  40. injector.getInstance(PersistService.class).start();
  41. }
  42. public void tearDown() {
  43. injector.getInstance(EntityManagerFactory.class).close();
  44. }
  45. public void testSimpleCrossTxnWork() {
  46. injector.getInstance(UnitOfWork.class).begin();
  47. //pretend that the request was started here
  48. EntityManager em = injector.getInstance(EntityManager.class);
  49. JpaTestEntity entity = injector.getInstance(TransactionalObject.class).runOperationInTxn();
  50. injector.getInstance(TransactionalObject.class).runOperationInTxn2();
  51. //persisted entity should remain in the same em (which should still be open)
  52. assertTrue("EntityManager appears to have been closed across txns!",
  53. injector.getInstance(EntityManager.class).contains(entity));
  54. assertTrue("EntityManager appears to have been closed across txns!", em.contains(entity));
  55. assertTrue("EntityManager appears to have been closed across txns!", em.isOpen());
  56. injector.getInstance(UnitOfWork.class).end();
  57. injector.getInstance(UnitOfWork.class).begin();
  58. //try to query them back out
  59. em = injector.getInstance(EntityManager.class);
  60. assertNotNull(em.createQuery("from JpaTestEntity where text = :text")
  61. .setParameter("text", UNIQUE_TEXT).getSingleResult());
  62. assertNotNull(em.createQuery("from JpaTestEntity where text = :text")
  63. .setParameter("text", UNIQUE_TEXT_2).getSingleResult());
  64. em.close();
  65. assertFalse(em.isOpen());
  66. }
  67. public static class TransactionalObject {
  68. @Inject EntityManager em;
  69. @Transactional
  70. public JpaTestEntity runOperationInTxn() {
  71. JpaTestEntity entity = new JpaTestEntity();
  72. entity.setText(UNIQUE_TEXT);
  73. em.persist(entity);
  74. return entity;
  75. }
  76. @Transactional
  77. public void runOperationInTxn2() {
  78. JpaTestEntity entity = new JpaTestEntity();
  79. entity.setText(UNIQUE_TEXT_2);
  80. em.persist(entity);
  81. }
  82. }
  83. }