1
0
mirror of https://github.com/postgres/postgres.git synced 2025-08-28 18:48:04 +03:00

pgjindent jdbc files. First time jdbc files were formatted.

This commit is contained in:
Bruce Momjian
2001-10-25 06:00:05 +00:00
parent b81844b173
commit d2e27b0674
85 changed files with 23804 additions and 22165 deletions

View File

@@ -9,38 +9,48 @@ import java.sql.*;
/**
* Executes all known tests for JDBC2 and includes some utility methods.
*/
public class JDBC2Tests extends TestSuite {
public class JDBC2Tests extends TestSuite
{
/**
* Returns the Test database JDBC URL
*/
public static String getURL() {
public static String getURL()
{
return System.getProperty("database");
}
/**
* Returns the Postgresql username
*/
public static String getUser() {
public static String getUser()
{
return System.getProperty("username");
}
/**
* Returns the user's password
*/
public static String getPassword() {
public static String getPassword()
{
return System.getProperty("password");
}
/**
* Helper - opens a connection.
*/
public static java.sql.Connection openDB() {
try {
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) {
return java.sql.DriverManager.getConnection(JDBC2Tests.getURL(), JDBC2Tests.getUser(), JDBC2Tests.getPassword());
}
catch (ClassNotFoundException ex)
{
TestCase.fail(ex.getMessage());
} catch(SQLException ex) {
}
catch (SQLException ex)
{
TestCase.fail(ex.getMessage());
}
return null;
@@ -50,11 +60,15 @@ public class JDBC2Tests extends TestSuite {
* 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 {
public static void closeDB(Connection con)
{
try
{
if (con != null)
con.close();
} catch (SQLException ex) {
}
catch (SQLException ex)
{
TestCase.fail(ex.getMessage());
}
}
@@ -64,19 +78,26 @@ public class JDBC2Tests extends TestSuite {
*/
public static void createTable(Connection con,
String table,
String columns) {
try {
String columns)
{
try
{
Statement st = con.createStatement();
try {
try
{
// Drop the table
dropTable(con, table);
// Now create the table
st.executeUpdate("create table " + table + " (" + columns + ")");
} finally {
}
finally
{
st.close();
}
} catch(SQLException ex) {
}
catch (SQLException ex)
{
TestCase.fail(ex.getMessage());
}
}
@@ -84,15 +105,22 @@ public class JDBC2Tests extends TestSuite {
/**
* Helper - drops a table
*/
public static void dropTable(Connection con, String table) {
try {
public static void dropTable(Connection con, String table)
{
try
{
Statement stmt = con.createStatement();
try {
try
{
stmt.executeUpdate("DROP TABLE " + table);
} catch (SQLException ex) {
}
catch (SQLException ex)
{
// ignore
}
} catch (SQLException ex) {
}
catch (SQLException ex)
{
TestCase.fail(ex.getMessage());
}
}
@@ -100,47 +128,53 @@ public class JDBC2Tests extends TestSuite {
/**
* Helper - generates INSERT SQL - very simple
*/
public static String insertSQL(String table, String values) {
public static String insertSQL(String table, String values)
{
return insertSQL(table, null, values);
}
public static String insertSQL(String table, String columns, String 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) {
public static String selectSQL(String table, String columns)
{
return selectSQL(table, columns, null, null);
}
public static String selectSQL(String table, String columns, String where) {
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) {
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) {
public static String fix(int v, int l)
{
String s = "0000000000".substring(0, l) + Integer.toString(v);
return s.substring(s.length() - l);
}
@@ -148,8 +182,9 @@ public class JDBC2Tests extends TestSuite {
/**
* The main entry point for JUnit
*/
public static TestSuite suite() {
TestSuite suite= new TestSuite();
public static TestSuite suite()
{
TestSuite suite = new TestSuite();
//
// Add one line per class in our test cases. These should be in order of

View File

@@ -2,25 +2,28 @@ package org.postgresql.test.jdbc2;
import junit.framework.TestCase;
public class ANTTest extends TestCase {
public ANTTest(String name) {
super(name);
}
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");
/**
* 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");
assertNotNull(url);
assertNotNull(usr);
assertNotNull(psw);
assertNotNull(url);
assertNotNull(usr);
assertNotNull(psw);
assertTrue(! url.equals(""));
assertTrue(! usr.equals(""));
}
assertTrue(! url.equals(""));
assertTrue(! usr.equals(""));
}
}

View File

@@ -12,24 +12,27 @@ import java.sql.*;
/**
* Test case for Statement.batchExecute()
*/
public class BatchExecuteTest extends TestCase {
public class BatchExecuteTest extends TestCase
{
private Connection con;
public BatchExecuteTest(String name) {
public BatchExecuteTest(String name)
{
super(name);
}
// Set up the fixture for this testcase: a connection to a database with
// a table for this test.
protected void setUp() throws Exception {
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
@@ -38,19 +41,22 @@ public class BatchExecuteTest extends TestCase {
}
// Tear down the fixture for this test case.
protected void tearDown() throws Exception {
protected void tearDown() throws Exception
{
con.setAutoCommit(true);
JDBC2Tests.dropTable(con, "testbatch");
JDBC2Tests.closeDB(con);
}
public void testSupportsBatchUpdates() throws Exception {
public void testSupportsBatchUpdates() throws Exception
{
DatabaseMetaData dbmd = con.getMetaData();
assertTrue(dbmd.supportsBatchUpdates());
}
private void assertCol1HasValue(int expected) throws Exception {
private void assertCol1HasValue(int expected) throws Exception
{
Statement getCol1 = con.createStatement();
ResultSet rs =
@@ -67,21 +73,23 @@ public class BatchExecuteTest extends TestCase {
getCol1.close();
}
public void testExecuteEmptyBatch() throws Exception {
public void testExecuteEmptyBatch() throws Exception
{
Statement stmt = con.createStatement();
int[] updateCount = stmt.executeBatch();
assertEquals(0,updateCount.length);
assertEquals(0, updateCount.length);
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
stmt.clearBatch();
updateCount = stmt.executeBatch();
assertEquals(0,updateCount.length);
assertEquals(0, updateCount.length);
stmt.close();
}
public void testClearBatch() throws Exception {
public void testClearBatch() throws Exception
{
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
assertCol1HasValue(0);
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
@@ -98,44 +106,51 @@ public class BatchExecuteTest extends TestCase {
stmt.close();
}
public void testSelectThrowsException() throws Exception {
public void testSelectThrowsException() throws Exception
{
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
stmt.addBatch("SELECT col1 FROM testbatch WHERE pk = 1");
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
try {
try
{
stmt.executeBatch();
fail("Should raise a BatchUpdateException because of the SELECT");
} catch (BatchUpdateException e) {
}
catch (BatchUpdateException e)
{
int [] updateCounts = e.getUpdateCounts();
assertEquals(1,updateCounts.length);
assertEquals(1,updateCounts[0]);
} catch (SQLException e) {
assertEquals(1, updateCounts.length);
assertEquals(1, updateCounts[0]);
}
catch (SQLException e)
{
fail( "Should throw a BatchUpdateException instead of " +
"a generic SQLException: " + e);
"a generic SQLException: " + e);
}
stmt.close();
}
public void testPreparedStatement() throws Exception {
public void testPreparedStatement() throws Exception
{
PreparedStatement pstmt = con.prepareStatement(
"UPDATE testbatch SET col1 = col1 + ? WHERE PK = ?" );
"UPDATE testbatch SET col1 = col1 + ? WHERE PK = ?" );
// Note that the first parameter changes for every statement in the
// batch, whereas the second parameter remains constant.
pstmt.setInt(1,1);
pstmt.setInt(2,1);
pstmt.setInt(1, 1);
pstmt.setInt(2, 1);
pstmt.addBatch();
assertCol1HasValue(0);
pstmt.setInt(1,2);
pstmt.setInt(1, 2);
pstmt.addBatch();
assertCol1HasValue(0);
pstmt.setInt(1,4);
pstmt.setInt(1, 4);
pstmt.addBatch();
assertCol1HasValue(0);
@@ -153,9 +168,10 @@ public class BatchExecuteTest extends TestCase {
/**
*/
public void testTransactionalBehaviour() throws Exception {
public void testTransactionalBehaviour() throws Exception
{
Statement stmt = con.createStatement();
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
stmt.executeBatch();
@@ -170,9 +186,9 @@ public class BatchExecuteTest extends TestCase {
assertCol1HasValue(0);
int[] updateCounts = stmt.executeBatch();
assertEquals(2,updateCounts.length);
assertEquals(1,updateCounts[0]);
assertEquals(1,updateCounts[1]);
assertEquals(2, updateCounts.length);
assertEquals(1, updateCounts[0]);
assertEquals(1, updateCounts[1]);
assertCol1HasValue(12);
con.commit();

View File

@@ -8,39 +8,45 @@ import java.sql.*;
import org.postgresql.largeobject.*;
/**
* $Id: BlobTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: BlobTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
*
*/
public class BlobTest extends TestCase {
public class BlobTest extends TestCase
{
private Connection con;
private Connection con;
private static final int LOOP = 0; // LargeObject API using loop
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
private static final int JDBC_STREAM = 2; // JDBC API using OutputStream
public BlobTest(String name) {
public BlobTest(String name)
{
super(name);
}
}
protected void setUp() throws Exception {
protected void setUp() throws Exception
{
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "testblob", "id name,lo oid");
}
protected void tearDown() throws Exception {
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 {
public void testUploadBlob_LOOP()
{
try
{
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
@@ -51,7 +57,9 @@ public class BlobTest extends TestCase {
assertTrue(compareBlobs());
con.setAutoCommit(true);
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -59,8 +67,10 @@ public class BlobTest extends TestCase {
/**
* Tests one method of uploading a blob to the database
*/
public void testUploadBlob_NATIVE() {
try {
public void testUploadBlob_NATIVE()
{
try
{
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
@@ -71,7 +81,9 @@ public class BlobTest extends TestCase {
assertTrue(compareBlobs());
con.setAutoCommit(true);
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -81,7 +93,8 @@ public class BlobTest extends TestCase {
* 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 {
private int uploadFile(String file, int method) throws Exception
{
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
FileInputStream fis = new FileInputStream(file);
@@ -89,40 +102,42 @@ public class BlobTest extends TestCase {
int oid = lom.create(LargeObjectManager.READWRITE);
LargeObject blob = lom.open(oid);
int s,t;
int s, t;
byte buf[];
OutputStream os;
switch(method)
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 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 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;
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);
default:
assertTrue("Unknown method in uploadFile", false);
}
blob.close();
@@ -130,7 +145,7 @@ public class BlobTest extends TestCase {
// Insert into the table
Statement st = con.createStatement();
st.executeUpdate(JDBC2Tests.insertSQL("testblob", "id,lo","'"+file+"',"+oid));
st.executeUpdate(JDBC2Tests.insertSQL("testblob", "id,lo", "'" + file + "'," + oid));
con.commit();
st.close();
@@ -141,8 +156,9 @@ public class BlobTest extends TestCase {
* 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;
private boolean compareBlobs() throws Exception
{
boolean result = true;
LargeObjectManager lom = ((org.postgresql.Connection)con).getLargeObjectAPI();
@@ -150,7 +166,8 @@ public class BlobTest extends TestCase {
ResultSet rs = st.executeQuery(JDBC2Tests.selectSQL("testblob", "id,lo"));
assertNotNull(rs);
while(rs.next()) {
while (rs.next())
{
String file = rs.getString(1);
int oid = rs.getInt(2);
@@ -158,19 +175,20 @@ public class BlobTest extends TestCase {
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();
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;
result = result && f == -1 && b == -1;
if(!result)
System.out.println("\nBlob compare failed at "+c+" of "+blob.size());
if (!result)
System.out.println("\nBlob compare failed at " + c + " of " + blob.size());
blob.close();
fis.close();

View File

@@ -10,194 +10,219 @@ import java.sql.*;
*
* PS: Do you know how difficult it is to type on a train? ;-)
*
* $Id: ConnectionTest.java,v 1.5 2001/09/23 04:11:14 momjian Exp $
* $Id: ConnectionTest.java,v 1.6 2001/10/25 05:59:59 momjian Exp $
*/
public class ConnectionTest extends TestCase {
public class ConnectionTest extends TestCase
{
/**
* Constructor
*/
public ConnectionTest(String name) {
super(name);
}
/**
* Constructor
*/
public ConnectionTest(String name)
{
super(name);
}
// Set up the fixture for this testcase: the tables for this test.
protected void setUp() throws Exception {
Connection con = JDBC2Tests.openDB();
// Set up the fixture for this testcase: the tables for this test.
protected void setUp() throws Exception
{
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "test_a", "imagename name,image oid,id int4");
JDBC2Tests.createTable(con, "test_c", "source text,cost money,imageid int4");
JDBC2Tests.createTable(con, "test_a", "imagename name,image oid,id int4");
JDBC2Tests.createTable(con, "test_c", "source text,cost money,imageid int4");
JDBC2Tests.closeDB(con);
}
JDBC2Tests.closeDB(con);
}
// Tear down the fixture for this test case.
protected void tearDown() throws Exception {
Connection con = JDBC2Tests.openDB();
// 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);
}
JDBC2Tests.dropTable(con, "test_a");
JDBC2Tests.dropTable(con, "test_c");
/**
* Tests the two forms of createStatement()
*/
public void testCreateStatement() {
try {
java.sql.Connection conn = JDBC2Tests.openDB();
JDBC2Tests.closeDB(con);
}
// A standard Statement
java.sql.Statement stat = conn.createStatement();
assertNotNull(stat);
stat.close();
/**
* Tests the two forms of createStatement()
*/
public void testCreateStatement()
{
try
{
java.sql.Connection conn = JDBC2Tests.openDB();
// Ask for Updateable ResultSets
stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
assertNotNull(stat);
stat.close();
// A standard Statement
java.sql.Statement stat = conn.createStatement();
assertNotNull(stat);
stat.close();
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
// Ask for Updateable ResultSets
stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE);
assertNotNull(stat);
stat.close();
/**
* Tests the two forms of prepareStatement()
*/
public void testPrepareStatement() {
try {
java.sql.Connection conn = JDBC2Tests.openDB();
}
catch (SQLException ex)
{
assertTrue(ex.getMessage(), false);
}
}
String sql = "select source,cost,imageid from test_c";
/**
* Tests the two forms of prepareStatement()
*/
public void testPrepareStatement()
{
try
{
java.sql.Connection conn = JDBC2Tests.openDB();
// A standard Statement
java.sql.PreparedStatement stat = conn.prepareStatement(sql);
assertNotNull(stat);
stat.close();
String sql = "select source,cost,imageid from test_c";
// Ask for Updateable ResultSets
stat = conn.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
assertNotNull(stat);
stat.close();
// A standard Statement
java.sql.PreparedStatement stat = conn.prepareStatement(sql);
assertNotNull(stat);
stat.close();
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
// Ask for Updateable ResultSets
stat = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE);
assertNotNull(stat);
stat.close();
/**
* Put the test for createPrepareCall here
*/
public void testPrepareCall() {
}
}
catch (SQLException ex)
{
assertTrue(ex.getMessage(), false);
}
}
/**
* Test nativeSQL
*/
public void testNativeSQL() {
// For now do nothing as it returns itself
}
/**
* Put the test for createPrepareCall here
*/
public void testPrepareCall()
{}
/**
* Test autoCommit (both get & set)
*/
public void testTransactions() {
try {
java.sql.Connection con = JDBC2Tests.openDB();
java.sql.Statement st;
java.sql.ResultSet rs;
/**
* Test nativeSQL
*/
public void testNativeSQL()
{
// For now do nothing as it returns itself
}
// Turn it off
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
/**
* 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 back on
con.setAutoCommit(true);
assertTrue(con.getAutoCommit());
// Turn it off
con.setAutoCommit(false);
assertTrue(!con.getAutoCommit());
// Now test commit
st = con.createStatement();
st.executeUpdate("insert into test_a (imagename,image,id) values ('comttest',1234,5678)");
// Turn it back on
con.setAutoCommit(true);
assertTrue(con.getAutoCommit());
con.setAutoCommit(false);
// Now test commit
st = con.createStatement();
st.executeUpdate("insert into test_a (imagename,image,id) values ('comttest',1234,5678)");
// 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");
assertTrue(rs.next());
assertEquals(9876, rs.getInt(1));
rs.close();
con.setAutoCommit(false);
// 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();
// 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");
assertTrue(rs.next());
assertEquals(9876, rs.getInt(1));
rs.close();
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
// 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();
/**
* Simple test to see if isClosed works.
*/
public void testIsClosed() {
try {
Connection con = JDBC2Tests.openDB();
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
assertTrue(ex.getMessage(), false);
}
}
// Should not say closed
assertTrue(!con.isClosed());
/**
* Simple test to see if isClosed works.
*/
public void testIsClosed()
{
try
{
Connection con = JDBC2Tests.openDB();
JDBC2Tests.closeDB(con);
// Should not say closed
assertTrue(!con.isClosed());
// Should now say closed
assertTrue(con.isClosed());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
// Should now say closed
assertTrue(con.isClosed());
/**
* Test the warnings system
*/
public void testWarnings() {
try {
Connection con = JDBC2Tests.openDB();
}
catch (SQLException ex)
{
assertTrue(ex.getMessage(), false);
}
}
String testStr = "This Is OuR TeSt message";
/**
* Test the warnings system
*/
public void testWarnings()
{
try
{
Connection con = JDBC2Tests.openDB();
// The connection must be ours!
assertTrue(con instanceof org.postgresql.Connection);
String testStr = "This Is OuR TeSt message";
// Clear any existing warnings
con.clearWarnings();
// The connection must be ours!
assertTrue(con instanceof org.postgresql.Connection);
// Set the test warning
((org.postgresql.Connection)con).addWarning(testStr);
// Clear any existing warnings
con.clearWarnings();
// Retrieve it
SQLWarning warning = con.getWarnings();
assertNotNull(warning);
assertEquals(testStr, warning.getMessage());
// Set the test warning
((org.postgresql.Connection)con).addWarning(testStr);
// Finally test clearWarnings() this time there must be something to delete
con.clearWarnings();
assertTrue(con.getWarnings()==null);
// Retrieve it
SQLWarning warning = con.getWarnings();
assertNotNull(warning);
assertEquals(testStr, warning.getMessage());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
// 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);
}
}
/**
* Transaction Isolation Levels
@@ -210,14 +235,14 @@ public class ConnectionTest extends TestCase {
// PostgreSQL defaults to READ COMMITTED
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
con.getTransactionIsolation());
con.getTransactionIsolation());
// Begin a transaction
con.setAutoCommit(false);
// The isolation level should not have changed
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
con.getTransactionIsolation());
con.getTransactionIsolation());
// Now change the default for future transactions
con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
@@ -227,14 +252,14 @@ public class ConnectionTest extends TestCase {
// transaction did not change. It affects only future transactions.
// This behaviour is recommended by the JDBC spec.
assertEquals(Connection.TRANSACTION_READ_COMMITTED,
con.getTransactionIsolation());
con.getTransactionIsolation());
// Begin a new transaction
con.commit();
// Now we should see the new isolation level
assertEquals(Connection.TRANSACTION_SERIALIZABLE,
con.getTransactionIsolation());
con.getTransactionIsolation());
// Repeat the steps above with the transition back to
// READ COMMITTED.
@@ -277,35 +302,39 @@ public class ConnectionTest extends TestCase {
con.getTransactionIsolation());
JDBC2Tests.closeDB(con);
}
catch ( SQLException ex )
}
catch ( SQLException ex )
{
fail( ex.getMessage() );
}
}
/**
* JDBC2 Type mappings
*/
public void testTypeMaps() {
try {
Connection con = JDBC2Tests.openDB();
/**
* JDBC2 Type mappings
*/
public void testTypeMaps()
{
try
{
Connection con = JDBC2Tests.openDB();
// preserve the current map
java.util.Map oldmap = con.getTypeMap();
// preserve the current map
java.util.Map oldmap = con.getTypeMap();
// now change it for an empty one
java.util.Map newmap = new java.util.HashMap();
con.setTypeMap(newmap);
assertEquals(newmap, con.getTypeMap());
// 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());
// restore the old one
con.setTypeMap(oldmap);
assertEquals(oldmap, con.getTypeMap());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
assertTrue(ex.getMessage(),false);
}
}
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
assertTrue(ex.getMessage(), false);
}
}
}

View File

@@ -9,273 +9,323 @@ import java.sql.*;
*
* PS: Do you know how difficult it is to type on a train? ;-)
*
* $Id: DatabaseMetaDataTest.java,v 1.2 2001/09/10 15:07:58 momjian Exp $
* $Id: DatabaseMetaDataTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*/
public class DatabaseMetaDataTest extends TestCase {
/**
* Constructor
*/
public DatabaseMetaDataTest(String name) {
super(name);
}
/**
* The spec says this may return null, but we always do!
*/
public void testGetMetaData() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
/**
* Test default capabilities
*/
public void testCapabilities() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.allProceduresAreCallable());
assertTrue(dbmd.allTablesAreSelectable()); // not true all the time
// This should always be false for postgresql (at least for 7.x)
assertTrue(!dbmd.isReadOnly());
// does the backend support this yet? The protocol does...
assertTrue(!dbmd.supportsMultipleResultSets());
// yes, as multiple backends can have transactions open
assertTrue(dbmd.supportsMultipleTransactions());
assertTrue(dbmd.supportsMinimumSQLGrammar());
assertTrue(!dbmd.supportsCoreSQLGrammar());
assertTrue(!dbmd.supportsExtendedSQLGrammar());
assertTrue(!dbmd.supportsANSI92EntryLevelSQL());
assertTrue(!dbmd.supportsANSI92IntermediateSQL());
assertTrue(!dbmd.supportsANSI92FullSQL());
assertTrue(!dbmd.supportsIntegrityEnhancementFacility());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testJoins() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.supportsOuterJoins());
assertTrue(dbmd.supportsFullOuterJoins());
assertTrue(dbmd.supportsLimitedOuterJoins());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testCursors() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsPositionedDelete());
assertTrue(!dbmd.supportsPositionedUpdate());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testNulls() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// We need to type cast the connection to get access to the
// PostgreSQL-specific method haveMinimumServerVersion().
// This is not available through the java.sql.Connection interface.
assertTrue( con instanceof org.postgresql.Connection );
assertTrue(!dbmd.nullsAreSortedAtStart());
assertTrue( dbmd.nullsAreSortedAtEnd() !=
((org.postgresql.Connection)con).haveMinimumServerVersion("7.2"));
assertTrue( dbmd.nullsAreSortedHigh() ==
((org.postgresql.Connection)con).haveMinimumServerVersion("7.2"));
assertTrue(!dbmd.nullsAreSortedLow());
assertTrue(dbmd.nullPlusNonNullIsNull());
assertTrue(dbmd.supportsNonNullableColumns());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testLocalFiles() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.usesLocalFilePerTable());
assertTrue(!dbmd.usesLocalFiles());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testIdentifiers() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsMixedCaseIdentifiers()); // always false
assertTrue(dbmd.supportsMixedCaseQuotedIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseIdentifiers()); // always false
assertTrue(dbmd.storesLowerCaseIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesLowerCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesMixedCaseQuotedIdentifiers()); // always false
assertTrue(dbmd.getIdentifierQuoteString().equals("\""));
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testTables() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// we can add columns
assertTrue(dbmd.supportsAlterTableWithAddColumn());
// we can't drop columns (yet)
assertTrue(!dbmd.supportsAlterTableWithDropColumn());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testSelect() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// yes we can?: SELECT col a FROM a;
assertTrue(dbmd.supportsColumnAliasing());
// yes we can have expressions in ORDERBY
assertTrue(dbmd.supportsExpressionsInOrderBy());
// Yes, an ORDER BY clause can contain columns that are not in the
// SELECT clause.
assertTrue(dbmd.supportsOrderByUnrelated());
assertTrue(dbmd.supportsGroupBy());
assertTrue(dbmd.supportsGroupByUnrelated());
assertTrue(dbmd.supportsGroupByBeyondSelect()); // needs checking
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testDBParams() {
try {
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getURL().equals(JDBC2Tests.getURL()));
assertTrue(dbmd.getUserName().equals(JDBC2Tests.getUser()));
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testDbProductDetails() {
try {
Connection con = JDBC2Tests.openDB();
assertTrue(con instanceof org.postgresql.Connection);
org.postgresql.Connection pc = (org.postgresql.Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL"));
assertTrue(dbmd.getDatabaseProductVersion().startsWith(Integer.toString(pc.this_driver.getMajorVersion())+"."+Integer.toString(pc.this_driver.getMinorVersion())));
assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver"));
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public void testDriverVersioning() {
try {
Connection con = JDBC2Tests.openDB();
assertTrue(con instanceof org.postgresql.Connection);
org.postgresql.Connection pc = (org.postgresql.Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDriverVersion().equals(pc.this_driver.getVersion()));
assertTrue(dbmd.getDriverMajorVersion()==pc.this_driver.getMajorVersion());
assertTrue(dbmd.getDriverMinorVersion()==pc.this_driver.getMinorVersion());
JDBC2Tests.closeDB(con);
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
public class DatabaseMetaDataTest extends TestCase
{
/**
* Constructor
*/
public DatabaseMetaDataTest(String name)
{
super(name);
}
/**
* The spec says this may return null, but we always do!
*/
public void testGetMetaData()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
/**
* Test default capabilities
*/
public void testCapabilities()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.allProceduresAreCallable());
assertTrue(dbmd.allTablesAreSelectable()); // not true all the time
// This should always be false for postgresql (at least for 7.x)
assertTrue(!dbmd.isReadOnly());
// does the backend support this yet? The protocol does...
assertTrue(!dbmd.supportsMultipleResultSets());
// yes, as multiple backends can have transactions open
assertTrue(dbmd.supportsMultipleTransactions());
assertTrue(dbmd.supportsMinimumSQLGrammar());
assertTrue(!dbmd.supportsCoreSQLGrammar());
assertTrue(!dbmd.supportsExtendedSQLGrammar());
assertTrue(!dbmd.supportsANSI92EntryLevelSQL());
assertTrue(!dbmd.supportsANSI92IntermediateSQL());
assertTrue(!dbmd.supportsANSI92FullSQL());
assertTrue(!dbmd.supportsIntegrityEnhancementFacility());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testJoins()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.supportsOuterJoins());
assertTrue(dbmd.supportsFullOuterJoins());
assertTrue(dbmd.supportsLimitedOuterJoins());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testCursors()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsPositionedDelete());
assertTrue(!dbmd.supportsPositionedUpdate());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testNulls()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// We need to type cast the connection to get access to the
// PostgreSQL-specific method haveMinimumServerVersion().
// This is not available through the java.sql.Connection interface.
assertTrue( con instanceof org.postgresql.Connection );
assertTrue(!dbmd.nullsAreSortedAtStart());
assertTrue( dbmd.nullsAreSortedAtEnd() !=
((org.postgresql.Connection)con).haveMinimumServerVersion("7.2"));
assertTrue( dbmd.nullsAreSortedHigh() ==
((org.postgresql.Connection)con).haveMinimumServerVersion("7.2"));
assertTrue(!dbmd.nullsAreSortedLow());
assertTrue(dbmd.nullPlusNonNullIsNull());
assertTrue(dbmd.supportsNonNullableColumns());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testLocalFiles()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.usesLocalFilePerTable());
assertTrue(!dbmd.usesLocalFiles());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testIdentifiers()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(!dbmd.supportsMixedCaseIdentifiers()); // always false
assertTrue(dbmd.supportsMixedCaseQuotedIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseIdentifiers()); // always false
assertTrue(dbmd.storesLowerCaseIdentifiers()); // always true
assertTrue(!dbmd.storesUpperCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesLowerCaseQuotedIdentifiers()); // always false
assertTrue(!dbmd.storesMixedCaseQuotedIdentifiers()); // always false
assertTrue(dbmd.getIdentifierQuoteString().equals("\""));
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testTables()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// we can add columns
assertTrue(dbmd.supportsAlterTableWithAddColumn());
// we can't drop columns (yet)
assertTrue(!dbmd.supportsAlterTableWithDropColumn());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testSelect()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
// yes we can?: SELECT col a FROM a;
assertTrue(dbmd.supportsColumnAliasing());
// yes we can have expressions in ORDERBY
assertTrue(dbmd.supportsExpressionsInOrderBy());
// Yes, an ORDER BY clause can contain columns that are not in the
// SELECT clause.
assertTrue(dbmd.supportsOrderByUnrelated());
assertTrue(dbmd.supportsGroupBy());
assertTrue(dbmd.supportsGroupByUnrelated());
assertTrue(dbmd.supportsGroupByBeyondSelect()); // needs checking
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testDBParams()
{
try
{
Connection con = JDBC2Tests.openDB();
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getURL().equals(JDBC2Tests.getURL()));
assertTrue(dbmd.getUserName().equals(JDBC2Tests.getUser()));
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testDbProductDetails()
{
try
{
Connection con = JDBC2Tests.openDB();
assertTrue(con instanceof org.postgresql.Connection);
org.postgresql.Connection pc = (org.postgresql.Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL"));
assertTrue(dbmd.getDatabaseProductVersion().startsWith(Integer.toString(pc.this_driver.getMajorVersion()) + "." + Integer.toString(pc.this_driver.getMinorVersion())));
assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver"));
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
public void testDriverVersioning()
{
try
{
Connection con = JDBC2Tests.openDB();
assertTrue(con instanceof org.postgresql.Connection);
org.postgresql.Connection pc = (org.postgresql.Connection) con;
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);
assertTrue(dbmd.getDriverVersion().equals(pc.this_driver.getVersion()));
assertTrue(dbmd.getDriverMajorVersion() == pc.this_driver.getMajorVersion());
assertTrue(dbmd.getDriverMinorVersion() == pc.this_driver.getMinorVersion());
JDBC2Tests.closeDB(con);
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
}

View File

@@ -5,37 +5,43 @@ import junit.framework.TestCase;
import java.sql.*;
/**
* $Id: DateTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: DateTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
*
*/
public class DateTest extends TestCase {
public class DateTest extends TestCase
{
private Connection con;
public DateTest(String name) {
public DateTest(String name)
{
super(name);
}
protected void setUp() throws Exception {
protected void setUp() throws Exception
{
con = JDBC2Tests.openDB();
JDBC2Tests.createTable(con, "testdate", "dt date");
}
protected void tearDown() throws Exception {
protected void tearDown() throws Exception
{
JDBC2Tests.dropTable(con, "testdate");
JDBC2Tests.closeDB(con);
}
/**
* Tests the time methods in ResultSet
*/
public void testGetDate() {
try {
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'")));
@@ -46,7 +52,9 @@ public class DateTest extends TestCase {
assertEquals(4, stmt.executeUpdate("DELETE FROM " + "testdate"));
stmt.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -54,8 +62,10 @@ public class DateTest extends TestCase {
/**
* Tests the time methods in PreparedStatement
*/
public void testSetDate() {
try {
public void testSetDate()
{
try
{
Statement stmt = con.createStatement();
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testdate", "?"));
@@ -78,7 +88,9 @@ public class DateTest extends TestCase {
assertEquals(4, stmt.executeUpdate("DELETE FROM testdate"));
stmt.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -86,7 +98,8 @@ public class DateTest extends TestCase {
/**
* Helper for the date tests. It tests what should be in the db
*/
private void dateTest() throws SQLException {
private void dateTest() throws SQLException
{
Statement st = con.createStatement();
ResultSet rs;
java.sql.Date d;
@@ -97,7 +110,7 @@ public class DateTest extends TestCase {
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1950, 2, 7));
assertEquals(d, makeDate(1950, 2, 7));
assertTrue(rs.next());
d = rs.getDate(1);
@@ -108,7 +121,7 @@ public class DateTest extends TestCase {
d = rs.getDate(1);
assertNotNull(d);
assertEquals(d, makeDate(1999, 8, 11));
assertTrue(rs.next());
d = rs.getDate(1);
assertNotNull(d);
@@ -120,7 +133,8 @@ public class DateTest extends TestCase {
st.close();
}
private java.sql.Date makeDate(int y, int m, int d) {
private java.sql.Date makeDate(int y, int m, int d)
{
return java.sql.Date.valueOf(JDBC2Tests.fix(y, 4) + "-" +
JDBC2Tests.fix(m, 2) + "-" +
JDBC2Tests.fix(d, 2));

View File

@@ -5,68 +5,80 @@ import junit.framework.TestCase;
import java.sql.*;
/**
* $Id: DriverTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: DriverTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*
* Tests the dynamically created class org.postgresql.Driver
*
*/
public class DriverTest extends TestCase {
public class DriverTest extends TestCase
{
public DriverTest(String name) {
super(name);
}
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 {
/**
* 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();
assertNotNull(drv);
// 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"));
// 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"));
// Badly formatted url's
assertTrue(!drv.acceptsURL("jdbc:postgres:test"));
assertTrue(!drv.acceptsURL("postgresql:test"));
} catch(SQLException ex) {
fail(ex.getMessage());
}
}
}
catch (SQLException ex)
{
fail(ex.getMessage());
}
}
/**
* 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");
/**
* 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());
assertNotNull(con);
con.close();
// 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());
}
}
// 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());
}
}
}

View File

@@ -8,50 +8,55 @@ import java.io.*;
/**
* Tests for the Encoding class.
*
* $Id: EncodingTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: EncodingTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*/
public class EncodingTest extends TestCase {
public class EncodingTest extends TestCase
{
public EncodingTest(String name) {
super(name);
}
public EncodingTest(String name)
{
super(name);
}
public void testCreation() throws Exception {
Encoding encoding;
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);
}
public void testCreation() throws Exception
{
Encoding encoding;
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);
}
public void testTransformations() throws Exception {
Encoding encoding = Encoding.getEncoding("UNICODE", null);
assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));
public void testTransformations() throws Exception
{
Encoding encoding = Encoding.getEncoding("UNICODE", null);
assertEquals("ab", encoding.decode(new byte[] { 97, 98 }));
assertEquals(2, encoding.encode("ab").length);
assertEquals(97, encoding.encode("a")[0]);
assertEquals(98, encoding.encode("b")[0]);
assertEquals(2, encoding.encode("ab").length);
assertEquals(97, encoding.encode("a")[0]);
assertEquals(98, encoding.encode("b")[0]);
encoding = Encoding.defaultEncoding();
assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
assertEquals(new String(new byte[] { 97 }),
encoding.decode(new byte[] { 97 }));
}
encoding = Encoding.defaultEncoding();
assertEquals("a".getBytes()[0], encoding.encode("a")[0]);
assertEquals(new String(new byte[] { 97 }),
encoding.decode(new byte[] { 97 }));
}
public void testReader() throws Exception {
Encoding encoding = Encoding.getEncoding("SQL_ASCII", null);
InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
Reader reader = encoding.getDecodingReader(stream);
assertEquals(97, reader.read());
assertEquals(98, reader.read());
assertEquals(-1, reader.read());
}
public void testReader() throws Exception
{
Encoding encoding = Encoding.getEncoding("SQL_ASCII", null);
InputStream stream = new ByteArrayInputStream(new byte[] { 97, 98 });
Reader reader = encoding.getDecodingReader(stream);
assertEquals(97, reader.read());
assertEquals(98, reader.read());
assertEquals( -1, reader.read());
}
}

View File

@@ -6,56 +6,65 @@ import java.sql.*;
import java.math.BigDecimal;
/**
* $Id: JBuilderTest.java,v 1.3 2001/09/23 04:11:14 momjian Exp $
* $Id: JBuilderTest.java,v 1.4 2001/10/25 05:59:59 momjian Exp $
*
* Some simple tests to check that the required components needed for JBuilder
* stay working
*
*/
public class JBuilderTest extends TestCase {
public class JBuilderTest extends TestCase
{
public JBuilderTest(String name) {
super(name);
}
public JBuilderTest(String name)
{
super(name);
}
// Set up the fixture for this testcase: the tables for this test.
protected void setUp() throws Exception {
Connection con = JDBC2Tests.openDB();
// Set up the fixture for this testcase: the tables for this test.
protected void setUp() throws Exception
{
Connection con = JDBC2Tests.openDB();
JDBC2Tests.createTable( con, "test_c",
"source text,cost money,imageid int4" );
JDBC2Tests.createTable( con, "test_c",
"source text,cost money,imageid int4" );
JDBC2Tests.closeDB(con);
}
JDBC2Tests.closeDB(con);
}
// 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);
}
// 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);
}
/**
* This tests that Money types work. JDBCExplorer barfs if this fails.
*/
public void testMoney() {
try {
Connection con = JDBC2Tests.openDB();
/**
* This tests that Money types work. JDBCExplorer barfs if this fails.
*/
public void testMoney()
{
try
{
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select cost from test_c");
assertNotNull(rs);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select cost from test_c");
assertNotNull(rs);
while(rs.next()){
double bd = rs.getDouble(1);
}
while (rs.next())
{
double bd = rs.getDouble(1);
}
rs.close();
st.close();
rs.close();
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) {
fail(ex.getMessage());
}
}
JDBC2Tests.closeDB(con);
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
}

View File

@@ -5,43 +5,50 @@ import junit.framework.TestCase;
import java.sql.*;
/**
* $Id: MiscTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: MiscTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
*
*/
public class MiscTest extends TestCase {
public class MiscTest extends TestCase
{
public MiscTest(String name) {
super(name);
}
public MiscTest(String name)
{
super(name);
}
/**
* Some versions of the driver would return rs as a null?
*
* Sasha <ber0806@iperbole.bologna.it> was having this problem.
*
* Added Feb 13 2001
*/
public void testDatabaseSelectNullBug() {
try {
Connection con = JDBC2Tests.openDB();
/**
* Some versions of the driver would return rs as a null?
*
* Sasha <ber0806@iperbole.bologna.it> was having this problem.
*
* Added Feb 13 2001
*/
public void testDatabaseSelectNullBug()
{
try
{
Connection con = JDBC2Tests.openDB();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select datname from pg_database");
assertNotNull(rs);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select datname from pg_database");
assertNotNull(rs);
while(rs.next()){
String s = rs.getString(1);
}
while (rs.next())
{
String s = rs.getString(1);
}
rs.close();
st.close();
rs.close();
st.close();
JDBC2Tests.closeDB(con);
} catch(Exception ex) {
fail(ex.getMessage());
}
}
JDBC2Tests.closeDB(con);
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
}

View File

@@ -5,37 +5,43 @@ import junit.framework.TestCase;
import java.sql.*;
/**
* $Id: TimeTest.java,v 1.2 2001/09/23 04:11:14 momjian Exp $
* $Id: TimeTest.java,v 1.3 2001/10/25 05:59:59 momjian Exp $
*
* Some simple tests based on problems reported by users. Hopefully these will
* help prevent previous problems from re-occuring ;-)
*
*/
public class TimeTest extends TestCase {
public class TimeTest extends TestCase
{
private Connection con;
public TimeTest(String name) {
super(name);
}
protected void setUp() throws Exception {
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 {
protected void tearDown() throws Exception
{
JDBC2Tests.dropTable(con, "testtime");
JDBC2Tests.closeDB(con);
}
/**
* Tests the time methods in ResultSet
*/
public void testGetTime() {
try {
}
/**
* 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'")));
@@ -44,16 +50,20 @@ public class TimeTest extends TestCase {
assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
}
/**
* Tests the time methods in PreparedStatement
*/
public void testSetTime() {
try {
/**
* Tests the time methods in PreparedStatement
*/
public void testSetTime()
{
try
{
PreparedStatement ps = con.prepareStatement(JDBC2Tests.insertSQL("testtime", "?"));
Statement stmt = con.createStatement();
@@ -69,15 +79,18 @@ public class TimeTest extends TestCase {
assertEquals(2, stmt.executeUpdate("DELETE FROM testtime"));
stmt.close();
ps.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
}
/**
* Helper for the TimeTests. It tests what should be in the db
*/
private void timeTest() throws SQLException {
/**
* 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;
@@ -98,11 +111,12 @@ public class TimeTest extends TestCase {
assertTrue(! rs.next());
rs.close();
}
}
private java.sql.Time makeTime(int h, int m, int s) {
private java.sql.Time makeTime(int h, int m, int s)
{
return java.sql.Time.valueOf(JDBC2Tests.fix(h, 2) + ":" +
JDBC2Tests.fix(m, 2) + ":" +
JDBC2Tests.fix(s, 2));
}
}
}

View File

@@ -5,7 +5,7 @@ import junit.framework.TestCase;
import java.sql.*;
/**
* $Id: TimestampTest.java,v 1.4 2001/09/29 03:11:11 momjian Exp $
* $Id: TimestampTest.java,v 1.5 2001/10/25 05:59:59 momjian Exp $
*
* This has been the most controversial pair of methods since 6.5 was released!
*
@@ -13,22 +13,26 @@ import java.sql.*;
* MUST PASS this TestCase!!!
*
*/
public class TimestampTest extends TestCase {
public class TimestampTest extends TestCase
{
private Connection con;
public TimestampTest(String name) {
public TimestampTest(String name)
{
super(name);
}
protected void setUp() throws Exception {
protected void setUp() throws Exception
{
con = JDBC2Tests.openDB();
Statement stmt = con.createStatement();
JDBC2Tests.createTable(con, "testtimestamp", "ts timestamp");
}
protected void tearDown() throws Exception {
protected void tearDown() throws Exception
{
JDBC2Tests.dropTable(con, "testtimestamp");
JDBC2Tests.closeDB(con);
}
@@ -36,19 +40,21 @@ public class TimestampTest extends TestCase {
/**
* Tests the time methods in ResultSet
*/
public void testGetTimestamp() {
try {
public void testGetTimestamp()
{
try
{
Statement stmt = con.createStatement();
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
"'1950-02-07 15:00:00'")));
"'1950-02-07 15:00:00'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp", "'" +
getTimestamp(1970, 6, 2, 8, 13, 0, 0).toString() +
"'")));
getTimestamp(1970, 6, 2, 8, 13, 0, 0).toString() +
"'")));
assertEquals(1, stmt.executeUpdate(JDBC2Tests.insertSQL("testtimestamp",
"'1970-06-02 08:13:00'")));
"'1970-06-02 08:13:00'")));
// Fall through helper
timestampTest();
@@ -56,7 +62,9 @@ public class TimestampTest extends TestCase {
assertEquals(3, stmt.executeUpdate("DELETE FROM testtimestamp"));
stmt.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -64,8 +72,10 @@ public class TimestampTest extends TestCase {
/**
* Tests the time methods in PreparedStatement
*/
public void testSetTimestamp() {
try {
public void testSetTimestamp()
{
try
{
Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement(JDBC2Tests.insertSQL("testtimestamp", "?"));
@@ -85,7 +95,9 @@ public class TimestampTest extends TestCase {
pstmt.close();
stmt.close();
} catch(Exception ex) {
}
catch (Exception ex)
{
fail(ex.getMessage());
}
}
@@ -93,7 +105,8 @@ public class TimestampTest extends TestCase {
/**
* Helper for the TimeTests. It tests what should be in the db
*/
private void timestampTest() throws SQLException {
private void timestampTest() throws SQLException
{
Statement stmt = con.createStatement();
ResultSet rs;
java.sql.Timestamp t;
@@ -115,20 +128,21 @@ public class TimestampTest extends TestCase {
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) + ":" +
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));
JDBC2Tests.fix(f, 9));
}
}