PageRenderTime 103ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/code-generation/content-repo-dao-impl/src/test/java/com/smartitengineering/cms/repo/dao/impl/tx/TransactionalInterceptorTest.java

https://github.com/frodooooo39/smart-cms
Java | 288 lines | 258 code | 26 blank | 4 comment | 0 complexity | 9c997b7bb42b03dd18ac9b6f62c34106 MD5 | raw file
  1. package com.smartitengineering.cms.repo.dao.impl.tx;
  2. import com.google.inject.AbstractModule;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Injector;
  5. import com.smartitengineering.cms.repo.dao.tx.Transaction;
  6. import com.smartitengineering.cms.repo.dao.tx.TransactionException;
  7. import com.smartitengineering.cms.repo.dao.tx.TransactionManager;
  8. import com.smartitengineering.cms.repo.dao.tx.Transactional;
  9. import java.lang.reflect.Method;
  10. import org.aopalliance.intercept.MethodInvocation;
  11. import org.jmock.Expectations;
  12. import org.jmock.Mockery;
  13. import org.jmock.Sequence;
  14. import org.junit.Assert;
  15. import org.junit.Before;
  16. import org.junit.Test;
  17. /**
  18. *
  19. * @author imyousuf
  20. */
  21. public class TransactionalInterceptorTest {
  22. public static class TransactionalSampleUse {
  23. @Transactional(propagationRequired = false)
  24. public void singleTransactionWithoutPropagation() {
  25. }
  26. @Transactional(isolated = true)
  27. public void singleTransactionWithIsolation() {
  28. }
  29. @Transactional(isolated = false)
  30. public void singleTransactionWithNonIsolation() {
  31. }
  32. @Transactional
  33. public void nestedTransactionWithNonPropagation() {
  34. singleTransactionWithoutPropagation();
  35. }
  36. @Transactional
  37. public void nestedTransactionWithPropagation() {
  38. singleTransactionWithIsolation();
  39. }
  40. public void nonTransactional() {
  41. }
  42. }
  43. private final Method propagtedIsolatedTransaction;
  44. private final Method propagtedNonIsolatedTransaction;
  45. private final Method propagtedIsolatedNestedTransaction;
  46. private final Method nonPropagtedIsolatedNestedTransaction;
  47. private final Method nonTransaction;
  48. private final TransactionalSampleUse sampleService;
  49. private Mockery mockery;
  50. private TransactionManager mockManager;
  51. private Injector injector;
  52. private TransactionalInterceptor interceptor;
  53. public TransactionalInterceptorTest() {
  54. try {
  55. propagtedIsolatedTransaction = TransactionalSampleUse.class.getDeclaredMethod("singleTransactionWithIsolation");
  56. propagtedNonIsolatedTransaction = TransactionalSampleUse.class.getDeclaredMethod(
  57. "singleTransactionWithNonIsolation");
  58. propagtedIsolatedNestedTransaction = TransactionalSampleUse.class.getDeclaredMethod(
  59. "nestedTransactionWithPropagation");
  60. nonPropagtedIsolatedNestedTransaction = TransactionalSampleUse.class.getDeclaredMethod(
  61. "singleTransactionWithoutPropagation");
  62. nonTransaction = TransactionalSampleUse.class.getDeclaredMethod("nonTransactional");
  63. sampleService = new TransactionalSampleUse();
  64. }
  65. catch (Exception ex) {
  66. throw new RuntimeException(ex);
  67. }
  68. }
  69. @Before
  70. public void setup() {
  71. mockery = new Mockery();
  72. mockManager = mockery.mock(TransactionManager.class);
  73. injector = Guice.createInjector(new AbstractModule() {
  74. @Override
  75. protected void configure() {
  76. bind(TransactionManager.class).toInstance(mockManager);
  77. bind(TransactionalInterceptor.class);
  78. }
  79. });
  80. interceptor = injector.getInstance(TransactionalInterceptor.class);
  81. }
  82. @Test
  83. public void testNullMethod() throws Throwable {
  84. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  85. mockery.checking(new Expectations() {
  86. {
  87. exactly(1).of(invocation).getMethod();
  88. will(returnValue(null));
  89. }
  90. });
  91. interceptor.invoke(invocation);
  92. mockery.assertIsSatisfied();
  93. }
  94. @Test
  95. public void testNullAnnotation() throws Throwable {
  96. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  97. mockery.checking(new Expectations() {
  98. {
  99. exactly(1).of(invocation).getMethod();
  100. will(returnValue(nonTransaction));
  101. exactly(1).of(invocation).proceed();
  102. will(returnValue(sampleService));
  103. }
  104. });
  105. Assert.assertSame(sampleService, interceptor.invoke(invocation));
  106. mockery.assertIsSatisfied();
  107. }
  108. @Test
  109. public void testNewTransactionWithSuccess() throws Throwable {
  110. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  111. mockery.checking(new Expectations() {
  112. {
  113. Sequence seq = mockery.sequence("newTx");
  114. exactly(1).of(invocation).getMethod();
  115. will(returnValue(propagtedIsolatedTransaction));
  116. inSequence(seq);
  117. exactly(1).of(mockManager).getCurrentTransaction();
  118. will(returnValue(null));
  119. inSequence(seq);
  120. Transaction tx = mockery.mock(Transaction.class);
  121. exactly(1).of(mockManager).beginTransaction(true);
  122. will(returnValue(tx));
  123. inSequence(seq);
  124. exactly(1).of(invocation).proceed();
  125. will(returnValue(sampleService));
  126. inSequence(seq);
  127. exactly(1).of(tx).commit();
  128. inSequence(seq);
  129. }
  130. });
  131. Assert.assertSame(sampleService, interceptor.invoke(invocation));
  132. mockery.assertIsSatisfied();
  133. }
  134. @Test(expected = TransactionException.class)
  135. public void testNewTransactionWithError() throws Throwable {
  136. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  137. mockery.checking(new Expectations() {
  138. {
  139. Sequence seq = mockery.sequence("newTxWithError");
  140. exactly(1).of(invocation).getMethod();
  141. will(returnValue(propagtedIsolatedTransaction));
  142. inSequence(seq);
  143. exactly(1).of(mockManager).getCurrentTransaction();
  144. will(returnValue(null));
  145. inSequence(seq);
  146. Transaction tx = mockery.mock(Transaction.class);
  147. exactly(1).of(mockManager).beginTransaction(true);
  148. will(returnValue(tx));
  149. inSequence(seq);
  150. exactly(1).of(invocation).proceed();
  151. will(throwException(new TransactionException(new NullPointerException())));
  152. inSequence(seq);
  153. exactly(1).of(tx).rollback();
  154. inSequence(seq);
  155. }
  156. });
  157. try {
  158. interceptor.invoke(invocation);
  159. }
  160. finally {
  161. mockery.assertIsSatisfied();
  162. }
  163. }
  164. @Test
  165. public void testNewNonIsolatedTransactionWithSuccess() throws Throwable {
  166. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  167. mockery.checking(new Expectations() {
  168. {
  169. Sequence seq = mockery.sequence("newTx");
  170. exactly(1).of(invocation).getMethod();
  171. will(returnValue(propagtedNonIsolatedTransaction));
  172. inSequence(seq);
  173. exactly(1).of(mockManager).getCurrentTransaction();
  174. will(returnValue(null));
  175. inSequence(seq);
  176. Transaction tx = mockery.mock(Transaction.class);
  177. exactly(1).of(mockManager).beginTransaction(false);
  178. will(returnValue(tx));
  179. inSequence(seq);
  180. exactly(1).of(invocation).proceed();
  181. will(returnValue(sampleService));
  182. inSequence(seq);
  183. exactly(1).of(tx).commit();
  184. inSequence(seq);
  185. }
  186. });
  187. Assert.assertSame(sampleService, interceptor.invoke(invocation));
  188. mockery.assertIsSatisfied();
  189. }
  190. @Test
  191. public void testNestedTransactionWithoutPropagationWithSuccess() throws Throwable {
  192. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  193. mockery.checking(new Expectations() {
  194. {
  195. Sequence seq = mockery.sequence("newTx");
  196. Transaction tx = mockery.mock(Transaction.class, "tx0");
  197. exactly(1).of(mockManager).beginTransaction(true);
  198. will(returnValue(tx));
  199. inSequence(seq);
  200. exactly(1).of(invocation).getMethod();
  201. will(returnValue(nonPropagtedIsolatedNestedTransaction));
  202. inSequence(seq);
  203. exactly(1).of(mockManager).getCurrentTransaction();
  204. will(returnValue(tx));
  205. inSequence(seq);
  206. Transaction tx1 = mockery.mock(Transaction.class, "tx1");
  207. exactly(1).of(mockManager).beginTransaction(true);
  208. will(returnValue(tx1));
  209. inSequence(seq);
  210. exactly(1).of(invocation).proceed();
  211. will(returnValue(sampleService));
  212. inSequence(seq);
  213. exactly(1).of(tx1).commit();
  214. inSequence(seq);
  215. exactly(1).of(tx).commit();
  216. inSequence(seq);
  217. }
  218. });
  219. TransactionManager manager = injector.getInstance(TransactionManager.class);
  220. Transaction tx = manager.beginTransaction(true);
  221. try {
  222. Assert.assertSame(sampleService, interceptor.invoke(invocation));
  223. }
  224. finally {
  225. tx.commit();
  226. }
  227. mockery.assertIsSatisfied();
  228. }
  229. @Test
  230. public void testNestedTransactionWithPropagationWithSuccess() throws Throwable {
  231. final MethodInvocation invocation = mockery.mock(MethodInvocation.class);
  232. mockery.checking(new Expectations() {
  233. {
  234. Sequence seq = mockery.sequence("newTx");
  235. Transaction tx = mockery.mock(Transaction.class, "tx0");
  236. exactly(1).of(mockManager).beginTransaction(true);
  237. will(returnValue(tx));
  238. inSequence(seq);
  239. exactly(1).of(invocation).getMethod();
  240. will(returnValue(propagtedIsolatedNestedTransaction));
  241. inSequence(seq);
  242. exactly(1).of(mockManager).getCurrentTransaction();
  243. will(returnValue(tx));
  244. inSequence(seq);
  245. exactly(1).of(invocation).proceed();
  246. will(returnValue(sampleService));
  247. inSequence(seq);
  248. exactly(1).of(tx).commit();
  249. inSequence(seq);
  250. }
  251. });
  252. TransactionManager manager = injector.getInstance(TransactionManager.class);
  253. Transaction tx = manager.beginTransaction(true);
  254. try {
  255. Assert.assertSame(sampleService, interceptor.invoke(invocation));
  256. }
  257. finally {
  258. tx.commit();
  259. }
  260. mockery.assertIsSatisfied();
  261. }
  262. }