PageRenderTime 6263ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/warp-persist/test/com/wideplay/warp/persist/hibernate/ManualLocalReadOnlyTransactionsTest.java

http://warp-persist.googlecode.com/
Java | 182 lines | 110 code | 43 blank | 29 comment | 5 complexity | 31b3878ce9da2447fbaa3868d9690111 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.hibernate;
  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.codemonkey.web.startup.Initializer;
  23. import com.wideplay.warp.persist.PersistenceService;
  24. import static com.wideplay.warp.persist.TransactionType.READ_ONLY;
  25. import static com.wideplay.warp.persist.TransactionType.READ_WRITE;
  26. import com.wideplay.warp.persist.Transactional;
  27. import com.wideplay.warp.persist.UnitOfWork;
  28. import org.hibernate.Query;
  29. import org.hibernate.SessionFactory;
  30. import org.hibernate.cfg.AnnotationConfiguration;
  31. import org.hibernate.cfg.Configuration;
  32. import org.hibernate.classic.Session;
  33. import org.hibernate.context.ManagedSessionContext;
  34. import org.testng.annotations.AfterClass;
  35. import org.testng.annotations.BeforeClass;
  36. import org.testng.annotations.Test;
  37. import java.util.Date;
  38. /**
  39. * Created with IntelliJ IDEA.
  40. * On: 2/06/2007
  41. *
  42. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  43. * @since 1.0
  44. */
  45. public class ManualLocalReadOnlyTransactionsTest {
  46. private Injector injector;
  47. private static final String UNIQUE_TEXT_PERSISTENT = "some unique text originally" + new Date();
  48. private static final String UNIQUE_TEXT_PERSISTENT2 = "some unique text taht should change" + new Date();
  49. private static final String UNIQUE_TEXT_TRANSIENT = "some unique text that you should never see!" + new Date();
  50. @BeforeClass
  51. public void pre() {
  52. injector = Guice.createInjector(PersistenceService.usingHibernate()
  53. .across(UnitOfWork.REQUEST)
  54. .forAll(Matchers.any())
  55. .buildModule(),
  56. new AbstractModule() {
  57. protected void configure() {
  58. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  59. .addAnnotatedClass(HibernateTestEntity.class)
  60. .setProperties(Initializer.loadProperties("spr-managed-persistence.properties")));
  61. }
  62. });
  63. //startup persistence
  64. injector.getInstance(PersistenceService.class)
  65. .start();
  66. }
  67. @AfterClass
  68. void post() {
  69. final SessionFactory sessionFactory = injector.getInstance(SessionFactory.class);
  70. sessionFactory.close();
  71. ManagedSessionContext.unbind(sessionFactory);
  72. }
  73. @Test
  74. public void testSimpleCrossTxnWork() {
  75. Session session1 = injector.getInstance(SessionFactory.class).openSession();
  76. ManagedSessionContext.bind(session1);
  77. final TransactionalObject txnal = injector.getInstance(TransactionalObject.class);
  78. final HibernateTestEntity entity = txnal.runOperationInTxn();
  79. //save the id
  80. txnal.id = entity.getId();
  81. assert session1.contains(entity) : "Entity was not persisted!";
  82. assert UNIQUE_TEXT_PERSISTENT.equals(entity.getText()) : "entity was not stored correctly";
  83. //run read-only txn
  84. txnal.runReadOnlyTxn();
  85. assert UNIQUE_TEXT_TRANSIENT.equals(entity.getText()) : "entity dirty state was not modified in read-only txn";
  86. Query query = session1.createQuery("from HibernateTestEntity where text = :text");
  87. query.setParameter("text", UNIQUE_TEXT_TRANSIENT);
  88. assert null == query.uniqueResult() : "Text from read-only txn was found in persistent store!";
  89. query = session1.createQuery("from HibernateTestEntity where text = :text");
  90. query.setParameter("text", UNIQUE_TEXT_PERSISTENT);
  91. assert null != query.uniqueResult() : "Read-only txn affected persistent store!";
  92. //run read-write txn
  93. txnal.runReadWriteTxn();
  94. assert UNIQUE_TEXT_PERSISTENT2.equals(entity.getText()) : "entity was not modified in read-write txn!!";
  95. query = session1.createQuery("from HibernateTestEntity where text = :text");
  96. query.setParameter("text", UNIQUE_TEXT_PERSISTENT);
  97. assert null == query.uniqueResult() : "Text from original txn was found in persistent store!";
  98. //run read-only txn again, after having run a read-write txn
  99. txnal.runReadOnlyTxn();
  100. assert UNIQUE_TEXT_TRANSIENT.equals(entity.getText()) : "entity appears modified in read-only txn!!";
  101. query = session1.createQuery("from HibernateTestEntity where text = :text");
  102. query.setParameter("text", UNIQUE_TEXT_TRANSIENT);
  103. assert null == query.uniqueResult() : "Text from read-only txn was found in persistent store!";
  104. session1.close();
  105. //open a new session
  106. session1 = injector.getInstance(SessionFactory.class).openSession();
  107. ManagedSessionContext.bind(session1);
  108. //assert that the persistent copy of the entity is as it should be
  109. query = session1.createQuery("from HibernateTestEntity where text = :text");
  110. query.setParameter("text", UNIQUE_TEXT_PERSISTENT2);
  111. final HibernateTestEntity persistentCopy = (HibernateTestEntity) query.uniqueResult();
  112. assert null != persistentCopy : "Text from read-only txn was found in persistent store!";
  113. System.out.println(persistentCopy.getText());
  114. assert UNIQUE_TEXT_PERSISTENT2.equals(persistentCopy.getText()) : "Persistent copy of entity appears modified by read-only txn";
  115. session1.close();
  116. }
  117. public static class TransactionalObject {
  118. @Inject
  119. org.hibernate.Session session;
  120. private Long id;
  121. @Transactional
  122. public HibernateTestEntity runOperationInTxn() {
  123. HibernateTestEntity entity = new HibernateTestEntity();
  124. entity.setText(UNIQUE_TEXT_PERSISTENT);
  125. session.persist(entity);
  126. return entity;
  127. }
  128. @Transactional(type = READ_ONLY)
  129. public void runReadOnlyTxn() {
  130. HibernateTestEntity entity = (HibernateTestEntity) session.get(HibernateTestEntity.class, id);
  131. entity.setText(UNIQUE_TEXT_TRANSIENT);
  132. }
  133. @Transactional(type = READ_WRITE)
  134. public void runReadWriteTxn() {
  135. HibernateTestEntity entity = (HibernateTestEntity) session.get(HibernateTestEntity.class, id);
  136. entity.setText(UNIQUE_TEXT_PERSISTENT2);
  137. }
  138. }
  139. }