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

@ -10,206 +10,188 @@ import java.sql.*;
* Executes all known tests for JDBC2 and includes some utility methods. * Executes all known tests for JDBC2 and includes some utility methods.
*/ */
public class JDBC2Tests extends TestSuite { public class JDBC2Tests extends TestSuite {
/** /**
* Returns the Test database JDBC URL * Returns the Test database JDBC URL
*/ */
public static String getURL() { public static String getURL() {
return System.getProperty("database"); return System.getProperty("database");
} }
/** /**
* Returns the Postgresql username * Returns the Postgresql username
*/ */
public static String getUser() { public static String getUser() {
return System.getProperty("username"); return System.getProperty("username");
} }
/** /**
* Returns the user's password * Returns the user's password
*/ */
public static String getPassword() { public static String getPassword() {
return System.getProperty("password"); return System.getProperty("password");
} }
/** /**
* 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;
} }
/** /**
* 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 select(String columns,String where,String other) {
String s = "SELECT "+columns+" FROM "+getTableName();
if(where!=null)
s=s+" WHERE "+where;
if(other!=null)
s=s+" "+other;
return s;
}
/** public static String selectSQL(String table, String columns, String where) {
* Helper - returns the test table's name return selectSQL(table, columns, where, null);
* This is defined by the tablename property. If not defined it defaults to }
* jdbctest
*/ public static String selectSQL(String table, String columns, String where, String other) {
public static String getTableName() { String s = "SELECT " + columns + " FROM " + table;
if(tablename==null)
tablename=System.getProperty("tablename","jdbctest"); if (where != null)
return tablename; s = s + " WHERE " + where;
} if (other != null)
s = s + " " + other;
return s;
}
/**
* Helper to prefix a number with leading zeros - ugly but it works...
* @param v value to prefix
* @param l number of digits (0-10)
*/
public static String fix(int v, int l) {
String s = "0000000000".substring(0, l) + Integer.toString(v);
return s.substring(s.length() - l);
}
/** /**
* As getTableName() but the id is a suffix. Used when more than one table is * The main entry point for JUnit
* required in a test. */
*/ public static TestSuite suite() {
public static String getTableName(String id) { TestSuite suite= new TestSuite();
if(tablename==null)
tablename=System.getProperty("tablename","jdbctest");
return tablename+"_"+id;
}
/** //
* Cache used by getTableName() [its used a lot!] // Add one line per class in our test cases. These should be in order of
*/ // complexity.
private static String tablename;
/** // ANTTest should be first as it ensures that test parameters are
* Helper to prefix a number with leading zeros - ugly but it works... // being sent to the suite. It also initialises the database (if required)
* @param v value to prefix // with some simple global tables (will make each testcase use its own later).
* @param l number of digits (0-10) //
*/ suite.addTestSuite(ANTTest.class);
public static String fix(int v,int l) {
String s = "0000000000".substring(0,l)+Integer.toString(v);
return s.substring(s.length()-l);
}
/** // Basic Driver internals
* Number of milliseconds in a day suite.addTestSuite(DriverTest.class);
*/ suite.addTestSuite(ConnectionTest.class);
public static final long DAYMILLIS = 24*3600*1000; suite.addTestSuite(DatabaseMetaDataTest.class);
suite.addTestSuite(EncodingTest.class);
/** // Connectivity/Protocols
* The main entry point for JUnit
*/
public static TestSuite suite() {
TestSuite suite= new TestSuite();
// // ResultSet
// Add one line per class in our test cases. These should be in order of
// complexity.
// ANTTest should be first as it ensures that test parameters are // Time, Date, Timestamp
// being sent to the suite. It also initialises the database (if required) suite.addTestSuite(DateTest.class);
// with some simple global tables (will make each testcase use its own later). suite.addTestSuite(TimeTest.class);
// suite.addTestSuite(TimestampTest.class);
suite.addTestSuite(ANTTest.class);
// Basic Driver internals // PreparedStatement
suite.addTestSuite(DriverTest.class);
suite.addTestSuite(ConnectionTest.class);
suite.addTestSuite(DatabaseMetaDataTest.class);
suite.addTestSuite(EncodingTest.class);
// Connectivity/Protocols // BatchExecute
suite.addTestSuite(BatchExecuteTest.class);
// ResultSet // MetaData
suite.addTestSuite(DateTest.class);
suite.addTestSuite(TimeTest.class);
suite.addTestSuite(TimestampTest.class);
// PreparedStatement // Other misc tests, based on previous problems users have had or specific
suite.addTestSuite(BatchExecuteTest.class); // features some applications require.
suite.addTestSuite(JBuilderTest.class);
suite.addTestSuite(MiscTest.class);
// BatchExecute // Fastpath/LargeObject
suite.addTestSuite(BlobTest.class);
// That's all folks
// MetaData return suite;
}
// Other misc tests, based on previous problems users have had or specific
// features some applications require.
suite.addTestSuite(JBuilderTest.class);
suite.addTestSuite(MiscTest.class);
// Fastpath/LargeObject
suite.addTestSuite(BlobTest.class);
// That's all folks
return suite;
}
} }

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) { stmt.executeUpdate("INSERT INTO testbatch VALUES (1, 0)");
// 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)");
// 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,13 +40,9 @@ 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(); JDBC2Tests.closeDB(con);
}
if (con != null) {
JDBC2Tests.closeDB(con);
}
} }
public void testSupportsBatchUpdates() throws Exception { public void testSupportsBatchUpdates() throws Exception {
@ -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,169 +16,168 @@ import org.postgresql.largeobject.*;
*/ */
public class BlobTest extends TestCase { public class BlobTest extends TestCase {
public BlobTest(String name) { private Connection con;
super(name);
}
/** private static final int LOOP = 0; // LargeObject API using loop
* The table format used by this TestCase private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream
*/ private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
private static final String BLOB_TABLE_FMT = "id name,lo oid";
/** public BlobTest(String name) {
* Tests one method of uploading a blob to the database super(name);
*/
public void testUploadBlob_LOOP() {
try {
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con,BLOB_TABLE_FMT);
con.setAutoCommit(false);
assert(!con.getAutoCommit());
assert(uploadFile(con,"build.xml",LOOP)>0);
// Now compare the blob & the file. Note this actually tests the
// InputStream implementation!
assert(compareBlobs(con));
JDBC2Tests.closeDB(con);
} catch(Exception ex) {
assert(ex.getMessage(),false);
} }
}
/** protected void setUp() throws Exception {
* Tests one method of uploading a blob to the database con = JDBC2Tests.openDB();
*/ JDBC2Tests.createTable(con, "testblob", "id name,lo oid");
public void testUploadBlob_NATIVE() { }
try {
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con,BLOB_TABLE_FMT); protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testblob");
JDBC2Tests.closeDB(con);
}
/**
* Tests one method of uploading a blob to the database
*/
public void testUploadBlob_LOOP() {
try {
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
con.setAutoCommit(false); assertTrue(uploadFile("build.xml", LOOP) > 0);
assert(!con.getAutoCommit());
assert(uploadFile(con,"build.xml",NATIVE_STREAM)>0); // Now compare the blob & the file. Note this actually tests the
// InputStream implementation!
assertTrue(compareBlobs());
// Now compare the blob & the file. Note this actually tests the con.setAutoCommit(true);
// InputStream implementation! } catch(Exception ex) {
assert(compareBlobs(con)); fail(ex.getMessage());
}
}
JDBC2Tests.closeDB(con); /**
} catch(Exception ex) { * Tests one method of uploading a blob to the database
assert(ex.getMessage(),false); */
} public void testUploadBlob_NATIVE() {
} try {
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
private static final int LOOP = 0; // LargeObject API using loop assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0);
private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream
private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
/** // Now compare the blob & the file. Note this actually tests the
* Helper - uploads a file into a blob using old style methods. We use this // InputStream implementation!
* because it always works, and we can use it as a base to test the new assertTrue(compareBlobs());
* methods.
*/
private int uploadFile(Connection con,String file,int method) throws Exception {
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
FileInputStream fis = new FileInputStream(file); con.setAutoCommit(true);
} catch(Exception ex) {
fail(ex.getMessage());
}
}
int oid = lom.create(LargeObjectManager.READWRITE); /**
LargeObject blob = lom.open(oid); * 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
* methods.
*/
private int uploadFile(String file, int method) throws Exception {
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
int s,t; FileInputStream fis = new FileInputStream(file);
byte buf[];
OutputStream os;
switch(method) int oid = lom.create(LargeObjectManager.READWRITE);
{ LargeObject blob = lom.open(oid);
case LOOP:
buf = new byte[2048];
t=0;
while((s=fis.read(buf,0,buf.length))>0) {
t+=s;
blob.write(buf,0,s);
}
break;
case NATIVE_STREAM: int s,t;
os = blob.getOutputStream(); byte buf[];
s= fis.read(); OutputStream os;
while(s>-1) {
os.write(s);
s=fis.read();
}
os.close();
break;
case JDBC_STREAM: switch(method)
File f = new File(file); {
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); case LOOP:
ps.setBinaryStream(1,fis,(int) f.length()); buf = new byte[2048];
ps.execute(); t=0;
break; while((s=fis.read(buf,0,buf.length))>0) {
t+=s;
blob.write(buf,0,s);
}
break;
default: case NATIVE_STREAM:
assert("Unknown method in uploadFile",false); os = blob.getOutputStream();
} s= fis.read();
while(s>-1) {
os.write(s);
s=fis.read();
}
os.close();
break;
blob.close(); case JDBC_STREAM:
fis.close(); File f = new File(file);
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testblob", "?"));
ps.setBinaryStream(1,fis,(int) f.length());
ps.execute();
break;
// Insert into the table default:
Statement st = con.createStatement(); assertTrue("Unknown method in uploadFile",false);
st.executeUpdate(JDBC2Tests.insert("id,lo","'"+file+"',"+oid)); }
con.commit();
st.close();
return oid; blob.close();
} fis.close();
/** // Insert into the table
* Helper - compares the blobs in a table with a local file. Note this alone Statement st = con.createStatement();
* tests the InputStream methods! st.executeUpdate(JDBC2Tests.insertSQL("testblob", "id,lo","'"+file+"',"+oid));
*/ con.commit();
private boolean compareBlobs(Connection con) throws Exception { st.close();
boolean result=true;
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); return oid;
}
Statement st = con.createStatement(); /**
ResultSet rs = st.executeQuery(JDBC2Tests.select("id,lo")); * Helper - compares the blobs in a table with a local file. Note this alone
assert(rs!=null); * tests the InputStream methods!
*/
private boolean compareBlobs() throws Exception {
boolean result=true;
while(rs.next()) { LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
String file = rs.getString(1);
int oid = rs.getInt(2);
FileInputStream fis = new FileInputStream(file); Statement st = con.createStatement();
LargeObject blob = lom.open(oid); ResultSet rs = st.executeQuery(JDBC2Tests.selectSQL("testblob", "id,lo"));
InputStream bis = blob.getInputStream(); assertNotNull(rs);
int f=fis.read(); while(rs.next()) {
int b=bis.read(); String file = rs.getString(1);
int c=0; int oid = rs.getInt(2);
while(f>=0 && b>=0 & result) {
result=(f==b);
f=fis.read();
b=bis.read();
c++;
}
result=result && f==-1 && b==-1;
if(!result) FileInputStream fis = new FileInputStream(file);
System.out.println("\nBlob compare failed at "+c+" of "+blob.size()); LargeObject blob = lom.open(oid);
InputStream bis = blob.getInputStream();
blob.close(); int f=fis.read();
fis.close(); int b=bis.read();
} int c=0;
rs.close(); while(f>=0 && b>=0 & result) {
st.close(); result=(f==b);
f=fis.read();
b=bis.read();
c++;
}
result=result && f==-1 && b==-1;
return result; if(!result)
} System.out.println("\nBlob compare failed at "+c+" of "+blob.size());
blob.close();
fis.close();
}
rs.close();
st.close();
return result;
}
} }

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,114 +13,116 @@ import java.sql.*;
*/ */
public class DateTest extends TestCase { public class DateTest extends TestCase {
public DateTest(String name) { private Connection con;
super(name);
} public DateTest(String name) {
super(name);
}
/** protected void setUp() throws Exception {
* Tests the time methods in ResultSet con = JDBC2Tests.openDB();
*/ JDBC2Tests.createTable(con, "testdate", "dt date");
public void testGetDate() { }
try {
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement(); protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testdate");
JDBC2Tests.closeDB(con);
}
/**
* Tests the time methods in ResultSet
*/
public void testGetDate() {
try {
Statement stmt = 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'")); } catch(Exception ex) {
st.executeUpdate(JDBC2Tests.insert("'2001-02-13'")); fail(ex.getMessage());
}
}
// Fall through helper /**
checkTimeTest(con,st); * Tests the time methods in PreparedStatement
*/
public void testSetDate() {
try {
Statement stmt = con.createStatement();
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testdate", "?"));
st.close(); ps.setDate(1, makeDate(1950, 2, 7));
assertEquals(1, ps.executeUpdate());
JDBC2Tests.closeDB(con); ps.setDate(1, makeDate(1970, 6, 2));
} catch(Exception ex) { assertEquals(1, ps.executeUpdate());
assert(ex.getMessage(),false);
}
}
/** ps.setDate(1, makeDate(1999, 8, 11));
* Tests the time methods in PreparedStatement assertEquals(1, ps.executeUpdate());
*/
public void testSetDate() {
try {
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement(); ps.setDate(1, makeDate(2001, 2, 13));
assertEquals(1, ps.executeUpdate());
JDBC2Tests.createTable(con,"dt date"); ps.close();
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); // Fall through helper
dateTest();
ps.setDate(1,getDate(1950,2,7)); assertEquals(4, stmt.executeUpdate("DELETE FROM testdate"));
assert(!ps.execute()); // false as its an update! stmt.close();
} catch(Exception ex) {
fail(ex.getMessage());
}
}
ps.setDate(1,getDate(1970,6,2)); /**
assert(!ps.execute()); // false as its an update! * Helper for the date tests. It tests what should be in the db
*/
private void dateTest() throws SQLException {
Statement st = con.createStatement();
ResultSet rs;
java.sql.Date d;
ps.setDate(1,getDate(1999,8,11)); rs = st.executeQuery(JDBC2Tests.selectSQL("testdate", "dt"));
assert(!ps.execute()); // false as its an update! assertNotNull(rs);
ps.setDate(1,getDate(2001,2,13)); assertTrue(rs.next());
assert(!ps.execute()); // false as its an update! d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1950, 2, 7));
// Fall through helper assertTrue(rs.next());
checkTimeTest(con,st); d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1970, 6, 2));
ps.close(); assertTrue(rs.next());
st.close(); d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1999, 8, 11));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(2001, 2, 13));
JDBC2Tests.closeDB(con); assertTrue(!rs.next());
} catch(Exception ex) {
assert(ex.getMessage(),false);
}
}
/** rs.close();
* Helper for the TimeTests. It tests what should be in the db st.close();
*/ }
private void checkTimeTest(Connection con,Statement st) throws SQLException {
ResultSet rs=null;
java.sql.Date t=null;
rs=st.executeQuery(JDBC2Tests.select("dt"));
assert(rs!=null);
assert(rs.next());
t = rs.getDate(1);
assert(t!=null);
assert(t.equals(getDate(1950,2,7)));
assert(rs.next());
t = rs.getDate(1);
assert(t!=null);
assert(t.equals(getDate(1970,6,2)));
assert(rs.next());
t = rs.getDate(1);
assert(t!=null);
assert(t.equals(getDate(1999,8,11)));
assert(rs.next());
t = rs.getDate(1);
assert(t!=null);
assert(t.equals(getDate(2001,2,13)));
assert(!rs.next());
rs.close();
}
/**
* Yes this is ugly, but it gets the test done ;-)
*/
private java.sql.Date getDate(int y,int m,int d) {
return java.sql.Date.valueOf(JDBC2Tests.fix(y,4)+"-"+JDBC2Tests.fix(m,2)+"-"+JDBC2Tests.fix(d,2));
}
private java.sql.Date makeDate(int y, int m, int d) {
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,11 +31,8 @@ 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");
JDBC2Tests.closeDB(con);
stmt.executeUpdate("DROP TABLE test_c");
stmt.close();
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,111 +13,96 @@ import java.sql.*;
*/ */
public class TimeTest extends TestCase { public class TimeTest extends TestCase {
public TimeTest(String name) { private Connection con;
super(name);
} public TimeTest(String name) {
super(name);
/**
* Tests the time methods in ResultSet
*/
public void testGetTime() {
try {
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement();
JDBC2Tests.createTable(con,"tm time");
st.executeUpdate(JDBC2Tests.insert("'01:02:03'"));
st.executeUpdate(JDBC2Tests.insert("'23:59:59'"));
// Fall through helper
checkTimeTest(con,st);
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) {
assert(ex.getMessage(),false);
} }
}
/** protected void setUp() throws Exception {
* Tests the time methods in PreparedStatement con = JDBC2Tests.openDB();
*/ JDBC2Tests.createTable(con, "testtime", "tm time");
public void testSetTime() {
try {
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement();
JDBC2Tests.createTable(con,"tm time");
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
checkTimeTest(con,st);
ps.close();
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) {
assert(ex.getMessage(),false);
} }
}
/** protected void tearDown() throws Exception {
* Helper for the TimeTests. It tests what should be in the db JDBC2Tests.dropTable(con, "testtime");
*/ JDBC2Tests.closeDB(con);
private void checkTimeTest(Connection con,Statement st) throws SQLException { }
ResultSet rs=null;
Time t=null; /**
* Tests the time methods in ResultSet
*/
public void testGetTime() {
try {
Statement stmt = con.createStatement();
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'01:02:03'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtime", "'23:59:59'")));
rs=st.executeQuery(JDBC2Tests.select("tm")); // Fall through helper
assert(rs!=null); timeTest();
assert(rs.next()); assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
t = rs.getTime(1); stmt.close();
assert(t!=null); } catch(Exception ex) {
assert(getHours(t)==1); fail(ex.getMessage());
assert(getMinutes(t)==2); }
assert(getSeconds(t)==3); }
assert(rs.next()); /**
t = rs.getTime(1); * Tests the time methods in PreparedStatement
assert(t!=null); */
assert(getHours(t)==23); public void testSetTime() {
assert(getMinutes(t)==59); try {
assert(getSeconds(t)==59); PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testtime", "?"));
Statement stmt = con.createStatement();
assert(!rs.next()); ps.setTime(1, makeTime(1, 2, 3));
assertEquals(1, ps.executeUpdate());
rs.close(); ps.setTime(1, makeTime(23, 59, 59));
} assertEquals(1, ps.executeUpdate());
/** // Fall through helper
* These implement depreciated methods in java.sql.Time timeTest();
*/
private static long getHours(Time t) {
return (t.getTime() % JDBC2Tests.DAYMILLIS)/3600000;
}
private static long getMinutes(Time t) { assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
return ((t.getTime() % JDBC2Tests.DAYMILLIS)/60000)%60; stmt.close();
} ps.close();
} catch(Exception ex) {
fail(ex.getMessage());
}
}
private static long getSeconds(Time t) { /**
return ((t.getTime() % JDBC2Tests.DAYMILLIS)/1000)%60; * Helper for the TimeTests. It tests what should be in the db
} */
private void timeTest() throws SQLException {
Statement st = con.createStatement();
ResultSet rs;
java.sql.Time t;
private Time getTime(int h,int m,int s) { rs = st.executeQuery(JDBC2Tests.selectSQL("testtime", "tm"));
return new Time(1000*(s+(m*60)+(h*3600))); assertNotNull(rs);
}
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(1, 2, 3), t);
assertTrue(rs.next());
t = rs.getTime(1);
assertNotNull(t);
assertEquals(makeTime(23, 59, 59), t);
assertTrue(! rs.next());
rs.close();
}
private java.sql.Time makeTime(int h, int m, int s) {
return java.sql.Time.valueOf(JDBC2Tests.fix(h, 2) + ":" +
JDBC2Tests.fix(m, 2) + ":" +
JDBC2Tests.fix(s, 2));
}
} }

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,123 +15,118 @@ import java.sql.*;
*/ */
public class TimestampTest extends TestCase { public class TimestampTest extends TestCase {
public TimestampTest(String name) { private Connection con;
super(name);
}
/** public TimestampTest(String name) {
* Tests the time methods in ResultSet super(name);
*/ }
public void testGetTimestamp() {
try {
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement(); protected void setUp() throws Exception {
con = JDBC2Tests.openDB();
Statement stmt = con.createStatement();
JDBC2Tests.createTable(con, "testtimestamp", "ts timestamp");
}
JDBC2Tests.createTable(con,"ts timestamp"); protected void tearDown() throws Exception {
JDBC2Tests.dropTable(con, "testtimestamp");
JDBC2Tests.closeDB(con);
}
st.executeUpdate(JDBC2Tests.insert("'1950-02-07 15:00:00'")); /**
* Tests the time methods in ResultSet
*/
public void testGetTimestamp() {
try {
Statement stmt = con.createStatement();
// Before you ask why 8:13:00 and not 7:13:00, this is a problem with the assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
// getTimestamp method in this TestCase. It's simple, brain-dead. It "'1950-02-07 15:00:00'")));
// 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'")); assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", "'" +
getTimestamp(1970, 6, 2, 8, 13, 0, 0).toString() +
"'")));
// Fall through helper assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
checkTimeTest(con,st); "'1970-06-02 08:13:00'")));
st.close(); // Fall through helper
timestampTest();
JDBC2Tests.closeDB(con); assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp"));
} catch(Exception ex) {
assert(ex.getMessage(),false);
}
}
/** stmt.close();
* Tests the time methods in PreparedStatement } catch(Exception ex) {
*/ fail(ex.getMessage());
public void testSetTimestamp() { }
try { }
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement(); /**
* Tests the time methods in PreparedStatement
*/
public void testSetTimestamp() {
try {
Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement(JDBC2Tests.insertSQL("testtimestamp", "?"));
JDBC2Tests.createTable(con,"ts timestamp"); pstmt.setTimestamp(1, getTimestamp(1950, 2, 7, 15, 0, 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)); pstmt.setTimestamp(1, getTimestamp(1970, 6, 2, 8, 13, 0, 0));
assert(!ps.execute()); // false as its an update! assertEquals(1, pstmt.executeUpdate());
// Before you ask why 8:13:00 and not 7:13:00, this is a problem with the // Fall through helper
// getTimestamp method in this TestCase. It's simple, brain-dead. It timestampTest();
// 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 assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp"));
checkTimeTest(con,st);
ps.close(); pstmt.close();
st.close(); stmt.close();
} catch(Exception ex) {
fail(ex.getMessage());
}
}
JDBC2Tests.closeDB(con); /**
} catch(Exception ex) { * Helper for the TimeTests. It tests what should be in the db
assert(ex.getMessage(),false); */
} private void timestampTest() throws SQLException {
} Statement stmt = con.createStatement();
ResultSet rs;
java.sql.Timestamp t;
/** rs = stmt.executeQuery(JDBC2Tests.selectSQL("testtimestamp", "ts"));
* Helper for the TimeTests. It tests what should be in the db assertNotNull(rs);
*/
private void checkTimeTest(Connection con,Statement st) throws SQLException {
ResultSet rs=null;
java.sql.Timestamp t=null;
rs=st.executeQuery(JDBC2Tests.select("ts")); assertTrue(rs.next());
assert(rs!=null); t = rs.getTimestamp(1);
assertNotNull(t);
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);
assert(t.equals(getTimestamp(1950,2,7,15,0,0))); assertTrue(t.equals(getTimestamp(1970, 6, 2, 8, 13, 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)));
assertTrue(! rs.next()); // end of table. Fail if more entries exist.
// Seems Daylight saving is ignored? rs.close();
assert(t.equals(getTimestamp(1970,6,2,8,13,0))); stmt.close();
}
assert(!rs.next()); // end of table. Fail if more entries exist.
rs.close();
}
/**
* These implement depreciated methods in java.sql.Time
*/
private static final long dayms = 24*3600*1000;
/**
* Yes this is ugly, but it gets the test done ;-)
*
* 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));
}
private java.sql.Timestamp getTimestamp(int y, int m, int d, int h, int mn, int se, int f) {
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) + "." +
} }