1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-11 00:12:06 +03:00

Run pgjindent for Java folks.

This commit is contained in:
Bruce Momjian
2002-09-06 21:23:06 +00:00
parent b4295d052e
commit f37c1c486a
71 changed files with 8582 additions and 7709 deletions

View File

@@ -22,17 +22,17 @@ public class CallableStmtTest extends TestCase
{
con = TestUtil.openDB();
Statement stmt = con.createStatement ();
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getString (varchar) " +
"RETURNS varchar AS ' DECLARE inString alias for $1; begin "+
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getString (varchar) " +
"RETURNS varchar AS ' DECLARE inString alias for $1; begin " +
"return ''bob''; end; ' LANGUAGE 'plpgsql';");
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getDouble (float) " +
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getDouble (float) " +
"RETURNS float AS ' DECLARE inString alias for $1; begin " +
"return 42.42; end; ' LANGUAGE 'plpgsql';");
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getInt (int) RETURNS int " +
" AS 'DECLARE inString alias for $1; begin " +
"return 42; end;' LANGUAGE 'plpgsql';");
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getNumeric (numeric) " +
"RETURNS numeric AS ' DECLARE inString alias for $1; " +
" AS 'DECLARE inString alias for $1; begin " +
"return 42; end;' LANGUAGE 'plpgsql';");
stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getNumeric (numeric) " +
"RETURNS numeric AS ' DECLARE inString alias for $1; " +
"begin return 42; end; ' LANGUAGE 'plpgsql';");
stmt.close ();
}
@@ -54,7 +54,8 @@ public class CallableStmtTest extends TestCase
//testGetString ();
//}
public void testGetDouble () throws Throwable {
public void testGetDouble () throws Throwable
{
// System.out.println ("Testing CallableStmt Types.DOUBLE");
CallableStatement call = con.prepareCall (func + pkgName + "getDouble (?) }");
call.setDouble (2, (double)3.04);
@@ -64,7 +65,8 @@ public class CallableStmtTest extends TestCase
assertTrue ("correct return from getString ()", result == 42.42);
}
public void testGetInt () throws Throwable {
public void testGetInt () throws Throwable
{
// System.out.println ("Testing CallableStmt Types.INTEGER");
CallableStatement call = con.prepareCall (func + pkgName + "getInt (?) }");
call.setInt (2, 4);
@@ -74,18 +76,20 @@ public class CallableStmtTest extends TestCase
assertTrue ("correct return from getString ()", result == 42);
}
public void testGetNumeric () throws Throwable {
public void testGetNumeric () throws Throwable
{
// System.out.println ("Testing CallableStmt Types.NUMERIC");
CallableStatement call = con.prepareCall (func + pkgName + "getNumeric (?) }");
call.setBigDecimal (2, new java.math.BigDecimal(4));
call.registerOutParameter (1, Types.NUMERIC);
call.execute ();
java.math.BigDecimal result = call.getBigDecimal (1);
assertTrue ("correct return from getString ()",
assertTrue ("correct return from getString ()",
result.equals (new java.math.BigDecimal(42)));
}
public void testGetString () throws Throwable {
public void testGetString () throws Throwable
{
// System.out.println ("Testing CallableStmt Types.VARCHAR");
CallableStatement call = con.prepareCall (func + pkgName + "getString (?) }");
call.setString (2, "foo");
@@ -96,20 +100,25 @@ public class CallableStmtTest extends TestCase
}
public void testBadStmt () throws Throwable {
public void testBadStmt () throws Throwable
{
tryOneBadStmt ("{ ?= " + pkgName + "getString (?) }");
tryOneBadStmt ("{ ?= call getString (?) ");
tryOneBadStmt ("{ = ? call getString (?); }");
}
protected void tryOneBadStmt (String sql) throws Throwable {
protected void tryOneBadStmt (String sql) throws Throwable
{
boolean wasCaught = false;
try {
try
{
CallableStatement call = con.prepareCall (sql);
} catch (SQLException e) {
}
catch (SQLException e)
{
wasCaught = true; // good -> this statement was missing something
}
assertTrue ("bad statment ('"+sql+"')was not caught", wasCaught);
assertTrue ("bad statment ('" + sql + "')was not caught", wasCaught);
}
}

View File

@@ -9,7 +9,7 @@ import java.sql.*;
*
* PS: Do you know how difficult it is to type on a train? ;-)
*
* $Id: DatabaseMetaDataTest.java,v 1.12 2002/08/23 20:45:49 barry Exp $
* $Id: DatabaseMetaDataTest.java,v 1.13 2002/09/06 21:23:06 momjian Exp $
*/
public class DatabaseMetaDataTest extends TestCase
@@ -48,7 +48,7 @@ public class DatabaseMetaDataTest extends TestCase
ResultSet rs = dbmd.getTables( null, null, "test%", new String[] {"TABLE"});
assertTrue( rs.next() );
String tableName = rs.getString("TABLE_NAME");
String tableName = rs.getString("TABLE_NAME");
assertTrue( tableName.equals("testmetadata") );
rs.close();
@@ -102,10 +102,10 @@ public class DatabaseMetaDataTest extends TestCase
assertTrue(dbmd.supportsMinimumSQLGrammar());
assertTrue(!dbmd.supportsCoreSQLGrammar());
assertTrue(!dbmd.supportsExtendedSQLGrammar());
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3"))
assertTrue(dbmd.supportsANSI92EntryLevelSQL());
else
assertTrue(!dbmd.supportsANSI92EntryLevelSQL());
if (((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion("7.3"))
assertTrue(dbmd.supportsANSI92EntryLevelSQL());
else
assertTrue(!dbmd.supportsANSI92EntryLevelSQL());
assertTrue(!dbmd.supportsANSI92IntermediateSQL());
assertTrue(!dbmd.supportsANSI92FullSQL());
@@ -232,124 +232,124 @@ public class DatabaseMetaDataTest extends TestCase
}
}
public void testCrossReference()
{
public void testCrossReference()
{
try
{
Connection con1 = TestUtil.openDB();
Connection con1 = TestUtil.openDB();
TestUtil.createTable( con1, "vv", "a int not null, b int not null, primary key ( a, b )" );
TestUtil.createTable( con1, "vv", "a int not null, b int not null, primary key ( a, b )" );
TestUtil.createTable( con1, "ww", "m int not null, n int not null, primary key ( m, n ), foreign key ( m, n ) references vv ( a, b )" );
TestUtil.createTable( con1, "ww", "m int not null, n int not null, primary key ( m, n ), foreign key ( m, n ) references vv ( a, b )" );
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getCrossReference(null, "", "vv", null, "", "ww" );
ResultSet rs = dbmd.getCrossReference(null, "", "vv", null, "", "ww" );
for (int j=1; rs.next(); j++ )
{
for (int j = 1; rs.next(); j++ )
{
String pkTableName = rs.getString( "PKTABLE_NAME" );
assertTrue ( pkTableName.equals("vv") );
String pkTableName = rs.getString( "PKTABLE_NAME" );
assertTrue ( pkTableName.equals("vv") );
String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
assertTrue( pkColumnName.equals("a") || pkColumnName.equals("b"));
String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
assertTrue( pkColumnName.equals("a") || pkColumnName.equals("b"));
String fkTableName = rs.getString( "FKTABLE_NAME" );
assertTrue( fkTableName.equals( "ww" ) );
String fkTableName = rs.getString( "FKTABLE_NAME" );
assertTrue( fkTableName.equals( "ww" ) );
String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
assertTrue( fkColumnName.equals( "m" ) || fkColumnName.equals( "n" ) ) ;
String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
assertTrue( fkColumnName.equals( "m" ) || fkColumnName.equals( "n" ) ) ;
String fkName = rs.getString( "FK_NAME" );
assertTrue( fkName.equals( "<unnamed>") );
String fkName = rs.getString( "FK_NAME" );
assertTrue( fkName.equals( "<unnamed>") );
String pkName = rs.getString( "PK_NAME" );
assertTrue( pkName.equals("vv_pkey") );
String pkName = rs.getString( "PK_NAME" );
assertTrue( pkName.equals("vv_pkey") );
int keySeq = rs.getInt( "KEY_SEQ" );
assertTrue( keySeq == j );
}
int keySeq = rs.getInt( "KEY_SEQ" );
assertTrue( keySeq == j );
}
TestUtil.dropTable( con1, "vv" );
TestUtil.dropTable( con1, "ww" );
TestUtil.dropTable( con1, "vv" );
TestUtil.dropTable( con1, "ww" );
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testForeignKeys()
{
}
public void testForeignKeys()
{
try
{
Connection con1 = TestUtil.openDB();
TestUtil.createTable( con1, "people", "id int4 primary key, name text" );
TestUtil.createTable( con1, "policy", "id int4 primary key, name text" );
Connection con1 = TestUtil.openDB();
TestUtil.createTable( con1, "people", "id int4 primary key, name text" );
TestUtil.createTable( con1, "policy", "id int4 primary key, name text" );
TestUtil.createTable( con1, "users", "id int4 primary key, people_id int4, policy_id int4,"+
"CONSTRAINT people FOREIGN KEY (people_id) references people(id),"+
"constraint policy FOREIGN KEY (policy_id) references policy(id)" );
TestUtil.createTable( con1, "users", "id int4 primary key, people_id int4, policy_id int4," +
"CONSTRAINT people FOREIGN KEY (people_id) references people(id)," +
"constraint policy FOREIGN KEY (policy_id) references policy(id)" );
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
ResultSet rs = dbmd.getImportedKeys(null, "", "users" );
int j = 0;
for (; rs.next(); j++ )
{
ResultSet rs = dbmd.getImportedKeys(null, "", "users" );
int j = 0;
for (; rs.next(); j++ )
{
String pkTableName = rs.getString( "PKTABLE_NAME" );
assertTrue ( pkTableName.equals("people") || pkTableName.equals("policy") );
String pkTableName = rs.getString( "PKTABLE_NAME" );
assertTrue ( pkTableName.equals("people") || pkTableName.equals("policy") );
String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
assertTrue( pkColumnName.equals("id") );
String pkColumnName = rs.getString( "PKCOLUMN_NAME" );
assertTrue( pkColumnName.equals("id") );
String fkTableName = rs.getString( "FKTABLE_NAME" );
assertTrue( fkTableName.equals( "users" ) );
String fkTableName = rs.getString( "FKTABLE_NAME" );
assertTrue( fkTableName.equals( "users" ) );
String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
assertTrue( fkColumnName.equals( "people_id" ) || fkColumnName.equals( "policy_id" ) ) ;
String fkColumnName = rs.getString( "FKCOLUMN_NAME" );
assertTrue( fkColumnName.equals( "people_id" ) || fkColumnName.equals( "policy_id" ) ) ;
String fkName = rs.getString( "FK_NAME" );
assertTrue( fkName.equals( "people") || fkName.equals( "policy" ) );
String fkName = rs.getString( "FK_NAME" );
assertTrue( fkName.equals( "people") || fkName.equals( "policy" ) );
String pkName = rs.getString( "PK_NAME" );
assertTrue( pkName.equals( "people_pkey") || pkName.equals( "policy_pkey" ) );
String pkName = rs.getString( "PK_NAME" );
assertTrue( pkName.equals( "people_pkey") || pkName.equals( "policy_pkey" ) );
}
}
assertTrue ( j== 2 );
assertTrue ( j == 2 );
rs = dbmd.getExportedKeys( null, "", "people" );
rs = dbmd.getExportedKeys( null, "", "people" );
// this is hacky, but it will serve the purpose
assertTrue ( rs.next() );
// this is hacky, but it will serve the purpose
assertTrue ( rs.next() );
assertTrue( rs.getString( "PKTABLE_NAME" ).equals( "people" ) );
assertTrue( rs.getString( "PKCOLUMN_NAME" ).equals( "id" ) );
assertTrue( rs.getString( "PKTABLE_NAME" ).equals( "people" ) );
assertTrue( rs.getString( "PKCOLUMN_NAME" ).equals( "id" ) );
assertTrue( rs.getString( "FKTABLE_NAME" ).equals( "users" ) );
assertTrue( rs.getString( "FKCOLUMN_NAME" ).equals( "people_id" ) );
assertTrue( rs.getString( "FKTABLE_NAME" ).equals( "users" ) );
assertTrue( rs.getString( "FKCOLUMN_NAME" ).equals( "people_id" ) );
assertTrue( rs.getString( "FK_NAME" ).equals( "people" ) );
assertTrue( rs.getString( "FK_NAME" ).equals( "people" ) );
TestUtil.dropTable( con1, "users" );
TestUtil.dropTable( con1, "people" );
TestUtil.dropTable( con1, "policy" );
TestUtil.dropTable( con1, "users" );
TestUtil.dropTable( con1, "people" );
TestUtil.dropTable( con1, "policy" );
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
}
public void testTables()
{
try
@@ -422,20 +422,20 @@ public class DatabaseMetaDataTest extends TestCase
{
try
{
assertTrue(con instanceof org.postgresql.PGConnection);
assertTrue(con instanceof org.postgresql.PGConnection);
org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL"));
//The test below doesn't make sense to me, it tests that
//the version of the driver = the version of the database it is connected to
//since the driver should be backwardly compatible this test is commented out
//The test below doesn't make sense to me, it tests that
//the version of the driver = the version of the database it is connected to
//since the driver should be backwardly compatible this test is commented out
//assertTrue(dbmd.getDatabaseProductVersion().startsWith(
// Integer.toString(pc.getDriver().getMajorVersion())
// + "."
// + Integer.toString(pc.getDriver().getMinorVersion())));
// Integer.toString(pc.getDriver().getMajorVersion())
// + "."
// + Integer.toString(pc.getDriver().getMinorVersion())));
assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver"));
}

View File

@@ -6,7 +6,7 @@ import java.sql.*;
import java.math.BigDecimal;
/*
* $Id: JBuilderTest.java,v 1.6 2002/08/14 20:35:40 barry Exp $
* $Id: JBuilderTest.java,v 1.7 2002/09/06 21:23:06 momjian Exp $
*
* Some simple tests to check that the required components needed for JBuilder
* stay working
@@ -26,7 +26,7 @@ public class JBuilderTest extends TestCase
Connection con = TestUtil.openDB();
TestUtil.createTable( con, "test_c",
"source text,cost money,imageid int4" );
"source text,cost money,imageid int4" );
TestUtil.closeDB(con);
}

View File

@@ -63,7 +63,7 @@ public class Jdbc2TestSuite extends TestSuite
suite.addTestSuite( UpdateableResultTest.class );
suite.addTestSuite( CallableStmtTest.class );
// That's all folks
return suite;
}

View File

@@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*;
/*
* $Id: MiscTest.java,v 1.7 2002/08/14 20:35:40 barry Exp $
* $Id: MiscTest.java,v 1.8 2002/09/06 21:23:06 momjian Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
@@ -52,28 +52,29 @@ public class MiscTest extends TestCase
}
}
public void testError()
{
Connection con = TestUtil.openDB();
public void testError()
{
Connection con = TestUtil.openDB();
try
{
// transaction mode
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.execute("select 1/0");
// transaction mode
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.execute("select 1/0");
fail( "Should not execute this, as a SQLException s/b thrown" );
con.commit();
con.commit();
}
catch ( Exception ex )
{}
try
{
con.commit();
con.close();
}
try
{
con.commit();
con.close();
}catch ( Exception ex) {}
}
catch ( Exception ex)
{}
}
public void xtestLocking()
{
@@ -90,7 +91,7 @@ public class MiscTest extends TestCase
con.setAutoCommit(false);
st.execute("lock table test_lock");
st2.executeUpdate( "insert into test_lock ( name ) values ('hello')" );
con.commit();
con.commit();
TestUtil.dropTable(con, "test_lock");
con.close();
}

View File

@@ -63,24 +63,24 @@ public class ResultSetTest extends TestCase
stmt.close();
}
public void testEmptyResult()
{
try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM testrs where id=100");
rs.beforeFirst();
rs.afterLast();
assertTrue(!rs.first());
assertTrue(!rs.last());
assertTrue(!rs.next());
public void testEmptyResult()
{
try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM testrs where id=100");
rs.beforeFirst();
rs.afterLast();
assertTrue(!rs.first());
assertTrue(!rs.last());
assertTrue(!rs.next());
}
catch ( Exception ex )
{
fail( ex.getMessage() );
}
}
catch ( Exception ex )
{
fail( ex.getMessage() );
}
}
}
}

View File

@@ -5,9 +5,9 @@ import junit.framework.TestCase;
import java.sql.*;
/*
* $Id: TimestampTest.java,v 1.8 2002/08/14 20:35:40 barry Exp $
* $Id: TimestampTest.java,v 1.9 2002/09/06 21:23:06 momjian Exp $
*
* Test get/setTimestamp for both timestamp with time zone and
* Test get/setTimestamp for both timestamp with time zone and
* timestamp without time zone datatypes
*
*/
@@ -39,8 +39,8 @@ public class TimestampTest extends TestCase
/*
* Tests the timestamp methods in ResultSet on timestamp with time zone
* we insert a known string value (don't use setTimestamp) then see that
* we get back the same value from getTimestamp
* we insert a known string value (don't use setTimestamp) then see that
* we get back the same value from getTimestamp
*/
public void testGetTimestampWTZ()
{
@@ -48,15 +48,15 @@ public class TimestampTest extends TestCase
{
Statement stmt = con.createStatement();
//Insert the three timestamp values in raw pg format
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE,"'" + TS1WTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE,"'" + TS2WTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE,"'" + TS3WTZ_PGFORMAT + "'")));
//Insert the three timestamp values in raw pg format
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS1WTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS2WTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS3WTZ_PGFORMAT + "'")));
// Fall through helper
timestampTestWTZ();
timestampTestWTZ();
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE));
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE));
stmt.close();
}
@@ -68,9 +68,9 @@ public class TimestampTest extends TestCase
/*
* Tests the timestamp methods in PreparedStatement on timestamp with time zone
* we insert a value using setTimestamp then see that
* we get back the same value from getTimestamp (which we know works as it was tested
* independently of setTimestamp
* we insert a value using setTimestamp then see that
* we get back the same value from getTimestamp (which we know works as it was tested
* independently of setTimestamp
*/
public void testSetTimestampWTZ()
{
@@ -80,18 +80,18 @@ public class TimestampTest extends TestCase
PreparedStatement pstmt = con.prepareStatement(TestUtil.insertSQL(TSWTZ_TABLE, "?"));
pstmt.setTimestamp(1, TS1WTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
pstmt.setTimestamp(1, TS2WTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
pstmt.setTimestamp(1, TS3WTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
// Fall through helper
timestampTestWTZ();
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE));
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE));
pstmt.close();
stmt.close();
@@ -104,8 +104,8 @@ public class TimestampTest extends TestCase
/*
* Tests the timestamp methods in ResultSet on timestamp without time zone
* we insert a known string value (don't use setTimestamp) then see that
* we get back the same value from getTimestamp
* we insert a known string value (don't use setTimestamp) then see that
* we get back the same value from getTimestamp
*/
public void testGetTimestampWOTZ()
{
@@ -113,15 +113,15 @@ public class TimestampTest extends TestCase
{
Statement stmt = con.createStatement();
//Insert the three timestamp values in raw pg format
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,"'" + TS1WOTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,"'" + TS2WOTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE,"'" + TS3WOTZ_PGFORMAT + "'")));
//Insert the three timestamp values in raw pg format
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS1WOTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS2WOTZ_PGFORMAT + "'")));
assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS3WOTZ_PGFORMAT + "'")));
// Fall through helper
timestampTestWOTZ();
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
stmt.close();
}
@@ -134,9 +134,9 @@ public class TimestampTest extends TestCase
/*
* Tests the timestamp methods in PreparedStatement on timestamp without time zone
* we insert a value using setTimestamp then see that
* we get back the same value from getTimestamp (which we know works as it was tested
* independently of setTimestamp
* we insert a value using setTimestamp then see that
* we get back the same value from getTimestamp (which we know works as it was tested
* independently of setTimestamp
*/
public void testSetTimestampWOTZ()
{
@@ -146,18 +146,18 @@ public class TimestampTest extends TestCase
PreparedStatement pstmt = con.prepareStatement(TestUtil.insertSQL(TSWOTZ_TABLE, "?"));
pstmt.setTimestamp(1, TS1WOTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
pstmt.setTimestamp(1, TS2WOTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
pstmt.setTimestamp(1, TS3WOTZ);
assertEquals(1, pstmt.executeUpdate());
assertEquals(1, pstmt.executeUpdate());
// Fall through helper
timestampTestWOTZ();
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
assertEquals(3, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE));
pstmt.close();
stmt.close();
@@ -178,22 +178,22 @@ public class TimestampTest extends TestCase
java.sql.Timestamp t;
rs = stmt.executeQuery("select ts from " + TSWTZ_TABLE + " order by ts");
assertNotNull(rs);
assertNotNull(rs);
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertNotNull(t);
assertTrue(t.equals(TS1WTZ));
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertNotNull(t);
assertTrue(t.equals(TS2WTZ));
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertTrue(t.equals(TS3WTZ));
assertNotNull(t);
assertTrue(t.equals(TS3WTZ));
assertTrue(! rs.next()); // end of table. Fail if more entries exist.
@@ -211,21 +211,21 @@ public class TimestampTest extends TestCase
java.sql.Timestamp t;
rs = stmt.executeQuery("select ts from " + TSWOTZ_TABLE + " order by ts");
assertNotNull(rs);
assertNotNull(rs);
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertNotNull(t);
assertTrue(t.toString().equals(TS1WOTZ_JAVAFORMAT));
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertNotNull(t);
assertTrue(t.toString().equals(TS2WOTZ_JAVAFORMAT));
assertTrue(rs.next());
t = rs.getTimestamp(1);
assertNotNull(t);
assertNotNull(t);
assertTrue(t.toString().equals(TS3WOTZ_JAVAFORMAT));
assertTrue(! rs.next()); // end of table. Fail if more entries exist.
@@ -236,57 +236,63 @@ public class TimestampTest extends TestCase
private static java.sql.Timestamp getTimestamp(int y, int m, int d, int h, int mn, int se, int f, String tz)
{
java.sql.Timestamp l_return = null;
java.text.DateFormat l_df;
try {
String l_ts;
l_ts = TestUtil.fix(y, 4) + "-" +
TestUtil.fix(m, 2) + "-" +
TestUtil.fix(d, 2) + " " +
TestUtil.fix(h, 2) + ":" +
TestUtil.fix(mn, 2) + ":" +
TestUtil.fix(se, 2) + " ";
java.sql.Timestamp l_return = null;
java.text.DateFormat l_df;
try
{
String l_ts;
l_ts = TestUtil.fix(y, 4) + "-" +
TestUtil.fix(m, 2) + "-" +
TestUtil.fix(d, 2) + " " +
TestUtil.fix(h, 2) + ":" +
TestUtil.fix(mn, 2) + ":" +
TestUtil.fix(se, 2) + " ";
if (tz == null) {
l_df = new java.text.SimpleDateFormat("y-M-d H:m:s");
} else {
l_ts = l_ts + tz;
l_df = new java.text.SimpleDateFormat("y-M-d H:m:s z");
if (tz == null)
{
l_df = new java.text.SimpleDateFormat("y-M-d H:m:s");
}
else
{
l_ts = l_ts + tz;
l_df = new java.text.SimpleDateFormat("y-M-d H:m:s z");
}
java.util.Date l_date = l_df.parse(l_ts);
l_return = new java.sql.Timestamp(l_date.getTime());
l_return.setNanos(f);
}
java.util.Date l_date = l_df.parse(l_ts);
l_return = new java.sql.Timestamp(l_date.getTime());
l_return.setNanos(f);
} catch (Exception ex) {
fail(ex.getMessage());
}
return l_return;
catch (Exception ex)
{
fail(ex.getMessage());
}
return l_return;
}
private static final java.sql.Timestamp TS1WTZ = getTimestamp(1950, 2, 7, 15, 0, 0, 100000000, "PST");
private static final String TS1WTZ_PGFORMAT = "1950-02-07 15:00:00.1-08";
private static final java.sql.Timestamp TS2WTZ = getTimestamp(2000, 2, 7, 15, 0, 0, 120000000, "GMT");
private static final String TS2WTZ_PGFORMAT = "2000-02-07 15:00:00.12+00";
private static final java.sql.Timestamp TS1WTZ = getTimestamp(1950, 2, 7, 15, 0, 0, 100000000, "PST");
private static final String TS1WTZ_PGFORMAT = "1950-02-07 15:00:00.1-08";
private static final java.sql.Timestamp TS3WTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123000000, "GMT");
private static final String TS3WTZ_PGFORMAT = "2000-07-07 15:00:00.123+00";
private static final java.sql.Timestamp TS2WTZ = getTimestamp(2000, 2, 7, 15, 0, 0, 120000000, "GMT");
private static final String TS2WTZ_PGFORMAT = "2000-02-07 15:00:00.12+00";
private static final java.sql.Timestamp TS3WTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123000000, "GMT");
private static final String TS3WTZ_PGFORMAT = "2000-07-07 15:00:00.123+00";
private static final java.sql.Timestamp TS1WOTZ = getTimestamp(1950, 2, 7, 15, 0, 0, 100000000, null);
private static final String TS1WOTZ_PGFORMAT = "1950-02-07 15:00:00.1";
private static final String TS1WOTZ_JAVAFORMAT = "1950-02-07 15:00:00.1";
private static final java.sql.Timestamp TS2WOTZ = getTimestamp(2000, 2, 7, 15, 0, 0, 120000000, null);
private static final String TS2WOTZ_PGFORMAT = "2000-02-07 15:00:00.12";
//there is probably a bug here in that this needs to be .1 instead of .12, but I couldn't find it now
private static final String TS2WOTZ_JAVAFORMAT = "2000-02-07 15:00:00.1";
private static final java.sql.Timestamp TS1WOTZ = getTimestamp(1950, 2, 7, 15, 0, 0, 100000000, null);
private static final String TS1WOTZ_PGFORMAT = "1950-02-07 15:00:00.1";
private static final String TS1WOTZ_JAVAFORMAT = "1950-02-07 15:00:00.1";
private static final java.sql.Timestamp TS3WOTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123000000, null);
private static final String TS3WOTZ_PGFORMAT = "2000-07-07 15:00:00.123";
//there is probably a bug here in that this needs to be .12 instead of .123, but I couldn't find it now
private static final String TS3WOTZ_JAVAFORMAT = "2000-07-07 15:00:00.12";
private static final java.sql.Timestamp TS2WOTZ = getTimestamp(2000, 2, 7, 15, 0, 0, 120000000, null);
private static final String TS2WOTZ_PGFORMAT = "2000-02-07 15:00:00.12";
//there is probably a bug here in that this needs to be .1 instead of .12, but I couldn't find it now
private static final String TS2WOTZ_JAVAFORMAT = "2000-02-07 15:00:00.1";
private static final String TSWTZ_TABLE = "testtimestampwtz";
private static final String TSWOTZ_TABLE = "testtimestampwotz";
private static final java.sql.Timestamp TS3WOTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123000000, null);
private static final String TS3WOTZ_PGFORMAT = "2000-07-07 15:00:00.123";
//there is probably a bug here in that this needs to be .12 instead of .123, but I couldn't find it now
private static final String TS3WOTZ_JAVAFORMAT = "2000-07-07 15:00:00.12";
private static final String TSWTZ_TABLE = "testtimestampwtz";
private static final String TSWOTZ_TABLE = "testtimestampwotz";
}

View File

@@ -16,120 +16,121 @@ import org.postgresql.test.TestUtil;
public class UpdateableResultTest extends TestCase
{
public UpdateableResultTest( String name )
{
super( name );
}
public UpdateableResultTest( String name )
{
super( name );
}
public void testUpdateable()
{
try
{
Connection con = TestUtil.openDB();
TestUtil.createTable(con, "updateable","id int primary key, name text, notselected text");
TestUtil.createTable(con, "second","id1 int primary key, name1 text");
public void testUpdateable()
{
try
{
Connection con = TestUtil.openDB();
TestUtil.createTable(con, "updateable", "id int primary key, name text, notselected text");
TestUtil.createTable(con, "second", "id1 int primary key, name1 text");
// put some dummy data into second
Statement st2 = con.createStatement();
st2.execute( "insert into second values (1,'anyvalue' )");
st2.close();
// put some dummy data into second
Statement st2 = con.createStatement();
st2.execute( "insert into second values (1,'anyvalue' )");
st2.close();
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable");
assertNotNull( rs );
rs.moveToInsertRow();
rs.updateInt( 1, 1 );
rs.updateString( 2, "jake" );
rs.updateString( 3, "avalue" );
rs.insertRow();
rs.first();
Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs = st.executeQuery( "select * from updateable");
assertNotNull( rs );
rs.moveToInsertRow();
rs.updateInt( 1, 1 );
rs.updateString( 2, "jake" );
rs.updateString( 3, "avalue" );
rs.insertRow();
rs.first();
rs.updateInt( "id",2 );
rs.updateString( "name","dave" );
rs.updateRow();
rs.updateInt( "id", 2 );
rs.updateString( "name", "dave" );
rs.updateRow();
assertTrue( rs.getInt("id") == 2 );
assertTrue( rs.getString("name").equals("dave"));
assertTrue( rs.getString("notselected").equals("avalue") );
assertTrue( rs.getInt("id") == 2 );
assertTrue( rs.getString("name").equals("dave"));
assertTrue( rs.getString("notselected").equals("avalue") );
rs.deleteRow();
rs.moveToInsertRow();
rs.updateInt("id",3);
rs.updateString("name", "paul");
rs.deleteRow();
rs.moveToInsertRow();
rs.updateInt("id", 3);
rs.updateString("name", "paul");
rs.insertRow();
rs.refreshRow();
assertTrue( rs.getInt("id") == 3 );
assertTrue( rs.getString("name").equals("paul"));
assertTrue( rs.getString("notselected") == null );
rs.insertRow();
rs.refreshRow();
assertTrue( rs.getInt("id") == 3 );
assertTrue( rs.getString("name").equals("paul"));
assertTrue( rs.getString("notselected") == null );
rs.close();
rs.close();
rs = st.executeQuery("select id1, id, name, name1 from updateable, second" );
try
{
while( rs.next() )
{
rs.updateInt( "id",2 );
rs.updateString( "name","dave" );
rs.updateRow();
}
rs = st.executeQuery("select id1, id, name, name1 from updateable, second" );
try
{
while ( rs.next() )
{
rs.updateInt( "id", 2 );
rs.updateString( "name", "dave" );
rs.updateRow();
}
assertTrue( "should not get here, update should fail", false );
}
catch (SQLException ex){}
assertTrue( "should not get here, update should fail", false );
}
catch (SQLException ex)
{}
try
{
rs = st.executeQuery("select oid,* from updateable");
if ( rs.first() )
{
rs.updateInt( "id", 3 );
rs.updateString( "name", "dave3");
rs.updateRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
try
{
rs = st.executeQuery("select oid,* from updateable");
if ( rs.first() )
{
rs.updateInt( "id", 3 );
rs.updateString( "name", "dave3");
rs.updateRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
rs.moveToInsertRow();
rs.updateInt( "id", 4 );
rs.updateString( "name", "dave4" );
rs.moveToInsertRow();
rs.updateInt( "id", 4 );
rs.updateString( "name", "dave4" );
rs.insertRow();
rs.updateInt("id", 5 );
rs.updateString( "name", "dave5" );
rs.insertRow();
rs.insertRow();
rs.updateInt("id", 5 );
rs.updateString( "name", "dave5" );
rs.insertRow();
rs.moveToCurrentRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
rs.moveToCurrentRow();
assertTrue(rs.getInt("id") == 3 );
assertTrue(rs.getString("name").equals("dave3"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 4 );
assertTrue(rs.getString("name").equals("dave4"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 4 );
assertTrue(rs.getString("name").equals("dave4"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 5 );
assertTrue(rs.getString("name").equals("dave5"));
assertTrue( rs.next() );
assertTrue(rs.getInt("id") == 5 );
assertTrue(rs.getString("name").equals("dave5"));
}
}
catch(SQLException ex)
{
fail(ex.getMessage());
}
}
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
st.close();
st.close();
TestUtil.dropTable( con,"updateable" );
TestUtil.closeDB( con );
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
TestUtil.dropTable( con, "updateable" );
TestUtil.closeDB( con );
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
}

View File

@@ -10,151 +10,179 @@ import java.sql.*;
/**
* Common tests for all the BaseDataSource implementations. This is
* a small variety to make sure that a connection can be opened and
* some basic queries run. The different BaseDataSource subclasses
* some basic queries run. The different BaseDataSource subclasses
* have different subclasses of this which add additional custom
* tests.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
public abstract class BaseDataSourceTest extends TestCase {
protected Connection con;
protected BaseDataSource bds;
public abstract class BaseDataSourceTest extends TestCase
{
protected Connection con;
protected BaseDataSource bds;
/**
* Constructor required by JUnit
*/
public BaseDataSourceTest(String name) {
super(name);
}
/**
* Constructor required by JUnit
*/
public BaseDataSourceTest(String name)
{
super(name);
}
/**
* Creates a test table using a standard connection (not from a
* DataSource).
*/
protected void setUp() throws Exception {
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO poolingtest VALUES (1, 'Test Row 1')");
stmt.executeUpdate("INSERT INTO poolingtest VALUES (2, 'Test Row 2')");
JDBC2Tests.closeDB(con);
}
/**
* Creates a test table using a standard connection (not from a
* DataSource).
*/
protected void setUp() throws Exception
{
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
Statement stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO poolingtest VALUES (1, 'Test Row 1')");
stmt.executeUpdate("INSERT INTO poolingtest VALUES (2, 'Test Row 2')");
JDBC2Tests.closeDB(con);
}
/**
* Removes the test table using a standard connection (not from
* a DataSource)
*/
protected void tearDown() throws Exception {
con = JDBC2Tests.openDB();
JDBC2Tests.dropTable(con, "poolingtest");
JDBC2Tests.closeDB(con);
}
/**
* Removes the test table using a standard connection (not from
* a DataSource)
*/
protected void tearDown() throws Exception
{
con = JDBC2Tests.openDB();
JDBC2Tests.dropTable(con, "poolingtest");
JDBC2Tests.closeDB(con);
}
/**
* Gets a connection from the current BaseDataSource
*/
protected Connection getDataSourceConnection() throws SQLException {
initializeDataSource();
return bds.getConnection();
}
/**
* Gets a connection from the current BaseDataSource
*/
protected Connection getDataSourceConnection() throws SQLException
{
initializeDataSource();
return bds.getConnection();
}
/**
* Creates an instance of the current BaseDataSource for
* testing. Must be customized by each subclass.
*/
protected abstract void initializeDataSource();
/**
* Creates an instance of the current BaseDataSource for
* testing. Must be customized by each subclass.
*/
protected abstract void initializeDataSource();
/**
* Test to make sure you can instantiate and configure the
* appropriate DataSource
*/
public void testCreateDataSource() {
initializeDataSource();
}
/**
* Test to make sure you can instantiate and configure the
* appropriate DataSource
*/
public void testCreateDataSource()
{
initializeDataSource();
}
/**
* Test to make sure you can get a connection from the DataSource,
* which in turn means the DataSource was able to open it.
*/
public void testGetConnection() {
try {
con = getDataSourceConnection();
con.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Test to make sure you can get a connection from the DataSource,
* which in turn means the DataSource was able to open it.
*/
public void testGetConnection()
{
try
{
con = getDataSourceConnection();
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A simple test to make sure you can execute SQL using the
* Connection from the DataSource
*/
public void testUseConnection() {
try {
con = getDataSourceConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM poolingtest");
if(rs.next()) {
int count = rs.getInt(1);
if(rs.next()) {
fail("Should only have one row in SELECT COUNT result set");
}
if(count != 2) {
fail("Count returned "+count+" expecting 2");
}
} else {
fail("Should have one row in SELECT COUNT result set");
}
rs.close();
st.close();
con.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* A simple test to make sure you can execute SQL using the
* Connection from the DataSource
*/
public void testUseConnection()
{
try
{
con = getDataSourceConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM poolingtest");
if (rs.next())
{
int count = rs.getInt(1);
if (rs.next())
{
fail("Should only have one row in SELECT COUNT result set");
}
if (count != 2)
{
fail("Count returned " + count + " expecting 2");
}
}
else
{
fail("Should have one row in SELECT COUNT result set");
}
rs.close();
st.close();
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A test to make sure you can execute DDL SQL using the
* Connection from the DataSource.
*/
public void testDdlOverConnection() {
try {
con = getDataSourceConnection();
JDBC2Tests.dropTable(con, "poolingtest");
JDBC2Tests.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
con.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* A test to make sure you can execute DDL SQL using the
* Connection from the DataSource.
*/
public void testDdlOverConnection()
{
try
{
con = getDataSourceConnection();
JDBC2Tests.dropTable(con, "poolingtest");
JDBC2Tests.createTable(con, "poolingtest", "id int4 not null primary key, name varchar(50)");
con.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* A test to make sure the connections are not being pooled by the
* current DataSource. Obviously need to be overridden in the case
* of a pooling Datasource.
*/
public void testNotPooledConnection() {
try {
con = getDataSourceConnection();
String name = con.toString();
con.close();
con = getDataSourceConnection();
String name2 = con.toString();
con.close();
assertTrue(!name.equals(name2));
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* A test to make sure the connections are not being pooled by the
* current DataSource. Obviously need to be overridden in the case
* of a pooling Datasource.
*/
public void testNotPooledConnection()
{
try
{
con = getDataSourceConnection();
String name = con.toString();
con.close();
con = getDataSourceConnection();
String name2 = con.toString();
con.close();
assertTrue(!name.equals(name2));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Eventually, we must test stuffing the DataSource in JNDI and
* then getting it back out and make sure it's still usable. This
* should ideally test both Serializable and Referenceable
* mechanisms. Will probably be multiple tests when implemented.
*/
public void testJndi() {
// TODO: Put the DS in JNDI, retrieve it, and try some of this stuff again
}
/**
* Eventually, we must test stuffing the DataSource in JNDI and
* then getting it back out and make sure it's still usable. This
* should ideally test both Serializable and Referenceable
* mechanisms. Will probably be multiple tests when implemented.
*/
public void testJndi()
{
// TODO: Put the DS in JNDI, retrieve it, and try some of this stuff again
}
}

View File

@@ -11,317 +11,382 @@ import java.sql.*;
* interface to the PooledConnection is through the CPDS.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
public class ConnectionPoolTest extends BaseDataSourceTest {
/**
* Constructor required by JUnit
*/
public ConnectionPoolTest(String name) {
super(name);
}
public class ConnectionPoolTest extends BaseDataSourceTest
{
/**
* Constructor required by JUnit
*/
public ConnectionPoolTest(String name)
{
super(name);
}
/**
* Creates and configures a ConnectionPool
*/
protected void initializeDataSource() {
if(bds == null) {
bds = new ConnectionPool();
String db = JDBC2Tests.getURL();
if(db.indexOf('/') > -1) {
db = db.substring(db.lastIndexOf('/')+1);
} else if(db.indexOf(':') > -1) {
db = db.substring(db.lastIndexOf(':')+1);
}
bds.setDatabaseName(db);
bds.setUser(JDBC2Tests.getUser());
bds.setPassword(JDBC2Tests.getPassword());
}
}
/**
* Creates and configures a ConnectionPool
*/
protected void initializeDataSource()
{
if (bds == null)
{
bds = new ConnectionPool();
String db = JDBC2Tests.getURL();
if (db.indexOf('/') > -1)
{
db = db.substring(db.lastIndexOf('/') + 1);
}
else if (db.indexOf(':') > -1)
{
db = db.substring(db.lastIndexOf(':') + 1);
}
bds.setDatabaseName(db);
bds.setUser(JDBC2Tests.getUser());
bds.setPassword(JDBC2Tests.getPassword());
}
}
/**
* Though the normal client interface is to grab a Connection, in
* order to test the middleware/server interface, we need to deal
* with PooledConnections. Some tests use each.
*/
protected PooledConnection getPooledConnection() throws SQLException {
initializeDataSource();
return ((ConnectionPool)bds).getPooledConnection();
}
/**
* Though the normal client interface is to grab a Connection, in
* order to test the middleware/server interface, we need to deal
* with PooledConnections. Some tests use each.
*/
protected PooledConnection getPooledConnection() throws SQLException
{
initializeDataSource();
return ((ConnectionPool)bds).getPooledConnection();
}
/**
* Instead of just fetching a Connection from the ConnectionPool,
* get a PooledConnection, add a listener to close it when the
* Connection is closed, and then get the Connection. Without
* the listener the PooledConnection (and thus the physical connection)
* would never by closed. Probably not a disaster during testing, but
* you never know.
*/
protected Connection getDataSourceConnection() throws SQLException {
initializeDataSource();
final PooledConnection pc = getPooledConnection();
// Since the pooled connection won't be reused in these basic tests, close it when the connection is closed
pc.addConnectionEventListener(new ConnectionEventListener() {
public void connectionClosed(ConnectionEvent event) {
try {
pc.close();
} catch (SQLException e) {
fail("Unable to close PooledConnection: "+e);
}
}
/**
* Instead of just fetching a Connection from the ConnectionPool,
* get a PooledConnection, add a listener to close it when the
* Connection is closed, and then get the Connection. Without
* the listener the PooledConnection (and thus the physical connection)
* would never by closed. Probably not a disaster during testing, but
* you never know.
*/
protected Connection getDataSourceConnection() throws SQLException
{
initializeDataSource();
final PooledConnection pc = getPooledConnection();
// Since the pooled connection won't be reused in these basic tests, close it when the connection is closed
pc.addConnectionEventListener(new ConnectionEventListener()
{
public void connectionClosed(ConnectionEvent event)
{
try
{
pc.close();
}
catch (SQLException e)
{
fail("Unable to close PooledConnection: " + e);
}
}
public void connectionErrorOccurred(ConnectionEvent event) {
}
});
return pc.getConnection();
}
public void connectionErrorOccurred(ConnectionEvent event)
{}
}
);
return pc.getConnection();
}
/**
* Makes sure that if you get a connection from a PooledConnection,
* close it, and then get another one, you're really using the same
* physical connection. Depends on the implementation of toString
* for the connection handle.
*/
public void testPoolReuse() {
try {
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
String name = con.toString();
con.close();
con = pc.getConnection();
String name2 = con.toString();
con.close();
pc.close();
assertTrue("Physical connection doesn't appear to be reused across PooledConnection wrappers", name.equals(name2));
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure that if you get a connection from a PooledConnection,
* close it, and then get another one, you're really using the same
* physical connection. Depends on the implementation of toString
* for the connection handle.
*/
public void testPoolReuse()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
String name = con.toString();
con.close();
con = pc.getConnection();
String name2 = con.toString();
con.close();
pc.close();
assertTrue("Physical connection doesn't appear to be reused across PooledConnection wrappers", name.equals(name2));
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that when you request a connection from the
* PooledConnection, and previous connection it might have given
* out is closed. See JDBC 2.0 Optional Package spec section
* 6.2.3
*/
public void testPoolCloseOldWrapper() {
try {
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
try {
con.createStatement();
fail("Original connection wrapper should be closed when new connection wrapper is generated");
} catch(SQLException e) {}
try {
con.close();
fail("Original connection wrapper should be closed when new connection wrapper is generated");
} catch(SQLException e) {}
con2.close();
pc.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure that when you request a connection from the
* PooledConnection, and previous connection it might have given
* out is closed. See JDBC 2.0 Optional Package spec section
* 6.2.3
*/
public void testPoolCloseOldWrapper()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
try
{
con.createStatement();
fail("Original connection wrapper should be closed when new connection wrapper is generated");
}
catch (SQLException e)
{}
try
{
con.close();
fail("Original connection wrapper should be closed when new connection wrapper is generated");
}
catch (SQLException e)
{}
con2.close();
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that if you get two connection wrappers from the same
* PooledConnection, they are different, even though the represent
* the same physical connection. See JDBC 2.0 Optional Pacakge spec
* section 6.2.2
*/
public void testPoolNewWrapper() {
try {
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
con2.close();
pc.close();
assertTrue("Two calls to PooledConnection.getConnection should not return the same connection wrapper", con != con2);
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure that if you get two connection wrappers from the same
* PooledConnection, they are different, even though the represent
* the same physical connection. See JDBC 2.0 Optional Pacakge spec
* section 6.2.2
*/
public void testPoolNewWrapper()
{
try
{
PooledConnection pc = getPooledConnection();
con = pc.getConnection();
Connection con2 = pc.getConnection();
con2.close();
pc.close();
assertTrue("Two calls to PooledConnection.getConnection should not return the same connection wrapper", con != con2);
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that exactly one close event is fired for each time a
* connection handle is closed. Also checks that events are not
* fired after a given handle has been closed once.
*/
public void testCloseEvent() {
try {
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
try {
con.close();
fail("Should not be able to close a connection wrapper twice");
} catch (SQLException e) {}
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure that exactly one close event is fired for each time a
* connection handle is closed. Also checks that events are not
* fired after a given handle has been closed once.
*/
public void testCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
try
{
con.close();
fail("Should not be able to close a connection wrapper twice");
}
catch (SQLException e)
{}
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that close events are not fired after a listener has
* been removed.
*/
public void testNoCloseEvent() {
try {
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
pc.removeConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure that close events are not fired after a listener has
* been removed.
*/
public void testNoCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
pc.removeConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure that a listener can be removed while dispatching
* events. Sometimes this causes a ConcurrentModificationException
* or something.
*/
public void testInlineCloseEvent() {
try {
PooledConnection pc = getPooledConnection();
RemoveClose rc1 = new RemoveClose();
RemoveClose rc2 = new RemoveClose();
RemoveClose rc3 = new RemoveClose();
pc.addConnectionEventListener(rc1);
pc.addConnectionEventListener(rc2);
pc.addConnectionEventListener(rc3);
con = pc.getConnection();
con.close();
con = pc.getConnection();
con.close();
} catch (Exception e) {
fail(e.getMessage());
}
}
/**
* Makes sure that a listener can be removed while dispatching
* events. Sometimes this causes a ConcurrentModificationException
* or something.
*/
public void testInlineCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
RemoveClose rc1 = new RemoveClose();
RemoveClose rc2 = new RemoveClose();
RemoveClose rc3 = new RemoveClose();
pc.addConnectionEventListener(rc1);
pc.addConnectionEventListener(rc2);
pc.addConnectionEventListener(rc3);
con = pc.getConnection();
con.close();
con = pc.getConnection();
con.close();
}
catch (Exception e)
{
fail(e.getMessage());
}
}
/**
* Tests that a close event is not generated when a connection
* handle is closed automatically due to a new connection handle
* being opened for the same PooledConnection. See JDBC 2.0
* Optional Package spec section 6.3
*/
public void testAutomaticCloseEvent() {
try {
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
// Open a 2nd connection, causing the first to be closed. No even should be generated.
Connection con2 = pc.getConnection();
assertTrue("Connection handle was not closed when new handle was opened", con.isClosed());
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con2.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Tests that a close event is not generated when a connection
* handle is closed automatically due to a new connection handle
* being opened for the same PooledConnection. See JDBC 2.0
* Optional Package spec section 6.3
*/
public void testAutomaticCloseEvent()
{
try
{
PooledConnection pc = getPooledConnection();
CountClose cc = new CountClose();
pc.addConnectionEventListener(cc);
con = pc.getConnection();
assertTrue(cc.getCount() == 0);
assertTrue(cc.getErrorCount() == 0);
con.close();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con = pc.getConnection();
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
// Open a 2nd connection, causing the first to be closed. No even should be generated.
Connection con2 = pc.getConnection();
assertTrue("Connection handle was not closed when new handle was opened", con.isClosed());
assertTrue(cc.getCount() == 1);
assertTrue(cc.getErrorCount() == 0);
con2.close();
assertTrue(cc.getCount() == 2);
assertTrue(cc.getErrorCount() == 0);
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Makes sure the isClosed method on a connection wrapper does what
* you'd expect. Checks the usual case, as well as automatic
* closure when a new handle is opened on the same physical connection.
*/
public void testIsClosed() {
try {
PooledConnection pc = getPooledConnection();
Connection con = pc.getConnection();
assertTrue(!con.isClosed());
con.close();
assertTrue(con.isClosed());
con = pc.getConnection();
Connection con2 = pc.getConnection();
assertTrue(con.isClosed());
assertTrue(!con2.isClosed());
con2.close();
assertTrue(con.isClosed());
pc.close();
} catch (SQLException e) {
fail(e.getMessage());
}
}
/**
* Makes sure the isClosed method on a connection wrapper does what
* you'd expect. Checks the usual case, as well as automatic
* closure when a new handle is opened on the same physical connection.
*/
public void testIsClosed()
{
try
{
PooledConnection pc = getPooledConnection();
Connection con = pc.getConnection();
assertTrue(!con.isClosed());
con.close();
assertTrue(con.isClosed());
con = pc.getConnection();
Connection con2 = pc.getConnection();
assertTrue(con.isClosed());
assertTrue(!con2.isClosed());
con2.close();
assertTrue(con.isClosed());
pc.close();
}
catch (SQLException e)
{
fail(e.getMessage());
}
}
/**
* Helper class to remove a listener during event dispatching.
*/
private class RemoveClose implements ConnectionEventListener {
public void connectionClosed(ConnectionEvent event) {
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
/**
* Helper class to remove a listener during event dispatching.
*/
private class RemoveClose implements ConnectionEventListener
{
public void connectionClosed(ConnectionEvent event)
{
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
public void connectionErrorOccurred(ConnectionEvent event) {
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
}
public void connectionErrorOccurred(ConnectionEvent event)
{
((PooledConnection)event.getSource()).removeConnectionEventListener(this);
}
}
/**
* Helper class that implements the event listener interface, and
* counts the number of events it sees.
*/
private class CountClose implements ConnectionEventListener {
private int count = 0, errorCount = 0;
public void connectionClosed(ConnectionEvent event) {
count++;
}
/**
* Helper class that implements the event listener interface, and
* counts the number of events it sees.
*/
private class CountClose implements ConnectionEventListener
{
private int count = 0, errorCount = 0;
public void connectionClosed(ConnectionEvent event)
{
count++;
}
public void connectionErrorOccurred(ConnectionEvent event) {
errorCount++;
}
public void connectionErrorOccurred(ConnectionEvent event)
{
errorCount++;
}
public int getCount() {
return count;
}
public int getCount()
{
return count;
}
public int getErrorCount() {
return errorCount;
}
public int getErrorCount()
{
return errorCount;
}
public void clear() {
count = errorCount = 0;
}
}
public void clear()
{
count = errorCount = 0;
}
}
}

View File

@@ -8,17 +8,19 @@ import junit.framework.TestSuite;
* PooledConnection implementations.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
public class OptionalTestSuite extends TestSuite {
/**
* Gets the test suite for the entire JDBC 2.0 Optional Package
* implementation.
*/
public static TestSuite suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(SimpleDataSourceTest.class);
suite.addTestSuite(ConnectionPoolTest.class);
return suite;
}
public class OptionalTestSuite extends TestSuite
{
/**
* Gets the test suite for the entire JDBC 2.0 Optional Package
* implementation.
*/
public static TestSuite suite()
{
TestSuite suite = new TestSuite();
suite.addTestSuite(SimpleDataSourceTest.class);
suite.addTestSuite(ConnectionPoolTest.class);
return suite;
}
}

View File

@@ -4,35 +4,42 @@ import org.postgresql.test.JDBC2Tests;
import org.postgresql.jdbc2.optional.SimpleDataSource;
/**
* Performs the basic tests defined in the superclass. Just adds the
* Performs the basic tests defined in the superclass. Just adds the
* configuration logic.
*
* @author Aaron Mulder (ammulder@chariotsolutions.com)
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
public class SimpleDataSourceTest extends BaseDataSourceTest {
/**
* Constructor required by JUnit
*/
public SimpleDataSourceTest(String name) {
super(name);
}
public class SimpleDataSourceTest extends BaseDataSourceTest
{
/**
* Constructor required by JUnit
*/
public SimpleDataSourceTest(String name)
{
super(name);
}
/**
* Creates and configures a new SimpleDataSource.
*/
protected void initializeDataSource() {
if(bds == null) {
bds = new SimpleDataSource();
String db = JDBC2Tests.getURL();
if(db.indexOf('/') > -1) {
db = db.substring(db.lastIndexOf('/')+1);
} else if(db.indexOf(':') > -1) {
db = db.substring(db.lastIndexOf(':')+1);
}
bds.setDatabaseName(db);
bds.setUser(JDBC2Tests.getUser());
bds.setPassword(JDBC2Tests.getPassword());
}
}
/**
* Creates and configures a new SimpleDataSource.
*/
protected void initializeDataSource()
{
if (bds == null)
{
bds = new SimpleDataSource();
String db = JDBC2Tests.getURL();
if (db.indexOf('/') > -1)
{
db = db.substring(db.lastIndexOf('/') + 1);
}
else if (db.indexOf(':') > -1)
{
db = db.substring(db.lastIndexOf(':') + 1);
}
bds.setDatabaseName(db);
bds.setUser(JDBC2Tests.getUser());
bds.setPassword(JDBC2Tests.getPassword());
}
}
}

View File

@@ -7,7 +7,7 @@ import junit.framework.Test;
import java.sql.*;
/*
* Executes all known tests for JDBC3
* Executes all known tests for JDBC3
*/
public class Jdbc3TestSuite extends TestSuite
{
@@ -17,7 +17,7 @@ public class Jdbc3TestSuite extends TestSuite
*/
public static TestSuite suite()
{
//Currently there are no specific jdbc3 tests so just run the jdbc2 tests
return org.postgresql.test.jdbc2.Jdbc2TestSuite.suite();
//Currently there are no specific jdbc3 tests so just run the jdbc2 tests
return org.postgresql.test.jdbc2.Jdbc2TestSuite.suite();
}
}