From b75814aee320ef2b67ad01ba72c266dbbf94db45 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Sun, 23 Sep 2001 04:11:14 +0000 Subject: [PATCH] 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 --- src/interfaces/jdbc/build.xml | 3 +- .../jdbc/org/postgresql/test/JDBC2Tests.java | 354 +++++++++--------- .../org/postgresql/test/jdbc2/ANTTest.java | 10 +- .../test/jdbc2/BatchExecuteTest.java | 50 +-- .../org/postgresql/test/jdbc2/BlobTest.java | 323 ++++++++-------- .../postgresql/test/jdbc2/ConnectionTest.java | 134 ++++--- .../org/postgresql/test/jdbc2/DateTest.java | 224 +++++------ .../org/postgresql/test/jdbc2/DriverTest.java | 31 +- .../postgresql/test/jdbc2/EncodingTest.java | 6 +- .../postgresql/test/jdbc2/JBuilderTest.java | 13 +- .../org/postgresql/test/jdbc2/MiscTest.java | 6 +- .../org/postgresql/test/jdbc2/TimeTest.java | 177 ++++----- .../postgresql/test/jdbc2/TimestampTest.java | 235 ++++++------ 13 files changed, 759 insertions(+), 807 deletions(-) diff --git a/src/interfaces/jdbc/build.xml b/src/interfaces/jdbc/build.xml index 114d05813b..4865a9a78f 100644 --- a/src/interfaces/jdbc/build.xml +++ b/src/interfaces/jdbc/build.xml @@ -4,7 +4,7 @@ build file to allow ant (http://jakarta.apache.org/ant/) to be used 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 @@ - diff --git a/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java b/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java index 37565cfcc3..76f8e368c5 100644 --- a/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java +++ b/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java @@ -10,206 +10,188 @@ import java.sql.*; * Executes all known tests for JDBC2 and includes some utility methods. */ public class JDBC2Tests extends TestSuite { - /** - * Returns the Test database JDBC URL - */ - public static String getURL() { - return System.getProperty("database"); - } - - /** - * Returns the Postgresql username - */ - public static String getUser() { - return System.getProperty("username"); - } - - /** - * Returns the user's password - */ - public static String getPassword() { - return System.getProperty("password"); - } - - /** - * helper - opens a connection. Static so other classes can call it. - */ - public static java.sql.Connection openDB() { - try { - Class.forName("org.postgresql.Driver"); - return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); - } catch(ClassNotFoundException ex) { - TestCase.assert(ex.getMessage(),false); - } catch(SQLException ex) { - TestCase.assert(ex.getMessage(),false); - } - return null; - } - - /** - * Helper - closes an open connection. This rewrites SQLException to a failed - * assertion. It's static so other classes can use it. - */ - public static void closeDB(Connection conn) { - try { - if(conn!=null) - conn.close(); - } catch(SQLException ex) { - TestCase.assert(ex.getMessage(),false); - } - } + /** + * Returns the Test database JDBC URL + */ + public static String getURL() { + return System.getProperty("database"); + } + + /** + * Returns the Postgresql username + */ + public static String getUser() { + return System.getProperty("username"); + } + + /** + * Returns the user's password + */ + public static String getPassword() { + return System.getProperty("password"); + } + + /** + * Helper - opens a connection. + */ + public static java.sql.Connection openDB() { + try { + Class.forName("org.postgresql.Driver"); + return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); + } catch(ClassNotFoundException ex) { + TestCase.fail(ex.getMessage()); + } catch(SQLException ex) { + TestCase.fail(ex.getMessage()); + } + return null; + } + + /** + * Helper - closes an open connection. This rewrites SQLException to a failed + * assertion. It's static so other classes can use it. + */ + public static void closeDB(Connection con) { + try { + if (con != null) + con.close(); + } catch (SQLException ex) { + TestCase.fail(ex.getMessage()); + } + } /** * Helper - creates a test table for use by a test - */ - public static void createTable( - Connection conn, String table, String columns) { + */ + public static void createTable(Connection con, + String table, + String columns) { try { - Statement st = conn.createStatement(); + Statement st = con.createStatement(); try { - try { - st.executeUpdate("drop table " + table); - } catch(SQLException se) { - // Intentionally ignore exception - } + // Drop the table + dropTable(con, table); // Now create the table - st.executeUpdate( "create table " + table + " (" + columns + - ")" ); + st.executeUpdate("create table " + table + " (" + columns + ")"); } finally { st.close(); } } catch(SQLException ex) { - TestCase.assert(ex.getMessage(),false); + TestCase.fail(ex.getMessage()); + } + } + + /** + * Helper - drops a table + */ + public static void dropTable(Connection con, String table) { + try { + Statement stmt = con.createStatement(); + try { + stmt.executeUpdate("DROP TABLE " + table); + } catch (SQLException ex) { + // ignore + } + } catch (SQLException ex) { + 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 - // this test suite intended to specify all test table names via the - // properties, but this was never fully implemented. - public static void createTable(Connection conn, String columns) { - createTable(conn, getTableName(), columns); + /** + * Helper - generates INSERT SQL - very simple + */ + public static String insertSQL(String table, String values) { + return insertSQL(table, null, values); } + + public static String insertSQL(String table, String columns, String values) { + String s = "INSERT INTO " + table; + + if (columns != null) + s = s + " (" + columns + ")"; + + return s + " VALUES (" + values + ")"; + } + + /** + * Helper - generates SELECT SQL - very simple + */ + public static String selectSQL(String table, String columns) { + return selectSQL(table, columns, null, null); + } + + public static String selectSQL(String table, String columns, String where) { + return selectSQL(table, columns, where, null); + } + + public static String selectSQL(String table, String columns, String where, String other) { + String s = "SELECT " + columns + " FROM " + table; + + if (where != null) + 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); + } + + /** + * The main entry point for JUnit + */ + public static TestSuite suite() { + TestSuite suite= new TestSuite(); - /** - * Helper - generates INSERT SQL - very simple - */ - public static String insert(String values) { - return insert(null,values); - } - public static String insert(String columns,String values) { - String s = "INSERT INTO "+getTableName(); - if(columns!=null) - s=s+" ("+columns+")"; - return s+" VALUES ("+values+")"; - } - - /** - * Helper - generates SELECT SQL - very simple - */ - public static String select(String columns) { - return select(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; - } - - /** - * 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... - * @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); - } - - /** - * Number of milliseconds in a day - */ - public static final long DAYMILLIS = 24*3600*1000; - - /** - * The main entry point for JUnit - */ - public static TestSuite suite() { - TestSuite suite= new TestSuite(); - - // - // 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 - // being sent to the suite. It also initialises the database (if required) - // with some simple global tables (will make each testcase use its own later). - // - suite.addTestSuite(ANTTest.class); - - // Basic Driver internals - suite.addTestSuite(DriverTest.class); - suite.addTestSuite(ConnectionTest.class); - suite.addTestSuite(DatabaseMetaDataTest.class); - suite.addTestSuite(EncodingTest.class); - - // Connectivity/Protocols - - // ResultSet - suite.addTestSuite(DateTest.class); - suite.addTestSuite(TimeTest.class); - suite.addTestSuite(TimestampTest.class); - - // PreparedStatement - suite.addTestSuite(BatchExecuteTest.class); - - // BatchExecute - - - // MetaData - - // 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; - } + // + // 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 + // being sent to the suite. It also initialises the database (if required) + // with some simple global tables (will make each testcase use its own later). + // + suite.addTestSuite(ANTTest.class); + + // Basic Driver internals + suite.addTestSuite(DriverTest.class); + suite.addTestSuite(ConnectionTest.class); + suite.addTestSuite(DatabaseMetaDataTest.class); + suite.addTestSuite(EncodingTest.class); + + // Connectivity/Protocols + + // ResultSet + + // Time, Date, Timestamp + suite.addTestSuite(DateTest.class); + suite.addTestSuite(TimeTest.class); + suite.addTestSuite(TimestampTest.class); + + // PreparedStatement + + // BatchExecute + suite.addTestSuite(BatchExecuteTest.class); + + // MetaData + + // 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; + } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java index 349c5bcf7d..b9b3524c20 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java @@ -16,11 +16,11 @@ public class ANTTest extends TestCase { String usr=System.getProperty("username"); String psw=System.getProperty("password"); - assert(url!=null); - assert(usr!=null); - assert(psw!=null); + assertNotNull(url); + assertNotNull(usr); + assertNotNull(psw); - assert(!url.equals("")); - assert(!usr.equals("")); + assertTrue(! url.equals("")); + assertTrue(! usr.equals("")); } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java index 783bf7b67f..d66de43dc3 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java @@ -4,13 +4,17 @@ import org.postgresql.test.JDBC2Tests; import junit.framework.TestCase; 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() */ public class BatchExecuteTest extends TestCase { private Connection con; - private Statement stmt; public BatchExecuteTest(String name) { super(name); @@ -20,20 +24,13 @@ public class BatchExecuteTest extends TestCase { // a table for this test. protected void setUp() throws Exception { con = JDBC2Tests.openDB(); - stmt = con.createStatement(); + Statement stmt = con.createStatement(); // Drop the test table if it already exists for some reason. It is // not an error if it doesn't exist. - try { - 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)"); + JDBC2Tests.createTable(con, "testbatch", "pk INTEGER, col1 INTEGER"); + + stmt.executeUpdate("INSERT INTO testbatch VALUES (1, 0)"); // Generally recommended with batch updates. By default we run all // 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. protected void tearDown() throws Exception { con.setAutoCommit(true); - if (stmt != null) { - stmt.executeUpdate("DROP TABLE testbatch"); - stmt.close(); - } - if (con != null) { - JDBC2Tests.closeDB(con); - } + + JDBC2Tests.dropTable(con, "testbatch"); + JDBC2Tests.closeDB(con); } public void testSupportsBatchUpdates() throws Exception { @@ -75,6 +68,7 @@ public class BatchExecuteTest extends TestCase { } public void testExecuteEmptyBatch() throws Exception { + Statement stmt = con.createStatement(); int[] updateCount = stmt.executeBatch(); assertEquals(0,updateCount.length); @@ -82,9 +76,12 @@ public class BatchExecuteTest extends TestCase { stmt.clearBatch(); updateCount = stmt.executeBatch(); assertEquals(0,updateCount.length); + stmt.close(); } public void testClearBatch() throws Exception { + Statement stmt = con.createStatement(); + stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); assertCol1HasValue(0); stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); @@ -97,9 +94,13 @@ public class BatchExecuteTest extends TestCase { assertCol1HasValue(4); con.commit(); assertCol1HasValue(4); + + stmt.close(); } public void testSelectThrowsException() throws Exception { + Statement stmt = con.createStatement(); + stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); stmt.addBatch("SELECT col1 FROM testbatch 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 " + "a generic SQLException: " + e); } + + stmt.close(); } public void testPreparedStatement() throws Exception { @@ -151,6 +154,8 @@ public class BatchExecuteTest extends TestCase { /** */ 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 + 2 WHERE pk = 1"); stmt.executeBatch(); @@ -174,10 +179,7 @@ public class BatchExecuteTest extends TestCase { assertCol1HasValue(12); con.rollback(); 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 - */ diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java index 28a61044cf..93e25abe2e 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java @@ -8,7 +8,7 @@ import java.sql.*; 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 * help prevent previous problems from re-occuring ;-) @@ -16,169 +16,168 @@ import org.postgresql.largeobject.*; */ public class BlobTest extends TestCase { - public BlobTest(String name) { - super(name); - } + private Connection con; - /** - * The table format used by this TestCase - */ - private static final String BLOB_TABLE_FMT = "id name,lo oid"; + 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 - /** - * Tests one method of uploading a blob to the database - */ - 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); - } - } - - /** - * Tests one method of uploading a blob to the database - */ - public void testUploadBlob_NATIVE() { - try { - Connection con = JDBC2Tests.openDB(); - - JDBC2Tests.createTable(con,BLOB_TABLE_FMT); - - con.setAutoCommit(false); - assert(!con.getAutoCommit()); - - assert(uploadFile(con,"build.xml",NATIVE_STREAM)>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); - } - } - - 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 - * because it always works, and we can use it as a base to test the new - * methods. - */ - private int uploadFile(Connection con,String file,int method) throws Exception { - LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); - - FileInputStream fis = new FileInputStream(file); - - int oid = lom.create(LargeObjectManager.READWRITE); - LargeObject blob = lom.open(oid); - - int s,t; - byte buf[]; - OutputStream os; - - switch(method) - { - 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: - os = blob.getOutputStream(); - s= fis.read(); - while(s>-1) { - os.write(s); - s=fis.read(); - } - os.close(); - break; - - case JDBC_STREAM: - File f = new File(file); - PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); - ps.setBinaryStream(1,fis,(int) f.length()); - ps.execute(); - break; - - default: - assert("Unknown method in uploadFile",false); - } - - blob.close(); - fis.close(); - - // Insert into the table - Statement st = con.createStatement(); - st.executeUpdate(JDBC2Tests.insert("id,lo","'"+file+"',"+oid)); - con.commit(); - st.close(); - - return oid; - } - - /** - * Helper - compares the blobs in a table with a local file. Note this alone - * tests the InputStream methods! - */ - private boolean compareBlobs(Connection con) throws Exception { - boolean result=true; - - LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery(JDBC2Tests.select("id,lo")); - assert(rs!=null); - - while(rs.next()) { - String file = rs.getString(1); - int oid = rs.getInt(2); - - FileInputStream fis = new FileInputStream(file); - LargeObject blob = lom.open(oid); - InputStream bis = blob.getInputStream(); - - int f=fis.read(); - int b=bis.read(); - int c=0; - while(f>=0 && b>=0 & result) { - result=(f==b); - f=fis.read(); - b=bis.read(); - c++; - } - result=result && f==-1 && b==-1; - - if(!result) - System.out.println("\nBlob compare failed at "+c+" of "+blob.size()); - - blob.close(); - fis.close(); + public BlobTest(String name) { + super(name); } - rs.close(); - st.close(); - return result; - } + protected void setUp() throws Exception { + con = JDBC2Tests.openDB(); + JDBC2Tests.createTable(con, "testblob", "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 + */ + public void testUploadBlob_LOOP() { + try { + con.setAutoCommit(false); + assertTrue(!con.getAutoCommit()); + + assertTrue(uploadFile("build.xml", LOOP) > 0); + + // Now compare the blob & the file. Note this actually tests the + // InputStream implementation! + assertTrue(compareBlobs()); + + con.setAutoCommit(true); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * Tests one method of uploading a blob to the database + */ + public void testUploadBlob_NATIVE() { + try { + con.setAutoCommit(false); + assertTrue(!con.getAutoCommit()); + + assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0); + + // Now compare the blob & the file. Note this actually tests the + // InputStream implementation! + assertTrue(compareBlobs()); + + con.setAutoCommit(true); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * 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(); + + FileInputStream fis = new FileInputStream(file); + + int oid = lom.create(LargeObjectManager.READWRITE); + LargeObject blob = lom.open(oid); + + int s,t; + byte buf[]; + OutputStream os; + + switch(method) + { + 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: + os = blob.getOutputStream(); + s= fis.read(); + while(s>-1) { + os.write(s); + s=fis.read(); + } + os.close(); + break; + + case JDBC_STREAM: + File f = new File(file); + PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testblob", "?")); + ps.setBinaryStream(1,fis,(int) f.length()); + ps.execute(); + break; + + default: + assertTrue("Unknown method in uploadFile",false); + } + + blob.close(); + fis.close(); + + // Insert into the table + Statement st = con.createStatement(); + st.executeUpdate(JDBC2Tests.insertSQL("testblob", "id,lo","'"+file+"',"+oid)); + con.commit(); + st.close(); + + return oid; + } + + /** + * Helper - compares the blobs in a table with a local file. Note this alone + * tests the InputStream methods! + */ + private boolean compareBlobs() throws Exception { + boolean result=true; + + LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI(); + + Statement st = con.createStatement(); + ResultSet rs = st.executeQuery(JDBC2Tests.selectSQL("testblob", "id,lo")); + assertNotNull(rs); + + while(rs.next()) { + String file = rs.getString(1); + int oid = rs.getInt(2); + + FileInputStream fis = new FileInputStream(file); + LargeObject blob = lom.open(oid); + InputStream bis = blob.getInputStream(); + + int f=fis.read(); + int b=bis.read(); + int c=0; + while(f>=0 && b>=0 & result) { + result=(f==b); + f=fis.read(); + b=bis.read(); + c++; + } + result=result && f==-1 && b==-1; + + if(!result) + System.out.println("\nBlob compare failed at "+c+" of "+blob.size()); + + blob.close(); + fis.close(); + } + rs.close(); + st.close(); + + return result; + } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java index 8d52bccce2..90136a506e 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java @@ -10,7 +10,7 @@ import java.sql.*; * * 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 { @@ -26,11 +26,8 @@ public class ConnectionTest extends TestCase { protected void setUp() throws Exception { Connection con = JDBC2Tests.openDB(); - JDBC2Tests.createTable( con, "test_a", - "imagename name,image oid,id int4" ); - - JDBC2Tests.createTable( con, "test_c", - "source text,cost money,imageid int4" ); + JDBC2Tests.createTable(con, "test_a", "imagename name,image oid,id int4"); + JDBC2Tests.createTable(con, "test_c", "source text,cost money,imageid int4"); JDBC2Tests.closeDB(con); } @@ -38,11 +35,10 @@ public class ConnectionTest extends TestCase { // Tear down the fixture for this test case. protected void tearDown() throws Exception { Connection con = JDBC2Tests.openDB(); - Statement stmt = con.createStatement(); - stmt.executeUpdate("DROP TABLE test_a"); - stmt.executeUpdate("DROP TABLE test_c"); - stmt.close(); + JDBC2Tests.dropTable(con, "test_a"); + JDBC2Tests.dropTable(con, "test_c"); + JDBC2Tests.closeDB(con); } @@ -55,16 +51,16 @@ public class ConnectionTest extends TestCase { // A standard Statement java.sql.Statement stat = conn.createStatement(); - assert(stat!=null); + assertNotNull(stat); stat.close(); // Ask for Updateable ResultSets stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); - assert(stat!=null); + assertNotNull(stat); stat.close(); } catch(SQLException ex) { - assert(ex.getMessage(),false); + assertTrue(ex.getMessage(),false); } } @@ -79,16 +75,16 @@ public class ConnectionTest extends TestCase { // A standard Statement java.sql.PreparedStatement stat = conn.prepareStatement(sql); - assert(stat!=null); + assertNotNull(stat); stat.close(); // Ask for Updateable ResultSets stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); - assert(stat!=null); + assertNotNull(stat); stat.close(); } catch(SQLException ex) { - assert(ex.getMessage(),false); + assertTrue(ex.getMessage(),false); } } @@ -116,11 +112,11 @@ public class ConnectionTest extends TestCase { // Turn it off con.setAutoCommit(false); - assert(!con.getAutoCommit()); + assertTrue(!con.getAutoCommit()); // Turn it back on con.setAutoCommit(true); - assert(con.getAutoCommit()); + assertTrue(con.getAutoCommit()); // Now test commit st = con.createStatement(); @@ -132,21 +128,21 @@ public class ConnectionTest extends TestCase { st.executeUpdate("update test_a set image=9876 where id=5678"); con.commit(); rs = st.executeQuery("select image from test_a where id=5678"); - assert(rs.next()); - assert(rs.getInt(1)==9876); + assertTrue(rs.next()); + assertEquals(9876, rs.getInt(1)); rs.close(); // Now try to change it but rollback st.executeUpdate("update test_a set image=1111 where id=5678"); con.rollback(); rs = st.executeQuery("select image from test_a where id=5678"); - assert(rs.next()); - assert(rs.getInt(1)==9876); // Should not change! + assertTrue(rs.next()); + assertEquals(9876, rs.getInt(1)); // Should not change! rs.close(); JDBC2Tests.closeDB(con); } catch(SQLException ex) { - assert(ex.getMessage(),false); + assertTrue(ex.getMessage(),false); } } @@ -158,15 +154,15 @@ public class ConnectionTest extends TestCase { Connection con = JDBC2Tests.openDB(); // Should not say closed - assert(!con.isClosed()); + assertTrue(!con.isClosed()); JDBC2Tests.closeDB(con); // Should now say closed - assert(con.isClosed()); + assertTrue(con.isClosed()); } 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"; // The connection must be ours! - assert(con instanceof org.postgresql.Connection); + assertTrue(con instanceof org.postgresql.Connection); // Clear any existing warnings con.clearWarnings(); @@ -190,16 +186,16 @@ public class ConnectionTest extends TestCase { // Retrieve it SQLWarning warning = con.getWarnings(); - assert(warning!=null); - assert(warning.getMessage().equals(testStr)); + assertNotNull(warning); + assertEquals(testStr, warning.getMessage()); // Finally test clearWarnings() this time there must be something to delete con.clearWarnings(); - assert(con.getWarnings()==null); + assertTrue(con.getWarnings()==null); JDBC2Tests.closeDB(con); } catch(SQLException ex) { - assert(ex.getMessage(),false); + assertTrue(ex.getMessage(),false); } } @@ -213,76 +209,72 @@ public class ConnectionTest extends TestCase { Connection con = JDBC2Tests.openDB(); // PostgreSQL defaults to READ COMMITTED - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); // Begin a transaction con.setAutoCommit(false); // The isolation level should not have changed - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); // 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 // inside the transaction, the isolation level of the current // transaction did not change. It affects only future transactions. // This behaviour is recommended by the JDBC spec. - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); // Begin a new transaction con.commit(); // Now we should see the new isolation level - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); // Repeat the steps above with the transition back to // READ COMMITTED. - con.setTransactionIsolation( - Connection.TRANSACTION_READ_COMMITTED ); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); + con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); con.commit(); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); // Now run some tests with autocommit enabled. con.setAutoCommit(true); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); - con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); + con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); - con.setTransactionIsolation( - Connection.TRANSACTION_READ_COMMITTED ); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation()); // Test if a change of isolation level before beginning the // transaction affects the isolation level inside the transaction. - con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); + con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); con.setAutoCommit(false); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); con.setAutoCommit(true); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_SERIALIZABLE ); - con.setTransactionIsolation( - Connection.TRANSACTION_READ_COMMITTED ); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_SERIALIZABLE, + con.getTransactionIsolation()); + con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); con.setAutoCommit(false); - assertEquals( con.getTransactionIsolation(), - Connection.TRANSACTION_READ_COMMITTED ); + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); JDBC2Tests.closeDB(con); } @@ -305,15 +297,15 @@ public class ConnectionTest extends TestCase { // now change it for an empty one java.util.Map newmap = new java.util.HashMap(); con.setTypeMap(newmap); - assert(con.getTypeMap()==newmap); + assertEquals(newmap, con.getTypeMap()); // restore the old one con.setTypeMap(oldmap); - assert(con.getTypeMap()==oldmap); + assertEquals(oldmap, con.getTypeMap()); JDBC2Tests.closeDB(con); } catch(SQLException ex) { - assert(ex.getMessage(),false); + assertTrue(ex.getMessage(),false); } } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java index 5150cd7f41..48d5ba5d21 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java @@ -5,7 +5,7 @@ import junit.framework.TestCase; 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 * help prevent previous problems from re-occuring ;-) @@ -13,114 +13,116 @@ import java.sql.*; */ public class DateTest extends TestCase { - public DateTest(String name) { - super(name); - } - - /** - * Tests the time methods in ResultSet - */ - public void testGetDate() { - try { - Connection con = JDBC2Tests.openDB(); - - Statement st=con.createStatement(); - - JDBC2Tests.createTable(con,"dt date"); - - st.executeUpdate(JDBC2Tests.insert("'1950-02-07'")); - st.executeUpdate(JDBC2Tests.insert("'1970-06-02'")); - 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) { - assert(ex.getMessage(),false); - } - } - - /** - * Tests the time methods in PreparedStatement - */ - public void testSetDate() { - try { - Connection con = JDBC2Tests.openDB(); - - Statement st=con.createStatement(); - - JDBC2Tests.createTable(con,"dt date"); - - PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); - - ps.setDate(1,getDate(1950,2,7)); - assert(!ps.execute()); // false as its an update! - - 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(); - st.close(); - - JDBC2Tests.closeDB(con); - } catch(Exception ex) { - assert(ex.getMessage(),false); - } - } - - /** - * Helper for the TimeTests. It tests what should be in the db - */ - 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 Connection con; + + public DateTest(String 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 + */ + 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'"))); + + /* dateTest() contains all of the tests */ + dateTest(); + + assertEquals(4, stmt.executeUpdate("DELETE FROM " + "testdate")); + stmt.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * Tests the time methods in PreparedStatement + */ + public void testSetDate() { + try { + Statement stmt = con.createStatement(); + PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testdate", "?")); + + ps.setDate(1, makeDate(1950, 2, 7)); + assertEquals(1, ps.executeUpdate()); + + ps.setDate(1, makeDate(1970, 6, 2)); + assertEquals(1, ps.executeUpdate()); + + ps.setDate(1, makeDate(1999, 8, 11)); + assertEquals(1, ps.executeUpdate()); + + ps.setDate(1, makeDate(2001, 2, 13)); + assertEquals(1, ps.executeUpdate()); + + ps.close(); + + // Fall through helper + dateTest(); + + assertEquals(4, stmt.executeUpdate("DELETE FROM testdate")); + stmt.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * 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; + + rs = st.executeQuery(JDBC2Tests.selectSQL("testdate", "dt")); + assertNotNull(rs); + + assertTrue(rs.next()); + d = rs.getDate(1); + assertNotNull(d); + assertEquals(d, makeDate(1950, 2, 7)); + + assertTrue(rs.next()); + d = rs.getDate(1); + assertNotNull(d); + assertEquals(d, makeDate(1970, 6, 2)); + + assertTrue(rs.next()); + 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)); + + assertTrue(!rs.next()); + + rs.close(); + st.close(); + } + + 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)); + } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java index 7337be46fe..cdd5f93cfa 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java @@ -5,7 +5,7 @@ import junit.framework.TestCase; 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 * @@ -25,21 +25,21 @@ public class DriverTest extends TestCase { // Load the driver (note clients should never do it this way!) org.postgresql.Driver drv = new org.postgresql.Driver(); - assert(drv!=null); + assertNotNull(drv); // These are always correct - assert(drv.acceptsURL("jdbc:postgresql:test")); - assert(drv.acceptsURL("jdbc:postgresql://localhost/test")); - assert(drv.acceptsURL("jdbc:postgresql://localhost:5432/test")); - assert(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:test")); + assertTrue(drv.acceptsURL("jdbc:postgresql://localhost/test")); + assertTrue(drv.acceptsURL("jdbc:postgresql://localhost:5432/test")); + assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1/anydbname")); + assertTrue(drv.acceptsURL("jdbc:postgresql://127.0.0.1:5433/hidden")); // Badly formatted url's - assert(!drv.acceptsURL("jdbc:postgres:test")); - assert(!drv.acceptsURL("postgresql:test")); + assertTrue(!drv.acceptsURL("jdbc:postgres:test")); + assertTrue(!drv.acceptsURL("postgresql:test")); } 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 con = DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); - assert(con!=null); + assertNotNull(con); con.close(); // Test with the username in the url con = DriverManager.getConnection(JDBC2Tests.getURL()+"?user="+JDBC2Tests.getUser()+"&password="+JDBC2Tests.getPassword()); - assert(con!=null); + assertNotNull(con); con.close(); - } catch(ClassNotFoundException ex) { - assert(ex.getMessage(),false); + fail(ex.getMessage()); } catch(SQLException ex) { - assert(ex.getMessage(),false); + fail(ex.getMessage()); } } -} \ No newline at end of file +} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java index 872708ace5..f1e893cc99 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java @@ -8,7 +8,7 @@ import java.io.*; /** * 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); assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase()); 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", Encoding.defaultEncoding(), Encoding.getEncoding("UNKNOWN", null)); 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); } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java index c114eb0c51..94282706c6 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java @@ -6,7 +6,7 @@ import java.sql.*; 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 * stay working @@ -31,11 +31,8 @@ public class JBuilderTest extends TestCase { // Tear down the fixture for this test case. protected void tearDown() throws Exception { Connection con = JDBC2Tests.openDB(); - Statement stmt = con.createStatement(); - - stmt.executeUpdate("DROP TABLE test_c"); - stmt.close(); - JDBC2Tests.closeDB(con); + JDBC2Tests.dropTable(con, "test_c"); + JDBC2Tests.closeDB(con); } /** @@ -47,7 +44,7 @@ public class JBuilderTest extends TestCase { Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select cost from test_c"); - assert(rs!=null); + assertNotNull(rs); while(rs.next()){ double bd = rs.getDouble(1); @@ -58,7 +55,7 @@ public class JBuilderTest extends TestCase { JDBC2Tests.closeDB(con); } catch(Exception ex) { - assert(ex.getMessage(),false); + fail(ex.getMessage()); } } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java index ecad2dc320..1e4e34fc84 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java @@ -5,7 +5,7 @@ import junit.framework.TestCase; 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 * help prevent previous problems from re-occuring ;-) @@ -30,7 +30,7 @@ public class MiscTest extends TestCase { Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select datname from pg_database"); - assert(rs!=null); + assertNotNull(rs); while(rs.next()){ String s = rs.getString(1); @@ -41,7 +41,7 @@ public class MiscTest extends TestCase { JDBC2Tests.closeDB(con); } catch(Exception ex) { - assert(ex.getMessage(),false); + fail(ex.getMessage()); } } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java index e48a35ce29..f4cbb3c3da 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java @@ -5,7 +5,7 @@ import junit.framework.TestCase; 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 * help prevent previous problems from re-occuring ;-) @@ -13,111 +13,96 @@ import java.sql.*; */ public class TimeTest extends TestCase { - 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(); + private Connection con; + + public TimeTest(String name) { + super(name); + } - JDBC2Tests.closeDB(con); - } catch(Exception ex) { - assert(ex.getMessage(),false); + protected void setUp() throws Exception { + con = JDBC2Tests.openDB(); + JDBC2Tests.createTable(con, "testtime", "tm time"); } - } - /** - * Tests the time methods in PreparedStatement - */ - public void testSetTime() { - try { - Connection con = JDBC2Tests.openDB(); + protected void tearDown() throws Exception { + JDBC2Tests.dropTable(con, "testtime"); + JDBC2Tests.closeDB(con); + } + + /** + * 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'"))); + + // Fall through helper + timeTest(); + + assertEquals(2, stmt.executeUpdate("DELETE FROM testtime")); + stmt.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } - Statement st=con.createStatement(); + /** + * Tests the time methods in PreparedStatement + */ + public void testSetTime() { + try { + PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testtime", "?")); + Statement stmt = con.createStatement(); + + ps.setTime(1, makeTime(1, 2, 3)); + assertEquals(1, ps.executeUpdate()); + + ps.setTime(1, makeTime(23, 59, 59)); + assertEquals(1, ps.executeUpdate()); + + // Fall through helper + timeTest(); + + assertEquals(2, stmt.executeUpdate("DELETE FROM testtime")); + stmt.close(); + ps.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } - JDBC2Tests.createTable(con,"tm time"); + /** + * 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; - PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); + rs = st.executeQuery(JDBC2Tests.selectSQL("testtime", "tm")); + assertNotNull(rs); - ps.setTime(1,getTime(1,2,3)); - assert(!ps.execute()); // false as its an update! + assertTrue(rs.next()); + t = rs.getTime(1); + assertNotNull(t); + assertEquals(makeTime(1, 2, 3), t); - ps.setTime(1,getTime(23,59,59)); - assert(!ps.execute()); // false as its an update! + assertTrue(rs.next()); + t = rs.getTime(1); + assertNotNull(t); + assertEquals(makeTime(23, 59, 59), t); - // Fall through helper - checkTimeTest(con,st); + assertTrue(! rs.next()); - ps.close(); - st.close(); + rs.close(); + } - JDBC2Tests.closeDB(con); - } catch(Exception ex) { - assert(ex.getMessage(),false); + 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)); } - } - - /** - * Helper for the TimeTests. It tests what should be in the db - */ - private void checkTimeTest(Connection con,Statement st) throws SQLException { - ResultSet rs=null; - Time t=null; - - rs=st.executeQuery(JDBC2Tests.select("tm")); - assert(rs!=null); - - assert(rs.next()); - t = rs.getTime(1); - assert(t!=null); - assert(getHours(t)==1); - assert(getMinutes(t)==2); - assert(getSeconds(t)==3); - - assert(rs.next()); - t = rs.getTime(1); - assert(t!=null); - assert(getHours(t)==23); - assert(getMinutes(t)==59); - assert(getSeconds(t)==59); - - assert(!rs.next()); - - rs.close(); - } - - /** - * These implement depreciated methods in java.sql.Time - */ - private static long getHours(Time t) { - 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))); - } } diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java index 6685e2531a..2d4051cef8 100644 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java @@ -5,7 +5,7 @@ import junit.framework.TestCase; 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! * @@ -15,123 +15,118 @@ import java.sql.*; */ public class TimestampTest extends TestCase { - public TimestampTest(String name) { - super(name); - } - - /** - * Tests the time methods in ResultSet - */ - public void testGetTimestamp() { - try { - Connection con = JDBC2Tests.openDB(); - - Statement st=con.createStatement(); - - JDBC2Tests.createTable(con,"ts timestamp"); - - st.executeUpdate(JDBC2Tests.insert("'1950-02-07 15:00: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 - checkTimeTest(con,st); - - st.close(); - - JDBC2Tests.closeDB(con); - } catch(Exception ex) { - assert(ex.getMessage(),false); - } - } - - /** - * Tests the time methods in PreparedStatement - */ - public void testSetTimestamp() { - try { - Connection con = JDBC2Tests.openDB(); - - Statement st=con.createStatement(); - - JDBC2Tests.createTable(con,"ts timestamp"); - - PreparedStatement ps = con.prepareStatement(JDBC2Tests.insert("?")); - - 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 - checkTimeTest(con,st); - - ps.close(); - st.close(); - - JDBC2Tests.closeDB(con); - } catch(Exception ex) { - assert(ex.getMessage(),false); - } - } - - /** - * Helper for the TimeTests. It tests what should be in the db - */ - private void checkTimeTest(Connection con,Statement st) throws SQLException { - ResultSet rs=null; - java.sql.Timestamp t=null; - - rs=st.executeQuery(JDBC2Tests.select("ts")); - assert(rs!=null); - - assert(rs.next()); - t = rs.getTimestamp(1); - assert(t!=null); - assert(t.equals(getTimestamp(1950,2,7,15,0,0))); - - assert(rs.next()); - t = rs.getTimestamp(1); - assert(t!=null); - - // Seems Daylight saving is ignored? - assert(t.equals(getTimestamp(1970,6,2,8,13,0))); - - 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 Connection con; + + public TimestampTest(String 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 + */ + public void testGetTimestamp() { + try { + Statement stmt = con.createStatement(); + + assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", + "'1950-02-07 15:00:00'"))); + + assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", "'" + + getTimestamp(1970, 6, 2, 8, 13, 0, 0).toString() + + "'"))); + + assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", + "'1970-06-02 08:13:00'"))); + + // Fall through helper + timestampTest(); + + assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp")); + + stmt.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * Tests the time methods in PreparedStatement + */ + public void testSetTimestamp() { + try { + Statement stmt = con.createStatement(); + PreparedStatement pstmt = con.prepareStatement(JDBC2Tests.insertSQL("testtimestamp", "?")); + + pstmt.setTimestamp(1, getTimestamp(1950, 2, 7, 15, 0, 0, 0)); + assertEquals(1, pstmt.executeUpdate()); + + pstmt.setTimestamp(1, getTimestamp(1970, 6, 2, 8, 13, 0, 0)); + assertEquals(1, pstmt.executeUpdate()); + + pstmt.setTimestamp(1, getTimestamp(1970, 6, 2, 8, 13, 0, 0)); + assertEquals(1, pstmt.executeUpdate()); + + // Fall through helper + timestampTest(); + + assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp")); + + pstmt.close(); + stmt.close(); + } catch(Exception ex) { + fail(ex.getMessage()); + } + } + + /** + * Helper for the TimeTests. It tests what should be in the db + */ + private void timestampTest() throws SQLException { + Statement stmt = con.createStatement(); + ResultSet rs; + java.sql.Timestamp t; + + rs = stmt.executeQuery(JDBC2Tests.selectSQL("testtimestamp", "ts")); + assertNotNull(rs); + + assertTrue(rs.next()); + t = rs.getTimestamp(1); + assertNotNull(t); + assertTrue(t.equals(getTimestamp(1950, 2, 7, 15, 0, 0, 0))); + + assertTrue(rs.next()); + t = rs.getTimestamp(1); + assertNotNull(t); + assertTrue(t.equals(getTimestamp(1970, 6, 2, 8, 13, 0, 0))); + + assertTrue(rs.next()); + t = rs.getTimestamp(1); + assertNotNull(t); + assertTrue(t.equals(getTimestamp(1970, 6, 2, 8, 13, 0, 0))); + + assertTrue(! rs.next()); // end of table. Fail if more entries exist. + + rs.close(); + stmt.close(); + } + + 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) + "." + } -- 2.40.0