1 25 package testsuite.simple; 26 27 import testsuite.BaseTestCase; 28 29 import java.sql.SQLException ; 30 31 37 public class TransactionTest extends BaseTestCase { 38 41 private static final double DOUBLE_CONST = 25.4312; 42 43 private static final double EPSILON = .0000001; 44 45 48 54 public TransactionTest(String name) { 55 super(name); 56 } 57 58 61 66 public static void main(String [] args) { 67 junit.textui.TestRunner.run(TransactionTest.class); 68 } 69 70 76 public void setUp() throws Exception { 77 super.setUp(); 78 createTestTable(); 79 } 80 81 87 public void testTransaction() throws SQLException { 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 { 117 try { 121 this.stmt.executeUpdate("DROP TABLE trans_test"); 122 } catch (SQLException 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 |