AbstractJDBCDriverTestpublic abstract class AbstractJDBCDriverTest extends TestCase Tests for the most commonly used methods of sql like creating a connection,
inserting, selecting, updating. |
Methods Summary |
---|
private void | assertAllFromTbl1(java.sql.Statement stmt, java.lang.String[] ones, short[] twos)Asserts that all values that where added to tbl1 are actually in tbl1.
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 void | autoCommitInsertSelectTest(java.sql.Statement stmt, java.lang.String[] ones, short[] twos)Inserts the values from 'ones' with the values from 'twos' into 'tbl1'
for (int i = 0; i < ones.length; i++) {
stmt.execute("insert into tbl1 values('" + ones[i] + "'," + twos[i]
+ ")");
}
assertAllFromTbl1(stmt, ones, twos);
| private void | closeConnections(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.String | getConnectionURL()
| protected abstract java.io.File | getDbFile()
| protected abstract java.lang.String | getJDBCDriverClassName()
| public void | testJDBCDriver()
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 void | updateOnes(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'
for (int i = 0; i < onesUpdated.length; i++) {
stmt.execute("UPDATE tbl1 SET one = '" + onesUpdated[i]
+ "' WHERE two = " + twos[i]);
}
| private void | updateSelectCommitSelectTest(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.
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 void | updateSelectRollbackSelectTest(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.
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);
}
|
|