mirror of
https://github.com/postgres/postgres.git
synced 2025-08-30 06:01:21 +03:00
pgjindent jdbc files. First time jdbc files were formatted.
This commit is contained in:
@@ -12,24 +12,27 @@ import java.sql.*;
|
||||
/**
|
||||
* Test case for Statement.batchExecute()
|
||||
*/
|
||||
public class BatchExecuteTest extends TestCase {
|
||||
public class BatchExecuteTest extends TestCase
|
||||
{
|
||||
|
||||
private Connection con;
|
||||
|
||||
public BatchExecuteTest(String name) {
|
||||
public BatchExecuteTest(String name)
|
||||
{
|
||||
super(name);
|
||||
}
|
||||
|
||||
// Set up the fixture for this testcase: a connection to a database with
|
||||
// a table for this test.
|
||||
protected void setUp() throws Exception {
|
||||
protected void setUp() throws Exception
|
||||
{
|
||||
con = JDBC2Tests.openDB();
|
||||
Statement stmt = con.createStatement();
|
||||
|
||||
// Drop the test table if it already exists for some reason. It is
|
||||
// not an error if it doesn't exist.
|
||||
JDBC2Tests.createTable(con, "testbatch", "pk INTEGER, col1 INTEGER");
|
||||
|
||||
|
||||
stmt.executeUpdate("INSERT INTO testbatch VALUES (1, 0)");
|
||||
|
||||
// Generally recommended with batch updates. By default we run all
|
||||
@@ -38,19 +41,22 @@ public class BatchExecuteTest extends TestCase {
|
||||
}
|
||||
|
||||
// Tear down the fixture for this test case.
|
||||
protected void tearDown() throws Exception {
|
||||
protected void tearDown() throws Exception
|
||||
{
|
||||
con.setAutoCommit(true);
|
||||
|
||||
JDBC2Tests.dropTable(con, "testbatch");
|
||||
JDBC2Tests.closeDB(con);
|
||||
}
|
||||
|
||||
public void testSupportsBatchUpdates() throws Exception {
|
||||
public void testSupportsBatchUpdates() throws Exception
|
||||
{
|
||||
DatabaseMetaData dbmd = con.getMetaData();
|
||||
assertTrue(dbmd.supportsBatchUpdates());
|
||||
}
|
||||
|
||||
private void assertCol1HasValue(int expected) throws Exception {
|
||||
private void assertCol1HasValue(int expected) throws Exception
|
||||
{
|
||||
Statement getCol1 = con.createStatement();
|
||||
|
||||
ResultSet rs =
|
||||
@@ -67,21 +73,23 @@ public class BatchExecuteTest extends TestCase {
|
||||
getCol1.close();
|
||||
}
|
||||
|
||||
public void testExecuteEmptyBatch() throws Exception {
|
||||
public void testExecuteEmptyBatch() throws Exception
|
||||
{
|
||||
Statement stmt = con.createStatement();
|
||||
int[] updateCount = stmt.executeBatch();
|
||||
assertEquals(0,updateCount.length);
|
||||
assertEquals(0, updateCount.length);
|
||||
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
|
||||
stmt.clearBatch();
|
||||
updateCount = stmt.executeBatch();
|
||||
assertEquals(0,updateCount.length);
|
||||
assertEquals(0, updateCount.length);
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
public void testClearBatch() throws Exception {
|
||||
public void testClearBatch() throws Exception
|
||||
{
|
||||
Statement stmt = con.createStatement();
|
||||
|
||||
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
|
||||
assertCol1HasValue(0);
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
|
||||
@@ -98,44 +106,51 @@ public class BatchExecuteTest extends TestCase {
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
public void testSelectThrowsException() throws Exception {
|
||||
public void testSelectThrowsException() throws Exception
|
||||
{
|
||||
Statement stmt = con.createStatement();
|
||||
|
||||
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
|
||||
stmt.addBatch("SELECT col1 FROM testbatch WHERE pk = 1");
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
stmt.executeBatch();
|
||||
fail("Should raise a BatchUpdateException because of the SELECT");
|
||||
} catch (BatchUpdateException e) {
|
||||
}
|
||||
catch (BatchUpdateException e)
|
||||
{
|
||||
int [] updateCounts = e.getUpdateCounts();
|
||||
assertEquals(1,updateCounts.length);
|
||||
assertEquals(1,updateCounts[0]);
|
||||
} catch (SQLException e) {
|
||||
assertEquals(1, updateCounts.length);
|
||||
assertEquals(1, updateCounts[0]);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
fail( "Should throw a BatchUpdateException instead of " +
|
||||
"a generic SQLException: " + e);
|
||||
"a generic SQLException: " + e);
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
}
|
||||
|
||||
public void testPreparedStatement() throws Exception {
|
||||
public void testPreparedStatement() throws Exception
|
||||
{
|
||||
PreparedStatement pstmt = con.prepareStatement(
|
||||
"UPDATE testbatch SET col1 = col1 + ? WHERE PK = ?" );
|
||||
"UPDATE testbatch SET col1 = col1 + ? WHERE PK = ?" );
|
||||
|
||||
// Note that the first parameter changes for every statement in the
|
||||
// batch, whereas the second parameter remains constant.
|
||||
pstmt.setInt(1,1);
|
||||
pstmt.setInt(2,1);
|
||||
pstmt.setInt(1, 1);
|
||||
pstmt.setInt(2, 1);
|
||||
pstmt.addBatch();
|
||||
assertCol1HasValue(0);
|
||||
|
||||
pstmt.setInt(1,2);
|
||||
pstmt.setInt(1, 2);
|
||||
pstmt.addBatch();
|
||||
assertCol1HasValue(0);
|
||||
|
||||
pstmt.setInt(1,4);
|
||||
pstmt.setInt(1, 4);
|
||||
pstmt.addBatch();
|
||||
assertCol1HasValue(0);
|
||||
|
||||
@@ -153,9 +168,10 @@ public class BatchExecuteTest extends TestCase {
|
||||
|
||||
/**
|
||||
*/
|
||||
public void testTransactionalBehaviour() throws Exception {
|
||||
public void testTransactionalBehaviour() throws Exception
|
||||
{
|
||||
Statement stmt = con.createStatement();
|
||||
|
||||
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 1 WHERE pk = 1");
|
||||
stmt.addBatch("UPDATE testbatch SET col1 = col1 + 2 WHERE pk = 1");
|
||||
stmt.executeBatch();
|
||||
@@ -170,9 +186,9 @@ public class BatchExecuteTest extends TestCase {
|
||||
assertCol1HasValue(0);
|
||||
|
||||
int[] updateCounts = stmt.executeBatch();
|
||||
assertEquals(2,updateCounts.length);
|
||||
assertEquals(1,updateCounts[0]);
|
||||
assertEquals(1,updateCounts[1]);
|
||||
assertEquals(2, updateCounts.length);
|
||||
assertEquals(1, updateCounts[0]);
|
||||
assertEquals(1, updateCounts[1]);
|
||||
|
||||
assertCol1HasValue(12);
|
||||
con.commit();
|
||||
|
Reference in New Issue
Block a user