From 8839b85ed8ffea2250b3976225044e3cb90b2eed Mon Sep 17 00:00:00 2001
From: Barry Lind <barry@xythos.com>
Date: Mon, 22 Sep 2003 05:38:01 +0000
Subject: [PATCH] Additional jdbc regression tests submitted by Oliver Jowett. 
 Some tests are currently commented out, pending fixes for the bugs these
 tests uncovered.

 Modified Files:
 	jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
 	jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
 Added Files:
 	jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java
---
 .../test/jdbc2/CursorFetchTest.java           | 92 +++++++++++++++++++
 .../postgresql/test/jdbc2/Jdbc2TestSuite.java |  1 +
 .../test/jdbc2/ServerPreparedStmtTest.java    | 33 +++++++
 3 files changed, 126 insertions(+)
 create mode 100644 src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java

diff --git a/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java
new file mode 100644
index 00000000000..def403fdeff
--- /dev/null
+++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/CursorFetchTest.java
@@ -0,0 +1,92 @@
+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]);
+			ResultSet rs = stmt.executeQuery();
+ 
+			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);
+		}
+	}
+
+	// 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/Jdbc2TestSuite.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
index 4a8548a3cf4..31af0c520b7 100644
--- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
+++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java
@@ -61,6 +61,7 @@ public class Jdbc2TestSuite extends TestSuite
 		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/ServerPreparedStmtTest.java b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
index aa5831385fc..aeb356f2b14 100644
--- a/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
+++ b/src/interfaces/jdbc/org/postgresql/test/jdbc2/ServerPreparedStmtTest.java
@@ -229,4 +229,37 @@ public class ServerPreparedStmtTest extends TestCase
 			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");
+		}
+	}
 }
-- 
GitLab