001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.database;
017:
018: import static org.easymock.EasyMock.*;
019: import org.junit.After;
020: import static org.junit.Assert.assertNotSame;
021: import static org.junit.Assert.assertSame;
022: import org.junit.Before;
023: import org.junit.Test;
024: import org.unitils.core.Unitils;
025: import org.unitils.database.annotations.Transactional;
026: import static org.unitils.database.util.TransactionMode.*;
027:
028: import javax.sql.DataSource;
029: import java.sql.Connection;
030:
031: /**
032: * Tests verifying whether the SimpleTransactionManager functions correctly.
033: *
034: * @author Filip Neven
035: * @author Tim Ducheyne
036: */
037: public class DatabaseModuleSimpleTransactionManagerTest extends
038: DatabaseModuleTransactionalTest {
039:
040: private DatabaseModule databaseModule;
041:
042: private TransactionsDisabledTest transactionsDisabledTest;
043:
044: private RollbackTest rollbackTest;
045:
046: private CommitTest commitTest;
047:
048: /**
049: * Initializes the test fixture.
050: */
051: @Before
052: public void setUp() throws Exception {
053: configuration.setProperty("unitils.module.spring.enabled",
054: "false");
055: Unitils.getInstance().init(configuration);
056: databaseModule = getDatabaseModule();
057:
058: transactionsDisabledTest = new TransactionsDisabledTest();
059: rollbackTest = new RollbackTest();
060: commitTest = new CommitTest();
061: }
062:
063: /**
064: * Cleans up test by resetting the unitils instance.
065: */
066: @After
067: public void tearDown() throws Exception {
068: Unitils.getInstance().init();
069: }
070:
071: /**
072: * Tests for a test with transactions disabled
073: */
074: @Test
075: public void testWithTransactionsDisabled() throws Exception {
076: expect(mockConnection1.getAutoCommit()).andReturn(true);
077: expect(mockConnection2.getAutoCommit()).andReturn(true);
078: mockConnection1.close();
079: mockConnection2.close();
080: replay(mockConnection1, mockConnection2);
081:
082: databaseModule.startTransaction(transactionsDisabledTest);
083: Connection conn1 = databaseModule.getDataSource()
084: .getConnection();
085: conn1.close();
086: Connection conn2 = databaseModule.getDataSource()
087: .getConnection();
088: conn2.close();
089: assertNotSame(conn1, conn2);
090: databaseModule.endTransaction(transactionsDisabledTest);
091:
092: verify(mockConnection1, mockConnection2);
093: }
094:
095: /**
096: * Tests with a test with transaction rollback configured
097: */
098: @Test
099: public void testRollback() throws Exception {
100: expect(mockConnection1.getAutoCommit()).andReturn(true)
101: .andReturn(false).anyTimes();
102: mockConnection1.setAutoCommit(false);
103: mockConnection1.rollback();
104: mockConnection1.close();
105: replay(mockConnection1, mockConnection2);
106:
107: DataSource dataSource = databaseModule.getDataSource();
108: databaseModule.startTransaction(rollbackTest);
109: Connection connection1 = dataSource.getConnection();
110: connection1.close();
111: Connection connection2 = dataSource.getConnection();
112: connection2.close();
113: assertSame(connection1, connection2);
114: databaseModule.endTransaction(rollbackTest);
115:
116: verify(mockConnection1, mockConnection2);
117: }
118:
119: /**
120: * Tests with a test with transaction commit configured
121: */
122: @Test
123: public void testCommit() throws Exception {
124: expect(mockConnection1.getAutoCommit()).andReturn(true)
125: .andReturn(false).anyTimes();
126: mockConnection1.setAutoCommit(false);
127: mockConnection1.commit();
128: mockConnection1.close();
129: replay(mockConnection1, mockConnection2);
130:
131: DataSource dataSource = databaseModule.getDataSource();
132: databaseModule.startTransaction(commitTest);
133: Connection connection1 = dataSource.getConnection();
134: connection1.close();
135: Connection connection2 = dataSource.getConnection();
136: connection2.close();
137: assertSame(connection1, connection2);
138: databaseModule.endTransaction(commitTest);
139:
140: verify(mockConnection1, mockConnection2);
141: }
142:
143: /**
144: * Class that plays the role of a unit test, with transactions disabled
145: */
146: @Transactional(DISABLED)
147: public static class TransactionsDisabledTest {
148:
149: public void test() {
150: }
151: }
152:
153: /**
154: * Class that plays the role of a unit test, with transaction rollback enabled (=default, so no
155: *
156: * @Transactional annotation required
157: */
158: @Transactional(ROLLBACK)
159: public static class RollbackTest {
160:
161: public void test() {
162: }
163: }
164:
165: /**
166: * Class that plays the role of a unit test, with transaction commit enabled
167: */
168: @Transactional(COMMIT)
169: public static class CommitTest {
170:
171: public void test() {
172: }
173: }
174:
175: }
|