Tests for prepared insert statements - Mailing list pgsql-jdbc

From Xavier Poinsard
Subject Tests for prepared insert statements
Date
Msg-id cnfqn2$99h$2@sea.gmane.org
Whole thread Raw
Responses Re: Tests for prepared insert statements
List pgsql-jdbc
Hi,
Since I was suspecting the driver, I wrote a two tests regarding
prepared insert statements. May be they could be included.

Xavier.
Index: Jdbc2TestSuite.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java,v
retrieving revision 1.18
diff -u -r1.18 Jdbc2TestSuite.java
--- Jdbc2TestSuite.java    9 Nov 2004 08:54:37 -0000    1.18
+++ Jdbc2TestSuite.java    17 Nov 2004 15:16:40 -0000
@@ -55,6 +55,7 @@
         // PreparedStatement
         suite.addTestSuite(PreparedStatementTest.class);
         suite.addTestSuite(StatementTest.class);
+        suite.addTestSuite(BatchPreparedInsertTest.class);

         // ServerSide Prepared Statements
         suite.addTestSuite(ServerPreparedStmtTest.class);
Index: BatchPreparedInsertTest.java
===================================================================
RCS file: BatchPreparedInsertTest.java
diff -N BatchPreparedInsertTest.java
--- /dev/null    1 Jan 1970 00:00:00 -0000
+++ BatchPreparedInsertTest.java    1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,151 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2004, PostgreSQL Global Development Group
+*
+* IDENTIFICATION
+*   $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java,v 1.11 2004/11/09 08:54:00 jurka Exp $
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.test.jdbc2;
+
+import org.postgresql.test.TestUtil;
+import junit.framework.TestCase;
+import java.sql.*;
+
+
+/*
+ * Test case for prepared insert statements
+ */
+public class BatchPreparedInsertTest extends TestCase
+{
+
+    private Connection con;
+
+    public BatchPreparedInsertTest(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, "testbatchinsert", "pk INTEGER, colInt INTEGER,colReal REAL, colVarchar
VARCHAR(10)");
+
+        stmt.executeUpdate("INSERT INTO testbatchinsert VALUES (1, 0,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, "testbatchinsert");
+        TestUtil.closeDB(con);
+    }
+
+    private void assertColIntHasValue(int expected) throws Exception
+    {
+        Statement getCol1 = con.createStatement();
+
+        ResultSet rs =
+            getCol1.executeQuery("SELECT colInt FROM testbatchinsert WHERE pk = 1");
+        assertTrue(rs.next());
+
+        int actual = rs.getInt("colInt");
+
+        assertEquals(expected, actual);
+
+        assertEquals(false, rs.next());
+
+        rs.close();
+        getCol1.close();
+    }
+
+    private void assertColRealHasValue(float expected) throws Exception
+    {
+        Statement getCol1 = con.createStatement();
+
+        ResultSet rs =
+            getCol1.executeQuery("SELECT colReal FROM testbatchinsert WHERE pk = 1");
+        assertTrue(rs.next());
+
+        float actual = rs.getFloat("colReal");
+
+        assertEquals(expected, actual,0f);
+
+        assertEquals(false, rs.next());
+
+        rs.close();
+        getCol1.close();
+    }
+
+    private void assertColVarcharHasValue(String expected) throws Exception
+    {
+        Statement getCol1 = con.createStatement();
+
+        ResultSet rs =
+            getCol1.executeQuery("SELECT colVarchar FROM testbatchinsert WHERE pk = 1");
+        assertTrue(rs.next());
+
+        String actual = rs.getString("colVarchar");
+
+        assertEquals(expected, actual);
+
+        assertEquals(false, rs.next());
+
+        rs.close();
+        getCol1.close();
+    }
+
+
+    /**
+     * test basic insert prepared statement
+     * @throws Exception
+     */
+    public void testPreparedStatement() throws Exception
+    {
+        PreparedStatement pstmt = con.prepareStatement(
+                                      "INSERT INTO  testbatchinsert (pk,colInt,colReal,colVarchar)" +
+                                      "VALUES (1,?,?,?)" );
+
+        pstmt.setInt(1, 1);
+        pstmt.setFloat(2, 0.3f);
+        pstmt.setString(3,"test");
+        pstmt.execute();
+        con.commit();
+        pstmt.close();
+    }
+    /**
+     * test a batch of prepared insert statements
+     * @throws Exception
+     */
+    public void testBatchPreparedStatement() throws Exception
+    {
+        PreparedStatement pstmt = con.prepareStatement(
+                                      "INSERT INTO  testbatchinsert (pk,colInt,colReal,colVarchar)" +
+                                      "VALUES (?,?,?,?)" );
+
+        for (int i=1;i<=TestUtil.getPrepareThreshold()+3;i++){
+            pstmt.setInt(1, i+1);
+            pstmt.setInt(2, i+1);
+            pstmt.setFloat(3, i*0.3f);
+            pstmt.setString(4,"test"+i);
+            pstmt.addBatch();
+        }
+        pstmt.executeBatch();
+        con.commit();
+        pstmt.close();
+    }
+
+}

pgsql-jdbc by date:

Previous
From: Xavier Poinsard
Date:
Subject: Permission denied error with testGetUDTQualified
Next
From: Kris Jurka
Date:
Subject: Re: Permission denied error with testGetUDTQualified