1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java,v 1.16 2008/01/08 06:56:30 jurka Exp $
*
*-------------------------------------------------------------------------
*/
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);
try
{
Class.forName("org.postgresql.Driver");
}
catch( Exception ex){}
}
// 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());
}
public void testEmptyClearBatch() throws Exception
{
Statement stmt = con.createStatement();
stmt.clearBatch(); // No-op.
PreparedStatement ps = con.prepareStatement("SELECT ?");
ps.clearBatch(); // No-op.
}
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 testStringAddBatchOnPreparedStatement() throws Exception
{
PreparedStatement pstmt = con.prepareStatement("UPDATE testbatch SET col1 = col1 + ? WHERE PK = ?" );
pstmt.setInt(1, 1);
pstmt.setInt(2, 1);
pstmt.addBatch();
try
{
pstmt.addBatch("UPDATE testbatch SET col1 = 3");
fail("Should have thrown an exception about using the string addBatch method on a prepared statement.");
}
catch (SQLException sqle)
{
}
pstmt.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();
}
public void testWarningsAreCleared() throws SQLException
{
Statement stmt = con.createStatement();
stmt.addBatch("CREATE TEMP TABLE unused (a int primary key)");
stmt.executeBatch();
// Execute an empty batch to clear warnings.
stmt.executeBatch();
assertNull(stmt.getWarnings());
stmt.close();
}
public void testBatchEscapeProcessing() throws SQLException
{
Statement stmt = con.createStatement();
stmt.execute("CREATE TEMP TABLE batchescape (d date)");
stmt.addBatch("INSERT INTO batchescape (d) VALUES ({d '2007-11-20'})");
stmt.executeBatch();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO batchescape (d) VALUES ({d '2007-11-20'})");
pstmt.addBatch();
pstmt.executeBatch();
pstmt.close();
ResultSet rs = stmt.executeQuery("SELECT d FROM batchescape");
assertTrue(rs.next());
assertEquals("2007-11-20", rs.getString(1));
assertTrue(rs.next());
assertEquals("2007-11-20", rs.getString(1));
assertTrue(!rs.next());
rs.close();
stmt.close();
}
}
|