PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  6package org.sigmah.server.dao.hibernate;
  7
  8import static org.easymock.EasyMock.createMock;
  9import static org.easymock.EasyMock.createStrictMock;
 10import static org.easymock.EasyMock.expect;
 11import static org.easymock.EasyMock.replay;
 12import static org.easymock.EasyMock.verify;
 13import static org.junit.Assert.assertNotNull;
 14
 15import javax.persistence.EntityManager;
 16import javax.persistence.EntityTransaction;
 17
 18import org.junit.Test;
 19import org.junit.runner.RunWith;
 20import org.sigmah.server.dao.Transactional;
 21import org.sigmah.server.util.BeanMappingModule;
 22import org.sigmah.test.InjectionSupport;
 23import org.sigmah.test.Modules;
 24
 25import com.google.inject.Guice;
 26import com.google.inject.Injector;
 27
 28@RunWith(InjectionSupport.class)
 29@Modules({
 30        BeanMappingModule.class
 31})
 32public class TransactionalInterceptorTest {
 33
 34
 35    @Test
 36    public void testSuccessfulCase() {
 37        EntityTransaction tx = createMock(EntityTransaction.class);
 38        expect(tx.isActive()).andReturn(false);
 39        tx.begin();
 40        tx.commit();
 41        replay(tx);
 42
 43        MockClass mock = getMockInstance(tx);
 44
 45        mock.succeedsWithoutException();
 46
 47        verify(tx);
 48    }
 49
 50
 51    @Test
 52    public void testFailedCase() {
 53        EntityTransaction tx = createMock(EntityTransaction.class);
 54        expect(tx.isActive()).andReturn(false);
 55        tx.begin();
 56        tx.rollback();
 57        replay(tx);
 58
 59        MockClass mock = getMockInstance(tx);
 60
 61        RuntimeException rte = null;
 62        try {
 63            mock.throwsRuntimeException();
 64        } catch (RuntimeException e) {
 65            rte = e;
 66        }
 67
 68        assertNotNull("exception is propagated", rte);
 69
 70        verify(tx);
 71    }
 72
 73
 74    @Test
 75    public void testNestedSuccessful() {
 76        EntityTransaction tx = createStrictMock(EntityTransaction.class);
 77        expect(tx.isActive()).andReturn(false);
 78        tx.begin();
 79        expect(tx.isActive()).andReturn(true);
 80        tx.commit();
 81        replay(tx);
 82
 83        MockClass mock = getMockInstance(tx);
 84
 85        mock.successfulNestedTransactions();
 86
 87        verify(tx);
 88    }
 89
 90    @Test
 91    public void testNestedUnsuccessful() {
 92        EntityTransaction tx = createStrictMock(EntityTransaction.class);
 93        expect(tx.isActive()).andReturn(false);
 94        tx.begin();
 95        expect(tx.isActive()).andReturn(true);
 96        tx.rollback();
 97        replay(tx);
 98
 99        MockClass mock = getMockInstance(tx);
100
101        RuntimeException rte = null;
102        try {
103            mock.nestedTransactionWithFailureOnSecondLevel();
104        } catch (RuntimeException e) {
105            rte = e;
106        }
107        assertNotNull("exception is propagated", rte);
108
109
110        verify(tx);
111    }
112
113    private MockClass getMockInstance(EntityTransaction tx) {
114        Injector injector = Guice.createInjector(new MockEntityManagerModule(tx));
115        MockClass mock = injector.getInstance(MockClass.class);
116        return mock;
117    }
118
119
120    public static class MockClass {
121
122        @Transactional
123        public void succeedsWithoutException() {
124        }
125
126        @Transactional
127        public void throwsRuntimeException() {
128            throw new RuntimeException();
129        }
130
131        @Transactional
132        public void successfulNestedTransactions() {
133            succeedsWithoutException();
134        }
135
136        @Transactional
137        public void nestedTransactionWithFailureOnSecondLevel() {
138            throwsRuntimeException();
139        }
140    }
141
142    private static class MockEntityManagerModule extends HibernateModule {
143        private EntityManager em;
144
145        public MockEntityManagerModule(EntityTransaction tx) {
146            em = createMock(EntityManager.class);
147            expect(em.getTransaction()).andReturn(tx).anyTimes();
148            replay(em);
149        }
150
151        @Override
152        protected void configureEm() {
153            bind(EntityManager.class).toInstance(em);
154        }
155    }
156
157}