The attached patch is my first run-through of the JDBC test suite. A

summary of changes:

 . removal of the tablename property from build.xml

 . addition of a dropTable method in JDBC2Tests and cleanups of many
methods in the same

 . all tests now use non-deprecated assertXYZ methods instead of the
deprecated assert method

 . failure in TimestampTest (testSetTimestamp) fixed. The failure is
because testSetTimestamp was inserting a timestamp with hour 7 but
checkTimeTest was expecting a timestamp with hour 8. AFAICS, there are
no issues wrt daylight savings time and timestamps being pushed in and
pulled out (but more explicit tests should be added in the future)

 . failure in TimeTest (testGetTime) fixed. Times to be inserted were
interpreted in the localtime zone but checking was done with the
assumption that the insertion was done in GMT.

 . formatting changes in a few of the source files (because I found
it convenient to have consistent formatting while working on them). The
formatting is consistent with the new format for java source files in
PostgreSQL.

Liam Stewart
This commit is contained in:
Bruce Momjian 2001-09-23 04:11:14 +00:00
parent c7bc0ddf76
commit b75814aee3
13 changed files with 669 additions and 717 deletions

View File

@ -4,7 +4,7 @@
build file to allow ant (http://jakarta.apache.org/ant/) to be used build file to allow ant (http://jakarta.apache.org/ant/) to be used
to build the PostgreSQL JDBC Driver to build the PostgreSQL JDBC Driver
$Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/build.xml,v 1.17 2001/07/06 23:07:20 petere Exp $ $Header: /cvsroot/pgsql/src/interfaces/jdbc/Attic/build.xml,v 1.18 2001/09/23 04:11:14 momjian Exp $
--> -->
@ -182,7 +182,6 @@
<property name="username" value="test" /> <property name="username" value="test" />
<!-- Password must be something. Doesn't matter if trust is used! --> <!-- Password must be something. Doesn't matter if trust is used! -->
<property name="password" value="password" /> <property name="password" value="password" />
<property name="tablename" value="jdbctest" />
<!-- junit.ui is one of textui, awtui, or swingui --> <!-- junit.ui is one of textui, awtui, or swingui -->
<property name="junit.ui" value="textui" /> <property name="junit.ui" value="textui" />

View File

@ -32,16 +32,16 @@ public class JDBC2Tests extends TestSuite {
} }
/** /**
* helper - opens a connection. Static so other classes can call it. * Helper - opens a connection.
*/ */
public static java.sql.Connection openDB() { public static java.sql.Connection openDB() {
try { try {
Class.forName("org.postgresql.Driver"); Class.forName("org.postgresql.Driver");
return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword());
} catch(ClassNotFoundException ex) { } catch(ClassNotFoundException ex) {
TestCase.assert(ex.getMessage(),false); TestCase.fail(ex.getMessage());
} catch(SQLException ex) { } catch(SQLException ex) {
TestCase.assert(ex.getMessage(),false); TestCase.fail(ex.getMessage());
} }
return null; return null;
} }
@ -50,120 +50,101 @@ public class JDBC2Tests extends TestSuite {
* Helper - closes an open connection. This rewrites SQLException to a failed * Helper - closes an open connection. This rewrites SQLException to a failed
* assertion. It's static so other classes can use it. * assertion. It's static so other classes can use it.
*/ */
public static void closeDB(Connection conn) { public static void closeDB(Connection con) {
try { try {
if(conn!=null) if (con != null)
conn.close(); con.close();
} catch(SQLException ex) { } catch (SQLException ex) {
TestCase.assert(ex.getMessage(),false); TestCase.fail(ex.getMessage());
} }
} }
/** /**
* Helper - creates a test table for use by a test * Helper - creates a test table for use by a test
*/ */
public static void createTable( public static void createTable(Connection con,
Connection conn, String table, String columns) { String table,
String columns) {
try { try {
Statement st = conn.createStatement(); Statement st = con.createStatement();
try { try {
try { // Drop the table
st.executeUpdate("drop table " + table); dropTable(con, table);
} catch(SQLException se) {
// Intentionally ignore exception
}
// Now create the table // Now create the table
st.executeUpdate( "create table " + table + " (" + columns + st.executeUpdate("create table " + table + " (" + columns + ")");
")" );
} finally { } finally {
st.close(); st.close();
} }
} catch(SQLException ex) { } catch(SQLException ex) {
TestCase.assert(ex.getMessage(),false); TestCase.fail(ex.getMessage());
} }
} }
// Create the test table whose name is passed via the properties /**
// (see ../../../build.xml). It appears that the original author of * Helper - drops a table
// this test suite intended to specify all test table names via the */
// properties, but this was never fully implemented. public static void dropTable(Connection con, String table) {
public static void createTable(Connection conn, String columns) { try {
createTable(conn, getTableName(), columns); Statement stmt = con.createStatement();
try {
stmt.executeUpdate("DROP TABLE " + table);
} catch (SQLException ex) {
// ignore
}
} catch (SQLException ex) {
TestCase.fail(ex.getMessage());
}
} }
/** /**
* Helper - generates INSERT SQL - very simple * Helper - generates INSERT SQL - very simple
*/ */
public static String insert(String values) { public static String insertSQL(String table, String values) {
return insert(null,values); return insertSQL(table, null, values);
} }
public static String insert(String columns,String values) {
String s = "INSERT INTO "+getTableName(); public static String insertSQL(String table, String columns, String values) {
if(columns!=null) String s = "INSERT INTO " + table;
s=s+" ("+columns+")";
return s+" VALUES ("+values+")"; if (columns != null)
s = s + " (" + columns + ")";
return s + " VALUES (" + values + ")";
} }
/** /**
* Helper - generates SELECT SQL - very simple * Helper - generates SELECT SQL - very simple
*/ */
public static String select(String columns) { public static String selectSQL(String table, String columns) {
return select(columns,null,null); return selectSQL(table, columns, null, null);
} }
public static String select(String columns,String where) {
return select(columns,where,null); public static String selectSQL(String table, String columns, String where) {
return selectSQL(table, columns, where, null);
} }
public static String select(String columns,String where,String other) {
String s = "SELECT "+columns+" FROM "+getTableName(); public static String selectSQL(String table, String columns, String where, String other) {
if(where!=null) String s = "SELECT " + columns + " FROM " + table;
s=s+" WHERE "+where;
if(other!=null) if (where != null)
s=s+" "+other; s = s + " WHERE " + where;
if (other != null)
s = s + " " + other;
return s; return s;
} }
/**
* Helper - returns the test table's name
* This is defined by the tablename property. If not defined it defaults to
* jdbctest
*/
public static String getTableName() {
if(tablename==null)
tablename=System.getProperty("tablename","jdbctest");
return tablename;
}
/**
* As getTableName() but the id is a suffix. Used when more than one table is
* required in a test.
*/
public static String getTableName(String id) {
if(tablename==null)
tablename=System.getProperty("tablename","jdbctest");
return tablename+"_"+id;
}
/**
* Cache used by getTableName() [its used a lot!]
*/
private static String tablename;
/** /**
* Helper to prefix a number with leading zeros - ugly but it works... * Helper to prefix a number with leading zeros - ugly but it works...
* @param v value to prefix * @param v value to prefix
* @param l number of digits (0-10) * @param l number of digits (0-10)
*/ */
public static String fix(int v,int l) { public static String fix(int v, int l) {
String s = "0000000000".substring(0,l)+Integer.toString(v); String s = "0000000000".substring(0, l) + Integer.toString(v);
return s.substring(s.length()-l); return s.substring(s.length() - l);
} }
/**
* Number of milliseconds in a day
*/
public static final long DAYMILLIS = 24*3600*1000;
/** /**
* The main entry point for JUnit * The main entry point for JUnit
*/ */
@ -189,15 +170,16 @@ public class JDBC2Tests extends TestSuite {
// Connectivity/Protocols // Connectivity/Protocols
// ResultSet // ResultSet
// Time, Date, Timestamp
suite.addTestSuite(DateTest.class); suite.addTestSuite(DateTest.class);
suite.addTestSuite(TimeTest.class); suite.addTestSuite(TimeTest.class);
suite.addTestSuite(TimestampTest.class); suite.addTestSuite(TimestampTest.class);
// PreparedStatement // PreparedStatement
suite.addTestSuite(BatchExecuteTest.class);
// BatchExecute // BatchExecute
suite.addTestSuite(BatchExecuteTest.class);
// MetaData // MetaData

View File

@ -16,11 +16,11 @@ public class ANTTest extends TestCase {
String usr=System.getProperty("username"); String usr=System.getProperty("username");
String psw=System.getProperty("password"); String psw=System.getProperty("password");
assert(url!=null); assertNotNull(url);
assert(usr!=null); assertNotNull(usr);
assert(psw!=null); assertNotNull(psw);
assert(!url.equals("")); assertTrue(! url.equals(""));
assert(!usr.equals("")); assertTrue(! usr.equals(""));
} }
} }

View File

@ -4,13 +4,17 @@ import org.postgresql.test.JDBC2Tests;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/* TODO tests that can be added to this test case
- SQLExceptions chained to a BatchUpdateException
- test PreparedStatement as thoroughly as Statement
*/
/** /**
* Test case for Statement.batchExecute() * Test case for Statement.batchExecute()
*/ */
public class BatchExecuteTest extends TestCase { public class BatchExecuteTest extends TestCase {
private Connection con; private Connection con;
private Statement stmt;
public BatchExecuteTest(String name) { public BatchExecuteTest(String name) {
super(name); super(name);
@ -20,20 +24,13 @@ public class BatchExecuteTest extends TestCase {
// a table for this test. // a table for this test.
protected void setUp() throws Exception { protected void setUp() throws Exception {
con = JDBC2Tests.openDB(); con = JDBC2Tests.openDB();
stmt = con.createStatement(); Statement stmt = con.createStatement();
// Drop the test table if it already exists for some reason. It is // Drop the test table if it already exists for some reason. It is
// not an error if it doesn't exist. // not an error if it doesn't exist.
try { JDBC2Tests.createTable(con, "testbatch", "pk INTEGER, col1 INTEGER");
stmt.executeUpdate("DROP TABLE testbatch");
} catch (SQLException e) {
// Intentionally ignore. We cannot distinguish "table does not
// exist" from other errors, since PostgreSQL doesn't support
// error codes yet.
}
stmt.executeUpdate("CREATE TABLE testbatch(pk INTEGER, col1 INTEGER)"); stmt.executeUpdate("INSERT INTO testbatch VALUES (1, 0)");
stmt.executeUpdate("INSERT INTO testbatch VALUES(1, 0)");
// Generally recommended with batch updates. By default we run all // Generally recommended with batch updates. By default we run all
// tests in this test case with autoCommit disabled. // tests in this test case with autoCommit disabled.
@ -43,14 +40,10 @@ public class BatchExecuteTest extends TestCase {
// Tear down the fixture for this test case. // Tear down the fixture for this test case.
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
con.setAutoCommit(true); con.setAutoCommit(true);
if (stmt != null) {
stmt.executeUpdate("DROP TABLE testbatch"); JDBC2Tests.dropTable(con, "testbatch");
stmt.close();
}
if (con != null) {
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} }
}
public void testSupportsBatchUpdates() throws Exception { public void testSupportsBatchUpdates() throws Exception {
DatabaseMetaData dbmd = con.getMetaData(); DatabaseMetaData dbmd = con.getMetaData();
@ -75,6 +68,7 @@ public class BatchExecuteTest extends TestCase {
} }
public void testExecuteEmptyBatch() throws Exception { public void testExecuteEmptyBatch() throws Exception {
Statement stmt = con.createStatement();
int[] updateCount = stmt.executeBatch(); int[] updateCount = stmt.executeBatch();
assertEquals(0,updateCount.length); assertEquals(0,updateCount.length);
@ -82,9 +76,12 @@ public class BatchExecuteTest extends TestCase {
stmt.clearBatch(); stmt.clearBatch();
updateCount = stmt.executeBatch(); updateCount = stmt.executeBatch();
assertEquals(0,updateCount.length); assertEquals(0,updateCount.length);
stmt.close();
} }
public void testClearBatch() throws Exception { public void testClearBatch() throws Exception {
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
assertCol1HasValue(0); assertCol1HasValue(0);
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
@ -97,9 +94,13 @@ public class BatchExecuteTest extends TestCase {
assertCol1HasValue(4); assertCol1HasValue(4);
con.commit(); con.commit();
assertCol1HasValue(4); assertCol1HasValue(4);
stmt.close();
} }
public void testSelectThrowsException() throws Exception { public void testSelectThrowsException() throws Exception {
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
stmt.addBatch("SELECT col1 FROM testbatch WHERE pk = 1"); stmt.addBatch("SELECT col1 FROM testbatch WHERE pk = 1");
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
@ -115,6 +116,8 @@ public class BatchExecuteTest extends TestCase {
fail( "Should throw a BatchUpdateException instead of " + fail( "Should throw a BatchUpdateException instead of " +
"a generic SQLException: " + e); "a generic SQLException: " + e);
} }
stmt.close();
} }
public void testPreparedStatement() throws Exception { public void testPreparedStatement() throws Exception {
@ -151,6 +154,8 @@ public class BatchExecuteTest extends TestCase {
/** /**
*/ */
public void testTransactionalBehaviour() throws Exception { public void testTransactionalBehaviour() throws Exception {
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
stmt.executeBatch(); stmt.executeBatch();
@ -174,10 +179,7 @@ public class BatchExecuteTest extends TestCase {
assertCol1HasValue(12); assertCol1HasValue(12);
con.rollback(); con.rollback();
assertCol1HasValue(12); assertCol1HasValue(12);
stmt.close();
} }
} }
/* TODO tests that can be added to this test case
- SQLExceptions chained to a BatchUpdateException
- test PreparedStatement as thoroughly as Statement
*/

View File

@ -8,7 +8,7 @@ import java.sql.*;
import org.postgresql.largeobject.*; import org.postgresql.largeobject.*;
/** /**
* $Id: BlobTest.java,v 1.1 2001/02/14 17:45:17 peter Exp $ * $Id: BlobTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* *
* Some simple tests based on problems reported by users. Hopefully these will * Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-) * help prevent previous problems from re-occuring ;-)
@ -16,36 +16,43 @@ import org.postgresql.largeobject.*;
*/ */
public class BlobTest extends TestCase { public class BlobTest extends TestCase {
private Connection con;
private static final int LOOP = 0; // LargeObject API using loop
private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream
private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
public BlobTest(String name) { public BlobTest(String name) {
super(name); super(name);
} }
/** protected void setUp() throws Exception {
* The table format used by this TestCase con = JDBC2Tests.openDB();
*/ JDBC2Tests.createTable(con, "testblob", "id name,lo oid");
private static final String BLOB_TABLE_FMT = "id name,lo oid"; }
protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testblob");
JDBC2Tests.closeDB(con);
}
/** /**
* Tests one method of uploading a blob to the database * Tests one method of uploading a blob to the database
*/ */
public void testUploadBlob_LOOP() { public void testUploadBlob_LOOP() {
try { try {
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con,BLOB_TABLE_FMT);
con.setAutoCommit(false); con.setAutoCommit(false);
assert(!con.getAutoCommit()); assertTrue(!con.getAutoCommit());
assert(uploadFile(con,"build.xml",LOOP)>0); assertTrue(uploadFile("build.xml", LOOP) > 0);
// Now compare the blob & the file. Note this actually tests the // Now compare the blob & the file. Note this actually tests the
// InputStream implementation! // InputStream implementation!
assert(compareBlobs(con)); assertTrue(compareBlobs());
JDBC2Tests.closeDB(con); con.setAutoCommit(true);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
@ -54,35 +61,27 @@ public class BlobTest extends TestCase {
*/ */
public void testUploadBlob_NATIVE() { public void testUploadBlob_NATIVE() {
try { try {
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con,BLOB_TABLE_FMT);
con.setAutoCommit(false); con.setAutoCommit(false);
assert(!con.getAutoCommit()); assertTrue(!con.getAutoCommit());
assert(uploadFile(con,"build.xml",NATIVE_STREAM)>0); assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0);
// Now compare the blob & the file. Note this actually tests the // Now compare the blob & the file. Note this actually tests the
// InputStream implementation! // InputStream implementation!
assert(compareBlobs(con)); assertTrue(compareBlobs());
JDBC2Tests.closeDB(con); con.setAutoCommit(true);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
private static final int LOOP = 0; // LargeObject API using loop
private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream
private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
/** /**
* Helper - uploads a file into a blob using old style methods. We use this * Helper - uploads a file into a blob using old style methods. We use this
* because it always works, and we can use it as a base to test the new * because it always works, and we can use it as a base to test the new
* methods. * methods.
*/ */
private int uploadFile(Connection con,String file,int method) throws Exception { private int uploadFile(String file, int method) throws Exception {
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream(file);
@ -117,13 +116,13 @@ public class BlobTest extends TestCase {
case JDBC_STREAM: case JDBC_STREAM:
File f = new File(file); File f = new File(file);
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testblob", "?"));
ps.setBinaryStream(1,fis,(int) f.length()); ps.setBinaryStream(1,fis,(int) f.length());
ps.execute(); ps.execute();
break; break;
default: default:
assert("Unknown method in uploadFile",false); assertTrue("Unknown method in uploadFile",false);
} }
blob.close(); blob.close();
@ -131,7 +130,7 @@ public class BlobTest extends TestCase {
// Insert into the table // Insert into the table
Statement st = con.createStatement(); Statement st = con.createStatement();
st.executeUpdate(JDBC2Tests.insert("id,lo","'"+file+"',"+oid)); st.executeUpdate(JDBC2Tests.insertSQL("testblob", "id,lo","'"+file+"',"+oid));
con.commit(); con.commit();
st.close(); st.close();
@ -142,14 +141,14 @@ public class BlobTest extends TestCase {
* Helper - compares the blobs in a table with a local file. Note this alone * Helper - compares the blobs in a table with a local file. Note this alone
* tests the InputStream methods! * tests the InputStream methods!
*/ */
private boolean compareBlobs(Connection con) throws Exception { private boolean compareBlobs() throws Exception {
boolean result=true; boolean result=true;
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
Statement st = con.createStatement(); Statement st = con.createStatement();
ResultSet rs = st.executeQuery(JDBC2Tests.select("id,lo")); ResultSet rs = st.executeQuery(JDBC2Tests.selectSQL("testblob", "id,lo"));
assert(rs!=null); assertNotNull(rs);
while(rs.next()) { while(rs.next()) {
String file = rs.getString(1); String file = rs.getString(1);

View File

@ -10,7 +10,7 @@ import java.sql.*;
* *
* PS: Do you know how difficult it is to type on a train? ;-) * PS: Do you know how difficult it is to type on a train? ;-)
* *
* $Id: ConnectionTest.java,v 1.4 2001/09/10 14:54:22 momjian Exp $ * $Id: ConnectionTest.java,v 1.5 2001/09/23 04:11:14 momjian Exp $
*/ */
public class ConnectionTest extends TestCase { public class ConnectionTest extends TestCase {
@ -26,11 +26,8 @@ public class ConnectionTest extends TestCase {
protected void setUp() throws Exception { protected void setUp() throws Exception {
Connection con = JDBC2Tests.openDB(); Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable( con, "test_a", JDBC2Tests.createTable(con, "test_a", "imagename name,image oid,id int4");
"imagename name,image oid,id int4" ); JDBC2Tests.createTable(con, "test_c", "source text,cost money,imageid int4");
JDBC2Tests.createTable( con, "test_c",
"source text,cost money,imageid int4" );
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} }
@ -38,11 +35,10 @@ public class ConnectionTest extends TestCase {
// Tear down the fixture for this test case. // Tear down the fixture for this test case.
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
Connection con = JDBC2Tests.openDB(); Connection con = JDBC2Tests.openDB();
Statement stmt = con.createStatement();
stmt.executeUpdate("DROP TABLE test_a"); JDBC2Tests.dropTable(con, "test_a");
stmt.executeUpdate("DROP TABLE test_c"); JDBC2Tests.dropTable(con, "test_c");
stmt.close();
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} }
@ -55,16 +51,16 @@ public class ConnectionTest extends TestCase {
// A standard Statement // A standard Statement
java.sql.Statement stat = conn.createStatement(); java.sql.Statement stat = conn.createStatement();
assert(stat!=null); assertNotNull(stat);
stat.close(); stat.close();
// Ask for Updateable ResultSets // Ask for Updateable ResultSets
stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
assert(stat!=null); assertNotNull(stat);
stat.close(); stat.close();
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
@ -79,16 +75,16 @@ public class ConnectionTest extends TestCase {
// A standard Statement // A standard Statement
java.sql.PreparedStatement stat = conn.prepareStatement(sql); java.sql.PreparedStatement stat = conn.prepareStatement(sql);
assert(stat!=null); assertNotNull(stat);
stat.close(); stat.close();
// Ask for Updateable ResultSets // Ask for Updateable ResultSets
stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
assert(stat!=null); assertNotNull(stat);
stat.close(); stat.close();
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
@ -116,11 +112,11 @@ public class ConnectionTest extends TestCase {
// Turn it off // Turn it off
con.setAutoCommit(false); con.setAutoCommit(false);
assert(!con.getAutoCommit()); assertTrue(!con.getAutoCommit());
// Turn it back on // Turn it back on
con.setAutoCommit(true); con.setAutoCommit(true);
assert(con.getAutoCommit()); assertTrue(con.getAutoCommit());
// Now test commit // Now test commit
st = con.createStatement(); st = con.createStatement();
@ -132,21 +128,21 @@ public class ConnectionTest extends TestCase {
st.executeUpdate("update test_a set image=9876 where id=5678"); st.executeUpdate("update test_a set image=9876 where id=5678");
con.commit(); con.commit();
rs = st.executeQuery("select image from test_a where id=5678"); rs = st.executeQuery("select image from test_a where id=5678");
assert(rs.next()); assertTrue(rs.next());
assert(rs.getInt(1)==9876); assertEquals(9876, rs.getInt(1));
rs.close(); rs.close();
// Now try to change it but rollback // Now try to change it but rollback
st.executeUpdate("update test_a set image=1111 where id=5678"); st.executeUpdate("update test_a set image=1111 where id=5678");
con.rollback(); con.rollback();
rs = st.executeQuery("select image from test_a where id=5678"); rs = st.executeQuery("select image from test_a where id=5678");
assert(rs.next()); assertTrue(rs.next());
assert(rs.getInt(1)==9876); // Should not change! assertEquals(9876, rs.getInt(1)); // Should not change!
rs.close(); rs.close();
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
@ -158,15 +154,15 @@ public class ConnectionTest extends TestCase {
Connection con = JDBC2Tests.openDB(); Connection con = JDBC2Tests.openDB();
// Should not say closed // Should not say closed
assert(!con.isClosed()); assertTrue(!con.isClosed());
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
// Should now say closed // Should now say closed
assert(con.isClosed()); assertTrue(con.isClosed());
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
@ -180,7 +176,7 @@ public class ConnectionTest extends TestCase {
String testStr = "This Is OuR TeSt message"; String testStr = "This Is OuR TeSt message";
// The connection must be ours! // The connection must be ours!
assert(con instanceof org.postgresql.Connection); assertTrue(con instanceof org.postgresql.Connection);
// Clear any existing warnings // Clear any existing warnings
con.clearWarnings(); con.clearWarnings();
@ -190,16 +186,16 @@ public class ConnectionTest extends TestCase {
// Retrieve it // Retrieve it
SQLWarning warning = con.getWarnings(); SQLWarning warning = con.getWarnings();
assert(warning!=null); assertNotNull(warning);
assert(warning.getMessage().equals(testStr)); assertEquals(testStr, warning.getMessage());
// Finally test clearWarnings() this time there must be something to delete // Finally test clearWarnings() this time there must be something to delete
con.clearWarnings(); con.clearWarnings();
assert(con.getWarnings()==null); assertTrue(con.getWarnings()==null);
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
@ -213,76 +209,72 @@ public class ConnectionTest extends TestCase {
Connection con = JDBC2Tests.openDB(); Connection con = JDBC2Tests.openDB();
// PostgreSQL defaults to READ COMMITTED // PostgreSQL defaults to READ COMMITTED
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
// Begin a transaction // Begin a transaction
con.setAutoCommit(false); con.setAutoCommit(false);
// The isolation level should not have changed // The isolation level should not have changed
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
// Now change the default for future transactions // Now change the default for future transactions
con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
// Since the call to setTransactionIsolation() above was made // Since the call to setTransactionIsolation() above was made
// inside the transaction, the isolation level of the current // inside the transaction, the isolation level of the current
// transaction did not change. It affects only future transactions. // transaction did not change. It affects only future transactions.
// This behaviour is recommended by the JDBC spec. // This behaviour is recommended by the JDBC spec.
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
// Begin a new transaction // Begin a new transaction
con.commit(); con.commit();
// Now we should see the new isolation level // Now we should see the new isolation level
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_SERIALIZABLE,
Connection.TRANSACTION_SERIALIZABLE ); con.getTransactionIsolation());
// Repeat the steps above with the transition back to // Repeat the steps above with the transition back to
// READ COMMITTED. // READ COMMITTED.
con.setTransactionIsolation( con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
Connection.TRANSACTION_READ_COMMITTED ); assertEquals(Connection.TRANSACTION_SERIALIZABLE,
assertEquals( con.getTransactionIsolation(), con.getTransactionIsolation());
Connection.TRANSACTION_SERIALIZABLE );
con.commit(); con.commit();
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
// Now run some tests with autocommit enabled. // Now run some tests with autocommit enabled.
con.setAutoCommit(true); con.setAutoCommit(true);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_SERIALIZABLE,
Connection.TRANSACTION_SERIALIZABLE ); con.getTransactionIsolation());
con.setTransactionIsolation( con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
Connection.TRANSACTION_READ_COMMITTED ); assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation());
assertEquals( con.getTransactionIsolation(),
Connection.TRANSACTION_READ_COMMITTED );
// Test if a change of isolation level before beginning the // Test if a change of isolation level before beginning the
// transaction affects the isolation level inside the transaction. // transaction affects the isolation level inside the transaction.
con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_SERIALIZABLE,
Connection.TRANSACTION_SERIALIZABLE ); con.getTransactionIsolation());
con.setAutoCommit(false); con.setAutoCommit(false);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_SERIALIZABLE,
Connection.TRANSACTION_SERIALIZABLE ); con.getTransactionIsolation());
con.setAutoCommit(true); con.setAutoCommit(true);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_SERIALIZABLE,
Connection.TRANSACTION_SERIALIZABLE ); con.getTransactionIsolation());
con.setTransactionIsolation( con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
Connection.TRANSACTION_READ_COMMITTED ); assertEquals(Connection.TRANSACTION_READ_COMMITTED,
assertEquals( con.getTransactionIsolation(), con.getTransactionIsolation());
Connection.TRANSACTION_READ_COMMITTED );
con.setAutoCommit(false); con.setAutoCommit(false);
assertEquals( con.getTransactionIsolation(), assertEquals(Connection.TRANSACTION_READ_COMMITTED,
Connection.TRANSACTION_READ_COMMITTED ); con.getTransactionIsolation());
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} }
@ -305,15 +297,15 @@ public class ConnectionTest extends TestCase {
// now change it for an empty one // now change it for an empty one
java.util.Map newmap = new java.util.HashMap(); java.util.Map newmap = new java.util.HashMap();
con.setTypeMap(newmap); con.setTypeMap(newmap);
assert(con.getTypeMap()==newmap); assertEquals(newmap, con.getTypeMap());
// restore the old one // restore the old one
con.setTypeMap(oldmap); con.setTypeMap(oldmap);
assert(con.getTypeMap()==oldmap); assertEquals(oldmap, con.getTypeMap());
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); assertTrue(ex.getMessage(),false);
} }
} }
} }

View File

@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/** /**
* $Id: DateTest.java,v 1.1 2001/02/13 16:39:05 peter Exp $ * $Id: DateTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* *
* Some simple tests based on problems reported by users. Hopefully these will * Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-) * help prevent previous problems from re-occuring ;-)
@ -13,34 +13,41 @@ import java.sql.*;
*/ */
public class DateTest extends TestCase { public class DateTest extends TestCase {
private Connection con;
public DateTest(String name) { public DateTest(String name) {
super(name); super(name);
} }
protected void setUp() throws Exception {
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "testdate", "dt date");
}
protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testdate");
JDBC2Tests.closeDB(con);
}
/** /**
* Tests the time methods in ResultSet * Tests the time methods in ResultSet
*/ */
public void testGetDate() { public void testGetDate() {
try { try {
Connection con = JDBC2Tests.openDB(); Statement stmt = con.createStatement();
Statement st=con.createStatement(); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testdate", "'1950-02-07'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testdate", "'1970-06-02'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testdate", "'1999-08-11'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testdate", "'2001-02-13'")));
JDBC2Tests.createTable(con,"dt date"); /* dateTest() contains all of the tests */
dateTest();
st.executeUpdate(JDBC2Tests.insert("'1950-02-07'")); assertEquals(4, stmt.executeUpdate("DELETE FROM " + "testdate"));
st.executeUpdate(JDBC2Tests.insert("'1970-06-02'")); stmt.close();
st.executeUpdate(JDBC2Tests.insert("'1999-08-11'"));
st.executeUpdate(JDBC2Tests.insert("'2001-02-13'"));
// Fall through helper
checkTimeTest(con,st);
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
@ -49,78 +56,73 @@ public class DateTest extends TestCase {
*/ */
public void testSetDate() { public void testSetDate() {
try { try {
Connection con = JDBC2Tests.openDB(); Statement stmt = con.createStatement();
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testdate", "?"));
Statement st=con.createStatement(); ps.setDate(1, makeDate(1950, 2, 7));
assertEquals(1, ps.executeUpdate());
JDBC2Tests.createTable(con,"dt date"); ps.setDate(1, makeDate(1970, 6, 2));
assertEquals(1, ps.executeUpdate());
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); ps.setDate(1, makeDate(1999, 8, 11));
assertEquals(1, ps.executeUpdate());
ps.setDate(1,getDate(1950,2,7)); ps.setDate(1, makeDate(2001, 2, 13));
assert(!ps.execute()); // false as its an update! assertEquals(1, ps.executeUpdate());
ps.setDate(1,getDate(1970,6,2));
assert(!ps.execute()); // false as its an update!
ps.setDate(1,getDate(1999,8,11));
assert(!ps.execute()); // false as its an update!
ps.setDate(1,getDate(2001,2,13));
assert(!ps.execute()); // false as its an update!
// Fall through helper
checkTimeTest(con,st);
ps.close(); ps.close();
st.close();
JDBC2Tests.closeDB(con); // Fall through helper
dateTest();
assertEquals(4, stmt.executeUpdate("DELETE FROM testdate"));
stmt.close();
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
/** /**
* Helper for the TimeTests. It tests what should be in the db * Helper for the date tests. It tests what should be in the db
*/ */
private void checkTimeTest(Connection con,Statement st) throws SQLException { private void dateTest() throws SQLException {
ResultSet rs=null; Statement st = con.createStatement();
java.sql.Date t=null; ResultSet rs;
java.sql.Date d;
rs=st.executeQuery(JDBC2Tests.select("dt")); rs = st.executeQuery(JDBC2Tests.selectSQL("testdate", "dt"));
assert(rs!=null); assertNotNull(rs);
assert(rs.next()); assertTrue(rs.next());
t = rs.getDate(1); d = rs.getDate(1);
assert(t!=null); assertNotNull(d);
assert(t.equals(getDate(1950,2,7))); assertEquals(d, makeDate(1950, 2, 7));
assert(rs.next()); assertTrue(rs.next());
t = rs.getDate(1); d = rs.getDate(1);
assert(t!=null); assertNotNull(d);
assert(t.equals(getDate(1970,6,2))); assertEquals(d, makeDate(1970, 6, 2));
assert(rs.next()); assertTrue(rs.next());
t = rs.getDate(1); d = rs.getDate(1);
assert(t!=null); assertNotNull(d);
assert(t.equals(getDate(1999,8,11))); assertEquals(d, makeDate(1999, 8, 11));
assert(rs.next()); assertTrue(rs.next());
t = rs.getDate(1); d = rs.getDate(1);
assert(t!=null); assertNotNull(d);
assert(t.equals(getDate(2001,2,13))); assertEquals(d, makeDate(2001, 2, 13));
assert(!rs.next()); assertTrue(!rs.next());
rs.close(); rs.close();
st.close();
} }
/** private java.sql.Date makeDate(int y, int m, int d) {
* Yes this is ugly, but it gets the test done ;-) return java.sql.Date.valueOf(JDBC2Tests.fix(y, 4) + "-" +
*/ JDBC2Tests.fix(m, 2) + "-" +
private java.sql.Date getDate(int y,int m,int d) { JDBC2Tests.fix(d, 2));
return java.sql.Date.valueOf(JDBC2Tests.fix(y,4)+"-"+JDBC2Tests.fix(m,2)+"-"+JDBC2Tests.fix(d,2));
} }
} }

View File

@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/** /**
* $Id: DriverTest.java,v 1.1 2001/02/07 09:13:20 peter Exp $ * $Id: DriverTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* *
* Tests the dynamically created class org.postgresql.Driver * Tests the dynamically created class org.postgresql.Driver
* *
@ -25,21 +25,21 @@ public class DriverTest extends TestCase {
// Load the driver (note clients should never do it this way!) // Load the driver (note clients should never do it this way!)
org.postgresql.Driver drv = new org.postgresql.Driver(); org.postgresql.Driver drv = new org.postgresql.Driver();
assert(drv!=null); assertNotNull(drv);
// These are always correct // These are always correct
assert(drv.acceptsURL("jdbc:postgresql:test")); assertTrue(drv.acceptsURL("jdbc:postgresql:test"));
assert(drv.acceptsURL("jdbc:postgresql://localhost/test")); assertTrue(drv.acceptsURL("jdbc:postgresql://localhost/test"));
assert(drv.acceptsURL("jdbc:postgresql://localhost:5432/test")); assertTrue(drv.acceptsURL("jdbc:postgresql://localhost:5432/test"));
assert(drv.acceptsURL("jdbc:postgresql://127.0.0.1/anydbname")); assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1/anydbname"));
assert(drv.acceptsURL("jdbc:postgresql://127.0.0.1:5433/hidden")); assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1:5433/hidden"));
// Badly formatted url's // Badly formatted url's
assert(!drv.acceptsURL("jdbc:postgres:test")); assertTrue(!drv.acceptsURL("jdbc:postgres:test"));
assert(!drv.acceptsURL("postgresql:test")); assertTrue(!drv.acceptsURL("postgresql:test"));
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
@ -56,18 +56,17 @@ public class DriverTest extends TestCase {
// Test with the url, username & password // Test with the url, username & password
con = DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); con = DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword());
assert(con!=null); assertNotNull(con);
con.close(); con.close();
// Test with the username in the url // Test with the username in the url
con = DriverManager.getConnection(JDBC2Tests.getURL()+"?user="+JDBC2Tests.getUser()+"&password="+JDBC2Tests.getPassword()); con = DriverManager.getConnection(JDBC2Tests.getURL()+"?user="+JDBC2Tests.getUser()+"&password="+JDBC2Tests.getPassword());
assert(con!=null); assertNotNull(con);
con.close(); con.close();
} catch(ClassNotFoundException ex) { } catch(ClassNotFoundException ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} catch(SQLException ex) { } catch(SQLException ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
} }

View File

@ -8,7 +8,7 @@ import java.io.*;
/** /**
* Tests for the Encoding class. * Tests for the Encoding class.
* *
* $Id: EncodingTest.java,v 1.1 2001/07/21 21:27:41 momjian Exp $ * $Id: EncodingTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
*/ */
@ -23,12 +23,12 @@ public class EncodingTest extends TestCase {
encoding = Encoding.getEncoding("UNICODE", null); encoding = Encoding.getEncoding("UNICODE", null);
assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase()); assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase());
encoding = Encoding.getEncoding("SQL_ASCII", null); encoding = Encoding.getEncoding("SQL_ASCII", null);
assert(encoding.name().toUpperCase().indexOf("ASCII") != -1); assertTrue(encoding.name().toUpperCase().indexOf("ASCII") != -1);
assertEquals("When encoding is unknown the default encoding should be used", assertEquals("When encoding is unknown the default encoding should be used",
Encoding.defaultEncoding(), Encoding.defaultEncoding(),
Encoding.getEncoding("UNKNOWN", null)); Encoding.getEncoding("UNKNOWN", null));
encoding = Encoding.getEncoding("SQL_ASCII", "utf-8"); encoding = Encoding.getEncoding("SQL_ASCII", "utf-8");
assert("Encoding passed in by the user should be preferred", assertTrue("Encoding passed in by the user should be preferred",
encoding.name().toUpperCase().indexOf("UTF") != -1); encoding.name().toUpperCase().indexOf("UTF") != -1);
} }

View File

@ -6,7 +6,7 @@ import java.sql.*;
import java.math.BigDecimal; import java.math.BigDecimal;
/** /**
* $Id: JBuilderTest.java,v 1.2 2001/09/07 22:17:48 momjian Exp $ * $Id: JBuilderTest.java,v 1.3 2001/09/23 04:11:14 momjian Exp $
* *
* Some simple tests to check that the required components needed for JBuilder * Some simple tests to check that the required components needed for JBuilder
* stay working * stay working
@ -31,10 +31,7 @@ public class JBuilderTest extends TestCase {
// Tear down the fixture for this test case. // Tear down the fixture for this test case.
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
Connection con = JDBC2Tests.openDB(); Connection con = JDBC2Tests.openDB();
Statement stmt = con.createStatement(); JDBC2Tests.dropTable(con, "test_c");
stmt.executeUpdate("DROP TABLE test_c");
stmt.close();
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} }
@ -47,7 +44,7 @@ public class JBuilderTest extends TestCase {
Statement st=con.createStatement(); Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select cost from test_c"); ResultSet rs=st.executeQuery("select cost from test_c");
assert(rs!=null); assertNotNull(rs);
while(rs.next()){ while(rs.next()){
double bd = rs.getDouble(1); double bd = rs.getDouble(1);
@ -58,7 +55,7 @@ public class JBuilderTest extends TestCase {
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
} }

View File

@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/** /**
* $Id: MiscTest.java,v 1.1 2001/02/13 16:39:05 peter Exp $ * $Id: MiscTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* *
* Some simple tests based on problems reported by users. Hopefully these will * Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-) * help prevent previous problems from re-occuring ;-)
@ -30,7 +30,7 @@ public class MiscTest extends TestCase {
Statement st=con.createStatement(); Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select datname from pg_database"); ResultSet rs=st.executeQuery("select datname from pg_database");
assert(rs!=null); assertNotNull(rs);
while(rs.next()){ while(rs.next()){
String s = rs.getString(1); String s = rs.getString(1);
@ -41,7 +41,7 @@ public class MiscTest extends TestCase {
JDBC2Tests.closeDB(con); JDBC2Tests.closeDB(con);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
} }

View File

@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/** /**
* $Id: TimeTest.java,v 1.1 2001/02/13 16:39:05 peter Exp $ * $Id: TimeTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* *
* Some simple tests based on problems reported by users. Hopefully these will * Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-) * help prevent previous problems from re-occuring ;-)
@ -13,32 +13,39 @@ import java.sql.*;
*/ */
public class TimeTest extends TestCase { public class TimeTest extends TestCase {
private Connection con;
public TimeTest(String name) { public TimeTest(String name) {
super(name); super(name);
} }
protected void setUp() throws Exception {
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "testtime", "tm time");
}
protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testtime");
JDBC2Tests.closeDB(con);
}
/** /**
* Tests the time methods in ResultSet * Tests the time methods in ResultSet
*/ */
public void testGetTime() { public void testGetTime() {
try { try {
Connection con = JDBC2Tests.openDB(); Statement stmt = con.createStatement();
Statement st=con.createStatement(); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'01:02:03'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'23:59:59'")));
JDBC2Tests.createTable(con,"tm time");
st.executeUpdate(JDBC2Tests.insert("'01:02:03'"));
st.executeUpdate(JDBC2Tests.insert("'23:59:59'"));
// Fall through helper // Fall through helper
checkTimeTest(con,st); timeTest();
st.close(); assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
@ -47,77 +54,55 @@ public class TimeTest extends TestCase {
*/ */
public void testSetTime() { public void testSetTime() {
try { try {
Connection con = JDBC2Tests.openDB(); PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testtime", "?"));
Statement stmt = con.createStatement();
Statement st=con.createStatement(); ps.setTime(1, makeTime(1, 2, 3));
assertEquals(1, ps.executeUpdate());
JDBC2Tests.createTable(con,"tm time"); ps.setTime(1, makeTime(23, 59, 59));
assertEquals(1, ps.executeUpdate());
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?"));
ps.setTime(1,getTime(1,2,3));
assert(!ps.execute()); // false as its an update!
ps.setTime(1,getTime(23,59,59));
assert(!ps.execute()); // false as its an update!
// Fall through helper // Fall through helper
checkTimeTest(con,st); timeTest();
assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
ps.close(); ps.close();
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
/** /**
* Helper for the TimeTests. It tests what should be in the db * Helper for the TimeTests. It tests what should be in the db
*/ */
private void checkTimeTest(Connection con,Statement st) throws SQLException { private void timeTest() throws SQLException {
ResultSet rs=null; Statement st = con.createStatement();
Time t=null; ResultSet rs;
java.sql.Time t;
rs=st.executeQuery(JDBC2Tests.select("tm")); rs = st.executeQuery(JDBC2Tests.selectSQL("testtime", "tm"));
assert(rs!=null); assertNotNull(rs);
assert(rs.next()); assertTrue(rs.next());
t = rs.getTime(1); t = rs.getTime(1);
assert(t!=null); assertNotNull(t);
assert(getHours(t)==1); assertEquals(makeTime(1, 2, 3), t);
assert(getMinutes(t)==2);
assert(getSeconds(t)==3);
assert(rs.next()); assertTrue(rs.next());
t = rs.getTime(1); t = rs.getTime(1);
assert(t!=null); assertNotNull(t);
assert(getHours(t)==23); assertEquals(makeTime(23, 59, 59), t);
assert(getMinutes(t)==59);
assert(getSeconds(t)==59);
assert(!rs.next()); assertTrue(! rs.next());
rs.close(); rs.close();
} }
/** private java.sql.Time makeTime(int h, int m, int s) {
* These implement depreciated methods in java.sql.Time return java.sql.Time.valueOf(JDBC2Tests.fix(h, 2) + ":" +
*/ JDBC2Tests.fix(m, 2) + ":" +
private static long getHours(Time t) { JDBC2Tests.fix(s, 2));
return (t.getTime() % JDBC2Tests.DAYMILLIS)/3600000;
}
private static long getMinutes(Time t) {
return ((t.getTime() % JDBC2Tests.DAYMILLIS)/60000)%60;
}
private static long getSeconds(Time t) {
return ((t.getTime() % JDBC2Tests.DAYMILLIS)/1000)%60;
}
private Time getTime(int h,int m,int s) {
return new Time(1000*(s+(m*60)+(h*3600)));
} }
} }

View File

@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*; import java.sql.*;
/** /**
* $Id: TimestampTest.java,v 1.2 2001/02/16 16:45:01 peter Exp $ * $Id: TimestampTest.java,v 1.3 2001/09/23 04:11:14 momjian Exp $
* *
* This has been the most controversial pair of methods since 6.5 was released! * This has been the most controversial pair of methods since 6.5 was released!
* *
@ -15,42 +15,49 @@ import java.sql.*;
*/ */
public class TimestampTest extends TestCase { public class TimestampTest extends TestCase {
private Connection con;
public TimestampTest(String name) { public TimestampTest(String name) {
super(name); super(name);
} }
protected void setUp() throws Exception {
con = JDBC2Tests.openDB();
Statement stmt = con.createStatement();
JDBC2Tests.createTable(con, "testtimestamp", "ts timestamp");
}
protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testtimestamp");
JDBC2Tests.closeDB(con);
}
/** /**
* Tests the time methods in ResultSet * Tests the time methods in ResultSet
*/ */
public void testGetTimestamp() { public void testGetTimestamp() {
try { try {
Connection con = JDBC2Tests.openDB(); Statement stmt = con.createStatement();
Statement st=con.createStatement(); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
"'1950-02-07 15:00:00'")));
JDBC2Tests.createTable(con,"ts timestamp"); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", "'" +
getTimestamp(1970, 6, 2, 8, 13, 0, 0).toString() +
"'")));
st.executeUpdate(JDBC2Tests.insert("'1950-02-07 15:00:00'")); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
"'1970-06-02 08:13:00'")));
// Before you ask why 8:13:00 and not 7:13:00, this is a problem with the
// getTimestamp method in this TestCase. It's simple, brain-dead. It
// simply doesn't know about summer time. As this date is in June, it's
// summer (GMT wise).
//
// This case needs some work done on it.
//
st.executeUpdate(JDBC2Tests.insert("'"+getTimestamp(1970,6,2,8,13,0).toString()+"'"));
//st.executeUpdate(JDBC2Tests.insert("'1950-02-07'"));
// Fall through helper // Fall through helper
checkTimeTest(con,st); timestampTest();
st.close(); assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp"));
JDBC2Tests.closeDB(con); stmt.close();
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
@ -59,79 +66,67 @@ public class TimestampTest extends TestCase {
*/ */
public void testSetTimestamp() { public void testSetTimestamp() {
try { try {
Connection con = JDBC2Tests.openDB(); Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement(JDBC2Tests.insertSQL("testtimestamp", "?"));
Statement st=con.createStatement(); pstmt.setTimestamp(1, getTimestamp(1950, 2, 7, 15, 0, 0, 0));
assertEquals(1, pstmt.executeUpdate());
JDBC2Tests.createTable(con,"ts timestamp"); pstmt.setTimestamp(1, getTimestamp(1970, 6, 2, 8, 13, 0, 0));
assertEquals(1, pstmt.executeUpdate());
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); pstmt.setTimestamp(1, getTimestamp(1970, 6, 2, 8, 13, 0, 0));
assertEquals(1, pstmt.executeUpdate());
ps.setTimestamp(1,getTimestamp(1950,2,7,15,0,0));
assert(!ps.execute()); // false as its an update!
// Before you ask why 8:13:00 and not 7:13:00, this is a problem with the
// getTimestamp method in this TestCase. It's simple, brain-dead. It
// simply doesn't know about summer time. As this date is in June, it's
// summer (GMT wise).
//
// This case needs some work done on it.
//
ps.setTimestamp(1,getTimestamp(1970,6,2,7,13,0));
assert(!ps.execute()); // false as its an update!
// Fall through helper // Fall through helper
checkTimeTest(con,st); timestampTest();
ps.close(); assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp"));
st.close();
JDBC2Tests.closeDB(con); pstmt.close();
stmt.close();
} catch(Exception ex) { } catch(Exception ex) {
assert(ex.getMessage(),false); fail(ex.getMessage());
} }
} }
/** /**
* Helper for the TimeTests. It tests what should be in the db * Helper for the TimeTests. It tests what should be in the db
*/ */
private void checkTimeTest(Connection con,Statement st) throws SQLException { private void timestampTest() throws SQLException {
ResultSet rs=null; Statement stmt = con.createStatement();
java.sql.Timestamp t=null; ResultSet rs;
java.sql.Timestamp t;
rs=st.executeQuery(JDBC2Tests.select("ts")); rs = stmt.executeQuery(JDBC2Tests.selectSQL("testtimestamp", "ts"));
assert(rs!=null); assertNotNull(rs);
assert(rs.next()); assertTrue(rs.next());
t = rs.getTimestamp(1); t = rs.getTimestamp(1);
assert(t!=null); assertNotNull(t);
assert(t.equals(getTimestamp(1950,2,7,15,0,0))); assertTrue(t.equals(getTimestamp(1950, 2, 7, 15, 0, 0, 0)));
assert(rs.next()); assertTrue(rs.next());
t = rs.getTimestamp(1); t = rs.getTimestamp(1);
assert(t!=null); assertNotNull(t);
assertTrue(t.equals(getTimestamp(1970, 6, 2, 8, 13, 0, 0)));
// Seems Daylight saving is ignored? assertTrue(rs.next());
assert(t.equals(getTimestamp(1970,6,2,8,13,0))); t = rs.getTimestamp(1);
assertNotNull(t);
assertTrue(t.equals(getTimestamp(1970, 6, 2, 8, 13, 0, 0)));
assert(!rs.next()); // end of table. Fail if more entries exist. assertTrue(! rs.next()); // end of table. Fail if more entries exist.
rs.close(); rs.close();
stmt.close();
} }
/** private java.sql.Timestamp getTimestamp(int y, int m, int d, int h, int mn, int se, int f) {
* These implement depreciated methods in java.sql.Time return java.sql.Timestamp.valueOf(JDBC2Tests.fix(y, 4) + "-" +
*/ JDBC2Tests.fix(m, 2) + "-" +
private static final long dayms = 24*3600*1000; JDBC2Tests.fix(d, 2) + " " +
JDBC2Tests.fix(h, 2) + ":" +
/** JDBC2Tests.fix(mn, 2) + ":" +
* Yes this is ugly, but it gets the test done ;-) JDBC2Tests.fix(se, 2) + "." +
*
* Actually its buggy. We need a better solution to this, then the hack of adding 1 hour to
* entries in June above don't need setting.
*/
private java.sql.Timestamp getTimestamp(int y,int m,int d,int h,int mn,int se) {
return java.sql.Timestamp.valueOf(JDBC2Tests.fix(y,4)+"-"+JDBC2Tests.fix(m,2)+"-"+JDBC2Tests.fix(d,2)+" "+JDBC2Tests.fix(h,2)+":"+JDBC2Tests.fix(mn,2)+":"+JDBC2Tests.fix(se,2)+"."+JDBC2Tests.fix(0,9));
}
} }