/sigmah/src/test/java/org/sigmah/server/dao/hibernate/TransactionalInterceptorTest.java

http://sigma-h.googlecode.com/ · Java · 157 lines · 115 code · 38 blank · 4 comment · 0 complexity · 40b818347769438dd7a757dcab383fcf MD5 · raw file

  1. /*
  2. * All Sigmah code is released under the GNU General Public License v3
  3. * See COPYRIGHT.txt and LICENSE.txt.
  4. */
  5. package org.sigmah.server.dao.hibernate;
  6. import static org.easymock.EasyMock.createMock;
  7. import static org.easymock.EasyMock.createStrictMock;
  8. import static org.easymock.EasyMock.expect;
  9. import static org.easymock.EasyMock.replay;
  10. import static org.easymock.EasyMock.verify;
  11. import static org.junit.Assert.assertNotNull;
  12. import javax.persistence.EntityManager;
  13. import javax.persistence.EntityTransaction;
  14. import org.junit.Test;
  15. import org.junit.runner.RunWith;
  16. import org.sigmah.server.dao.Transactional;
  17. import org.sigmah.server.util.BeanMappingModule;
  18. import org.sigmah.test.InjectionSupport;
  19. import org.sigmah.test.Modules;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. @RunWith(InjectionSupport.class)
  23. @Modules({
  24. BeanMappingModule.class
  25. })
  26. public class TransactionalInterceptorTest {
  27. @Test
  28. public void testSuccessfulCase() {
  29. EntityTransaction tx = createMock(EntityTransaction.class);
  30. expect(tx.isActive()).andReturn(false);
  31. tx.begin();
  32. tx.commit();
  33. replay(tx);
  34. MockClass mock = getMockInstance(tx);
  35. mock.succeedsWithoutException();
  36. verify(tx);
  37. }
  38. @Test
  39. public void testFailedCase() {
  40. EntityTransaction tx = createMock(EntityTransaction.class);
  41. expect(tx.isActive()).andReturn(false);
  42. tx.begin();
  43. tx.rollback();
  44. replay(tx);
  45. MockClass mock = getMockInstance(tx);
  46. RuntimeException rte = null;
  47. try {
  48. mock.throwsRuntimeException();
  49. } catch (RuntimeException e) {
  50. rte = e;
  51. }
  52. assertNotNull("exception is propagated", rte);
  53. verify(tx);
  54. }
  55. @Test
  56. public void testNestedSuccessful() {
  57. EntityTransaction tx = createStrictMock(EntityTransaction.class);
  58. expect(tx.isActive()).andReturn(false);
  59. tx.begin();
  60. expect(tx.isActive()).andReturn(true);
  61. tx.commit();
  62. replay(tx);
  63. MockClass mock = getMockInstance(tx);
  64. mock.successfulNestedTransactions();
  65. verify(tx);
  66. }
  67. @Test
  68. public void testNestedUnsuccessful() {
  69. EntityTransaction tx = createStrictMock(EntityTransaction.class);
  70. expect(tx.isActive()).andReturn(false);
  71. tx.begin();
  72. expect(tx.isActive()).andReturn(true);
  73. tx.rollback();
  74. replay(tx);
  75. MockClass mock = getMockInstance(tx);
  76. RuntimeException rte = null;
  77. try {
  78. mock.nestedTransactionWithFailureOnSecondLevel();
  79. } catch (RuntimeException e) {
  80. rte = e;
  81. }
  82. assertNotNull("exception is propagated", rte);
  83. verify(tx);
  84. }
  85. private MockClass getMockInstance(EntityTransaction tx) {
  86. Injector injector = Guice.createInjector(new MockEntityManagerModule(tx));
  87. MockClass mock = injector.getInstance(MockClass.class);
  88. return mock;
  89. }
  90. public static class MockClass {
  91. @Transactional
  92. public void succeedsWithoutException() {
  93. }
  94. @Transactional
  95. public void throwsRuntimeException() {
  96. throw new RuntimeException();
  97. }
  98. @Transactional
  99. public void successfulNestedTransactions() {
  100. succeedsWithoutException();
  101. }
  102. @Transactional
  103. public void nestedTransactionWithFailureOnSecondLevel() {
  104. throwsRuntimeException();
  105. }
  106. }
  107. private static class MockEntityManagerModule extends HibernateModule {
  108. private EntityManager em;
  109. public MockEntityManagerModule(EntityTransaction tx) {
  110. em = createMock(EntityManager.class);
  111. expect(em.getTransaction()).andReturn(tx).anyTimes();
  112. replay(em);
  113. }
  114. @Override
  115. protected void configureEm() {
  116. bind(EntityManager.class).toInstance(em);
  117. }
  118. }
  119. }