001: /*
002: * $Id: TestTransactionManager.java,v 1.2 2005/12/20 18:32:47 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.jdbc;
042:
043: import java.sql.Connection;
044: import java.sql.DriverManager;
045: import java.sql.PreparedStatement;
046: import java.sql.Statement;
047:
048: import junit.framework.Test;
049: import junit.framework.TestCase;
050: import junit.framework.TestSuite;
051:
052: import org.axiondb.Database;
053: import org.axiondb.Table;
054: import org.axiondb.Transaction;
055: import org.axiondb.engine.rows.SimpleRow;
056: import org.axiondb.jdbc.AxionConnection;
057:
058: /**
059: * @version $Revision: 1.2 $ $Date: 2005/12/20 18:32:47 $
060: * @author Rodney Waldhoff
061: */
062: public class TestTransactionManager extends TestCase {
063:
064: //------------------------------------------------------------ Conventional
065:
066: public TestTransactionManager(String testName) {
067: super (testName);
068: try {
069: Class.forName("org.axiondb.jdbc.AxionDriver");
070: } catch (Exception e) {
071: throw new RuntimeException(e.toString());
072: }
073: }
074:
075: public static Test suite() {
076: return new TestSuite(TestTransactionManager.class);
077: }
078:
079: //--------------------------------------------------------------- Lifecycle
080:
081: private Database _database = null;
082: private String _connectString = "jdbc:axiondb:memdb";
083:
084: public void setUp() throws Exception {
085: super .setUp();
086: Connection conn = DriverManager.getConnection(_connectString);
087: _database = ((AxionConnection) (conn)).getDatabase();
088: Statement stmt = conn.createStatement();
089: stmt.execute("create table FOO ( NUM integer, STR varchar2 )");
090: stmt.close();
091: PreparedStatement pstmt = conn
092: .prepareStatement("insert into FOO values ( ?, ? )");
093: for (int i = 0; i < 10; i++) {
094: pstmt.setInt(1, i);
095: pstmt.setString(2, String.valueOf(i));
096: pstmt.executeUpdate();
097: }
098: pstmt.close();
099: }
100:
101: public void tearDown() throws Exception {
102: super .tearDown();
103: _database.shutdown();
104: _database = null;
105: }
106:
107: //------------------------------------------------------------------- Tests
108:
109: public void testNoOpCommit() throws Exception {
110: Transaction t = _database.getTransactionManager()
111: .createTransaction();
112: assertEquals(Transaction.STATE_OPEN, t.getState());
113: _database.getTransactionManager().commitTransaction(t);
114: assertEquals(Transaction.STATE_APPLIED, t.getState());
115: }
116:
117: public void testNoOpRollback() throws Exception {
118: Transaction t = _database.getTransactionManager()
119: .createTransaction();
120: assertEquals(Transaction.STATE_OPEN, t.getState());
121: _database.getTransactionManager().abortTransaction(t);
122: assertEquals(Transaction.STATE_ABORTED, t.getState());
123: }
124:
125: public void testDontApplyWhenThereExistsOpenTransaction()
126: throws Exception {
127: Transaction t = _database.getTransactionManager()
128: .createTransaction();
129: assertEquals(Transaction.STATE_OPEN, t.getState());
130:
131: Transaction u = _database.getTransactionManager()
132: .createTransaction();
133: assertEquals(Transaction.STATE_OPEN, u.getState());
134:
135: Table foo = t.getTable("FOO");
136: SimpleRow row = new SimpleRow(2);
137: row.set(0, new Integer(17));
138: row.set(1, "seventeen");
139: foo.addRow(row);
140:
141: _database.getTransactionManager().commitTransaction(t);
142: assertEquals(Transaction.STATE_COMMITTED, t.getState());
143:
144: _database.getTransactionManager().commitTransaction(u);
145: assertEquals(Transaction.STATE_APPLIED, t.getState());
146: assertEquals(Transaction.STATE_APPLIED, u.getState());
147: }
148:
149: public void testOpenOnTransaction() throws Exception {
150: Transaction t = _database.getTransactionManager()
151: .createTransaction();
152: assertEquals(Transaction.STATE_OPEN, t.getState());
153:
154: Transaction u = _database.getTransactionManager()
155: .createTransaction();
156: assertEquals(Transaction.STATE_OPEN, u.getState());
157:
158: Table foo = t.getTable("FOO");
159: SimpleRow row = new SimpleRow(2);
160: row.set(0, new Integer(17));
161: row.set(1, "seventeen");
162: foo.addRow(row);
163:
164: _database.getTransactionManager().commitTransaction(t);
165: assertEquals(Transaction.STATE_COMMITTED, t.getState());
166:
167: Transaction v = _database.getTransactionManager()
168: .createTransaction();
169: assertEquals(Transaction.STATE_OPEN, v.getState());
170: assertEquals(t, v.getOpenOnTransaction());
171:
172: _database.getTransactionManager().commitTransaction(u);
173: _database.getTransactionManager().commitTransaction(v);
174: assertEquals(Transaction.STATE_APPLIED, t.getState());
175: assertEquals(Transaction.STATE_APPLIED, u.getState());
176: assertEquals(Transaction.STATE_APPLIED, v.getState());
177: }
178: }
|