PageRenderTime 6659ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/SmartITEngineering/smart-cms
Java | 100 lines | 86 code | 10 blank | 4 comment | 0 complexity | 25870b7efa77967c275773088b311139 MD5 | raw file
Possible License(s): GPL-3.0
  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.Inject;
  5. import com.google.inject.Injector;
  6. import com.google.inject.TypeLiteral;
  7. import com.smartitengineering.cms.repo.dao.tx.Transaction;
  8. import com.smartitengineering.cms.repo.dao.tx.TransactionManager;
  9. import com.smartitengineering.dao.common.CommonDao;
  10. import com.smartitengineering.dao.common.CommonReadDao;
  11. import com.smartitengineering.dao.common.CommonWriteDao;
  12. import org.jmock.Expectations;
  13. import org.jmock.Mockery;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16. /**
  17. *
  18. * @author imyousuf
  19. */
  20. public class DeclarativeTransactionTest {
  21. private static class Services {
  22. private final CommonDao<DemoDomain, String> cmnDao;
  23. private final CommonReadDao<DemoDomain, String> readDao;
  24. private final CommonWriteDao<DemoDomain> writeDao;
  25. @Inject
  26. public Services(CommonDao<DemoDomain, String> cmnDao,
  27. CommonReadDao<DemoDomain, String> readDao,
  28. CommonWriteDao<DemoDomain> writeDao) {
  29. this.cmnDao = cmnDao;
  30. this.readDao = readDao;
  31. this.writeDao = writeDao;
  32. }
  33. }
  34. private Mockery mockery;
  35. private CommonReadDao<DemoDomain, String> readDao;
  36. private CommonWriteDao<DemoDomain> writeDao;
  37. private Injector injector;
  38. private Services services;
  39. private TransactionManager manager;
  40. @Before
  41. public void setup() {
  42. mockery = new Mockery();
  43. readDao = mockery.mock(CommonReadDao.class);
  44. writeDao = mockery.mock(CommonWriteDao.class);
  45. injector = Guice.createInjector(new DemoDomainMasterModule(new AbstractModule() {
  46. @Override
  47. protected void configure() {
  48. bind(new TypeLiteral<Class<DemoDomain>>() {
  49. }).toInstance(DemoDomain.class);
  50. bind(new TypeLiteral<CommonWriteDao<DemoDomain>>() {
  51. }).annotatedWith(Transactionable.class).toInstance(writeDao);
  52. bind(new TypeLiteral<CommonReadDao<DemoDomain, String>>() {
  53. }).annotatedWith(Transactionable.class).toInstance(readDao);
  54. bind(Services.class);
  55. }
  56. }));
  57. services = injector.getInstance(Services.class);
  58. manager = injector.getInstance(TransactionManager.class);
  59. }
  60. @Test
  61. public void testDecalarativeCommit() {
  62. Transaction tx = manager.beginTransaction();
  63. try {
  64. final DemoDomain d1 = new DemoDomain();
  65. services.writeDao.save(d1);
  66. mockery.checking(new Expectations() {
  67. {
  68. exactly(1).of(writeDao).save(d1);
  69. }
  70. });
  71. tx.commit();
  72. mockery.assertIsSatisfied();
  73. }
  74. catch (Exception ex) {
  75. tx.rollback();
  76. }
  77. }
  78. @Test
  79. public void testDecalarativeRollback() {
  80. Transaction tx = manager.beginTransaction();
  81. try {
  82. final DemoDomain d1 = new DemoDomain();
  83. services.writeDao.save(d1);
  84. throw new NullPointerException();
  85. }
  86. catch (Exception ex) {
  87. tx.rollback();
  88. }
  89. }
  90. }