KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > testsuite > simple > TransactionTest


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package testsuite.simple;
26
27 import testsuite.BaseTestCase;
28
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  *
33  * @author Mark Matthews
34  * @version $Id: TransactionTest.java,v 1.1.2.1 2005/05/13 18:58:37 mmatthews
35  * Exp $
36  */

37 public class TransactionTest extends BaseTestCase {
38     // ~ Static fields/initializers
39
// ---------------------------------------------
40

41     private static final double DOUBLE_CONST = 25.4312;
42
43     private static final double EPSILON = .0000001;
44
45     // ~ Constructors
46
// -----------------------------------------------------------
47

48     /**
49      * Creates a new TransactionTest object.
50      *
51      * @param name
52      * DOCUMENT ME!
53      */

54     public TransactionTest(String JavaDoc name) {
55         super(name);
56     }
57
58     // ~ Methods
59
// ----------------------------------------------------------------
60

61     /**
62      * Runs all test cases in this test suite
63      *
64      * @param args
65      */

66     public static void main(String JavaDoc[] args) {
67         junit.textui.TestRunner.run(TransactionTest.class);
68     }
69
70     /**
71      * DOCUMENT ME!
72      *
73      * @throws Exception
74      * DOCUMENT ME!
75      */

76     public void setUp() throws Exception JavaDoc {
77         super.setUp();
78         createTestTable();
79     }
80
81     /**
82      * DOCUMENT ME!
83      *
84      * @throws SQLException
85      * DOCUMENT ME!
86      */

87     public void testTransaction() throws SQLException JavaDoc {
88         try {
89             this.conn.setAutoCommit(false);
90             this.stmt
91                     .executeUpdate("INSERT INTO trans_test (id, decdata) VALUES (1, 1.0)");
92             this.conn.rollback();
93             this.rs = this.stmt.executeQuery("SELECT * from trans_test");
94
95             boolean hasResults = this.rs.next();
96             assertTrue("Results returned, rollback to empty table failed",
97                     (hasResults != true));
98             this.stmt
99                     .executeUpdate("INSERT INTO trans_test (id, decdata) VALUES (2, "
100                             + DOUBLE_CONST + ")");
101             this.conn.commit();
102             this.rs = this.stmt
103                     .executeQuery("SELECT * from trans_test where id=2");
104             hasResults = this.rs.next();
105             assertTrue("No rows in table after INSERT", hasResults);
106
107             double doubleVal = this.rs.getDouble(2);
108             double delta = Math.abs(DOUBLE_CONST - doubleVal);
109             assertTrue("Double value returned != " + DOUBLE_CONST,
110                     (delta < EPSILON));
111         } finally {
112             this.conn.setAutoCommit(true);
113         }
114     }
115
116     private void createTestTable() throws SQLException JavaDoc {
117         //
118
// Catch the error, the table might exist
119
//
120
try {
121             this.stmt.executeUpdate("DROP TABLE trans_test");
122         } catch (SQLException JavaDoc sqlEx) {
123             ;
124         }
125
126         this.stmt
127                 .executeUpdate("CREATE TABLE trans_test (id INT NOT NULL PRIMARY KEY, decdata DOUBLE) TYPE=InnoDB");
128     }
129 }
130
Popular Tags