001: /*
002: Copyright (C) 2002-2004 MySQL AB
003:
004: This program is free software; you can redistribute it and/or modify
005: it under the terms of version 2 of the GNU General Public License as
006: published by the Free Software Foundation.
007:
008: There are special exceptions to the terms and conditions of the GPL
009: as it is applied to this software. View the full text of the
010: exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
011: software distribution.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU General Public License for more details.
017:
018: You should have received a copy of the GNU General Public License
019: along with this program; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022:
023:
024: */
025: package testsuite.simple;
026:
027: import testsuite.BaseTestCase;
028:
029: import java.sql.SQLException;
030:
031: /**
032: *
033: * @author Mark Matthews
034: * @version $Id: TransactionTest.java,v 1.1.2.1 2005/05/13 18:58:37 mmatthews
035: * Exp $
036: */
037: public class TransactionTest extends BaseTestCase {
038: // ~ Static fields/initializers
039: // ---------------------------------------------
040:
041: private static final double DOUBLE_CONST = 25.4312;
042:
043: private static final double EPSILON = .0000001;
044:
045: // ~ Constructors
046: // -----------------------------------------------------------
047:
048: /**
049: * Creates a new TransactionTest object.
050: *
051: * @param name
052: * DOCUMENT ME!
053: */
054: public TransactionTest(String name) {
055: super (name);
056: }
057:
058: // ~ Methods
059: // ----------------------------------------------------------------
060:
061: /**
062: * Runs all test cases in this test suite
063: *
064: * @param args
065: */
066: public static void main(String[] args) {
067: junit.textui.TestRunner.run(TransactionTest.class);
068: }
069:
070: /**
071: * DOCUMENT ME!
072: *
073: * @throws Exception
074: * DOCUMENT ME!
075: */
076: public void setUp() throws Exception {
077: super .setUp();
078: createTestTable();
079: }
080:
081: /**
082: * DOCUMENT ME!
083: *
084: * @throws SQLException
085: * DOCUMENT ME!
086: */
087: public void testTransaction() throws SQLException {
088: try {
089: this .conn.setAutoCommit(false);
090: this .stmt
091: .executeUpdate("INSERT INTO trans_test (id, decdata) VALUES (1, 1.0)");
092: this .conn.rollback();
093: this .rs = this .stmt
094: .executeQuery("SELECT * from trans_test");
095:
096: boolean hasResults = this .rs.next();
097: assertTrue(
098: "Results returned, rollback to empty table failed",
099: (hasResults != true));
100: this .stmt
101: .executeUpdate("INSERT INTO trans_test (id, decdata) VALUES (2, "
102: + DOUBLE_CONST + ")");
103: this .conn.commit();
104: this .rs = this .stmt
105: .executeQuery("SELECT * from trans_test where id=2");
106: hasResults = this .rs.next();
107: assertTrue("No rows in table after INSERT", hasResults);
108:
109: double doubleVal = this .rs.getDouble(2);
110: double delta = Math.abs(DOUBLE_CONST - doubleVal);
111: assertTrue("Double value returned != " + DOUBLE_CONST,
112: (delta < EPSILON));
113: } finally {
114: this .conn.setAutoCommit(true);
115: }
116: }
117:
118: private void createTestTable() throws SQLException {
119: //
120: // Catch the error, the table might exist
121: //
122: try {
123: this .stmt.executeUpdate("DROP TABLE trans_test");
124: } catch (SQLException sqlEx) {
125: ;
126: }
127:
128: this .stmt
129: .executeUpdate("CREATE TABLE trans_test (id INT NOT NULL PRIMARY KEY, decdata DOUBLE) TYPE=InnoDB");
130: }
131: }
|