FileDocCategorySizeDatePackage
AbstractJDBCDriverTest.javaAPI DocAndroid 1.5 API8330Wed May 06 22:42:02 BST 2009android.core

AbstractJDBCDriverTest

public abstract class AbstractJDBCDriverTest extends TestCase
Tests for the most commonly used methods of sql like creating a connection, inserting, selecting, updating.

Fields Summary
Constructors Summary
Methods Summary
private voidassertAllFromTbl1(java.sql.Statement stmt, java.lang.String[] ones, short[] twos)
Asserts that all values that where added to tbl1 are actually in tbl1.

param
stmt the statement to use for the select.
param
ones the string values that where added.
param
twos the numerical values that where added.
throws
SQLException in case of a problem during select.

        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"));
        }
        assertEquals(i, ones.length);
    
private voidautoCommitInsertSelectTest(java.sql.Statement stmt, java.lang.String[] ones, short[] twos)
Inserts the values from 'ones' with the values from 'twos' into 'tbl1'

param
stmt the statement to use for the inserts.
param
ones the string values to insert into tbl1.
param
twos the corresponding numerical values to insert into tbl1.
throws
SQLException in case of a problem during insert.

        for (int i = 0; i < ones.length; i++) {
            stmt.execute("insert into tbl1 values('" + ones[i] + "'," + twos[i]
                    + ")");
        }
        assertAllFromTbl1(stmt, ones, twos);
    
private voidcloseConnections(java.sql.Connection firstConnection, java.sql.Connection secondConnection, java.io.File dbFile, java.sql.Statement firstStmt, java.sql.Statement secondStmt)

        String failText = null;
        try {
            if (firstStmt != null) {
                firstStmt.execute("drop table tbl1");
            }
        } catch (SQLException e) {
            failText = e.getLocalizedMessage();
        }
        try {
            if (firstStmt != null) {
                firstStmt.close();
            }
        } catch (SQLException e) {
            failText = e.getLocalizedMessage();
        }
        try {
            if (firstConnection != null) {
                firstConnection.close();
            }
        } catch (SQLException e) {
            failText = e.getLocalizedMessage();
        }
        try {
            if (secondStmt != null) {
                secondStmt.close();
            }
        } catch (SQLException e) {
            failText = e.getLocalizedMessage();
        }
        try {
            if (secondConnection != null) {
                secondConnection.close();
            }
        } catch (SQLException e) {
            failText = e.getLocalizedMessage();
        }
        dbFile.delete();
        assertNull(failText, failText);
    
protected abstract java.lang.StringgetConnectionURL()

protected abstract java.io.FilegetDbFile()

protected abstract java.lang.StringgetJDBCDriverClassName()

public voidtestJDBCDriver()

        Connection firstConnection = null;
        Connection secondConnection = null;
        File dbFile = getDbFile();
        String connectionURL = getConnectionURL();
        Statement firstStmt = null;
        Statement secondStmt = null;
        try {
            Class.forName(getJDBCDriverClassName());
            firstConnection = DriverManager.getConnection(connectionURL);
            secondConnection = DriverManager.getConnection(connectionURL);

            String[] ones = {"hello!", "goodbye"};
            short[] twos = {10, 20};
            String[] onesUpdated = new String[ones.length];
            for (int i = 0; i < ones.length; i++) {
                onesUpdated[i] = ones[i] + twos[i];
            }
            firstStmt = firstConnection.createStatement();
            firstStmt.execute("create table tbl1(one varchar(10), two smallint)");
            secondStmt = secondConnection.createStatement();

            autoCommitInsertSelectTest(firstStmt, ones, twos);
            updateSelectCommitSelectTest(firstStmt, secondStmt, ones, onesUpdated, twos);
            updateSelectRollbackSelectTest(firstStmt, secondStmt, onesUpdated, ones, twos);
        } finally {
            closeConnections(firstConnection, secondConnection, dbFile, firstStmt, secondStmt);
        }
    
private voidupdateOnes(java.sql.Statement stmt, java.lang.String[] onesUpdated, short[] twos)
updates the sring values. the original values are stored in 'ones' and the updated values in 'ones_updated'

param
stmt the statement to use for the update.
param
onesUpdated the new string values.
param
twos the numerical values.
throws
SQLException in case of a problem during update.

        for (int i = 0; i < onesUpdated.length; i++) {
            stmt.execute("UPDATE tbl1 SET one = '" + onesUpdated[i]
                    + "' WHERE two = " + twos[i]);
        }
    
private voidupdateSelectCommitSelectTest(java.sql.Statement firstStmt, java.sql.Statement secondStmt, java.lang.String[] ones, java.lang.String[] onesUpdated, short[] twos)
Tests the results of an update followed bz a select on a diffrent statement. After that the first statement commits its update. and now the second statement should also be able to see the changed values in a select.

param
firstStmt the statement to use for the update and commit.
param
secondStmt the statement that should be used to check if the commit works
param
ones the original string values.
param
onesUpdated the updated string values.
param
twos the numerical values.
throws
SQLException in case of a problem during any of the executed commands.

        firstStmt.getConnection().setAutoCommit(false);
        try {
            updateOnes(firstStmt, onesUpdated, twos);
            assertAllFromTbl1(secondStmt, ones, twos);
            firstStmt.getConnection().commit();
            assertAllFromTbl1(secondStmt, onesUpdated, twos);
        } finally {
            firstStmt.getConnection().setAutoCommit(true);
        }
    
private voidupdateSelectRollbackSelectTest(java.sql.Statement firstStmt, java.sql.Statement secondStmt, java.lang.String[] ones, java.lang.String[] onesUpdated, short[] twos)
Tests if an update followed by a select works. After that a rollback will be made and again a select should show that the rollback worked.

param
firstStmt the statement to use for the update and the rollback
param
secondStmt the statement to use for checking if the rollback worked as intended.
param
ones the original string values.
param
onesUpdated the updated string values.
param
twos the nomerical values.
throws
SQLException in case of a problem during any command.

        firstStmt.getConnection().setAutoCommit(false);
        try {
            updateOnes(firstStmt, onesUpdated, twos);
            assertAllFromTbl1(secondStmt, ones, twos);
            firstStmt.getConnection().rollback();
            assertAllFromTbl1(secondStmt, ones, twos);
        } finally {
            firstStmt.getConnection().setAutoCommit(true);
        }