FileDocCategorySizeDatePackage
AbstractSqlTest.javaAPI DocAndroid 1.5 API8158Wed May 06 22:41:06 BST 2009tests.SQLite

AbstractSqlTest

public abstract class AbstractSqlTest extends TestCase
This class provides SQL unit test, which can be used by subclasses eg. to test JDBC drivers.

Fields Summary
private Connection
firstConnection
The first connection.
private Connection
secondConnection
The second connection.
private Statement
firstStmt
The statement from the first connection.
private Statement
secondStmt
The statement from the second connection.
private final String[]
ones
The values of the first column "one".
private final short[]
twos
The values of the second column "two".
private final String[]
ones_updated
The updated values of the first column "one".
Constructors Summary
public AbstractSqlTest()
Creates a new instance of this class


            
      
        super();
        ones_updated = new String[ones.length];
        for (int i = 0; i < ones.length; i++) {
            ones_updated[i] = ones[i] + twos[i];
        }
    
Methods Summary
private voidassertAllFromTbl1(java.sql.Statement stmt, java.lang.String[] ones, short[] twos)
Asserts that the expected values can be selected from the test table.

param
stmt the statement to be used for the selection of the data
param
ones the expected values of the column 'one'
param
twos the expected values of the column 'two'
throws
SQLException if there is a problem accessing the database

        ResultSet rs = stmt.executeQuery("select * from tbl1");
        int i = 0;
        for (; rs.next(); i++) {
            assertTrue(i < ones.length);
            assertEquals(ones[i], rs.getString("one"));
            assertEquals(twos[i], rs.getShort("two"));
        }
        assertTrue(i == ones.length);
    
private voidautoCommitInsertSelect()
Adds some rows to the test table and asserts that the rows can be retrieved again.

throws
SQLException if there is a problem accessing the database

        firstStmt.getConnection().setAutoCommit(true);
        for (int i = 0; i < ones.length; i++) {
            firstStmt.execute("insert into tbl1 values('" + ones[i] + "',"
                    + twos[i] + ")");
        }
        assertAllFromTbl1(firstStmt, ones, twos);
    
protected abstract java.lang.StringgetConnectionURL()

protected abstract java.lang.StringgetDriverClassName()

protected abstract intgetTransactionIsolation()

protected voidsetUp()
Sets up a unit test, by creating two statements from two connections and creating a test table.

exception
SQLException if there is a problem accessing the database
throws
Exception
exception
Exception may be thrown by subclasses

        Class.forName(getDriverClassName()).newInstance();
        firstConnection = DriverManager.getConnection(getConnectionURL());
        firstConnection.setTransactionIsolation(getTransactionIsolation());
        secondConnection = DriverManager.getConnection(getConnectionURL());
        secondConnection.setTransactionIsolation(getTransactionIsolation());
        firstStmt = firstConnection.createStatement();
        firstStmt.execute("create table tbl1(one varchar(10), two smallint)");
        secondStmt = secondConnection.createStatement();
    
protected voidtearDown()
Tears down a unit test, by setting the auto commit property of the first connection back to true, dropping the test table and closing the two connections.

        firstStmt.close();
        secondStmt.close();
        firstConnection.setAutoCommit(true);
        firstStmt = firstConnection.createStatement();
        firstStmt.execute("drop table tbl1");
        firstStmt.close();
        firstConnection.close();
        secondConnection.close();
    
public voidtestAutoCommitInsertSelect()

        autoCommitInsertSelect();
    
public voidtestUpdateSelectCommitSelect()
Tests the following sequence after successful insertion of some test data: - update data from connection one - select data from connection two (-> should have the old values) - commit data from connection one - select data from connection two (-> should have the new values)

throws
SQLException if there is a problem accessing the database

        autoCommitInsertSelect();
        firstStmt.getConnection().setAutoCommit(false);
        updateOnes(firstStmt, ones_updated, twos);
        assertAllFromTbl1(secondStmt, ones, twos);
        firstStmt.getConnection().commit();
        assertAllFromTbl1(secondStmt, ones_updated, twos);
    
public voidtestUpdateSelectRollbackSelect()
Tests the following sequence after successful insertion of some test data: - update data from connection one - select data from connection two (-> should have the old values) - rollback data from connection one - select data from connection two (-> should still have the old values)

throws
SQLException if there is a problem accessing the database

        autoCommitInsertSelect();
        firstStmt.getConnection().setAutoCommit(false);
        updateOnes(firstStmt, ones_updated, twos);
        assertAllFromTbl1(secondStmt, ones, twos);
        firstStmt.getConnection().rollback();
        assertAllFromTbl1(secondStmt, ones, twos);
    
private voidupdateOnes(java.sql.Statement stmt, java.lang.String[] ones_updated, short[] twos)
Updates the values in column 'one'

param
stmt the statement to be used to update the data
param
ones_updated the updated valus of column 'one'
param
twos the reference values of column 'two'
throws
SQLException if there is a problem accessing the database

        for (int i = 0; i < ones_updated.length; i++) {
            stmt.execute("UPDATE tbl1 SET one = '" + ones_updated[i]
                    + "' WHERE two = " + twos[i]);
        }