From ca5d71cd07b6c2df62ae6bf7810051af88767d9e Mon Sep 17 00:00:00 2001 From: Peter Mount Date: Wed, 7 Feb 2001 09:13:20 +0000 Subject: [PATCH] Some updates prior to retrieving a fresh cvs copy: Tue Feb 06 19:00:00 GMT 2001 peter@retep.org.uk - Completed first two TestCase's for the test suite. JUnit is now recognised by ant. --- src/interfaces/jdbc/CHANGELOG | 4 + src/interfaces/jdbc/build.xml | 42 ++++- src/interfaces/jdbc/jdbc.jpx | 4 +- .../jdbc/org/postgresql/jdbc2/ResultSet.java | 1 + .../jdbc/org/postgresql/test/JDBC2Tests.java | 94 +++++++++++ .../org/postgresql/test/jdbc2/ANTTest.java | 26 +++ .../postgresql/test/jdbc2/ConnectionTest.java | 149 ++++++++++++++++++ .../org/postgresql/test/jdbc2/DriverTest.java | 73 +++++++++ 8 files changed, 388 insertions(+), 5 deletions(-) create mode 100644 src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java create mode 100644 src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java create mode 100644 src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java create mode 100644 src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java diff --git a/src/interfaces/jdbc/CHANGELOG b/src/interfaces/jdbc/CHANGELOG index bbf3952f1a..1da6284119 100644 --- a/src/interfaces/jdbc/CHANGELOG +++ b/src/interfaces/jdbc/CHANGELOG @@ -1,3 +1,7 @@ +Tue Feb 06 19:00:00 GMT 2001 peter@retep.org.uk + - Completed first two TestCase's for the test suite. JUnit is now + recognised by ant. + Wed Jan 31 08:46:00 GMT 2001 peter@retep.org.uk - Some minor additions to Statement to make our own extensions more portable. diff --git a/src/interfaces/jdbc/build.xml b/src/interfaces/jdbc/build.xml index 2a923495ba..e95c646f5d 100644 --- a/src/interfaces/jdbc/build.xml +++ b/src/interfaces/jdbc/build.xml @@ -3,7 +3,7 @@ build file to allow ant (http://jakarta.apache.org/ant/) to be used to build the PostgreSQL JDBC Driver. - $Id: build.xml,v 1.4 2001/01/18 17:37:11 peter Exp $ + $Id: build.xml,v 1.5 2001/02/07 09:13:20 peter Exp $ --> @@ -17,6 +17,17 @@ + + + + + + + + + @@ -86,6 +102,7 @@ + @@ -115,7 +132,7 @@ - + @@ -132,4 +149,23 @@ - \ No newline at end of file + + + + + + + + + + + + + + + + + + diff --git a/src/interfaces/jdbc/jdbc.jpx b/src/interfaces/jdbc/jdbc.jpx index 19fb6df534..2fff9537a6 100644 --- a/src/interfaces/jdbc/jdbc.jpx +++ b/src/interfaces/jdbc/jdbc.jpx @@ -9,13 +9,13 @@ - + - + diff --git a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java index 14fe603b69..d88d3190b1 100644 --- a/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java +++ b/src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java @@ -353,6 +353,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu if (s != null) { + try { val = new BigDecimal(s); diff --git a/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java b/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java new file mode 100644 index 0000000000..c4ff4b29f6 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/test/JDBC2Tests.java @@ -0,0 +1,94 @@ +package org.postgresql.test; + +import junit.framework.TestSuite; +import junit.framework.TestCase; + +import org.postgresql.test.jdbc2.*; +import java.sql.*; + +/** + * Executes all known tests for JDBC2 + */ +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); + } + } + + /** + * 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. + // + // ie: ANTTest should be first as it ensures that test parameters are + // being sent to the suite. + // + + // Basic Driver internals + suite.addTestSuite(ANTTest.class); + suite.addTestSuite(DriverTest.class); + suite.addTestSuite(ConnectionTest.class); + + // Connectivity/Protocols + + // ResultSet + + // PreparedStatement + + // MetaData + + // Fastpath/LargeObject + + // That's all folks + return suite; + } +} \ No newline at end of file diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java new file mode 100644 index 0000000000..349c5bcf7d --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java @@ -0,0 +1,26 @@ +package org.postgresql.test.jdbc2; + +import junit.framework.TestCase; + +public class ANTTest extends TestCase { + public ANTTest(String name) { + super(name); + } + + /** + * This tests the acceptsURL() method with a couple of good and badly formed + * jdbc urls + */ + public void testANT() { + String url=System.getProperty("database"); + 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("")); + } +} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java new file mode 100644 index 0000000000..6f268d3af7 --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java @@ -0,0 +1,149 @@ +package org.postgresql.test.jdbc2; + +import org.postgresql.test.JDBC2Tests; +import junit.framework.TestCase; +import java.sql.*; + +/** + * TestCase to test the internal functionality of org.postgresql.jdbc2.Connection + * and it's superclass. + * + * PS: Do you know how difficult it is to type on a train? ;-) + * + * $Id: ConnectionTest.java,v 1.1 2001/02/07 09:13:20 peter Exp $ + */ + +public class ConnectionTest extends TestCase { + + /** + * Constructor + */ + public ConnectionTest(String name) { + super(name); + } + + /** + * Tests the two forms of createStatement() + */ + public void testCreateStatement() { + try { + java.sql.Connection conn = JDBC2Tests.openDB(); + + // 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); + } + } + + /** + * Tests the two forms of prepareStatement() + */ + public void testPrepareStatement() { + try { + java.sql.Connection conn = JDBC2Tests.openDB(); + + String sql = "select source,cost,imageid from test_c"; + + // 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); + } + } + + /** + * Put the test for createPrepareCall here + */ + public void testPrepareCall() { + } + + /** + * Test nativeSQL + */ + public void testNativeSQL() { + // For now do nothing as it returns itself + } + + /** + * Test autoCommit (both get & set) + */ + public void testTransactions() { + try { + java.sql.Connection con = JDBC2Tests.openDB(); + java.sql.Statement st; + java.sql.ResultSet rs; + + // 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(); + st.executeUpdate("insert into test_a (imagename,image,id) values ('comttest',1234,5678)"); + + con.setAutoCommit(false); + + // Now update image to 9876 and commit + 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); + } + } + + /** + * Simple test to see if isClosed works + */ + public void testIsClosed() { + try { + 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); + } + } + +} \ No newline at end of file diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java new file mode 100644 index 0000000000..7337be46fe --- /dev/null +++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java @@ -0,0 +1,73 @@ +package org.postgresql.test.jdbc2; + +import org.postgresql.test.JDBC2Tests; +import junit.framework.TestCase; +import java.sql.*; + +/** + * $Id: DriverTest.java,v 1.1 2001/02/07 09:13:20 peter Exp $ + * + * Tests the dynamically created class org.postgresql.Driver + * + */ +public class DriverTest extends TestCase { + + public DriverTest(String name) { + super(name); + } + + /** + * This tests the acceptsURL() method with a couple of good and badly formed + * jdbc urls + */ + public void testAcceptsURL() { + try { + + // 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); + } + } + + /** + * Tests parseURL (internal) + */ + /** + * Tests the connect method by connecting to the test database + */ + public void testConnect() { + Connection con=null; + try { + Class.forName("org.postgresql.Driver"); + + // 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