Index: src/interfaces/jdbc/build.xml =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/build.xml,v retrieving revision 1.17 diff -c -r1.17 build.xml *** src/interfaces/jdbc/build.xml 2001/07/06 23:07:20 1.17 --- src/interfaces/jdbc/build.xml 2001/09/14 21:39:18 *************** *** 182,188 **** - --- 182,187 ---- Index: src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java,v retrieving revision 1.6 diff -c -r1.6 JDBC2Tests.java *** src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java 2001/09/07 22:17:48 1.6 --- src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java 2001/09/14 21:39:19 *************** *** 10,215 **** * 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); ! } ! } /** * Helper - creates a test table for use by a test ! */ ! public static void createTable( ! Connection conn, String table, String columns) { try { ! Statement st = conn.createStatement(); try { ! try { ! st.executeUpdate("drop table " + table); ! } catch(SQLException se) { ! // Intentionally ignore exception ! } // Now create the table ! st.executeUpdate( "create table " + table + " (" + columns + ! ")" ); } finally { st.close(); } } catch(SQLException ex) { ! TestCase.assert(ex.getMessage(),false); } } ! // 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 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; ! } } --- 10,197 ---- * 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. ! */ ! 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 con, ! String table, ! String columns) { try { ! Statement st = con.createStatement(); try { ! // Drop the table ! dropTable(con, table); // Now create the table ! st.executeUpdate("create table " + table + " (" + columns + ")"); } finally { st.close(); } } catch(SQLException ex) { ! 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()); } } ! /** ! * 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(); ! ! // ! // 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; ! } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java,v retrieving revision 1.1 diff -c -r1.1 ANTTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java 2001/02/07 09:13:20 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java 2001/09/14 21:39:19 *************** *** 16,26 **** String usr=System.getProperty("username"); String psw=System.getProperty("password"); ! assert(url!=null); ! assert(usr!=null); ! assert(psw!=null); ! assert(!url.equals("")); ! assert(!usr.equals("")); } } --- 16,26 ---- String usr=System.getProperty("username"); String psw=System.getProperty("password"); ! assertNotNull(url); ! assertNotNull(usr); ! assertNotNull(psw); ! assertTrue(! url.equals("")); ! assertTrue(! usr.equals("")); } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java,v retrieving revision 1.1 diff -c -r1.1 BatchExecuteTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java 2001/09/06 03:11:59 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java 2001/09/14 21:39:19 *************** *** 4,16 **** import junit.framework.TestCase; import java.sql.*; /** * Test case for Statement.batchExecute() */ public class BatchExecuteTest extends TestCase { private Connection con; - private Statement stmt; public BatchExecuteTest(String name) { super(name); --- 4,20 ---- 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; public BatchExecuteTest(String name) { super(name); *************** *** 20,40 **** // a table for this test. protected void setUp() throws Exception { con = JDBC2Tests.openDB(); ! 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)"); - // Generally recommended with batch updates. By default we run all // tests in this test case with autoCommit disabled. con.setAutoCommit(false); --- 24,37 ---- // a table for this test. protected void setUp() throws Exception { con = JDBC2Tests.openDB(); ! 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. ! 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. con.setAutoCommit(false); *************** *** 43,55 **** // 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); ! } } public void testSupportsBatchUpdates() throws Exception { --- 40,48 ---- // Tear down the fixture for this test case. protected void tearDown() throws Exception { con.setAutoCommit(true); ! ! JDBC2Tests.dropTable(con, "testbatch"); ! JDBC2Tests.closeDB(con); } public void testSupportsBatchUpdates() throws Exception { *************** *** 75,80 **** --- 68,74 ---- } public void testExecuteEmptyBatch() throws Exception { + Statement stmt = con.createStatement(); int[] updateCount = stmt.executeBatch(); assertEquals(0,updateCount.length); *************** *** 82,90 **** --- 76,87 ---- 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,105 **** --- 94,106 ---- 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,120 **** --- 116,123 ---- fail( "Should throw a BatchUpdateException instead of " + "a generic SQLException: " + e); } + + stmt.close(); } public void testPreparedStatement() throws Exception { *************** *** 151,156 **** --- 154,161 ---- /** */ 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,183 **** assertCol1HasValue(12); con.rollback(); assertCol1HasValue(12); } } - - /* TODO tests that can be added to this test case - - SQLExceptions chained to a BatchUpdateException - - test PreparedStatement as thoroughly as Statement - */ --- 179,185 ---- assertCol1HasValue(12); con.rollback(); assertCol1HasValue(12); + + stmt.close(); } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java,v retrieving revision 1.1 diff -c -r1.1 BlobTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java 2001/02/14 17:45:17 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java 2001/09/14 21:39:19 *************** *** 16,184 **** */ public class BlobTest extends TestCase { ! public BlobTest(String name) { ! super(name); ! } ! ! /** ! * The table format used by this TestCase ! */ ! private static final String BLOB_TABLE_FMT = "id name,lo oid"; ! ! /** ! * 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(); ! } ! rs.close(); ! st.close(); ! return result; ! } } --- 16,183 ---- */ public class BlobTest extends TestCase { ! private Connection con; ! private static final int LOOP = 0; // LargeObject API using loop ! private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream ! private static final int JDBC_STREAM = 2; // JDBC API using OutputStream ! public BlobTest(String name) { ! super(name); } ! 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; ! } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java,v retrieving revision 1.4 diff -c -r1.4 ConnectionTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java 2001/09/10 14:54:22 1.4 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java 2001/09/14 21:39:19 *************** *** 26,48 **** 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.closeDB(con); } // 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.closeDB(con); } --- 26,44 ---- 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.closeDB(con); } // Tear down the fixture for this test case. protected void tearDown() throws Exception { Connection con = JDBC2Tests.openDB(); ! JDBC2Tests.dropTable(con, "test_a"); ! JDBC2Tests.dropTable(con, "test_c"); ! JDBC2Tests.closeDB(con); } *************** *** 55,70 **** // A standard Statement java.sql.Statement stat = conn.createStatement(); ! assert(stat!=null); stat.close(); // Ask for Updateable ResultSets stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); ! assert(stat!=null); stat.close(); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 51,66 ---- // A standard Statement java.sql.Statement stat = conn.createStatement(); ! assertNotNull(stat); stat.close(); // Ask for Updateable ResultSets stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); ! assertNotNull(stat); stat.close(); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } *************** *** 79,94 **** // A standard Statement java.sql.PreparedStatement stat = conn.prepareStatement(sql); ! assert(stat!=null); stat.close(); // Ask for Updateable ResultSets stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); ! assert(stat!=null); stat.close(); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 75,90 ---- // A standard Statement java.sql.PreparedStatement stat = conn.prepareStatement(sql); ! assertNotNull(stat); stat.close(); // Ask for Updateable ResultSets stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE); ! assertNotNull(stat); stat.close(); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } *************** *** 116,126 **** // Turn it off con.setAutoCommit(false); ! assert(!con.getAutoCommit()); // Turn it back on con.setAutoCommit(true); ! assert(con.getAutoCommit()); // Now test commit st = con.createStatement(); --- 112,122 ---- // Turn it off con.setAutoCommit(false); ! assertTrue(!con.getAutoCommit()); // Turn it back on con.setAutoCommit(true); ! assertTrue(con.getAutoCommit()); // Now test commit st = con.createStatement(); *************** *** 132,152 **** 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); 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! rs.close(); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 128,148 ---- st.executeUpdate("update test_a set image=9876 where id=5678"); con.commit(); rs = st.executeQuery("select image from test_a where id=5678"); ! 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"); ! assertTrue(rs.next()); ! assertEquals(9876, rs.getInt(1)); // Should not change! rs.close(); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } *************** *** 158,172 **** Connection con = JDBC2Tests.openDB(); // Should not say closed ! assert(!con.isClosed()); JDBC2Tests.closeDB(con); // Should now say closed ! assert(con.isClosed()); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 154,168 ---- Connection con = JDBC2Tests.openDB(); // Should not say closed ! assertTrue(!con.isClosed()); JDBC2Tests.closeDB(con); // Should now say closed ! assertTrue(con.isClosed()); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } *************** *** 180,186 **** String testStr = "This Is OuR TeSt message"; // The connection must be ours! ! assert(con instanceof org.postgresql.Connection); // Clear any existing warnings con.clearWarnings(); --- 176,182 ---- String testStr = "This Is OuR TeSt message"; // The connection must be ours! ! assertTrue(con instanceof org.postgresql.Connection); // Clear any existing warnings con.clearWarnings(); *************** *** 190,205 **** // Retrieve it SQLWarning warning = con.getWarnings(); ! assert(warning!=null); ! assert(warning.getMessage().equals(testStr)); // Finally test clearWarnings() this time there must be something to delete con.clearWarnings(); ! assert(con.getWarnings()==null); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 186,201 ---- // Retrieve it SQLWarning warning = con.getWarnings(); ! assertNotNull(warning); ! assertEquals(testStr, warning.getMessage()); // Finally test clearWarnings() this time there must be something to delete con.clearWarnings(); ! assertTrue(con.getWarnings()==null); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } *************** *** 213,288 **** Connection con = JDBC2Tests.openDB(); // PostgreSQL defaults to READ COMMITTED ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); // Begin a transaction con.setAutoCommit(false); // The isolation level should not have changed ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); // Now change the default for future transactions ! 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 ); // Begin a new transaction con.commit(); // Now we should see the new isolation level ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_SERIALIZABLE ); // Repeat the steps above with the transition back to // READ COMMITTED. ! con.setTransactionIsolation( ! Connection.TRANSACTION_READ_COMMITTED ); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_SERIALIZABLE ); con.commit(); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); // Now run some tests with autocommit enabled. con.setAutoCommit(true); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); ! con.setTransactionIsolation( Connection.TRANSACTION_SERIALIZABLE ); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_SERIALIZABLE ); ! ! con.setTransactionIsolation( ! Connection.TRANSACTION_READ_COMMITTED ); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); // 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.setAutoCommit(false); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_SERIALIZABLE ); con.setAutoCommit(true); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_SERIALIZABLE ); ! con.setTransactionIsolation( ! Connection.TRANSACTION_READ_COMMITTED ); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); con.setAutoCommit(false); ! assertEquals( con.getTransactionIsolation(), ! Connection.TRANSACTION_READ_COMMITTED ); JDBC2Tests.closeDB(con); } --- 209,280 ---- Connection con = JDBC2Tests.openDB(); // PostgreSQL defaults to READ COMMITTED ! assertEquals(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); // Begin a transaction con.setAutoCommit(false); // The isolation level should not have changed ! assertEquals(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); // Now change the default for future transactions ! 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(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); // Begin a new transaction con.commit(); // Now we should see the new isolation level ! assertEquals(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); // Repeat the steps above with the transition back to // READ COMMITTED. ! con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ! assertEquals(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); con.commit(); ! assertEquals(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); // Now run some tests with autocommit enabled. con.setAutoCommit(true); + + assertEquals(Connection.TRANSACTION_READ_COMMITTED, + con.getTransactionIsolation()); ! con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); ! assertEquals(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); ! 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(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); con.setAutoCommit(false); ! assertEquals(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); con.setAutoCommit(true); ! assertEquals(Connection.TRANSACTION_SERIALIZABLE, ! con.getTransactionIsolation()); ! con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); ! assertEquals(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); con.setAutoCommit(false); ! assertEquals(Connection.TRANSACTION_READ_COMMITTED, ! con.getTransactionIsolation()); JDBC2Tests.closeDB(con); } *************** *** 305,319 **** // now change it for an empty one java.util.Map newmap = new java.util.HashMap(); con.setTypeMap(newmap); ! assert(con.getTypeMap()==newmap); // restore the old one con.setTypeMap(oldmap); ! assert(con.getTypeMap()==oldmap); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } } --- 297,311 ---- // now change it for an empty one java.util.Map newmap = new java.util.HashMap(); con.setTypeMap(newmap); ! assertEquals(newmap, con.getTypeMap()); // restore the old one con.setTypeMap(oldmap); ! assertEquals(oldmap, con.getTypeMap()); JDBC2Tests.closeDB(con); } catch(SQLException ex) { ! assertTrue(ex.getMessage(),false); } } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java,v retrieving revision 1.1 diff -c -r1.1 DateTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java 2001/02/13 16:39:05 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java 2001/09/14 21:39:19 *************** *** 13,126 **** */ 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)); ! } ! } --- 13,128 ---- */ public class DateTest extends TestCase { ! 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)); ! } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java,v retrieving revision 1.1 diff -c -r1.1 DriverTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java 2001/02/07 09:13:20 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java 2001/09/14 21:39:19 *************** *** 25,45 **** // Load the driver (note clients should never do it this way!) org.postgresql.Driver drv = new org.postgresql.Driver(); ! assert(drv!=null); // 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")); // Badly formatted url's ! assert(!drv.acceptsURL("jdbc:postgres:test")); ! assert(!drv.acceptsURL("postgresql:test")); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } --- 25,45 ---- // Load the driver (note clients should never do it this way!) org.postgresql.Driver drv = new org.postgresql.Driver(); ! assertNotNull(drv); // These are always correct ! 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 ! assertTrue(!drv.acceptsURL("jdbc:postgres:test")); ! assertTrue(!drv.acceptsURL("postgresql:test")); } catch(SQLException ex) { ! fail(ex.getMessage()); } } *************** *** 56,73 **** // Test with the url, username & password con = DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); ! assert(con!=null); con.close(); // Test with the username in the url con = DriverManager.getConnection(JDBC2Tests.getURL()+"?user="+JDBC2Tests.getUser()+"&password="+JDBC2Tests.getPassword()); ! assert(con!=null); con.close(); - } catch(ClassNotFoundException ex) { ! assert(ex.getMessage(),false); } catch(SQLException ex) { ! assert(ex.getMessage(),false); } } ! } \ No newline at end of file --- 56,72 ---- // Test with the url, username & password con = DriverManager.getConnection(JDBC2Tests.getURL(),JDBC2Tests.getUser(),JDBC2Tests.getPassword()); ! assertNotNull(con); con.close(); // Test with the username in the url con = DriverManager.getConnection(JDBC2Tests.getURL()+"?user="+JDBC2Tests.getUser()+"&password="+JDBC2Tests.getPassword()); ! assertNotNull(con); con.close(); } catch(ClassNotFoundException ex) { ! fail(ex.getMessage()); } catch(SQLException ex) { ! fail(ex.getMessage()); } } ! } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java,v retrieving revision 1.1 diff -c -r1.1 EncodingTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java 2001/07/21 21:27:41 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java 2001/09/14 21:39:19 *************** *** 23,34 **** 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); 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", encoding.name().toUpperCase().indexOf("UTF") != -1); } --- 23,34 ---- encoding = Encoding.getEncoding("UNICODE", null); assertEquals("UTF", encoding.name().substring(0, 3).toUpperCase()); encoding = Encoding.getEncoding("SQL_ASCII", null); ! 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"); ! assertTrue("Encoding passed in by the user should be preferred", encoding.name().toUpperCase().indexOf("UTF") != -1); } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java,v retrieving revision 1.2 diff -c -r1.2 JBuilderTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java 2001/09/07 22:17:48 1.2 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java 2001/09/14 21:39:19 *************** *** 31,41 **** // 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); } /** --- 31,38 ---- // Tear down the fixture for this test case. protected void tearDown() throws Exception { Connection con = JDBC2Tests.openDB(); ! JDBC2Tests.dropTable(con, "test_c"); ! JDBC2Tests.closeDB(con); } /** *************** *** 47,53 **** Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select cost from test_c"); ! assert(rs!=null); while(rs.next()){ double bd = rs.getDouble(1); --- 44,50 ---- Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select cost from test_c"); ! assertNotNull(rs); while(rs.next()){ double bd = rs.getDouble(1); *************** *** 58,64 **** JDBC2Tests.closeDB(con); } catch(Exception ex) { ! assert(ex.getMessage(),false); } } } --- 55,61 ---- JDBC2Tests.closeDB(con); } catch(Exception ex) { ! fail(ex.getMessage()); } } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java,v retrieving revision 1.1 diff -c -r1.1 MiscTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java 2001/02/13 16:39:05 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java 2001/09/14 21:39:19 *************** *** 30,36 **** Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select datname from pg_database"); ! assert(rs!=null); while(rs.next()){ String s = rs.getString(1); --- 30,36 ---- Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select datname from pg_database"); ! assertNotNull(rs); while(rs.next()){ String s = rs.getString(1); *************** *** 41,47 **** JDBC2Tests.closeDB(con); } catch(Exception ex) { ! assert(ex.getMessage(),false); } } } --- 41,47 ---- JDBC2Tests.closeDB(con); } catch(Exception ex) { ! fail(ex.getMessage()); } } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java,v retrieving revision 1.1 diff -c -r1.1 TimeTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java 2001/02/13 16:39:05 1.1 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java 2001/09/14 21:39:19 *************** *** 13,123 **** */ 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(); ! ! JDBC2Tests.closeDB(con); ! } catch(Exception ex) { ! assert(ex.getMessage(),false); } - } - - /** - * Tests the time methods in PreparedStatement - */ - 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); } - } - - /** - * 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))); ! } } --- 13,108 ---- */ public class TimeTest extends TestCase { ! private Connection con; ! ! public TimeTest(String name) { ! super(name); } ! protected void setUp() throws Exception { ! con = JDBC2Tests.openDB(); ! JDBC2Tests.createTable(con, "testtime", "tm time"); } ! protected void tearDown() throws Exception { ! JDBC2Tests.dropTable(con, "testtime"); ! JDBC2Tests.closeDB(con); ! } ! ! /** ! * Tests the time methods in ResultSet ! */ ! 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()); ! } ! } ! /** ! * 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()); ! } ! } ! /** ! * 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; ! ! rs = st.executeQuery(JDBC2Tests.selectSQL("testtime", "tm")); ! 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)); ! } } Index: src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java =================================================================== RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java,v retrieving revision 1.2 diff -c -r1.2 TimestampTest.java *** src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java 2001/02/16 16:45:01 1.2 --- src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java 2001/09/14 21:39:19 *************** *** 15,137 **** */ 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)); ! } } --- 15,131 ---- */ public class TimestampTest extends TestCase { ! 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) + "." + + JDBC2Tests.fix(f, 9)); + } }