1 16 17 package org.springframework.jdbc.object; 18 19 import java.sql.DatabaseMetaData ; 20 import java.sql.PreparedStatement ; 21 import java.sql.Types ; 22 23 import org.easymock.MockControl; 24 25 import org.springframework.jdbc.AbstractJdbcTests; 26 import org.springframework.jdbc.core.SqlParameter; 27 28 32 public class BatchSqlUpdateTests extends AbstractJdbcTests { 33 34 public void testBatchUpdateWithExplicitFlush() throws Exception { 35 doTestBatchUpdate(false); 36 } 37 38 public void testBatchUpdateWithFlushThroughBatchSize() throws Exception { 39 doTestBatchUpdate(true); 40 } 41 42 private void doTestBatchUpdate(boolean flushThroughBatchSize) throws Exception { 43 final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"; 44 final int[] ids = new int[] { 100, 200 }; 45 final int[] rowsAffected = new int[] { 1, 2 }; 46 47 MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement .class); 48 PreparedStatement mockPreparedStatement = (PreparedStatement ) ctrlPreparedStatement.getMock(); 49 mockPreparedStatement.getConnection(); 50 ctrlPreparedStatement.setReturnValue(mockConnection); 51 mockPreparedStatement.setObject(1, new Integer (ids[0]), Types.INTEGER); 52 ctrlPreparedStatement.setVoidCallable(); 53 mockPreparedStatement.addBatch(); 54 ctrlPreparedStatement.setVoidCallable(); 55 mockPreparedStatement.setObject(1, new Integer (ids[1]), Types.INTEGER); 56 ctrlPreparedStatement.setVoidCallable(); 57 mockPreparedStatement.addBatch(); 58 ctrlPreparedStatement.setVoidCallable(); 59 mockPreparedStatement.executeBatch(); 60 ctrlPreparedStatement.setReturnValue(rowsAffected); 61 mockPreparedStatement.getWarnings(); 62 ctrlPreparedStatement.setReturnValue(null); 63 mockPreparedStatement.close(); 64 ctrlPreparedStatement.setVoidCallable(); 65 66 MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData .class); 67 DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData ) ctrlDatabaseMetaData.getMock(); 68 mockDatabaseMetaData.supportsBatchUpdates(); 69 ctrlDatabaseMetaData.setReturnValue(true); 70 71 mockConnection.prepareStatement(sql); 72 ctrlConnection.setReturnValue(mockPreparedStatement); 73 mockConnection.getMetaData(); 74 ctrlConnection.setReturnValue(mockDatabaseMetaData, 1); 75 76 ctrlPreparedStatement.replay(); 77 ctrlDatabaseMetaData.replay(); 78 replay(); 79 80 BatchSqlUpdate update = new BatchSqlUpdate(mockDataSource, sql); 81 update.declareParameter(new SqlParameter(Types.INTEGER)); 82 if (flushThroughBatchSize) { 83 update.setBatchSize(2); 84 } 85 86 update.update(ids[0]); 87 update.update(ids[1]); 88 89 if (flushThroughBatchSize) { 90 assertEquals(0, update.getQueueCount()); 91 assertEquals(2, update.getRowsAffected().length); 92 } 93 else { 94 assertEquals(2, update.getQueueCount()); 95 assertEquals(0, update.getRowsAffected().length); 96 } 97 98 int[] actualRowsAffected = update.flush(); 99 assertEquals(0, update.getQueueCount()); 100 101 if (flushThroughBatchSize) { 102 assertTrue("flush did not execute updates", actualRowsAffected.length == 0); 103 } 104 else { 105 assertTrue("executed 2 updates", actualRowsAffected.length == 2); 106 assertEquals(rowsAffected[0], actualRowsAffected[0]); 107 assertEquals(rowsAffected[1], actualRowsAffected[1]); 108 } 109 110 actualRowsAffected = update.getRowsAffected(); 111 assertTrue("executed 2 updates", actualRowsAffected.length == 2); 112 assertEquals(rowsAffected[0], actualRowsAffected[0]); 113 assertEquals(rowsAffected[1], actualRowsAffected[1]); 114 115 update.reset(); 116 assertEquals(0, update.getRowsAffected().length); 117 118 ctrlPreparedStatement.verify(); 119 ctrlDatabaseMetaData.verify(); 120 } 121 122 } 123 | Popular Tags |