diff options
| author | PostgreSQL Daemon <webmaster@postgresql.org> | 2004-01-19 20:07:14 +0000 |
|---|---|---|
| committer | PostgreSQL Daemon <webmaster@postgresql.org> | 2004-01-19 20:07:14 +0000 |
| commit | 2a9bf5b33d0b82e9f483f6a5ced9d71e1c009441 (patch) | |
| tree | 8c0c38494985b8dbfd2311b5be51fa76a271ba17 /src/interfaces/jdbc/org/postgresql/test | |
| parent | 9bd681a5220186230e0ea0f718a71af7ebe4b560 (diff) | |
JDBC is now on GBorg
Diffstat (limited to 'src/interfaces/jdbc/org/postgresql/test')
34 files changed, 0 insertions, 6507 deletions
diff --git a/src/interfaces/jdbc/org/postgresql/test/README b/src/interfaces/jdbc/org/postgresql/test/README deleted file mode 100644 index b65c2a98009..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/README +++ /dev/null @@ -1,323 +0,0 @@ -PostgreSQL/JDBC Test Suite Howto -================================ -1 Introduction -2 Installation -3 Configuration -4 Running the test suite -5 Extending the test suite with new tests -6 Guidelines for developing new tests -7 Example -8 Running the JDBC 2 test suite from Sun against PostgreSQL -9 Credits, feedback - - -1 Introduction --------------- -The PostgreSQL source tree contains an automated test suite for -the JDBC driver. This document explains how to install, -configure and run this test suite. Furthermore, it offers -guidelines and an example for developers to add new test cases. - -Sun provides two standard JDBC test suites that you may also -find useful. -http://java.sun.com/products/jdbc/download2.html (JDBC 1) -http://java.sun.com/products/jdbc/jdbctestsuite-1_2_1.html (JDBC -2, including J2EE requirements) -The JDBC 2 test suite is covered in section 8 below. The JDBC 1 -test suite is not currently covered in this document. - -2 Installation --------------- -Of course, you need to have a Java 2 JDK or JRE installed. The -standard JDK from Sun is OK. You can download it from -http://java.sun.com/. - -You need to install the Ant build utility. You can download it -from http://jakarta.apache.org/ant/. - -You also need to install the JUnit testing framework. You can -download it from http://www.junit.org/. Add junit.jar to your -CLASSPATH before you perform the following steps. Ant will -dynamically detect that JUnit is present and then build the JDBC -test suite. - -You need to install and build the PostgreSQL source tree. You -can download it from http://www.postgresql.org/devel-corner/. -See README and INSTALL in the top of the tree for more -information. - -You should run ./configure with the command line option ---with-java. You may also want to use --with-pgport to compile a -non-standard default port number (e.g. 5433) into all -components. This will cause the server to listen on this -non-standard port and it will cause the JDBC driver to connect -to this port by default. In this way your testing environment is -easily separated from other PostgreSQL applications on the same -system. - -In this Howto we'll use $JDBC_SRC to refer to the directory -src/interfaces/jdbc of the PostgreSQL source tree in your -environment. The test suite is located in the subdirectory -$JDBC_SRC/org/postgresql/test. - -3 Configuration ---------------- -The test suite requires a PostgreSQL database to run the tests -against and a user to login as. For a full regression test of -the entire PostgreSQL system, you should run the test against a -server built from the same source tree as the driver you're -testing. The tests will create and drop many objects in this -database, so it should not contain production tables to avoid -loss of data. We recommend you assign the following names: - - database: test - username: test - password: password - -These names correspond with the default names set for the test -suite in $JDBC_SRC/build.xml. If you have chosen other names you -need to edit this file and change the properties "database", -"username" and "password" accordingly. - -4 Running the test suite ------------------------- -%cd $JDBC_SRC -%make -%make check - -This will run the command line version of JUnit. If you'd like -to see an animated coloured progress bar as the tests are -executed, you may want to use one of the GUI versions of the -test runner. See the JUnit documentation for more information. - -If the test suite reports errors or failures that you cannot -explain, please post the relevant parts of the output to the -mailing list pgsql-jdbc@postgresql.org. - -5 Extending the test suite with new tests ------------------------------------------ -If you're not familiar with JUnit, we recommend that you -first read the introductory article "JUnit Test Infected: -Programmers Love Writing Tests" on -http://junit.sourceforge.net/doc/testinfected/testing.htm. -Before continuing, you should ensure you understand the -following concepts: test suite, test case, test, fixture, -assertion, failure. - -The test suite consists of test cases, which consist of tests. -A test case is a collection of tests that test a particular -feature. The test suite is a collection of test cases that -together test the driver - and to an extent the PostgreSQL -backend - as a whole. - -If you decide to add a test to an existing test case, all you -need to do is add a method with a name that begins with "test" -and which takes no arguments. JUnit will dynamically find this -method using reflection and run it when it runs the test case. -In your test method you can use the fixture that is setup for it -by the test case. - -If you decide to add a new test case, you should do two things: -1) Add a class that extends junit.framework.TestCase. It should -contain setUp() and tearDown() methods that create and destroy -the fixture respectively. -2) Edit $JDBC_SRC/org/postgresql/test/JDBC2Tests.java and add a -suite.addTestSuite() call for your class. This will make the -test case part of the test suite. - -6 Guidelines for developing new tests -------------------------------------- -Every test should create and drop its own tables. We suggest to -consider database objects (e.g. tables) part of the fixture for -the tests in the test case. The test should also succeed when a -table by the same name already exists in the test database, e.g. -by dropping the table before running the test (ignoring errors). -The recommended pattern for creating and dropping tables can be -found in the example in section 7 below. - -Please note that JUnit provides several convenience methods to -check for conditions. See the TestCase class in the Javadoc -documentation of JUnit, which is installed on your system. For -example, you can compare two integers using -TestCase.assertEquals(int expected, int actual). This method -will print both values in case of a failure. - -To simply report a failure use TestCase.fail(). - -The JUnit FAQ explains how to test for a thrown exception. - -Avoid the use of the deprecated TestCase.assert(), since it will -collide with the new assert keyword in the Java 2 platform -version 1.4. - -As a rule, the test suite should succeed. Any errors or failures -- which may be caused by bugs in the JDBC driver, the backend or -the test suite - should be fixed ASAP. Don't let a test fail -just to make it clear that something needs to be fixed somewhere. -That's what the TODO lists are for. - -Add some comments to your tests to explain to others what it is -you're testing. A long sequence of JDBC method calls and JUnit -assertions can be hard to comprehend. - -For example, in the comments you can explain where a certain test -condition originates from. Is it a JDBC requirement, PostgreSQL -behaviour or the intended implementation of a feature? - -7 Example (incomplete) ----------------------- -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * Test case for ... - */ -public class FooTest extends TestCase { - - private Connection con; - private Statement stmt; - - public FooTest(String name) { - super(name); - } - - protected void setUp() throws Exception { - con = TestUtil.openDB(); - stmt = con.createStatement(); - - // Drop the test table if it already exists for some - // reason. It is not an error if it doesn't exist. - try { - stmt.executeUpdate("DROP TABLE testfoo"); - } catch (SQLException e) { - // Intentionally ignore. We cannot distinguish - // "table does not exist" from other errors, since - // PostgreSQL doesn't support error codes yet. - } - - stmt.executeUpdate( - "CREATE TABLE testfoo(pk INTEGER, col1 INTEGER)"); - stmt.executeUpdate("INSERT INTO testfoo VALUES(1, 0)"); - - // You may want to call con.setAutoCommit(false) at - // this point, if most tests in this test case require - // the use of transactions. - } - - protected void tearDown() throws Exception { - con.setAutoCommit(true); - if (stmt != null) { - stmt.executeUpdate("DROP TABLE testfoo"); - stmt.close(); - } - if (con != null) { - TestUtil.closeDB(con); - } - } - - public void testFoo() { - // Use the assert methods in junit.framework.TestCase - // for the actual tests - - // Just some silly examples - assertNotNull(con); - if (stmt == null) { - fail("Where is my statement?"); - } - } - - public void testBar() { - // Another test. - } -} - -8. Running the JDBC 2 test suite from Sun against PostgreSQL ------------------------------------------------------------- -Download the test suite from -http://java.sun.com/products/jdbc/jdbctestsuite-1_2_1.html -This is the JDBC 2 test suite that includes J2EE requirements. - -1. Configure PostgreSQL so that it accepts TCP/IP connections and - start the server. Prepare PostgreSQL by creating two users (cts1 - and cts2) and two databases (DB1 and DB2) in the cluster that is - going to be used for JDBC testing. - -2. Download the latest release versions of the J2EE, J2SE, and JDBC - test suite from Sun's Java site (http://java.sun.com), and install - according to Sun's documentation. - -3. The following environment variables should be set: - - CTS_HOME=<path where JDBC test suite installed (eg: /usr/local/jdbccts)> - J2EE_HOME=<path where J2EE installed (eg: /usr/local/j2sdkee1.2.1)> - JAVA_HOME=<path where J2SE installed (eg: /usr/local/jdk1.3.1)> - NO_JAVATEST=Y - LOCAL_CLASSES=<path to PostgreSQL JDBC driver jar> - -4. In $J2EE_HOME/config/default.properties: - - jdbc.drivers=org.postgresql.Driver - jdbc.datasources=jdbc/DB1|jdbc:postgresql://localhost:5432/DB1|jdbc/DB2|jdbc:postgresq://localhost:5432/DB2 - - Of course, if PostgreSQL is running on a computer different from - the one running the application server, localhost should be changed - to the proper host. Also, 5432 should be changed to whatever port - PostgreSQL is listening on (5432 is the default). - - In $J2EE_HOME/bin/userconfig.sh: - - Add $CTS_HOME/lib/harness.jar, $CTS_HOME/lib/moo.jar, - $CTS_HOME/lib/util.jar to J2EE_CLASSPATH. Also add the path to - the PostgreSQL JDBC jar to J2EE_CLASSPATH. Set the JAVA_HOME - variable to where you installed the J2SE. You should end up with - something like this: - - CTS_HOME=/home/liams/linux/java/jdbccts - J2EE_CLASSPATH=/home/liams/work/inst/postgresql-7.1.2/share/java/postgresql.jar:$CTS_HOME/lib/harness.jar:$CTS_HOME/lib/moo.jar:$CTS_HOME/lib/util.jar - export J2EE_CLASSPATH - - JAVA_HOME=/home/liams/linux/java/jdk1.3.1 - export JAVA_HOME - - In $CTS_HOME/bin/cts.jte: - - webServerHost=localhost - webServerPort=8000 - servletServerHost=localhost - servletServerPort=8000 - -5. Start the application server (j2ee): - - $ cd $J2EE_HOME - $ bin/j2ee -verbose - - The server can be stopped after the tests have finished: - - $ cd $J2EE_HOME - $ bin/j2ee -stop - -6. Run the JDBC tests: - - $ cd $CTS_HOME/tests/jdbc/ee - $ make jdbc-tests - -At the time of writing of this document, a great number of tests -in this test suite fail. - -9 Credits, feedback -------------------- -The parts of this document describing the PostgreSQL test suite -were originally written by Rene Pijlman. Liam Stewart contributed -the section on the Sun JDBC 2 test suite. - -Please send your questions about the JDBC test suites or suggestions -for improvement to the pgsql-jdbc@postgresql.org mailing list. - -The source of this document is maintained in -src/interfaces/jdbc/org/postgresql/test/README in CVS. Patches for -improvement can be send to the mailing list -pgsql-patches@postgresql.org. - diff --git a/src/interfaces/jdbc/org/postgresql/test/TestUtil.java b/src/interfaces/jdbc/org/postgresql/test/TestUtil.java deleted file mode 100644 index 9ef0c7d899f..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/TestUtil.java +++ /dev/null @@ -1,266 +0,0 @@ -package org.postgresql.test; - -import java.sql.*; -import junit.framework.TestCase; -import java.util.Properties; - -/* - * Utility class for JDBC tests - */ -public class TestUtil -{ - /* - * Returns the Test database JDBC URL - */ - public static String getURL() - { - return "jdbc:postgresql://"+getServer()+":"+getPort()+"/"+getDatabase(); - } - - /* - * Returns the Test server - */ - public static String getServer() - { - return System.getProperty("server"); - } - - /* - * Returns the Test port - */ - public static int getPort() - { - return Integer.parseInt(System.getProperty("port")); - } - - /* - * Returns the Test database - */ - public static String getDatabase() - { - return System.getProperty("database"); - } - - /* - * Returns the Postgresql username - */ - public static String getUser() - { - return System.getProperty("username"); - } - - /* - * Returns the user's password - */ - public static String getPassword() - { - return System.getProperty("password"); - } - - /* - * Helper - opens a connection. - */ - public static java.sql.Connection openDB() - { - return openDB(new Properties()); - } - - /* - * Helper - opens a connection with the allowance for passing - * additional parameters, like "compatible". - */ - public static java.sql.Connection openDB(Properties props) - { - props.setProperty("user",getUser()); - props.setProperty("password",getPassword()); - try - { - Class.forName("org.postgresql.Driver"); - return java.sql.DriverManager.getConnection(getURL(), props); - } - catch (ClassNotFoundException ex) - { - TestCase.fail(ex.getMessage()); - } - catch (SQLException ex) - { - TestCase.fail(ex.getMessage()); - } - return null; - } - - /* - * Helper - closes an open connection. This rewrites SQLException to a failed - * assertion. It's static so other classes can use it. - */ - public static void closeDB(Connection con) - { - try - { - if (con != null) - con.close(); - } - catch (SQLException ex) - { - TestCase.fail(ex.getMessage()); - } - } - - /* - * Helper - creates a test table for use by a test - */ - public static void createTable(Connection con, - String table, - String columns) - { - try - { - Statement st = con.createStatement(); - try - { - // Drop the table - dropTable(con, table); - - // Now create the table - st.executeUpdate("create table " + table + " (" + columns + ")"); - } - finally - { - st.close(); - } - } - catch (SQLException ex) - { - TestCase.fail(ex.getMessage()); - } - } - - /* - * Helper - drops a table - */ - public static void dropTable(Connection con, String table) - { - try - { - Statement stmt = con.createStatement(); - try - { - String sql = "DROP TABLE " + table; - if (haveMinimumServerVersion(con,"7.3")) { - sql += " CASCADE "; - } - stmt.executeUpdate(sql); - } - catch (SQLException ex) - { - // Since every create table issues a drop table - // it's easy to get a table doesn't exist error. - // we want to ignore these, but if we're in a - // transaction we need to restart. - // If the test case wants to catch this error - // itself it should issue the drop SQL directly. - if (ex.getMessage().indexOf("does not exist") != -1) { - if (!con.getAutoCommit()) { - con.rollback(); - } - - } - } - } - catch (SQLException ex) - { - TestCase.fail(ex.getMessage()); - } - } - - /* - * Helper - generates INSERT SQL - very simple - */ - public static String insertSQL(String table, String values) - { - return insertSQL(table, null, values); - } - - public static String insertSQL(String table, String columns, String values) - { - String s = "INSERT INTO " + table; - - if (columns != null) - s = s + " (" + columns + ")"; - - return s + " VALUES (" + values + ")"; - } - - /* - * Helper - generates SELECT SQL - very simple - */ - public static String selectSQL(String table, String columns) - { - return selectSQL(table, columns, null, null); - } - - public static String selectSQL(String table, String columns, String where) - { - return selectSQL(table, columns, where, null); - } - - public static String selectSQL(String table, String columns, String where, String other) - { - String s = "SELECT " + columns + " FROM " + table; - - if (where != null) - s = s + " WHERE " + where; - if (other != null) - s = s + " " + other; - - return s; - } - - /* - * Helper to prefix a number with leading zeros - ugly but it works... - * @param v value to prefix - * @param l number of digits (0-10) - */ - public static String fix(int v, int l) - { - String s = "0000000000".substring(0, l) + Integer.toString(v); - return s.substring(s.length() - l); - } - - /** - * Determine if the given connection is connected to a server with - * a version of at least the given version. - * This is convenient because we are working with a java.sql.Connection, - * not an Postgres connection. - */ - public static boolean haveMinimumServerVersion(Connection con, String version) throws SQLException { - if (con instanceof org.postgresql.jdbc1.AbstractJdbc1Connection) { - return ((org.postgresql.jdbc1.AbstractJdbc1Connection)con).haveMinimumServerVersion(version); - } - return false; - } - - /** - * Print a ResultSet to System.out. - * This is useful for debugging tests. - */ - public static void printResultSet(ResultSet rs) throws SQLException { - ResultSetMetaData rsmd = rs.getMetaData(); - for (int i=1; i<=rsmd.getColumnCount(); i++) { - if (i != 1) { - System.out.print(", "); - } - System.out.print(rsmd.getColumnName(i)); - } - System.out.println(); - while (rs.next()) { - for (int i=1; i<=rsmd.getColumnCount(); i++) { - if (i != 1) { - System.out.print(", "); - } - System.out.print(rs.getString(i)); - } - System.out.println(); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java deleted file mode 100644 index b503ff422b9..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ANTTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.postgresql.test.jdbc2; - -import junit.framework.TestCase; - -public class ANTTest extends TestCase -{ - public ANTTest(String name) - { - super(name); - } - - /* - * This tests the acceptsURL() method with a couple of good and badly formed - * jdbc urls - */ - public void testANT() - { - String url = System.getProperty("database"); - String usr = System.getProperty("username"); - String psw = System.getProperty("password"); - - assertNotNull(url); - assertNotNull(usr); - assertNotNull(psw); - - assertTrue(! url.equals("")); - assertTrue(! usr.equals("")); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java deleted file mode 100644 index e7f27ed575e..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java +++ /dev/null @@ -1,207 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* TODO tests that can be added to this test case - * - SQLExceptions chained to a BatchUpdateException - * - test PreparedStatement as thoroughly as Statement - */ - -/* - * Test case for Statement.batchExecute() - */ -public class BatchExecuteTest extends TestCase -{ - - private Connection con; - - public BatchExecuteTest(String name) - { - super(name); - } - - // Set up the fixture for this testcase: a connection to a database with - // a table for this test. - protected void setUp() throws Exception - { - con = TestUtil.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. - TestUtil.createTable(con, "testbatch", "pk INTEGER, col1 INTEGER"); - - stmt.executeUpdate("INSERT INTO testbatch VALUES (1, 0)"); - - // Generally recommended with batch updates. By default we run all - // tests in this test case with autoCommit disabled. - con.setAutoCommit(false); - } - - // Tear down the fixture for this test case. - protected void tearDown() throws Exception - { - con.setAutoCommit(true); - - TestUtil.dropTable(con, "testbatch"); - TestUtil.closeDB(con); - } - - public void testSupportsBatchUpdates() throws Exception - { - DatabaseMetaData dbmd = con.getMetaData(); - assertTrue(dbmd.supportsBatchUpdates()); - } - - private void assertCol1HasValue(int expected) throws Exception - { - Statement getCol1 = con.createStatement(); - - ResultSet rs = - getCol1.executeQuery("SELECT col1 FROM testbatch WHERE pk = 1"); - assertTrue(rs.next()); - - int actual = rs.getInt("col1"); - - assertEquals(expected, actual); - - assertEquals(false, rs.next()); - - rs.close(); - getCol1.close(); - } - - public void testExecuteEmptyBatch() throws Exception - { - Statement stmt = con.createStatement(); - int[] updateCount = stmt.executeBatch(); - assertEquals(0, updateCount.length); - - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); - stmt.clearBatch(); - updateCount = stmt.executeBatch(); - assertEquals(0, updateCount.length); - stmt.close(); - } - - public void testClearBatch() throws Exception - { - Statement stmt = con.createStatement(); - - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); - assertCol1HasValue(0); - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); - assertCol1HasValue(0); - stmt.clearBatch(); - assertCol1HasValue(0); - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 4 WHERE pk = 1"); - assertCol1HasValue(0); - stmt.executeBatch(); - assertCol1HasValue(4); - con.commit(); - assertCol1HasValue(4); - - stmt.close(); - } - - public void testSelectThrowsException() throws Exception - { - Statement stmt = con.createStatement(); - - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1"); - stmt.addBatch("SELECT col1 FROM testbatch WHERE pk = 1"); - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1"); - - try - { - stmt.executeBatch(); - fail("Should raise a BatchUpdateException because of the SELECT"); - } - catch (BatchUpdateException e) - { - int [] updateCounts = e.getUpdateCounts(); - assertEquals(1, updateCounts.length); - assertEquals(1, updateCounts[0]); - } - catch (SQLException e) - { - fail( "Should throw a BatchUpdateException instead of " + - "a generic SQLException: " + e); - } - - stmt.close(); - } - - public void testPreparedStatement() throws Exception - { - PreparedStatement pstmt = con.prepareStatement( - "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.addBatch(); - assertCol1HasValue(0); - - pstmt.setInt(1, 2); - pstmt.addBatch(); - assertCol1HasValue(0); - - pstmt.setInt(1, 4); - pstmt.addBatch(); - assertCol1HasValue(0); - - pstmt.executeBatch(); - assertCol1HasValue(7); - - //now test to see that we can still use the statement after the execute - pstmt.setInt(1, 3); - pstmt.addBatch(); - assertCol1HasValue(7); - - pstmt.executeBatch(); - assertCol1HasValue(10); - - con.commit(); - assertCol1HasValue(10); - - con.rollback(); - assertCol1HasValue(10); - - pstmt.close(); - } - - 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(); - con.rollback(); - assertCol1HasValue(0); - - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 4 WHERE pk = 1"); - stmt.addBatch("UPDATE testbatch SET col1 = col1 + 8 WHERE pk = 1"); - - // The statement has been added to the batch, but it should not yet - // have been executed. - assertCol1HasValue(0); - - int[] updateCounts = stmt.executeBatch(); - assertEquals(2, updateCounts.length); - assertEquals(1, updateCounts[0]); - assertEquals(1, updateCounts[1]); - - assertCol1HasValue(12); - con.commit(); - assertCol1HasValue(12); - con.rollback(); - assertCol1HasValue(12); - - stmt.close(); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java deleted file mode 100644 index 3032fc22fd6..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java +++ /dev/null @@ -1,290 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.io.*; -import java.sql.*; - -import org.postgresql.largeobject.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/BlobTest.java,v 1.10 2003/11/29 22:41:23 pgsql 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 -{ - - private Connection con; - - private static final int LOOP = 0; // LargeObject API using loop - private static final int NATIVE_STREAM = 1; // LargeObject API using OutputStream - private static final int JDBC_STREAM = 2; // JDBC API using OutputStream - - public BlobTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable(con, "testblob", "id name,lo oid"); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "testblob"); - TestUtil.closeDB(con); - } - - /* - * Tests one method of uploading a blob to the database - */ - public void testUploadBlob_LOOP() - { - try - { - con.setAutoCommit(false); - assertTrue(!con.getAutoCommit()); - - assertTrue(uploadFile("build.xml", LOOP) > 0); - - // Now compare the blob & the file. Note this actually tests the - // InputStream implementation! - assertTrue(compareBlobsLOAPI()); - assertTrue(compareBlobs()); - assertTrue(compareClobs()); - - con.setAutoCommit(true); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Tests one method of uploading a blob to the database - */ - public void testUploadBlob_NATIVE() - { - try - { - con.setAutoCommit(false); - assertTrue(!con.getAutoCommit()); - - assertTrue(uploadFile("build.xml", NATIVE_STREAM) > 0); - - // Now compare the blob & the file. Note this actually tests the - // InputStream implementation! - assertTrue(compareBlobs()); - - con.setAutoCommit(true); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Helper - uploads a file into a blob using old style methods. We use this - * because it always works, and we can use it as a base to test the new - * methods. - */ - private int uploadFile(String file, int method) throws Exception - { - LargeObjectManager lom = ((org.postgresql.PGConnection)con).getLargeObjectAPI(); - - FileInputStream fis = new FileInputStream(file); - - int oid = lom.create(LargeObjectManager.READWRITE); - LargeObject blob = lom.open(oid); - - int s, t; - byte buf[]; - OutputStream os; - - switch (method) - { - case LOOP: - buf = new byte[2048]; - t = 0; - while ((s = fis.read(buf, 0, buf.length)) > 0) - { - t += s; - blob.write(buf, 0, s); - } - break; - - case NATIVE_STREAM: - os = blob.getOutputStream(); - s = fis.read(); - while (s > -1) - { - os.write(s); - s = fis.read(); - } - os.close(); - break; - - case JDBC_STREAM: - File f = new File(file); - PreparedStatement ps = con.prepareStatement(TestUtil.insertSQL("testblob", "?")); - ps.setBinaryStream(1, fis, (int) f.length()); - ps.execute(); - break; - - default: - assertTrue("Unknown method in uploadFile", false); - } - - blob.close(); - fis.close(); - - // Insert into the table - Statement st = con.createStatement(); - st.executeUpdate(TestUtil.insertSQL("testblob", "id,lo", "'" + file + "'," + oid)); - con.commit(); - st.close(); - - return oid; - } - - /* - * Helper - compares the blobs in a table with a local file. Note this uses - * the postgresql specific Large Object API - */ - private boolean compareBlobsLOAPI() throws Exception - { - boolean result = true; - - LargeObjectManager lom = ((org.postgresql.PGConnection)con).getLargeObjectAPI(); - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery(TestUtil.selectSQL("testblob", "id,lo")); - assertNotNull(rs); - - while (rs.next()) - { - String file = rs.getString(1); - int oid = rs.getInt(2); - - FileInputStream fis = new FileInputStream(file); - LargeObject blob = lom.open(oid); - InputStream bis = blob.getInputStream(); - - int f = fis.read(); - int b = bis.read(); - int c = 0; - while (f >= 0 && b >= 0 & result) - { - result = (f == b); - f = fis.read(); - b = bis.read(); - c++; - } - result = result && f == -1 && b == -1; - - if (!result) - assertTrue("Large Object API Blob compare failed at " + c + " of " + blob.size(), false); - - blob.close(); - fis.close(); - } - rs.close(); - st.close(); - - return result; - } - - /* - * Helper - compares the blobs in a table with a local file. This uses the - * jdbc java.sql.Blob api - */ - private boolean compareBlobs() throws Exception - { - boolean result = true; - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery(TestUtil.selectSQL("testblob", "id,lo")); - assertNotNull(rs); - - while (rs.next()) - { - String file = rs.getString(1); - Blob blob = rs.getBlob(2); - - FileInputStream fis = new FileInputStream(file); - InputStream bis = blob.getBinaryStream(); - - int f = fis.read(); - int b = bis.read(); - int c = 0; - while (f >= 0 && b >= 0 & result) - { - result = (f == b); - f = fis.read(); - b = bis.read(); - c++; - } - result = result && f == -1 && b == -1; - - if (!result) - assertTrue("JDBC API Blob compare failed at " + c + " of " + blob.length(), false); - - bis.close(); - fis.close(); - } - rs.close(); - st.close(); - - return result; - } - - /* - * Helper - compares the clobs in a table with a local file. - */ - private boolean compareClobs() throws Exception - { - boolean result = true; - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery(TestUtil.selectSQL("testblob", "id,lo")); - assertNotNull(rs); - - while (rs.next()) - { - String file = rs.getString(1); - Clob clob = rs.getClob(2); - - FileInputStream fis = new FileInputStream(file); - InputStream bis = clob.getAsciiStream(); - - int f = fis.read(); - int b = bis.read(); - int c = 0; - while (f >= 0 && b >= 0 & result) - { - result = (f == b); - f = fis.read(); - b = bis.read(); - c++; - } - result = result && f == -1 && b == -1; - - if (!result) - assertTrue("Clob compare failed at " + c + " of " + clob.length(), false); - - bis.close(); - fis.close(); - } - rs.close(); - st.close(); - - return result; - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/CallableStmtTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/CallableStmtTest.java deleted file mode 100644 index 3f4538efa27..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/CallableStmtTest.java +++ /dev/null @@ -1,124 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import java.sql.CallableStatement; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Types; - -import junit.framework.TestCase; - -/* - * CallableStatement tests. - * @author Paul Bethe - */ -public class CallableStmtTest extends TestCase -{ - private Connection con; - - public CallableStmtTest (String name) - { - super(name); - } - - protected void setUp() throws Exception - { - 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 " + - "return ''bob''; end; ' LANGUAGE 'plpgsql';"); - 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; " + - "begin return 42; end; ' LANGUAGE 'plpgsql';"); - stmt.close (); - } - - protected void tearDown() throws Exception - { - Statement stmt = con.createStatement (); - stmt.execute ("drop FUNCTION testspg__getString (varchar);"); - stmt.execute ("drop FUNCTION testspg__getDouble (float);"); - stmt.execute ("drop FUNCTION testspg__getInt (int);"); - stmt.execute ("drop FUNCTION testspg__getNumeric (numeric);"); - TestUtil.closeDB(con); - } - - - final String func = "{ ? = call "; - final String pkgName = "testspg__"; - // protected void runTest () throws Throwable { - //testGetString (); - //} - - public void testGetDouble () throws Throwable - { - CallableStatement call = con.prepareCall (func + pkgName + "getDouble (?) }"); - call.setDouble (2, (double)3.04); - call.registerOutParameter (1, Types.DOUBLE); - call.execute (); - double result = call.getDouble (1); - assertTrue ("correct return from getString ()", result == 42.42); - } - - public void testGetInt () throws Throwable - { - CallableStatement call = con.prepareCall (func + pkgName + "getInt (?) }"); - call.setInt (2, 4); - call.registerOutParameter (1, Types.INTEGER); - call.execute (); - int result = call.getInt (1); - assertTrue ("correct return from getString ()", result == 42); - } - - public void testGetNumeric () throws Throwable - { - 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 ()", - result.equals (new java.math.BigDecimal(42))); - } - - public void testGetString () throws Throwable - { - CallableStatement call = con.prepareCall (func + pkgName + "getString (?) }"); - call.setString (2, "foo"); - call.registerOutParameter (1, Types.VARCHAR); - call.execute (); - String result = call.getString (1); - assertTrue ("correct return from getString ()", result.equals ("bob")); - - } - - public void testBadStmt () throws Throwable - { - tryOneBadStmt ("{ ?= " + pkgName + "getString (?) }"); - tryOneBadStmt ("{ ?= call getString (?) "); - tryOneBadStmt ("{ = ? call getString (?); }"); - } - - protected void tryOneBadStmt (String sql) throws Throwable - { - boolean wasCaught = false; - try - { - CallableStatement call = con.prepareCall (sql); - } - catch (SQLException e) - { - wasCaught = true; // good -> this statement was missing something - } - assertTrue ("bad statment ('" + sql + "')was not caught", wasCaught); - } - -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java deleted file mode 100644 index 7460f7dc81e..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java +++ /dev/null @@ -1,350 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * TestCase to test the internal functionality of org.postgresql.jdbc2.Connection - * and it's superclass. - * - * PS: Do you know how difficult it is to type on a train? ;-) - * - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/ConnectionTest.java,v 1.11 2003/11/29 22:41:23 pgsql Exp $ - */ - -public class ConnectionTest extends TestCase -{ - - /* - * 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 = TestUtil.openDB(); - - TestUtil.createTable(con, "test_a", "imagename name,image oid,id int4"); - TestUtil.createTable(con, "test_c", "source text,cost money,imageid int4"); - - TestUtil.closeDB(con); - } - - // Tear down the fixture for this test case. - protected void tearDown() throws Exception - { - Connection con = TestUtil.openDB(); - - TestUtil.dropTable(con, "test_a"); - TestUtil.dropTable(con, "test_c"); - - TestUtil.closeDB(con); - } - - /* - * Tests the two forms of createStatement() - */ - public void testCreateStatement() - { - try - { - java.sql.Connection conn = TestUtil.openDB(); - - // A standard Statement - java.sql.Statement stat = conn.createStatement(); - assertNotNull(stat); - stat.close(); - - // Ask for Updateable ResultSets - stat = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE); - assertNotNull(stat); - stat.close(); - - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } - - /* - * Tests the two forms of prepareStatement() - */ - public void testPrepareStatement() - { - try - { - java.sql.Connection conn = TestUtil.openDB(); - - String sql = "select source,cost,imageid from test_c"; - - // A standard Statement - java.sql.PreparedStatement stat = conn.prepareStatement(sql); - assertNotNull(stat); - stat.close(); - - // Ask for Updateable ResultSets - stat = conn.prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE); - assertNotNull(stat); - stat.close(); - - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } - - /* - * Put the test for createPrepareCall here - */ - public void testPrepareCall() - {} - - /* - * Test nativeSQL - */ - public void testNativeSQL() - { - // For now do nothing as it returns itself - } - - /* - * Test autoCommit (both get & set) - */ - public void testTransactions() - { - try - { - java.sql.Connection con = TestUtil.openDB(); - java.sql.Statement st; - java.sql.ResultSet rs; - - // Turn it off - con.setAutoCommit(false); - assertTrue(!con.getAutoCommit()); - - // Turn it back on - con.setAutoCommit(true); - assertTrue(con.getAutoCommit()); - - // Now test commit - st = con.createStatement(); - st.executeUpdate("insert into test_a (imagename,image,id) values ('comttest',1234,5678)"); - - con.setAutoCommit(false); - - // Now update image to 9876 and commit - st.executeUpdate("update test_a set image=9876 where id=5678"); - con.commit(); - rs = st.executeQuery("select image from test_a where id=5678"); - assertTrue(rs.next()); - assertEquals(9876, rs.getInt(1)); - rs.close(); - - // Now try to change it but rollback - st.executeUpdate("update test_a set image=1111 where id=5678"); - con.rollback(); - rs = st.executeQuery("select image from test_a where id=5678"); - assertTrue(rs.next()); - assertEquals(9876, rs.getInt(1)); // Should not change! - rs.close(); - - TestUtil.closeDB(con); - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } - - /* - * Simple test to see if isClosed works. - */ - public void testIsClosed() - { - try - { - Connection con = TestUtil.openDB(); - - // Should not say closed - assertTrue(!con.isClosed()); - - TestUtil.closeDB(con); - - // Should now say closed - assertTrue(con.isClosed()); - - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } - - /* - * Test the warnings system - */ - public void testWarnings() - { - try - { - Connection con = TestUtil.openDB(); - - String testStr = "This Is OuR TeSt message"; - - // The connection must be ours! - assertTrue(con instanceof org.postgresql.PGConnection); - - // Clear any existing warnings - con.clearWarnings(); - - // Set the test warning - ((org.postgresql.jdbc2.AbstractJdbc2Connection)con).addWarning(testStr); - - // Retrieve it - SQLWarning warning = con.getWarnings(); - assertNotNull(warning); - assertEquals(testStr, warning.getMessage()); - - // Finally test clearWarnings() this time there must be something to delete - con.clearWarnings(); - assertTrue(con.getWarnings() == null); - - TestUtil.closeDB(con); - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } - - /* - * Transaction Isolation Levels - */ - public void testTransactionIsolation() - { - try - { - Connection con = TestUtil.openDB(); - - // PostgreSQL defaults to READ COMMITTED - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - // Begin a transaction - con.setAutoCommit(false); - - // The isolation level should not have changed - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - - // Note the behavior on when a transaction starts is different - // under 7.3 than previous versions. In 7.3 a transaction - // starts with the first sql command, whereas previously - // you were always in a transaction in autocommit=false - // so we issue a select to ensure we are in a transaction - Statement stmt = con.createStatement(); - stmt.executeQuery("select 1"); - - // Now change the default for future transactions - con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); - - // Since the call to setTransactionIsolation() above was made - // inside the transaction, the isolation level of the current - // transaction did not change. It affects only future ones. - // This behaviour is recommended by the JDBC spec. - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - // Begin a new transaction - con.commit(); - stmt.executeQuery("select 1"); - - // Now we should see the new isolation level - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - - // Repeat the steps above with the transition back to - // READ COMMITTED. - con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - con.commit(); - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - // Now run some tests with autocommit enabled. - con.setAutoCommit(true); - - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - - con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); - assertEquals(Connection.TRANSACTION_READ_COMMITTED, con.getTransactionIsolation()); - - // Test if a change of isolation level before beginning the - // transaction affects the isolation level inside the transaction. - con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - con.setAutoCommit(false); - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - con.setAutoCommit(true); - assertEquals(Connection.TRANSACTION_SERIALIZABLE, - con.getTransactionIsolation()); - con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - con.setAutoCommit(false); - assertEquals(Connection.TRANSACTION_READ_COMMITTED, - con.getTransactionIsolation()); - - TestUtil.closeDB(con); - } - catch ( SQLException ex ) - { - fail( ex.getMessage() ); - } - } - - /* - * JDBC2 Type mappings - */ - public void testTypeMaps() - { - try - { - Connection con = TestUtil.openDB(); - - // 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()); - - // restore the old one - con.setTypeMap(oldmap); - assertEquals(oldmap, con.getTypeMap()); - - TestUtil.closeDB(con); - } - catch (SQLException ex) - { - assertTrue(ex.getMessage(), false); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java deleted file mode 100644 index 716a2cb9b4f..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java +++ /dev/null @@ -1,204 +0,0 @@ -package org.postgresql.test.jdbc2; - -import java.sql.*; - -import junit.framework.TestCase; - -import org.postgresql.test.TestUtil; - -/* - * Tests for using non-zero setFetchSize(). - */ -public class CursorFetchTest extends TestCase -{ - private Connection con; - - public CursorFetchTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable(con, "test_fetch", "value integer"); - con.setAutoCommit(false); - } - - protected void tearDown() throws Exception - { - con.rollback(); - con.setAutoCommit(true); - TestUtil.dropTable(con, "test_fetch"); - TestUtil.closeDB(con); - } - - protected void createRows(int count) throws Exception - { - PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(?)"); - for (int i = 0; i < count; ++i) { - stmt.setInt(1,i); - stmt.executeUpdate(); - } - } - - // Test various fetchsizes. - public void testBasicFetch() throws Exception - { - createRows(100); - - PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value"); - int[] testSizes = { 0, 1, 49, 50, 51, 99, 100, 101 }; - for (int i = 0; i < testSizes.length; ++i) { - stmt.setFetchSize(testSizes[i]); - assertEquals(testSizes[i], stmt.getFetchSize()); - - ResultSet rs = stmt.executeQuery(); - assertEquals(testSizes[i], rs.getFetchSize()); - - int count = 0; - while (rs.next()) { - assertEquals("query value error with fetch size " + testSizes[i], count, rs.getInt(1)); - ++count; - } - - assertEquals("total query size error with fetch size " + testSizes[i], 100, count); - } - } - - // - // Tests for ResultSet.setFetchSize(). - // - - // test one: - // set fetchsize = 0 - // run query (all rows should be fetched) - // set fetchsize = 50 (should have no effect) - // process results - public void testResultSetFetchSizeOne() throws Exception - { - createRows(100); - - PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value"); - stmt.setFetchSize(0); - ResultSet rs = stmt.executeQuery(); - rs.setFetchSize(50); // Should have no effect. - - int count = 0; - while (rs.next()) { - assertEquals(count, rs.getInt(1)); - ++count; - } - - assertEquals(100, count); - } - - // test two: - // set fetchsize = 25 - // run query (25 rows fetched) - // set fetchsize = 0 - // process results: - // process 25 rows - // should do a FETCH ALL to get more data - // process 75 rows - public void testResultSetFetchSizeTwo() throws Exception - { - createRows(100); - - PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value"); - stmt.setFetchSize(25); - ResultSet rs = stmt.executeQuery(); - rs.setFetchSize(0); - - int count = 0; - while (rs.next()) { - assertEquals(count, rs.getInt(1)); - ++count; - } - - assertEquals(100, count); - } - - // test three: - // set fetchsize = 25 - // run query (25 rows fetched) - // set fetchsize = 50 - // process results: - // process 25 rows. should NOT hit end-of-results here. - // do a FETCH FORWARD 50 - // process 50 rows - // do a FETCH FORWARD 50 - // process 25 rows. end of results. - public void testResultSetFetchSizeThree() throws Exception - { - createRows(100); - - PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value"); - stmt.setFetchSize(25); - ResultSet rs = stmt.executeQuery(); - rs.setFetchSize(50); - - int count = 0; - while (rs.next()) { - assertEquals(count, rs.getInt(1)); - ++count; - } - - assertEquals(100, count); - } - - // test four: - // set fetchsize = 50 - // run query (50 rows fetched) - // set fetchsize = 25 - // process results: - // process 50 rows. - // do a FETCH FORWARD 25 - // process 25 rows - // do a FETCH FORWARD 25 - // process 25 rows. end of results. - public void testResultSetFetchSizeFour() throws Exception - { - createRows(100); - - PreparedStatement stmt = con.prepareStatement("select * from test_fetch order by value"); - stmt.setFetchSize(50); - ResultSet rs = stmt.executeQuery(); - rs.setFetchSize(25); - - int count = 0; - while (rs.next()) { - assertEquals(count, rs.getInt(1)); - ++count; - } - - assertEquals(100, count); - } - - // Test odd queries that should not be transformed into cursor-based fetches. - public void TODO_FAILS_testInsert() throws Exception - { - // INSERT should not be transformed. - PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(1)"); - stmt.setFetchSize(100); // Should be meaningless. - stmt.executeUpdate(); - } - - public void TODO_FAILS_testMultistatement() throws Exception - { - // Queries with multiple statements should not be transformed. - - createRows(100); // 0 .. 99 - PreparedStatement stmt = con.prepareStatement("insert into test_fetch(value) values(100); select * from test_fetch order by value"); - stmt.setFetchSize(10); - ResultSet rs = stmt.executeQuery(); - - int count = 0; - while (rs.next()) { - assertEquals(count, rs.getInt(1)); - ++count; - } - - assertEquals(101, count); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataPropertiesTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataPropertiesTest.java deleted file mode 100644 index adf1dea2a42..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataPropertiesTest.java +++ /dev/null @@ -1,339 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * TestCase to test the internal functionality of - * org.postgresql.jdbc2.DatabaseMetaData's various properties. - * Methods which return a ResultSet are tested elsewhere. - * This avoids a complicated setUp/tearDown for something like - * assertTrue(dbmd.nullPlusNonNullIsNull()); - */ - -public class DatabaseMetaDataPropertiesTest extends TestCase -{ - - private Connection con; - /* - * Constructor - */ - public DatabaseMetaDataPropertiesTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - } - protected void tearDown() throws Exception - { - TestUtil.closeDB( con ); - } - - /* - * The spec says this may return null, but we always do! - */ - public void testGetMetaData() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - /* - * Test default capabilities - */ - public void testCapabilities() - { - try - { - - 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()); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) - assertTrue(dbmd.supportsANSI92EntryLevelSQL()); - else - assertTrue(!dbmd.supportsANSI92EntryLevelSQL()); - assertTrue(!dbmd.supportsANSI92IntermediateSQL()); - assertTrue(!dbmd.supportsANSI92FullSQL()); - - assertTrue(dbmd.supportsIntegrityEnhancementFacility()); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - - public void testJoins() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(dbmd.supportsOuterJoins()); - assertTrue(dbmd.supportsFullOuterJoins()); - assertTrue(dbmd.supportsLimitedOuterJoins()); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testCursors() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(!dbmd.supportsPositionedDelete()); - assertTrue(!dbmd.supportsPositionedUpdate()); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testValues() - { - try { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - int indexMaxKeys = dbmd.getMaxColumnsInIndex(); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertEquals(indexMaxKeys,32); - } else { - assertEquals(indexMaxKeys,16); - } - } catch (SQLException sqle) { - fail(sqle.getMessage()); - } - } - - public void testNulls() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(!dbmd.nullsAreSortedAtStart()); - assertTrue( dbmd.nullsAreSortedAtEnd() != TestUtil.haveMinimumServerVersion(con,"7.2")); - assertTrue( dbmd.nullsAreSortedHigh() == TestUtil.haveMinimumServerVersion(con,"7.2")); - assertTrue(!dbmd.nullsAreSortedLow()); - - assertTrue(dbmd.nullPlusNonNullIsNull()); - - assertTrue(dbmd.supportsNonNullableColumns()); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testLocalFiles() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(!dbmd.usesLocalFilePerTable()); - assertTrue(!dbmd.usesLocalFiles()); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testIdentifiers() - { - try - { - - 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("\"")); - - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testTables() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - // we can add columns - assertTrue(dbmd.supportsAlterTableWithAddColumn()); - - // we can only drop columns in >= 7.3 - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(dbmd.supportsAlterTableWithDropColumn()); - } else { - assertTrue(!dbmd.supportsAlterTableWithDropColumn()); - } - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testSelect() - { - try - { - - 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 - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testDBParams() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(dbmd.getURL().equals(TestUtil.getURL())); - assertTrue(dbmd.getUserName().equals(TestUtil.getUser())); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testDbProductDetails() - { - try - { - 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 - //assertTrue(dbmd.getDatabaseProductVersion().startsWith( - // Integer.toString(pc.getDriver().getMajorVersion()) - // + "." - // + Integer.toString(pc.getDriver().getMinorVersion()))); - assertTrue(dbmd.getDriverName().equals("PostgreSQL Native Driver")); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testDriverVersioning() - { - try - { - assertTrue(con instanceof org.postgresql.PGConnection); - org.postgresql.jdbc2.AbstractJdbc2Connection pc = (org.postgresql.jdbc2.AbstractJdbc2Connection) con; - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - assertTrue(dbmd.getDriverVersion().equals(org.postgresql.Driver.getVersion())); - assertTrue(dbmd.getDriverMajorVersion() == pc.getDriver().getMajorVersion()); - assertTrue(dbmd.getDriverMinorVersion() == pc.getDriver().getMinorVersion()); - - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } -} - diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java deleted file mode 100644 index dd7bef0f975..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java +++ /dev/null @@ -1,527 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * TestCase to test the internal functionality of org.postgresql.jdbc2.DatabaseMetaData - * - * PS: Do you know how difficult it is to type on a train? ;-) - * - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/DatabaseMetaDataTest.java,v 1.22 2003/12/12 18:30:27 davec Exp $ - */ - -public class DatabaseMetaDataTest extends TestCase -{ - - private Connection con; - /* - * Constructor - */ - public DatabaseMetaDataTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable( con, "testmetadata", "id int4, name text, updated timestamp" ); - Statement stmt = con.createStatement(); - //we add the following comments to ensure the joins to the comments - //are done correctly. This ensures we correctly test that case. - stmt.execute("comment on table testmetadata is 'this is a table comment'"); - stmt.execute("comment on column testmetadata.id is 'this is a column comment'"); - } - protected void tearDown() throws Exception - { - TestUtil.dropTable( con, "testmetadata" ); - - TestUtil.closeDB( con ); - } - - public void testTables() - { - try - { - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - ResultSet rs = dbmd.getTables( null, null, "testmetadat%", new String[] {"TABLE"}); - assertTrue( rs.next() ); - String tableName = rs.getString("TABLE_NAME"); - assertEquals( "testmetadata", tableName ); - String tableType = rs.getString("TABLE_TYPE"); - assertEquals( "TABLE", tableType ); - //There should only be one row returned - assertTrue( "getTables() returned too many rows", rs.next() == false); - rs.close(); - - rs = dbmd.getColumns("", "", "test%", "%" ); - assertTrue( rs.next() ); - assertEquals( "testmetadata", rs.getString("TABLE_NAME") ); - assertEquals( "id", rs.getString("COLUMN_NAME") ); - assertEquals( java.sql.Types.INTEGER, rs.getInt("DATA_TYPE") ); - - assertTrue( rs.next() ); - assertEquals( "testmetadata", rs.getString("TABLE_NAME") ); - assertEquals( "name", rs.getString("COLUMN_NAME") ); - assertEquals( java.sql.Types.VARCHAR, rs.getInt("DATA_TYPE") ); - - assertTrue( rs.next() ); - assertEquals( "testmetadata", rs.getString("TABLE_NAME") ); - assertEquals( "updated", rs.getString("COLUMN_NAME") ); - assertEquals( java.sql.Types.TIMESTAMP, rs.getInt("DATA_TYPE") ); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testCrossReference() - { - try - { - Connection con1 = TestUtil.openDB(); - - 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 )" ); - - - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - ResultSet rs = dbmd.getCrossReference(null, "", "vv", null, "", "ww" ); - - for (int j = 1; rs.next(); j++ ) - { - - String pkTableName = rs.getString( "PKTABLE_NAME" ); - assertEquals ( "vv", pkTableName ); - - String pkColumnName = rs.getString( "PKCOLUMN_NAME" ); - assertTrue( pkColumnName.equals("a") || pkColumnName.equals("b")); - - String fkTableName = rs.getString( "FKTABLE_NAME" ); - assertEquals( "ww", fkTableName ); - - String fkColumnName = rs.getString( "FKCOLUMN_NAME" ); - assertTrue( fkColumnName.equals( "m" ) || fkColumnName.equals( "n" ) ) ; - - String fkName = rs.getString( "FK_NAME" ); - if (TestUtil.haveMinimumServerVersion(con1,"7.3")) { - assertTrue(fkName.startsWith("$1")); - } else { - assertTrue( fkName.startsWith( "<unnamed>") ); - } - - String pkName = rs.getString( "PK_NAME" ); - assertEquals( "vv_pkey", pkName ); - - int keySeq = rs.getInt( "KEY_SEQ" ); - assertEquals( j, keySeq ); - } - - - TestUtil.dropTable( con1, "vv" ); - TestUtil.dropTable( con1, "ww" ); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testForeignKeyActions() - { - try { - Connection conn = TestUtil.openDB(); - TestUtil.createTable(conn, "pkt", "id int primary key"); - TestUtil.createTable(conn, "fkt1", "id int references pkt on update restrict on delete cascade"); - TestUtil.createTable(conn, "fkt2", "id int references pkt on update set null on delete set default"); - DatabaseMetaData dbmd = conn.getMetaData(); - - ResultSet rs = dbmd.getImportedKeys(null,"","fkt1"); - assertTrue(rs.next()); - assertTrue(rs.getInt("UPDATE_RULE") == DatabaseMetaData.importedKeyRestrict); - assertTrue(rs.getInt("DELETE_RULE") == DatabaseMetaData.importedKeyCascade); - rs.close(); - - rs = dbmd.getImportedKeys(null,"","fkt2"); - assertTrue(rs.next()); - assertTrue(rs.getInt("UPDATE_RULE") == DatabaseMetaData.importedKeySetNull); - assertTrue(rs.getInt("DELETE_RULE") == DatabaseMetaData.importedKeySetDefault); - rs.close(); - - TestUtil.dropTable(conn,"fkt2"); - TestUtil.dropTable(conn,"fkt1"); - TestUtil.dropTable(conn,"pkt"); - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testForeignKeysToUniqueIndexes() - { - try - { - if (!TestUtil.haveMinimumServerVersion(con,"7.4")) - return; - - Connection con1 = TestUtil.openDB(); - TestUtil.createTable( con1, "pkt", "a int not null, b int not null, CONSTRAINT pkt_pk_a PRIMARY KEY (a), CONSTRAINT pkt_un_b UNIQUE (b)"); - TestUtil.createTable( con1, "fkt", "c int, d int, CONSTRAINT fkt_fk_c FOREIGN KEY (c) REFERENCES pkt(b)"); - - DatabaseMetaData dbmd = con.getMetaData(); - ResultSet rs = dbmd.getImportedKeys("","","fkt"); - int j = 0; - for (; rs.next(); j++) - { - assertTrue("pkt".equals(rs.getString("PKTABLE_NAME"))); - assertTrue("fkt".equals(rs.getString("FKTABLE_NAME"))); - assertTrue("pkt_un_b".equals(rs.getString("PK_NAME"))); - assertTrue("b".equals(rs.getString("PKCOLUMN_NAME"))); - } - assertTrue(j == 1); - - TestUtil.dropTable(con1, "fkt"); - TestUtil.dropTable(con1, "pkt"); - con1.close(); - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testMultiColumnForeignKeys() - { - try - { - Connection con1 = TestUtil.openDB(); - TestUtil.createTable( con1, "pkt", "a int not null, b int not null, CONSTRAINT pkt_pk PRIMARY KEY (a,b)"); - TestUtil.createTable( con1, "fkt", "c int, d int, CONSTRAINT fkt_fk_pkt FOREIGN KEY (c,d) REFERENCES pkt(b,a)"); - - DatabaseMetaData dbmd = con.getMetaData(); - ResultSet rs = dbmd.getImportedKeys("","","fkt"); - int j = 0; - for (; rs.next(); j++) - { - assertTrue("pkt".equals(rs.getString("PKTABLE_NAME"))); - assertTrue("fkt".equals(rs.getString("FKTABLE_NAME"))); - assertTrue(j+1 == rs.getInt("KEY_SEQ")); - if (j == 0) { - assertTrue("b".equals(rs.getString("PKCOLUMN_NAME"))); - assertTrue("c".equals(rs.getString("FKCOLUMN_NAME"))); - } else { - assertTrue("a".equals(rs.getString("PKCOLUMN_NAME"))); - assertTrue("d".equals(rs.getString("FKCOLUMN_NAME"))); - } - } - assertTrue(j == 2); - - TestUtil.dropTable(con1, "fkt"); - TestUtil.dropTable(con1, "pkt"); - con1.close(); - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - 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" ); - - 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++ ) - { - - String pkTableName = rs.getString( "PKTABLE_NAME" ); - assertTrue ( pkTableName.equals("people") || pkTableName.equals("policy") ); - - String pkColumnName = rs.getString( "PKCOLUMN_NAME" ); - assertEquals( "id", pkColumnName ); - - String fkTableName = rs.getString( "FKTABLE_NAME" ); - assertEquals( "users", fkTableName ); - - String fkColumnName = rs.getString( "FKCOLUMN_NAME" ); - assertTrue( fkColumnName.equals( "people_id" ) || fkColumnName.equals( "policy_id" ) ) ; - - String fkName = rs.getString( "FK_NAME" ); - assertTrue( fkName.startsWith( "people") || fkName.startsWith( "policy" ) ); - - String pkName = rs.getString( "PK_NAME" ); - assertTrue( pkName.equals( "people_pkey") || pkName.equals( "policy_pkey" ) ); - - } - - assertTrue ( j == 2 ); - - rs = dbmd.getExportedKeys( null, "", "people" ); - - // this is hacky, but it will serve the purpose - assertTrue ( rs.next() ); - - assertEquals( "people", rs.getString( "PKTABLE_NAME" ) ); - assertEquals( "id", rs.getString( "PKCOLUMN_NAME" ) ); - - assertEquals( "users", rs.getString( "FKTABLE_NAME" ) ); - assertEquals( "people_id", rs.getString( "FKCOLUMN_NAME" ) ); - - assertTrue( rs.getString( "FK_NAME" ).startsWith( "people" ) ); - - - TestUtil.dropTable( con1, "users" ); - TestUtil.dropTable( con1, "people" ); - TestUtil.dropTable( con1, "policy" ); - - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } - - public void testColumns() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getColumns(null,null,"pg_class",null); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testColumnPrivileges() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getColumnPrivileges(null,null,"pg_statistic",null); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testTablePrivileges() - { - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getTablePrivileges(null,null,"testmetadata"); - boolean l_foundSelect = false; - while (rs.next()) { - if (rs.getString("GRANTEE").equals(TestUtil.getUser()) - && rs.getString("PRIVILEGE").equals("SELECT")) l_foundSelect = true; - } - rs.close(); - //Test that the table owner has select priv - assertTrue("Couldn't find SELECT priv on table testmetadata for " + TestUtil.getUser(),l_foundSelect); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testPrimaryKeys() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getPrimaryKeys(null,null,"pg_class"); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testIndexInfo() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getIndexInfo(null,null,"pg_class",false,false); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testTableTypes() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getTableTypes(); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testProcedureColumns() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getProcedureColumns(null,null,null,null); - rs.close(); - } catch (SQLException sqle) { - sqle.printStackTrace(); - fail(sqle.getMessage()); - } - } - - public void testVersionColumns() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getVersionColumns(null,null,"pg_class"); - rs.close(); - } catch (SQLException sqle) { - fail(sqle.getMessage()); - } - } - - public void testBestRowIdentifier() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getBestRowIdentifier(null,null,"pg_type",DatabaseMetaData.bestRowSession,false); - rs.close(); - } catch (SQLException sqle) { - fail(sqle.getMessage()); - } - } - - public void testProcedures() - { - // At the moment just test that no exceptions are thrown KJ - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getProcedures(null,null,null); - rs.close(); - } catch (SQLException sqle) { - fail(sqle.getMessage()); - } - } - - public void testCatalogs() - { - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - ResultSet rs = dbmd.getCatalogs(); - boolean foundTemplate0 = false; - boolean foundTemplate1 = false; - while(rs.next()) { - String database = rs.getString("TABLE_CAT"); - if ("template0".equals(database)) { - foundTemplate0 = true; - } else if ("template1".equals(database)) { - foundTemplate1 = true; - } - } - rs.close(); - assertTrue(foundTemplate0); - assertTrue(foundTemplate1); - } catch(SQLException sqle) { - fail(sqle.getMessage()); - } - } - - public void testSchemas() - { - try - { - DatabaseMetaData dbmd = con.getMetaData(); - assertNotNull(dbmd); - - ResultSet rs = dbmd.getSchemas(); - boolean foundPublic = false; - boolean foundEmpty = false; - boolean foundPGCatalog = false; - int count; - - for(count=0; rs.next(); count++) { - String schema = rs.getString("TABLE_SCHEM"); - if ("public".equals(schema)) { - foundPublic = true; - } else if ("".equals(schema)) { - foundEmpty = true; - } else if ("pg_catalog".equals(schema)) { - foundPGCatalog = true; - } - } - rs.close(); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(count >= 2); - assertTrue(foundPublic); - assertTrue(foundPGCatalog); - assertTrue(!foundEmpty); - } else { - assertEquals(count,1); - assertTrue(foundEmpty); - assertTrue(!foundPublic); - assertTrue(!foundPGCatalog); - } - } catch (SQLException sqle) { - fail(sqle.getMessage()); - } - } - -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java deleted file mode 100644 index ecef3f17bb7..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java +++ /dev/null @@ -1,322 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/DateTest.java,v 1.7 2003/11/29 22:41:23 pgsql 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 -{ - - private Connection con; - private boolean testingSetDate = false; - - public DateTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable(con, "testdate", "dt date"); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "testdate"); - TestUtil.closeDB(con); - } - - /* - * Tests the time methods in ResultSet - */ - public void testGetDate() - { - try - { - Statement stmt = con.createStatement(); - - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1950-02-07'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1970-06-02'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1999-08-11'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2001-02-13'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1950-04-02'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1970-11-30'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1988-01-01'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2003-07-09'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1934-02-28'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1969-04-03'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1982-08-03'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2012-03-15'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1912-05-01'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1971-12-15'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'1984-12-03'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testdate", "'2000-01-01'"))); - - /* dateTest() contains all of the tests */ - dateTest(); - - assertEquals(16, stmt.executeUpdate("DELETE FROM " + "testdate")); - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Tests the time methods in PreparedStatement - */ - public void testSetDate() - { - try - { - Statement stmt = con.createStatement(); - PreparedStatement ps = con.prepareStatement(TestUtil.insertSQL("testdate", "?")); - - ps.setDate(1, makeDate(1950, 2, 7)); - assertEquals(1, ps.executeUpdate()); - - ps.setDate(1, makeDate(1970, 6, 2)); - assertEquals(1, ps.executeUpdate()); - - ps.setDate(1, makeDate(1999, 8, 11)); - assertEquals(1, ps.executeUpdate()); - - ps.setDate(1, makeDate(2001, 2, 13)); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Timestamp.valueOf("1950-04-02 12:00:00"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Timestamp.valueOf("1970-11-30 3:00:00"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Timestamp.valueOf("1988-1-1 13:00:00"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Timestamp.valueOf("2003-07-09 12:00:00"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1934-02-28", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1969-04-3", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1982-08-03", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "2012-3-15", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Date.valueOf("1912-5-1"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Date.valueOf("1971-12-15"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Date.valueOf("1984-12-03"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Date.valueOf("2000-1-1"), java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1944-4-04-01", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1970-01-1-10", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "1982-12-14+13", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "2010-08-3+05", java.sql.Types.DATE); - assertEquals(1, ps.executeUpdate()); - - ps.close(); - - // Need to set a flag so that the method knows there is an extra test. - testingSetDate = true; - // Fall through helper - dateTest(); - testingSetDate = false; - - assertEquals(20, stmt.executeUpdate("DELETE FROM testdate")); - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Helper for the date tests. It tests what should be in the db - */ - private void dateTest() throws SQLException - { - Statement st = con.createStatement(); - ResultSet rs; - java.sql.Date d; - - rs = st.executeQuery(TestUtil.selectSQL("testdate", "dt")); - assertNotNull(rs); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1950, 2, 7)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1970, 6, 2)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1999, 8, 11)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(2001, 2, 13)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1950, 4, 2)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1970, 11, 30)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1988, 1, 1)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(2003, 7, 9)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1934, 2, 28)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1969, 4, 3)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1982, 8, 3)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(2012, 3, 15)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1912, 5, 1)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1971, 12, 15)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(1984, 12, 3)); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - assertEquals(d, makeDate(2000, 1, 1)); - - //now we have to convert the date, cause I fed it a timezone. IF it used it. hence the check - if (testingSetDate) - { - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - java.sql.Date tmpDate = java.sql.Date.valueOf("1944-4-4"); - int localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate)) - { - localoffset += 60 * 60 * 1000; - } - int Dateoffset = 60 * 60 * 1000; - tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset); - assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate())); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - tmpDate = java.sql.Date.valueOf("1970-1-1"); - localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate)) - { - localoffset += 60 * 60 * 1000; - } - Dateoffset = 10 * 60 * 60 * 1000; - tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset); - assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate())); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - tmpDate = java.sql.Date.valueOf("1982-12-14"); - localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate)) - { - localoffset += 60 * 60 * 1000; - } - Dateoffset = -13 * 60 * 60 * 1000; - tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset); - assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate())); - - assertTrue(rs.next()); - d = rs.getDate(1); - assertNotNull(d); - tmpDate = java.sql.Date.valueOf("2010-08-03"); - localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpDate)) - { - localoffset += 60 * 60 * 1000; - } - Dateoffset = -5 * 60 * 60 * 1000; - tmpDate.setTime(tmpDate.getTime() + Dateoffset + localoffset); - assertEquals(d, makeDate(tmpDate.getYear() + 1900, tmpDate.getMonth()+1, tmpDate.getDate())); - } - - assertTrue(!rs.next()); - - rs.close(); - st.close(); - } - - private java.sql.Date makeDate(int y, int m, int d) - { - return java.sql.Date.valueOf(TestUtil.fix(y, 4) + "-" + - TestUtil.fix(m, 2) + "-" + - TestUtil.fix(d, 2)); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java deleted file mode 100644 index 0b7d934871b..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/DriverTest.java,v 1.7 2003/11/29 22:41:23 pgsql Exp $ - * - * Tests the dynamically created class org.postgresql.Driver - * - */ -public class DriverTest extends TestCase -{ - - public DriverTest(String name) - { - super(name); - } - - /* - * This tests the acceptsURL() method with a couple of good and badly formed - * jdbc urls - */ - public void testAcceptsURL() - { - try - { - - // Load the driver (note clients should never do it this way!) - org.postgresql.Driver drv = new org.postgresql.Driver(); - 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")); - assertTrue(drv.acceptsURL("jdbc:postgresql://[::1]:5740/db")); - - // Badly formatted url's - assertTrue(!drv.acceptsURL("jdbc:postgres:test")); - assertTrue(!drv.acceptsURL("postgresql:test")); - assertTrue(!drv.acceptsURL("db")); - - } - 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"); - - // Test with the url, username & password - con = DriverManager.getConnection(TestUtil.getURL(), TestUtil.getUser(), TestUtil.getPassword()); - assertNotNull(con); - con.close(); - - // Test with the username in the url - con = DriverManager.getConnection(TestUtil.getURL() + "?user=" + TestUtil.getUser() + "&password=" + TestUtil.getPassword()); - assertNotNull(con); - con.close(); - } - catch (ClassNotFoundException ex) - { - fail(ex.getMessage()); - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java deleted file mode 100644 index ce1e31830ff..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java +++ /dev/null @@ -1,62 +0,0 @@ - -package org.postgresql.test.jdbc2; - -import junit.framework.*; -import org.postgresql.core.Encoding; -import java.io.*; - -/* - * Tests for the Encoding class. - * - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/EncodingTest.java,v 1.5 2003/11/29 22:41:23 pgsql Exp $ - */ - - -public class EncodingTest extends TestCase -{ - - 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 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]); - - 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()); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java deleted file mode 100644 index da7949bfbbe..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.Statement; - -import junit.framework.TestCase; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/JBuilderTest.java,v 1.9 2003/11/29 22:41:23 pgsql Exp $ - * - * Some simple tests to check that the required components needed for JBuilder - * stay working - * - */ -public class JBuilderTest extends TestCase -{ - - 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 = TestUtil.openDB(); - - TestUtil.createTable( con, "test_c", - "source text,cost money,imageid int4" ); - - TestUtil.closeDB(con); - } - - // Tear down the fixture for this test case. - protected void tearDown() throws Exception - { - Connection con = TestUtil.openDB(); - TestUtil.dropTable(con, "test_c"); - TestUtil.closeDB(con); - } - - /* - * This tests that Money types work. JDBCExplorer barfs if this fails. - */ - public void testMoney() - { - try - { - Connection con = TestUtil.openDB(); - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery("select cost from test_c"); - assertNotNull(rs); - - while (rs.next()) - { - rs.getDouble(1); - } - - rs.close(); - st.close(); - - TestUtil.closeDB(con); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java deleted file mode 100644 index 7a5acab8e26..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.postgresql.test.jdbc2; - -import junit.framework.TestSuite; - -/* - * Executes all known tests for JDBC2 and includes some utility methods. - */ -public class Jdbc2TestSuite extends TestSuite -{ - - /* - * The main entry point for JUnit - */ - public static TestSuite suite() - { - TestSuite suite = new TestSuite(); - - // - // Add one line per class in our test cases. These should be in order of - // complexity. - - // ANTTest should be first as it ensures that test parameters are - // being sent to the suite. - // - suite.addTestSuite(ANTTest.class); - - // Basic Driver internals - suite.addTestSuite(DriverTest.class); - suite.addTestSuite(ConnectionTest.class); - suite.addTestSuite(DatabaseMetaDataTest.class); - suite.addTestSuite(DatabaseMetaDataPropertiesTest.class); - suite.addTestSuite(EncodingTest.class); - - // Connectivity/Protocols - - // ResultSet - suite.addTestSuite(ResultSetTest.class); - - // Time, Date, Timestamp - suite.addTestSuite(DateTest.class); - suite.addTestSuite(TimeTest.class); - suite.addTestSuite(TimestampTest.class); - - // PreparedStatement - - // ServerSide Prepared Statements - suite.addTestSuite(ServerPreparedStmtTest.class); - - // BatchExecute - suite.addTestSuite(BatchExecuteTest.class); - - - // Other misc tests, based on previous problems users have had or specific - // features some applications require. - suite.addTestSuite(JBuilderTest.class); - suite.addTestSuite(MiscTest.class); - - // Fastpath/LargeObject - suite.addTestSuite(BlobTest.class); - suite.addTestSuite(OID74Test.class); - - suite.addTestSuite(UpdateableResultTest.class ); - - suite.addTestSuite(CallableStmtTest.class ); - suite.addTestSuite(CursorFetchTest.class); - - // That's all folks - return suite; - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java deleted file mode 100644 index c6b2e04b955..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; -import java.io.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/MiscTest.java,v 1.12 2003/12/11 15:11:43 davec 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 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 = TestUtil.openDB(); - - Statement st = con.createStatement(); - ResultSet rs = st.executeQuery("select datname from pg_database"); - assertNotNull(rs); - - while (rs.next()) - { - rs.getString(1); - } - - rs.close(); - st.close(); - - TestUtil.closeDB(con); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - public void testError() - { - Connection con = TestUtil.openDB(); - try - { - - // 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(); - } - catch ( SQLException ex ) - { - // Verify that the SQLException is serializable. - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); - oos.writeObject(ex); - oos.close(); - } catch (IOException ioe) { - fail(ioe.getMessage()); - } - } - try - { - con.commit(); - con.close(); - } - catch ( Exception ex) - {} - } - - public void xtestLocking() - { - - try - { - Connection con = TestUtil.openDB(); - Connection con2 = TestUtil.openDB(); - - TestUtil.createTable(con, "test_lock", "name text"); - Statement st = con.createStatement(); - Statement st2 = con2.createStatement(); - con.setAutoCommit(false); - st.execute("lock table test_lock"); - st2.executeUpdate( "insert into test_lock ( name ) values ('hello')" ); - con.commit(); - TestUtil.dropTable(con, "test_lock"); - con.close(); - } - catch ( Exception ex ) - { - fail( ex.getMessage() ); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/OID74Test.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/OID74Test.java deleted file mode 100644 index eb83cd7a695..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/OID74Test.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.io.*; -import java.sql.*; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.Properties; -import java.sql.*; - -/** - * User: alexei - * Date: 17-Dec-2003 - * Time: 11:01:44 - * @version $Id: OID74Test.java,v 1.3 2003/12/18 04:08:30 davec Exp $ - */ -public class OID74Test extends TestCase -{ - - public OID74Test( String name ) - { - super(name); - } - public void setUp() throws Exception - { - } - public void tearDown() throws Exception - { - } - public void testBinaryStream() throws SQLException - { - //set up conection here - Properties props = new Properties(); - props.setProperty("compatible","7.1"); - Connection c = TestUtil.openDB(props); - c.setAutoCommit(false); - - TestUtil.createTable(c,"temp","col oid"); - - Statement st = null; - - PreparedStatement pstmt = null; - try - { - - pstmt = c.prepareStatement("INSERT INTO temp VALUES (?)"); - pstmt.setBinaryStream(1, new ByteArrayInputStream(new byte[]{1, 2, 3, 4, 5}), 5); - assertTrue( (pstmt.executeUpdate() == 1) ); - pstmt.close(); - - pstmt = c.prepareStatement("SELECT col FROM temp LIMIT 1"); - ResultSet rs = pstmt.executeQuery(); - - assertTrue("No results from query", rs.next() ); - - InputStream in = rs.getBinaryStream(1); - int data; - int i = 1; - while ((data = in.read()) != -1) - assertEquals(data,i++); - rs.close(); - pstmt.close(); - c.createStatement().executeUpdate("DELETE FROM temp"); - c.commit(); - } - catch ( IOException ioex ) - { - fail( ioex.getMessage() ); - } - catch (SQLException ex) - { - fail( ex.getMessage() ); - } - - TestUtil.dropTable(c,"temp"); - TestUtil.closeDB(c); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/RefCursorTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/RefCursorTest.java deleted file mode 100644 index 556bb403be1..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/RefCursorTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import java.sql.CallableStatement; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.Statement; -import java.sql.Types; - -import junit.framework.TestCase; - -/* - * RefCursor ResultSet tests. - * This test case is basically the same as the ResultSet test case. - * - * @author Nic Ferrier <nferrier@tapsellferrier.co.uk> - */ -public class RefCursorTest extends TestCase -{ - private Connection con; - - public RefCursorTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - // this is the same as the ResultSet setup. - con = TestUtil.openDB(); - Statement stmt = con.createStatement(); - - TestUtil.createTable(con, "testrs", "id integer"); - - stmt.executeUpdate("INSERT INTO testrs VALUES (1)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (2)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (3)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (4)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (6)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (9)"); - - - // Create the functions. - stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getRefcursor () RETURNS refcursor AS '" - + "declare v_resset; begin open v_resset for select id from testrs order by id; " - + "return v_resset; end;' LANGUAGE 'plpgsql';"); - stmt.execute ("CREATE OR REPLACE FUNCTION testspg__getEmptyRefcursor () RETURNS refcursor AS '" - + "declare v_resset; begin open v_resset for select id from testrs where id < 1 order by id; " - + "return v_resset; end;' LANGUAGE 'plpgsql';"); - stmt.close(); - } - - protected void tearDown() throws Exception - { - Statement stmt = con.createStatement (); - stmt.execute ("drop FUNCTION testspg__getRefcursor ();"); - stmt.execute ("drop FUNCTION testspg__getEmptyRefcursor ();"); - TestUtil.dropTable(con, "testrs"); - TestUtil.closeDB(con); - } - - public void testResult() throws Exception - { - CallableStatement call = con.prepareCall("{ ? = call testspg__getRefcursor () }"); - call.registerOutParameter(1, Types.OTHER); - call.execute(); - ResultSet rs = (ResultSet) call.getObject(1); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 1); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 2); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 3); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 4); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 6); - - assertTrue(rs.next()); - assertTrue(rs.getInt(1) == 9); - - assertTrue(!rs.next()); - - call.close(); - } - - - public void testEmptyResult() throws Exception - { - CallableStatement call = con.prepareCall("{ ? = call testspg__getRefcursor () }"); - call.registerOutParameter(1, Types.OTHER); - call.execute(); - - ResultSet rs = (ResultSet) call.getObject(1); - assertTrue(!rs.next()); - - call.close(); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ResultSetTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ResultSetTest.java deleted file mode 100644 index c09ac49a982..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ResultSetTest.java +++ /dev/null @@ -1,289 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.Statement; - -import junit.framework.TestCase; - -/* - * ResultSet tests. - */ -public class ResultSetTest extends TestCase -{ - private Connection con; - - public ResultSetTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - Statement stmt = con.createStatement(); - - TestUtil.createTable(con, "testrs", "id integer"); - - stmt.executeUpdate("INSERT INTO testrs VALUES (1)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (2)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (3)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (4)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (6)"); - stmt.executeUpdate("INSERT INTO testrs VALUES (9)"); - - TestUtil.createTable(con, "teststring", "a text"); - stmt.executeUpdate("INSERT INTO teststring VALUES ('12345')"); - - TestUtil.createTable(con, "testint", "a int"); - stmt.executeUpdate("INSERT INTO testint VALUES (12345)"); - - TestUtil.createTable(con, "testbool", "a boolean"); - - TestUtil.createTable(con, "testbit", "a bit"); - - TestUtil.createTable(con, "testboolstring", "a varchar(30)"); - - stmt.executeUpdate("INSERT INTO testboolstring VALUES('true')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('false')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('t')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('f')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('1.0')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('0.0')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('TRUE')"); - stmt.executeUpdate("INSERT INTO testboolstring VALUES('this is not true')"); - - TestUtil.createTable(con, "testnumeric", "a numeric"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('1.0')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('0.0')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('-1.0')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('1.2')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999.2')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('99999')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('-2.5')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999.2')"); - stmt.executeUpdate("INSERT INTO testnumeric VALUES('-99999')"); - - stmt.close(); - - - stmt.close(); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "testrs"); - TestUtil.dropTable(con, "teststring"); - TestUtil.dropTable(con, "testint"); - TestUtil.dropTable(con, "testbool"); - TestUtil.dropTable(con, "testbit"); - TestUtil.dropTable(con, "testboolstring"); - TestUtil.dropTable(con, "testnumeric"); - TestUtil.closeDB(con); - } - - public void testBackward() throws Exception - { - Statement stmt = con.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT * FROM testrs"); - rs.afterLast(); - assertTrue(rs.previous()); - rs.close(); - stmt.close(); - } - - public void testAbsolute() throws Exception - { - Statement stmt = con.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT * FROM testrs"); - - assertTrue(rs.absolute( -1)); - assertEquals(6, rs.getRow()); - - assertTrue(rs.absolute(1)); - assertEquals(1, rs.getRow()); - - assertTrue(!rs.absolute( -10)); - assertEquals(0, rs.getRow()); - assertTrue(rs.next()); - assertEquals(1, rs.getRow()); - - assertTrue(!rs.absolute(10)); - assertEquals(0, rs.getRow()); - assertTrue(rs.previous()); - assertEquals(6, rs.getRow()); - - 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()); - - - } - catch ( Exception ex ) - { - fail( ex.getMessage() ); - } - - } - - public void testMaxFieldSize() throws Exception - { - Statement stmt = con.createStatement(); - stmt.setMaxFieldSize(2); - - ResultSet rs = stmt.executeQuery("select * from testint"); - - //max should not apply to the following since per the spec - //it should apply only to binary and char/varchar columns - rs.next(); - assertEquals(rs.getString(1),"12345"); - assertEquals(new String(rs.getBytes(1)), "12345"); - - //max should apply to the following since the column is - //a varchar column - rs = stmt.executeQuery("select * from teststring"); - rs.next(); - assertEquals(rs.getString(1), "12"); - assertEquals(new String(rs.getBytes(1)), "12"); - } - - public void booleanTests(boolean useServerPrepare) throws Exception - { - java.sql.PreparedStatement pstmt = con.prepareStatement("insert into testbool values (?)"); - if (useServerPrepare) - ((org.postgresql.PGStatement)pstmt).setUseServerPrepare(true); - - pstmt.setObject(1, new Float(0), java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, new Float(1), java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, "False", java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, "True", java.sql.Types.BIT); - pstmt.executeUpdate(); - - ResultSet rs = con.createStatement().executeQuery("select * from testbool"); - for (int i = 0; i<2; i++) - { - assertTrue(rs.next()); - assertEquals(false, rs.getBoolean(1)); - assertTrue(rs.next()); - assertEquals(true, rs.getBoolean(1)); - } - - pstmt = con.prepareStatement("insert into testbit values (?)"); - - pstmt.setObject(1, new Float(0), java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, new Float(1), java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, "false", java.sql.Types.BIT); - pstmt.executeUpdate(); - - pstmt.setObject(1, "true", java.sql.Types.BIT); - pstmt.executeUpdate(); - - rs = con.createStatement().executeQuery("select * from testbit"); - - for (int i = 0;i<2; i++) - { - assertTrue(rs.next()); - assertEquals(false, rs.getBoolean(1)); - assertTrue(rs.next()); - assertEquals(true, rs.getBoolean(1)); - } - - rs = con.createStatement().executeQuery("select * from testboolstring"); - - for (int i = 0;i<4; i++) - { - assertTrue(rs.next()); - assertEquals(true, rs.getBoolean(1)); - assertTrue(rs.next()); - assertEquals(false, rs.getBoolean(1)); - } - } - - public void testBoolean() throws Exception - { - booleanTests(true); - booleanTests(false); - } - - public void testgetByte() throws Exception - { - ResultSet rs = con.createStatement().executeQuery("select * from testnumeric"); - boolean thrown = false; - - assertTrue(rs.next()); - assertEquals(1,rs.getByte(1)); - - assertTrue(rs.next()); - assertEquals(0,rs.getByte(1)); - - assertTrue(rs.next()); - assertEquals(-1,rs.getByte(1)); - - while (rs.next()) - { - thrown = false; - try - { - rs.getByte(1); - } - catch (Exception e) - { - thrown = true; - } - if (!thrown) - fail("Exception expected."); - } - } - - public void testgetShort() throws Exception - { - ResultSet rs = con.createStatement().executeQuery("select * from testnumeric"); - boolean thrown = false; - - assertTrue(rs.next()); - assertEquals(1,rs.getShort(1)); - - assertTrue(rs.next()); - assertEquals(0,rs.getShort(1)); - - assertTrue(rs.next()); - assertEquals(-1,rs.getShort(1)); - - while (rs.next()) - { - thrown = false; - try - { - rs.getShort(1); - } - catch (Exception e) - { - thrown = true; - } - if (!thrown) - fail("Exception expected."); - } - } - -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java deleted file mode 100644 index aeb356f2b14..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java +++ /dev/null @@ -1,265 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.PGStatement; -import org.postgresql.test.TestUtil; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.Statement; - -import junit.framework.TestCase; - -/* - * Tests for using server side prepared statements - */ -public class ServerPreparedStmtTest extends TestCase -{ - private Connection con; - - public ServerPreparedStmtTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - Statement stmt = con.createStatement(); - - TestUtil.createTable(con, "testsps", "id integer, value boolean"); - - stmt.executeUpdate("INSERT INTO testsps VALUES (1,'t')"); - stmt.executeUpdate("INSERT INTO testsps VALUES (2,'t')"); - stmt.executeUpdate("INSERT INTO testsps VALUES (3,'t')"); - stmt.executeUpdate("INSERT INTO testsps VALUES (4,'t')"); - stmt.executeUpdate("INSERT INTO testsps VALUES (6,'t')"); - stmt.executeUpdate("INSERT INTO testsps VALUES (9,'f')"); - - stmt.close(); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "testsps"); - TestUtil.closeDB(con); - } - - public void testPreparedStatementsNoBinds() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - //Test that basic functionality works - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - //Verify that subsequent calls still work - rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - //Verify that using the statement to execute a different query works - rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - - ((PGStatement)pstmt).setUseServerPrepare(false); - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - - //Verify that using the statement still works after turning off prepares - rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - - pstmt.close(); - } - - public void testPreparedStatementsWithOneBind() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ?"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - //Test that basic functionality works - pstmt.setInt(1,2); - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - //Verify that subsequent calls still work - rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - //Verify that using the statement to execute a different query works - rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - - ((PGStatement)pstmt).setUseServerPrepare(false); - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - - //Verify that using the statement still works after turning off prepares - rs = pstmt.executeQuery("SELECT * FROM testsps WHERE id = 9"); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - - pstmt.close(); - } - - // Verify we can bind booleans-as-objects ok. - public void testBooleanObjectBind() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE value = ?"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - pstmt.setObject(1, new Boolean(false), java.sql.Types.BIT); - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - } - - // Verify we can bind booleans-as-integers ok. - public void testBooleanIntegerBind() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ?"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - pstmt.setObject(1, new Boolean(true), java.sql.Types.INTEGER); - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(1, rs.getInt(1)); - rs.close(); - } - - // Verify we can bind booleans-as-native-types ok. - public void testBooleanBind() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE value = ?"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - pstmt.setBoolean(1, false); - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(9, rs.getInt(1)); - rs.close(); - } - - public void testPreparedStatementsWithBinds() throws Exception - { - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = ? or id = ?"); - ((PGStatement)pstmt).setUseServerPrepare(true); - if (TestUtil.haveMinimumServerVersion(con,"7.3")) { - assertTrue(((PGStatement)pstmt).isUseServerPrepare()); - } else { - assertTrue(!((PGStatement)pstmt).isUseServerPrepare()); - } - - //Test that basic functionality works - //bind different datatypes - pstmt.setInt(1,2); - pstmt.setString(2,"2"); - ResultSet rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - //Verify that subsequent calls still work - rs = pstmt.executeQuery(); - assertTrue(rs.next()); - assertEquals(2, rs.getInt(1)); - rs.close(); - - pstmt.close(); - } - - public void testSPSToggle() throws Exception - { - // Verify we can toggle UseServerPrepare safely before a query is executed. - PreparedStatement pstmt = con.prepareStatement("SELECT * FROM testsps WHERE id = 2"); - ((PGStatement)pstmt).setUseServerPrepare(true); - ((PGStatement)pstmt).setUseServerPrepare(false); - } - - public void testBytea() throws Exception - { - // Verify we can use setBytes() with a server-prepared update. - try { - TestUtil.createTable(con, "testsps_bytea", "data bytea"); - - PreparedStatement pstmt = con.prepareStatement("INSERT INTO testsps_bytea(data) VALUES (?)"); - ((PGStatement)pstmt).setUseServerPrepare(true); - pstmt.setBytes(1, new byte[100]); - pstmt.executeUpdate(); - } finally { - TestUtil.dropTable(con, "testsps_bytea"); - } - } - - // Check statements are not transformed when they shouldn't be. - public void TODO_FAILS_testCreateTable() throws Exception { - // CREATE TABLE isn't supported by PREPARE; the driver should realize this and - // still complete without error. - PreparedStatement pstmt = con.prepareStatement("CREATE TABLE testsps_bad(data int)"); - ((PGStatement)pstmt).setUseServerPrepare(true); - pstmt.executeUpdate(); - TestUtil.dropTable(con, "testsps_bad"); - } - - public void TODO_FAILS_testMultistatement() throws Exception { - // Shouldn't try to PREPARE this one, if we do we get: - // PREPARE x(int,int) AS INSERT .... $1 ; INSERT ... $2 -- syntax error - try { - TestUtil.createTable(con, "testsps_multiple", "data int"); - PreparedStatement pstmt = con.prepareStatement("INSERT INTO testsps_multiple(data) VALUES (?); INSERT INTO testsps_multiple(data) VALUES (?)"); - ((PGStatement)pstmt).setUseServerPrepare(true); - pstmt.setInt(1, 1); - pstmt.setInt(2, 2); - pstmt.executeUpdate(); // Two inserts. - - pstmt.setInt(1, 3); - pstmt.setInt(2, 4); - pstmt.executeUpdate(); // Two more inserts. - - ResultSet check = con.createStatement().executeQuery("SELECT COUNT(*) FROM testsps_multiple"); - assertTrue(check.next()); - assertEquals(4, check.getInt(1)); - } finally { - TestUtil.dropTable(con, "testsps_multiple"); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java deleted file mode 100644 index 0c0f733d70d..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java +++ /dev/null @@ -1,217 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimeTest.java,v 1.7 2003/11/29 22:41:23 pgsql 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 -{ - - private Connection con; - private boolean testSetTime = false; - - public TimeTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable(con, "testtime", "tm time"); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "testtime"); - TestUtil.closeDB(con); - } - - /* - * Tests the time methods in ResultSet - */ - public void testGetTime() - { - try - { - Statement stmt = con.createStatement(); - - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'01:02:03'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'23:59:59'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'12:00:00'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'05:15:21'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'16:21:51'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'12:15:12'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'22:12:01'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL("testtime", "'08:46:44'"))); - - - // Fall through helper - timeTest(); - - assertEquals(8, stmt.executeUpdate("DELETE FROM testtime")); - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Tests the time methods in PreparedStatement - */ - public void testSetTime() - { - try - { - PreparedStatement ps = con.prepareStatement(TestUtil.insertSQL("testtime", "?")); - Statement stmt = con.createStatement(); - - ps.setTime(1, makeTime(1, 2, 3)); - assertEquals(1, ps.executeUpdate()); - - ps.setTime(1, makeTime(23, 59, 59)); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Time.valueOf("12:00:00"), java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Time.valueOf("05:15:21"), java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Time.valueOf("16:21:51"), java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, java.sql.Time.valueOf("12:15:12"), java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "22:12:1", java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "8:46:44", java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "5:1:2-03", java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - ps.setObject(1, "23:59:59+11", java.sql.Types.TIME); - assertEquals(1, ps.executeUpdate()); - - // Need to let the test know this one has extra test cases. - testSetTime = true; - // Fall through helper - timeTest(); - testSetTime = false; - - assertEquals(10, stmt.executeUpdate("DELETE FROM testtime")); - stmt.close(); - ps.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Helper for the TimeTests. It tests what should be in the db - */ - private void timeTest() throws SQLException - { - Statement st = con.createStatement(); - ResultSet rs; - java.sql.Time t; - - rs = st.executeQuery(TestUtil.selectSQL("testtime", "tm")); - assertNotNull(rs); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(1, 2, 3), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(23, 59, 59), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(12, 0, 0), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(5, 15, 21), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(16, 21, 51), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(12, 15, 12), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(22, 12, 1), t); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - assertEquals(makeTime(8, 46, 44), t); - - // If we're checking for timezones. - if (testSetTime) - { - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - java.sql.Time tmpTime = java.sql.Time.valueOf("5:1:2"); - int localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpTime)) - { - localoffset += 60 * 60 * 1000; - } - int Timeoffset = 3 * 60 * 60 * 1000; - tmpTime.setTime(tmpTime.getTime() + Timeoffset + localoffset); - assertEquals(t, makeTime(tmpTime.getHours(), tmpTime.getMinutes(), tmpTime.getSeconds())); - - assertTrue(rs.next()); - t = rs.getTime(1); - assertNotNull(t); - tmpTime= java.sql.Time.valueOf("23:59:59"); - localoffset = java.util.Calendar.getInstance().getTimeZone().getRawOffset(); - if (java.util.Calendar.getInstance().getTimeZone().inDaylightTime(tmpTime)) - { - localoffset += 60 * 60 * 1000; - } - Timeoffset = -11 * 60 * 60 * 1000; - tmpTime.setTime(tmpTime.getTime() + Timeoffset + localoffset); - assertEquals(t, makeTime(tmpTime.getHours(), tmpTime.getMinutes(), tmpTime.getSeconds())); - } - - assertTrue(! rs.next()); - - rs.close(); - } - - private java.sql.Time makeTime(int h, int m, int s) - { - return java.sql.Time.valueOf(TestUtil.fix(h, 2) + ":" + - TestUtil.fix(m, 2) + ":" + - TestUtil.fix(s, 2)); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java deleted file mode 100644 index 2affb1d873c..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java +++ /dev/null @@ -1,538 +0,0 @@ -package org.postgresql.test.jdbc2; - -import org.postgresql.test.TestUtil; -import junit.framework.TestCase; -import java.sql.*; - -/* - * $PostgreSQL: pgsql/src/interfaces/jdbc/org/postgresql/test/jdbc2/TimestampTest.java,v 1.13 2003/11/29 22:41:23 pgsql Exp $ - * - * Test get/setTimestamp for both timestamp with time zone and - * timestamp without time zone datatypes - * - */ -public class TimestampTest extends TestCase -{ - - private Connection con; - - public TimestampTest(String name) - { - super(name); - } - - protected void setUp() throws Exception - { - con = TestUtil.openDB(); - TestUtil.createTable(con, TSWTZ_TABLE, "ts timestamp with time zone"); - TestUtil.createTable(con, TSWOTZ_TABLE, "ts timestamp without time zone"); - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, TSWTZ_TABLE); - TestUtil.dropTable(con, TSWOTZ_TABLE); - TestUtil.closeDB(con); - } - - /* - * 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 - */ - public void testGetTimestampWTZ() - { - try - { - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS4WTZ_PGFORMAT + "'"))); - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS4WTZ_PGFORMAT + "'"))); - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + TS4WTZ_PGFORMAT + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate1.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate2.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate3.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate4.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime1.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime2.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime3.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime4.getTime()) + "'"))); - - - // Fall through helper - timestampTestWTZ(); - - assertEquals(20, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE)); - - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * 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 - */ - public void testSetTimestampWTZ() - { - try - { - Statement stmt = con.createStatement(); - PreparedStatement pstmt = con.prepareStatement(TestUtil.insertSQL(TSWTZ_TABLE, "?")); - - pstmt.setTimestamp(1, TS1WTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS2WTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS3WTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS4WTZ); - assertEquals(1, pstmt.executeUpdate()); - - // With java.sql.Timestamp - pstmt.setObject(1,TS1WTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS2WTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS3WTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS4WTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With Strings - pstmt.setObject(1,TS1WTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS2WTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS3WTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS4WTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With java.sql.Date - pstmt.setObject(1,tmpDate1, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate2, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate3, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate4, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With java.sql.Time - pstmt.setObject(1,tmpTime1, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime2, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime3, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime4, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // Fall through helper - timestampTestWTZ(); - - assertEquals(20, stmt.executeUpdate("DELETE FROM " + TSWTZ_TABLE)); - - pstmt.close(); - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * 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 - */ - public void testGetTimestampWOTZ() - { - try - { - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS4WOTZ_PGFORMAT + "'"))); - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS4WOTZ_PGFORMAT + "'"))); - 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 + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + TS4WOTZ_PGFORMAT + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate1WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate2WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate3WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpDate4WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime1WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime2WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime3WOTZ.getTime()) + "'"))); - assertEquals(1, stmt.executeUpdate(TestUtil.insertSQL(TSWOTZ_TABLE, "'" + new java.sql.Timestamp(tmpTime4WOTZ.getTime()) + "'"))); - - // Fall through helper - timestampTestWOTZ(); - - assertEquals(20, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE)); - - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - - /* - * 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 - */ - public void testSetTimestampWOTZ() - { - try - { - Statement stmt = con.createStatement(); - PreparedStatement pstmt = con.prepareStatement(TestUtil.insertSQL(TSWOTZ_TABLE, "?")); - - pstmt.setTimestamp(1, TS1WOTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS2WOTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS3WOTZ); - assertEquals(1, pstmt.executeUpdate()); - - pstmt.setTimestamp(1, TS4WOTZ); - assertEquals(1, pstmt.executeUpdate()); - - - // With java.sql.Timestamp - pstmt.setObject(1,TS1WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS2WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS3WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS4WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With Strings - pstmt.setObject(1,TS1WOTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS2WOTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS3WOTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,TS4WOTZ_PGFORMAT, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With java.sql.Date - pstmt.setObject(1,tmpDate1WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate2WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate3WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpDate4WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // With java.sql.Time - pstmt.setObject(1,tmpTime1WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime2WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime3WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - pstmt.setObject(1,tmpTime4WOTZ, java.sql.Types.TIMESTAMP); - assertEquals(1, pstmt.executeUpdate()); - - // Fall through helper - timestampTestWOTZ(); - - assertEquals(20, stmt.executeUpdate("DELETE FROM " + TSWOTZ_TABLE)); - - pstmt.close(); - stmt.close(); - } - catch (Exception ex) - { - fail(ex.getMessage()); - } - } - - /* - * Helper for the TimestampTests. It tests what should be in the db - */ - private void timestampTestWTZ() throws SQLException - { - Statement stmt = con.createStatement(); - ResultSet rs; - java.sql.Timestamp t; - - rs = stmt.executeQuery("select ts from " + TSWTZ_TABLE); //removed the order by ts - assertNotNull(rs); - - for (int i=0; i<3; i++) - { - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS1WTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS2WTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS3WTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS4WTZ)); - } - - // Testing for Date - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate1.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate2.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate3.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate4.getTime()); - - // Testing for Time - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime1.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime2.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime3.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime4.getTime()); - - assertTrue(! rs.next()); // end of table. Fail if more entries exist. - - rs.close(); - stmt.close(); - } - - /* - * Helper for the TimestampTests. It tests what should be in the db - */ - private void timestampTestWOTZ() throws SQLException - { - Statement stmt = con.createStatement(); - ResultSet rs; - java.sql.Timestamp t; - - rs = stmt.executeQuery("select ts from " + TSWOTZ_TABLE); //removed the order by ts - assertNotNull(rs); - - for (int i=0; i<3; i++) - { - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS1WOTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS2WOTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS3WOTZ)); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertTrue(t.equals(TS4WOTZ)); - } - - // Testing for Date - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate1WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate2WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate3WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpDate4WOTZ.getTime()); - - // Testing for Time - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime1WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime2WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime3WOTZ.getTime()); - - assertTrue(rs.next()); - t = rs.getTimestamp(1); - assertNotNull(t); - assertEquals(t.getTime(), tmpTime4WOTZ.getTime()); - - assertTrue(! rs.next()); // end of table. Fail if more entries exist. - - rs.close(); - stmt.close(); - } - - 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) + " "; - - 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); - } - 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 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 TS4WTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123456000, "GMT"); - private static final String TS4WTZ_PGFORMAT = "2000-07-07 15:00:00.123456+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 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"; - - 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"; - - private static final java.sql.Timestamp TS4WOTZ = getTimestamp(2000, 7, 7, 15, 0, 0, 123456000, null); - private static final String TS4WOTZ_PGFORMAT = "2000-07-07 15:00:00.123456"; - - private static final String TSWTZ_TABLE = "testtimestampwtz"; - private static final String TSWOTZ_TABLE = "testtimestampwotz"; - - private static final java.sql.Date tmpDate1 = new java.sql.Date(TS1WTZ.getTime()); - private static final java.sql.Time tmpTime1 = new java.sql.Time(TS1WTZ.getTime()); - private static final java.sql.Date tmpDate2 = new java.sql.Date(TS2WTZ.getTime()); - private static final java.sql.Time tmpTime2 = new java.sql.Time(TS2WTZ.getTime()); - private static final java.sql.Date tmpDate3 = new java.sql.Date(TS3WTZ.getTime()); - private static final java.sql.Time tmpTime3 = new java.sql.Time(TS3WTZ.getTime()); - private static final java.sql.Date tmpDate4 = new java.sql.Date(TS4WTZ.getTime()); - private static final java.sql.Time tmpTime4 = new java.sql.Time(TS4WTZ.getTime()); - - private static final java.sql.Date tmpDate1WOTZ = new java.sql.Date(TS1WOTZ.getTime()); - private static final java.sql.Time tmpTime1WOTZ = new java.sql.Time(TS1WOTZ.getTime()); - private static final java.sql.Date tmpDate2WOTZ = new java.sql.Date(TS2WOTZ.getTime()); - private static final java.sql.Time tmpTime2WOTZ = new java.sql.Time(TS2WOTZ.getTime()); - private static final java.sql.Date tmpDate3WOTZ = new java.sql.Date(TS3WOTZ.getTime()); - private static final java.sql.Time tmpTime3WOTZ = new java.sql.Time(TS3WOTZ.getTime()); - private static final java.sql.Date tmpDate4WOTZ = new java.sql.Date(TS4WOTZ.getTime()); - private static final java.sql.Time tmpTime4WOTZ = new java.sql.Time(TS4WOTZ.getTime()); - - -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java deleted file mode 100644 index 362f3ace63b..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/UpdateableResultTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package org.postgresql.test.jdbc2; - -import java.sql.*; -import junit.framework.TestCase; - -import org.postgresql.test.TestUtil; -/** - * <p>Title: </p> - * <p>Description: </p> - * <p>Copyright: Copyright (c) 2001</p> - * <p>Company: </p> - * @author unascribed - * @version 1.0 - */ - -public class UpdateableResultTest extends TestCase -{ - private Connection con; - - public UpdateableResultTest( String name ) - { - super( name ); - } - - protected void setUp() throws Exception - { - 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(); - - } - - protected void tearDown() throws Exception - { - TestUtil.dropTable(con, "updateable"); - TestUtil.dropTable(con, "second"); - TestUtil.closeDB(con); - } - - public void testCancelRowUpdates() throws Exception - { - Statement st = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); - ResultSet rs = st.executeQuery( "select * from second"); - - // make sure we're dealing with the correct row. - rs.first(); - assertEquals(1,rs.getInt(1)); - assertEquals("anyvalue",rs.getString(2)); - - // update, cancel and make sure nothings changed. - rs.updateInt(1,99); - rs.cancelRowUpdates(); - assertEquals(1,rs.getInt(1)); - assertEquals("anyvalue",rs.getString(2)); - - // real update - rs.updateInt(1,999); - rs.updateRow(); - assertEquals(999,rs.getInt(1)); - assertEquals("anyvalue",rs.getString(2)); - - // scroll some and make sure the update is still there - rs.beforeFirst(); - rs.next(); - assertEquals(999,rs.getInt(1)); - assertEquals("anyvalue",rs.getString(2)); - - - // make sure the update got to the db and the driver isn't lying to us. - rs.close(); - rs = st.executeQuery( "select * from second"); - rs.first(); - assertEquals(999,rs.getInt(1)); - assertEquals("anyvalue",rs.getString(2)); - - rs.close(); - st.close(); - } - - - - public void testUpdateable() - { - try - { - 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(); - - 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.insertRow(); - rs.refreshRow(); - assertTrue( rs.getInt("id") == 3 ); - assertTrue( rs.getString("name").equals("paul")); - assertTrue( rs.getString("notselected") == null ); - - - 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(); - } - - - 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")); - - rs.moveToInsertRow(); - rs.updateInt( "id", 4 ); - rs.updateString( "name", "dave4" ); - - 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")); - - 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")); - - } - } - catch (SQLException ex) - { - fail(ex.getMessage()); - } - - st.close(); - - } - catch (Exception ex) - { - ex.printStackTrace(); - fail(ex.getMessage()); - } - } - - -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java deleted file mode 100644 index 75157a81ccc..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/BaseDataSourceTest.java +++ /dev/null @@ -1,246 +0,0 @@ -package org.postgresql.test.jdbc2.optional; - -import junit.framework.TestCase; -import org.postgresql.test.TestUtil; -import org.postgresql.jdbc2.optional.BaseDataSource; -import org.postgresql.PGConnection; - -import java.sql.*; -import java.util.*; -import javax.naming.*; - -/** - * 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 - * have different subclasses of this which add additional custom - * tests. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.6 $ - */ -public abstract class BaseDataSourceTest extends TestCase -{ - public static String DATA_SOURCE_JNDI = "BaseDataSource"; - protected Connection con; - protected BaseDataSource bds; - - /** - * 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 = TestUtil.openDB(); - TestUtil.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')"); - TestUtil.closeDB(con); - } - - /** - * Removes the test table using a standard connection (not from - * a DataSource) - */ - protected void tearDown() throws Exception - { - con = TestUtil.openDB(); - TestUtil.dropTable(con, "poolingtest"); - TestUtil.closeDB(con); - } - - /** - * Gets a connection from the current BaseDataSource - */ - protected Connection getDataSourceConnection() throws SQLException - { - if(bds == null) - { - initializeDataSource(); - } - return bds.getConnection(); - } - - /** - * 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 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 test to make sure you can execute DDL SQL using the - * Connection from the DataSource. - */ - public void testDdlOverConnection() - { - try - { - con = getDataSourceConnection(); - TestUtil.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()); - } - } - - /** - * Test to make sure that PGConnection methods can be called on the - * pooled Connection. - */ - public void testPGConnection() - { - try - { - con = getDataSourceConnection(); - ((PGConnection)con).getNotifications(); - con.close(); - } - catch (Exception e) - { - fail("Unable to call PGConnection method on pooled connection due to "+e.getClass().getName()+" ("+e.getMessage()+")"); - } - } - - /** - * Uses the mini-JNDI implementation for testing purposes - */ - protected InitialContext getInitialContext() - { - Hashtable env = new Hashtable(); - env.put(Context.INITIAL_CONTEXT_FACTORY, "org.postgresql.test.util.MiniJndiContextFactory"); - try { - return new InitialContext(env); - } catch(NamingException e) { - fail("Unable to create InitialContext: "+e.getMessage()); - return null; - } - } - - /** - * 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() - { - initializeDataSource(); - BaseDataSource oldbds = bds; - InitialContext ic = getInitialContext(); - try { - ic.rebind(DATA_SOURCE_JNDI, bds); - bds = (BaseDataSource)ic.lookup(DATA_SOURCE_JNDI); - assertTrue("Got null looking up DataSource from JNDI!", bds != null); - compareJndiDataSource(oldbds, bds); - } catch (NamingException e) { - fail(e.getMessage()); - } - oldbds = bds; - testUseConnection(); - assertTrue("Test should not have changed DataSource ("+bds+" != "+oldbds+")!", bds == oldbds); - } - - /** - * Check whether a DS was dereferenced from JNDI or recreated. - */ - protected void compareJndiDataSource(BaseDataSource oldbds, BaseDataSource bds) { - assertTrue("DataSource was dereferenced, should have been serialized or recreated", bds != oldbds); - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java deleted file mode 100644 index d49f73c8be9..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/ConnectionPoolTest.java +++ /dev/null @@ -1,496 +0,0 @@ -package org.postgresql.test.jdbc2.optional; - -import org.postgresql.jdbc2.optional.ConnectionPool; -import org.postgresql.test.TestUtil; -import javax.sql.*; -import java.sql.*; - -/** - * Tests for the ConnectionPoolDataSource and PooledConnection - * implementations. They are tested together because the only client - * interface to the PooledConnection is through the CPDS. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.8 $ - */ -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(); - bds.setServerName(TestUtil.getServer()); - bds.setPortNumber(TestUtil.getPort()); - bds.setDatabaseName(TestUtil.getDatabase()); - bds.setUser(TestUtil.getUser()); - bds.setPassword(TestUtil.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(); - } - - /** - * 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(); - } - - /** - * 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 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 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()); - } - } - - /** - * 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()); - } - } - - /** - * Ensures that a statement generated by a proxied connection returns the - * proxied connection from getConnection() [not the physical connection]. - */ - public void testStatementConnection() { - try { - PooledConnection pc = getPooledConnection(); - Connection con = pc.getConnection(); - Statement s = con.createStatement(); - Connection conRetrieved = s.getConnection(); - - assertTrue(con.getClass().equals(conRetrieved.getClass())); - assertTrue(con.equals(conRetrieved)); - } catch (SQLException e) { - fail(e.getMessage()); - } - } - - /** - * Ensures that the Statement proxy generated by the Connection handle - * throws the correct kind of exception. - */ - public void testStatementProxy() { - Statement s = null; - try - { - PooledConnection pc = getPooledConnection(); - Connection con = pc.getConnection(); - s = con.createStatement(); - } - catch (SQLException e) - { - fail(e.getMessage()); - } - try - { - s.executeQuery("SELECT * FROM THIS_TABLE_SHOULD_NOT_EXIST"); - fail("An SQL exception was not thrown that should have been"); - } - catch (SQLException e) - { - ; // This is the expected and correct path - } - catch (Exception e) - { - fail("bad exception; was expecting SQLException, not" + - e.getClass().getName()); - } - } - - /** - * Ensures that a prepared statement generated by a proxied connection - * returns the proxied connection from getConnection() [not the physical - * connection]. - */ - public void testPreparedStatementConnection() { - try { - PooledConnection pc = getPooledConnection(); - Connection con = pc.getConnection(); - PreparedStatement s = con.prepareStatement("select 'x'"); - Connection conRetrieved = s.getConnection(); - - assertTrue(con.getClass().equals(conRetrieved.getClass())); - assertTrue(con.equals(conRetrieved)); - } catch (SQLException e) { - fail(e.getMessage()); - } - } - - /** - * Ensures that a callable statement generated by a proxied connection - * returns the proxied connection from getConnection() [not the physical - * connection]. - */ - public void testCallableStatementConnection() { - try { - PooledConnection pc = getPooledConnection(); - Connection con = pc.getConnection(); - CallableStatement s = con.prepareCall("select 'x'"); - Connection conRetrieved = s.getConnection(); - - assertTrue(con.getClass().equals(conRetrieved.getClass())); - assertTrue(con.equals(conRetrieved)); - } catch (SQLException e) { - fail(e.getMessage()); - } - } - - /** - * Ensure that a statement created from a pool can be used - * like any other statement in regard to pg extensions. - */ - public void testStatementsProxyPGStatement() { - try { - PooledConnection pc = getPooledConnection(); - con = pc.getConnection(); - - Statement s = con.createStatement(); - boolean b = ((org.postgresql.PGStatement)s).isUseServerPrepare(); - - PreparedStatement ps = con.prepareStatement("select 'x'"); - b = ((org.postgresql.PGStatement)ps).isUseServerPrepare(); - - CallableStatement cs = con.prepareCall("select 'x'"); - b = ((org.postgresql.PGStatement)cs).isUseServerPrepare(); - - } 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); - } - - 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++; - } - - public void connectionErrorOccurred(ConnectionEvent event) - { - errorCount++; - } - - public int getCount() - { - return count; - } - - public int getErrorCount() - { - return errorCount; - } - - public void clear() - { - count = errorCount = 0; - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java deleted file mode 100644 index 58e47bb272b..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/OptionalTestSuite.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.postgresql.test.jdbc2.optional; - -import junit.framework.TestSuite; - -/** - * Test suite for the JDBC 2.0 Optional Package implementation. This - * includes the DataSource, ConnectionPoolDataSource, and - * PooledConnection implementations. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.4 $ - */ -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); - suite.addTestSuite(PoolingDataSourceTest.class); - return suite; - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java deleted file mode 100644 index 6f4a6eaf376..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/PoolingDataSourceTest.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.postgresql.test.jdbc2.optional; - -import java.sql.SQLException; -import org.postgresql.test.TestUtil; -import org.postgresql.jdbc2.optional.PoolingDataSource; -import org.postgresql.jdbc2.optional.BaseDataSource; - -/** - * Minimal tests for pooling DataSource. Needs many more. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.2 $ - */ -public class PoolingDataSourceTest extends BaseDataSourceTest -{ - private final static String DS_NAME = "JDBC 2 SE Test DataSource"; - - /** - * Constructor required by JUnit - */ - public PoolingDataSourceTest(String name) - { - super(name); - } - - protected void tearDown() throws Exception - { - super.tearDown(); - if (bds instanceof PoolingDataSource) - { - ((PoolingDataSource) bds).close(); - } - } - - /** - * Creates and configures a new SimpleDataSource. - */ - protected void initializeDataSource() - { - if (bds == null) - { - bds = new PoolingDataSource(); - bds.setServerName(TestUtil.getServer()); - bds.setPortNumber(TestUtil.getPort()); - bds.setDatabaseName(TestUtil.getDatabase()); - bds.setUser(TestUtil.getUser()); - bds.setPassword(TestUtil.getPassword()); - ((PoolingDataSource) bds).setDataSourceName(DS_NAME); - ((PoolingDataSource) bds).setInitialConnections(2); - ((PoolingDataSource) bds).setMaxConnections(10); - } - } - - /** - * In this case, we *do* want it to be pooled. - */ - public void testNotPooledConnection() - { - try - { - con = getDataSourceConnection(); - String name = con.toString(); - con.close(); - con = getDataSourceConnection(); - String name2 = con.toString(); - con.close(); - assertTrue("Pooled DS doesn't appear to be pooling connections!", name.equals(name2)); - } - catch (SQLException e) - { - fail(e.getMessage()); - } - } - - /** - * In this case, the desired behavior is dereferencing. - */ - protected void compareJndiDataSource(BaseDataSource oldbds, BaseDataSource bds) - { - assertTrue("DataSource was serialized or recreated, should have been dereferenced", bds == oldbds); - } - - /** - * Check that 2 DS instances can't use the same name. - */ - public void testCantReuseName() - { - initializeDataSource(); - PoolingDataSource pds = new PoolingDataSource(); - try - { - pds.setDataSourceName(DS_NAME); - fail("Should have denied 2nd DataSource with same name"); - } - catch (IllegalArgumentException e) - { - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/SimpleDataSourceTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/SimpleDataSourceTest.java deleted file mode 100644 index 40511222357..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/optional/SimpleDataSourceTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.postgresql.test.jdbc2.optional; - -import org.postgresql.test.TestUtil; -import org.postgresql.jdbc2.optional.SimpleDataSource; - -/** - * Performs the basic tests defined in the superclass. Just adds the - * configuration logic. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.4 $ - */ -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(); - bds.setServerName(TestUtil.getServer()); - bds.setPortNumber(TestUtil.getPort()); - bds.setDatabaseName(TestUtil.getDatabase()); - bds.setUser(TestUtil.getUser()); - bds.setPassword(TestUtil.getPassword()); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3ConnectionPoolTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3ConnectionPoolTest.java deleted file mode 100644 index 013a18a8f94..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3ConnectionPoolTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.postgresql.test.jdbc3; - -import org.postgresql.jdbc3.Jdbc3ConnectionPool; -import org.postgresql.jdbc3.Jdbc3PooledConnection; -import org.postgresql.test.TestUtil; -import org.postgresql.test.jdbc2.optional.ConnectionPoolTest; -import java.sql.SQLException; - -import javax.sql.PooledConnection; - -/** - * Tests JDBC3 implementation of ConnectionPoolDataSource. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.3 $ - */ -public class Jdbc3ConnectionPoolTest extends ConnectionPoolTest -{ - public Jdbc3ConnectionPoolTest(String name) - { - super(name); - } - - /** - * Creates and configures a Jdbc3ConnectionPool - */ - protected void initializeDataSource() - { - if (bds == null) - { - bds = new Jdbc3ConnectionPool(); - bds.setServerName(TestUtil.getServer()); - bds.setPortNumber(TestUtil.getPort()); - bds.setDatabaseName(TestUtil.getDatabase()); - bds.setUser(TestUtil.getUser()); - bds.setPassword(TestUtil.getPassword()); - } - } - - /** - * Makes sure this is a JDBC 3 implementation producing JDBC3 - * connections. Depends on toString implementation of - * connection wrappers. - */ - public void testConfirmJdbc3Impl() - { - try - { - initializeDataSource(); - assertTrue("Wrong ConnectionPool impl used by test: " + bds.getClass().getName(), bds instanceof Jdbc3ConnectionPool); - PooledConnection pc = ((Jdbc3ConnectionPool) bds).getPooledConnection(); - assertTrue("Wrong PooledConnection impl generated by JDBC3 ConnectionPoolDataSource: " + pc.getClass().getName(), pc instanceof Jdbc3PooledConnection); - assertTrue("Wrong Connnection class used in JDBC3 ConnectionPoolDataSource's PooledConnection impl: " + pc.getConnection().toString(), pc.getConnection().toString().indexOf("Jdbc3") > -1); - pc.close(); - } - catch (SQLException e) - { - fail(e.getMessage()); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3PoolingDataSourceTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3PoolingDataSourceTest.java deleted file mode 100644 index c6c603b7f0c..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3PoolingDataSourceTest.java +++ /dev/null @@ -1,149 +0,0 @@ -package org.postgresql.test.jdbc3; - -import java.sql.Connection; -import java.sql.SQLException; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.postgresql.test.jdbc2.optional.PoolingDataSourceTest; -import org.postgresql.test.TestUtil; -import org.postgresql.jdbc3.Jdbc3PoolingDataSource; -import org.postgresql.jdbc2.optional.PoolingDataSource; - -/** - * Minimal tests for JDBC3 pooling DataSource. Needs many more. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.2 $ - */ -public class Jdbc3PoolingDataSourceTest extends PoolingDataSourceTest -{ - private final static String DS_NAME = "JDBC 3 Test DataSource"; - - /** - * Constructor required by JUnit - */ - public Jdbc3PoolingDataSourceTest(String name) - { - super(name); - } - - /** - * Creates and configures a new SimpleDataSource. - */ - protected void initializeDataSource() - { - if (bds == null) - { - bds = new Jdbc3PoolingDataSource(); - configureDataSource((Jdbc3PoolingDataSource) bds); - } - } - - private void configureDataSource(PoolingDataSource source) - { - String db = TestUtil.getURL(); - source.setServerName(TestUtil.getServer()); - source.setPortNumber(TestUtil.getPort()); - source.setDatabaseName(TestUtil.getDatabase()); - source.setUser(TestUtil.getUser()); - source.setPassword(TestUtil.getPassword()); - source.setDataSourceName(DS_NAME); - source.setInitialConnections(2); - source.setMaxConnections(10); - } - - /** - * Check that 2 DS instances can't use the same name. - */ - public void testCantReuseName() - { - initializeDataSource(); - Jdbc3PoolingDataSource pds = new Jdbc3PoolingDataSource(); - try - { - pds.setDataSourceName(DS_NAME); - fail("Should have denied 2nd DataSource with same name"); - } - catch (IllegalArgumentException e) - { - } - } - - /** - * Test that JDBC 2 and JDBC 3 DSs come from different buckets - * as far as creating with the same name - */ - public void testDifferentImplPools() - { - initializeDataSource(); - PoolingDataSource pds = new PoolingDataSource(); - try - { - configureDataSource(pds); - PoolingDataSource p2 = new PoolingDataSource(); - try - { - configureDataSource(p2); - fail("Shouldn't be able to create 2 JDBC 2 DSs with same name"); - } - catch (IllegalArgumentException e) - { - } - Jdbc3PoolingDataSource p3 = new Jdbc3PoolingDataSource(); - try - { - configureDataSource(p3); - fail("Shouldn't be able to create 2 JDBC 3 DSs with same name"); - } - catch (IllegalArgumentException e) - { - } - } - finally - { - pds.close(); - } - } - - /** - * Test that JDBC 2 and JDBC 3 DSs come from different buckets - * as far as fetching from JNDI - */ - public void testDifferentImplJndi() - { - initializeDataSource(); - PoolingDataSource pds = new PoolingDataSource(); - try - { - configureDataSource(pds); - try - { - Connection j3c = getDataSourceConnection(); - Connection j2c = pds.getConnection(); - j2c.close(); - j3c.close(); - InitialContext ctx = getInitialContext(); - ctx.bind("JDBC2", pds); - ctx.bind("JDBC3", bds); - pds = (PoolingDataSource) ctx.lookup("JDBC2"); - bds = (Jdbc3PoolingDataSource) ctx.lookup("JDBC3"); - j2c = pds.getConnection(); - j3c = bds.getConnection(); - j2c.close(); - j3c.close(); - } - catch (SQLException e) - { - fail(e.getMessage()); - } - catch (NamingException e) - { - fail(e.getMessage()); - } - } - finally - { - pds.close(); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3SimpleDataSourceTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3SimpleDataSourceTest.java deleted file mode 100644 index 7c1157deb31..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3SimpleDataSourceTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.postgresql.test.jdbc3; - -import java.sql.Connection; -import java.sql.SQLException; -import org.postgresql.test.jdbc2.optional.SimpleDataSourceTest; -import org.postgresql.test.TestUtil; -import org.postgresql.jdbc3.*; - -/** - * Tests JDBC3 non-pooling DataSource. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.2 $ - */ -public class Jdbc3SimpleDataSourceTest extends SimpleDataSourceTest { - /** - * Constructor required by JUnit - */ - public Jdbc3SimpleDataSourceTest(String name) - { - super(name); - } - - /** - * Creates and configures a new SimpleDataSource. - */ - protected void initializeDataSource() - { - if (bds == null) - { - bds = new Jdbc3SimpleDataSource(); - bds.setServerName(TestUtil.getServer()); - bds.setPortNumber(TestUtil.getPort()); - bds.setDatabaseName(TestUtil.getDatabase()); - bds.setUser(TestUtil.getUser()); - bds.setPassword(TestUtil.getPassword()); - } - } - /** - * Makes sure this is a JDBC 3 implementation producing JDBC3 - * connections. - */ - public void testConfirmJdbc3Impl() - { - try { - Connection con = getDataSourceConnection(); - assertTrue("Wrong SimpleDataSource impl used by test: "+bds.getClass().getName(), bds instanceof Jdbc3SimpleDataSource); - assertTrue("Wrong Connnection class generated by JDBC3 DataSource: "+con.getClass().getName(), con instanceof Jdbc3Connection); - } catch (SQLException e) { - fail(e.getMessage()); - } - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3TestSuite.java b/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3TestSuite.java deleted file mode 100644 index d4b4ea087be..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/jdbc3/Jdbc3TestSuite.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.postgresql.test.jdbc3; - -import junit.framework.TestSuite; - -/* - * Executes all known tests for JDBC3 - */ -public class Jdbc3TestSuite extends TestSuite -{ - - /* - * The main entry point for JUnit - */ - public static TestSuite suite() - { - TestSuite suite = new TestSuite(); - suite.addTestSuite(Jdbc3SimpleDataSourceTest.class); - suite.addTestSuite(Jdbc3ConnectionPoolTest.class); - suite.addTestSuite(Jdbc3PoolingDataSourceTest.class); - return suite; - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContext.java b/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContext.java deleted file mode 100644 index 4caf24120c5..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContext.java +++ /dev/null @@ -1,228 +0,0 @@ -package org.postgresql.test.util; - -import java.util.*; -import java.rmi.MarshalledObject; -import java.io.Serializable; -import javax.naming.*; -import javax.naming.spi.ObjectFactory; - -/** - * The Context for a trivial JNDI implementation. This is not meant to - * be very useful, beyond testing JNDI features of the connection - * pools. It is not a complete JNDI implementations. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.1 $ - */ -public class MiniJndiContext implements Context -{ - private Map map = new HashMap(); - - public MiniJndiContext() - { - } - - public Object lookup(Name name) throws NamingException - { - return lookup(name.get(0)); - } - - public Object lookup(String name) throws NamingException - { - Object o = map.get(name); - if (o == null) - { - return null; - } - if (o instanceof Reference) - { - Reference ref = (Reference) o; - try - { - Class factoryClass = Class.forName(ref.getFactoryClassName()); - ObjectFactory fac = (ObjectFactory) factoryClass.newInstance(); - Object result = fac.getObjectInstance(ref, null, this, null); - return result; - } - catch (Exception e) - { - throw new NamingException("Unable to dereference to object: " + e); - } - } - else if (o instanceof MarshalledObject) - { - try - { - Object result = ((MarshalledObject) o).get(); - return result; - } - catch (java.io.IOException e) - { - throw new NamingException("Unable to deserialize object: " + e); - } - catch (ClassNotFoundException e) - { - throw new NamingException("Unable to deserialize object: " + e); - } - } - else - { - throw new NamingException("JNDI Object is neither Referenceable nor Serializable"); - } - } - - public void bind(Name name, Object obj) throws NamingException - { - rebind(name.get(0), obj); - } - - public void bind(String name, Object obj) throws NamingException - { - rebind(name, obj); - } - - public void rebind(Name name, Object obj) throws NamingException - { - rebind(name.get(0), obj); - } - - public void rebind(String name, Object obj) throws NamingException - { - if (obj instanceof Referenceable) - { - Reference ref = ((Referenceable) obj).getReference(); - map.put(name, ref); - } - else if (obj instanceof Serializable) - { - try - { - MarshalledObject mo = new MarshalledObject(obj); - map.put(name, mo); - } - catch (java.io.IOException e) - { - throw new NamingException("Unable to serialize object to JNDI: " + e); - } - } - else - { - throw new NamingException("Object to store in JNDI is neither Referenceable nor Serializable"); - } - } - - public void unbind(Name name) throws NamingException - { - unbind(name.get(0)); - } - - public void unbind(String name) throws NamingException - { - map.remove(name); - } - - public void rename(Name oldName, Name newName) throws NamingException - { - rename(oldName.get(0), newName.get(0)); - } - - public void rename(String oldName, String newName) throws NamingException - { - map.put(newName, map.remove(oldName)); - } - - public NamingEnumeration list(Name name) throws NamingException - { - return null; - } - - public NamingEnumeration list(String name) throws NamingException - { - return null; - } - - public NamingEnumeration listBindings(Name name) throws NamingException - { - return null; - } - - public NamingEnumeration listBindings(String name) throws NamingException - { - return null; - } - - public void destroySubcontext(Name name) throws NamingException - { - } - - public void destroySubcontext(String name) throws NamingException - { - } - - public Context createSubcontext(Name name) throws NamingException - { - return null; - } - - public Context createSubcontext(String name) throws NamingException - { - return null; - } - - public Object lookupLink(Name name) throws NamingException - { - return null; - } - - public Object lookupLink(String name) throws NamingException - { - return null; - } - - public NameParser getNameParser(Name name) throws NamingException - { - return null; - } - - public NameParser getNameParser(String name) throws NamingException - { - return null; - } - - public Name composeName(Name name, Name prefix) throws NamingException - { - return null; - } - - public String composeName(String name, String prefix) - throws NamingException - { - return null; - } - - public Object addToEnvironment(String propName, Object propVal) - throws NamingException - { - return null; - } - - public Object removeFromEnvironment(String propName) - throws NamingException - { - return null; - } - - public Hashtable getEnvironment() throws NamingException - { - return null; - } - - public void close() throws NamingException - { - } - - public String getNameInNamespace() throws NamingException - { - return null; - } -} diff --git a/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContextFactory.java b/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContextFactory.java deleted file mode 100644 index 58cc533ce0e..00000000000 --- a/src/interfaces/jdbc/org/postgresql/test/util/MiniJndiContextFactory.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.postgresql.test.util; - -import java.util.*; -import javax.naming.*; -import javax.naming.spi.InitialContextFactory; - -/** - * The ICF for a trivial JNDI implementation. This is not meant to - * be very useful, beyond testing JNDI features of the connection - * pools. - * - * @author Aaron Mulder (ammulder@chariotsolutions.com) - * @version $Revision: 1.1 $ - */ -public class MiniJndiContextFactory implements InitialContextFactory -{ - public Context getInitialContext(Hashtable environment) - throws NamingException - { - return new MiniJndiContext(); - } -} |
