added batchUpdate methods to NamedParameterJdbcTemplate (SPR-3322)
This commit is contained in:
parent
88e32a3cfe
commit
4105957596
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.jdbc.core;
|
||||
|
||||
import java.util.List;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
|
||||
* within the framework.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public abstract class BatchUpdateUtils {
|
||||
|
||||
public static int[] executeBatchUpdate(String sql, final List<Object[]> batchValues, final int[] columnTypes, JdbcOperations jdbcOperations) {
|
||||
return jdbcOperations.batchUpdate(
|
||||
sql,
|
||||
new BatchPreparedStatementSetter() {
|
||||
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
Object[] values = batchValues.get(i);
|
||||
setStatementParameters(values, ps, columnTypes);
|
||||
}
|
||||
|
||||
public int getBatchSize() {
|
||||
return batchValues.size();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected static void setStatementParameters(Object[] values, PreparedStatement ps, int[] columnTypes) throws SQLException {
|
||||
int colIndex = 0;
|
||||
for (Object value : values) {
|
||||
colIndex++;
|
||||
if (value instanceof SqlParameterValue) {
|
||||
SqlParameterValue paramValue = (SqlParameterValue) value;
|
||||
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
|
||||
}
|
||||
else {
|
||||
int colType;
|
||||
if (columnTypes == null || columnTypes.length < colIndex) {
|
||||
colType = SqlTypeValue.TYPE_UNKNOWN;
|
||||
}
|
||||
else {
|
||||
colType = columnTypes[colIndex - 1];
|
||||
}
|
||||
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package org.springframework.jdbc.core.namedparam;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.springframework.jdbc.core.BatchUpdateUtils;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
|
||||
/**
|
||||
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
|
||||
* within the framework.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class NamedParameterBatchUpdateUtils extends BatchUpdateUtils {
|
||||
|
||||
public static int[] executeBatchUpdateWithNamedParameters(String sql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
|
||||
if (batchArgs.length <= 0) {
|
||||
return new int[] {0};
|
||||
}
|
||||
final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
|
||||
return jdbcOperations.batchUpdate(
|
||||
sqlToUse,
|
||||
new BatchPreparedStatementSetter() {
|
||||
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
|
||||
int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
|
||||
setStatementParameters(values, ps, columnTypes);
|
||||
}
|
||||
|
||||
public int getBatchSize() {
|
||||
return batchArgs.length;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -493,4 +493,20 @@ public interface NamedParameterJdbcOperations {
|
|||
int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
|
||||
throws DataAccessException;
|
||||
|
||||
/**
|
||||
* Executes a batch using the supplied SQL statement with the batch of supplied arguments.
|
||||
* @param sql the SQL statement to execute
|
||||
* @param batchValues the array of Maps containing the batch of arguments for the query
|
||||
* @return an array containing the numbers of rows affected by each update in the batch
|
||||
*/
|
||||
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues);
|
||||
|
||||
/**
|
||||
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
|
||||
* @param sql the SQL statement to execute
|
||||
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
|
||||
* @return an array containing the numbers of rows affected by each update in the batch
|
||||
*/
|
||||
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,6 +261,19 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
|
|||
return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
|
||||
}
|
||||
|
||||
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues) {
|
||||
SqlParameterSource[] batchArgs = new SqlParameterSource[batchValues.length];
|
||||
int i = 0;
|
||||
for (Map<String, Object> values : batchValues) {
|
||||
batchArgs[i] = new MapSqlParameterSource(values);
|
||||
i++;
|
||||
}
|
||||
return batchUpdate(sql, batchArgs);
|
||||
}
|
||||
|
||||
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
|
||||
return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(sql, batchArgs, getJdbcOperations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a PreparedStatementCreator based on the given SQL and named parameters.
|
||||
|
|
|
|||
|
|
@ -16,26 +16,20 @@
|
|||
|
||||
package org.springframework.jdbc.core.simple;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.SqlParameterValue;
|
||||
import org.springframework.jdbc.core.SqlTypeValue;
|
||||
import org.springframework.jdbc.core.StatementCreatorUtils;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.BatchUpdateUtils;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
|
||||
import org.springframework.jdbc.core.namedparam.ParsedSql;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -241,11 +235,11 @@ public class SimpleJdbcTemplate implements SimpleJdbcOperations {
|
|||
}
|
||||
|
||||
public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
|
||||
return doExecuteBatchUpdate(sql, batchArgs, new int[0]);
|
||||
return BatchUpdateUtils.executeBatchUpdate(sql, batchArgs, new int[0], getJdbcOperations());
|
||||
}
|
||||
|
||||
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) {
|
||||
return doExecuteBatchUpdate(sql, batchArgs, argTypes);
|
||||
return batchUpdate(sql, batchArgs, argTypes);
|
||||
}
|
||||
|
||||
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues) {
|
||||
|
|
@ -255,71 +249,11 @@ public class SimpleJdbcTemplate implements SimpleJdbcOperations {
|
|||
batchArgs[i] = new MapSqlParameterSource(values);
|
||||
i++;
|
||||
}
|
||||
return doExecuteBatchUpdateWithNamedParameters(sql, batchArgs);
|
||||
return batchUpdate(sql, batchArgs);
|
||||
}
|
||||
|
||||
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
|
||||
return doExecuteBatchUpdateWithNamedParameters(sql, batchArgs);
|
||||
}
|
||||
|
||||
|
||||
private int[] doExecuteBatchUpdate(String sql, final List<Object[]> batchValues, final int[] columnTypes) {
|
||||
return getJdbcOperations().batchUpdate(
|
||||
sql,
|
||||
new BatchPreparedStatementSetter() {
|
||||
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
Object[] values = batchValues.get(i);
|
||||
doSetStatementParameters(values, ps, columnTypes);
|
||||
}
|
||||
|
||||
public int getBatchSize() {
|
||||
return batchValues.size();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int[] doExecuteBatchUpdateWithNamedParameters(String sql, final SqlParameterSource[] batchArgs) {
|
||||
if (batchArgs.length <= 0) {
|
||||
return new int[] {0};
|
||||
}
|
||||
final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
|
||||
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
|
||||
return getJdbcOperations().batchUpdate(
|
||||
sqlToUse,
|
||||
new BatchPreparedStatementSetter() {
|
||||
|
||||
public void setValues(PreparedStatement ps, int i) throws SQLException {
|
||||
Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
|
||||
int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
|
||||
doSetStatementParameters(values, ps, columnTypes);
|
||||
}
|
||||
|
||||
public int getBatchSize() {
|
||||
return batchArgs.length;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void doSetStatementParameters(Object[] values, PreparedStatement ps, int[] columnTypes) throws SQLException {
|
||||
int colIndex = 0;
|
||||
for (Object value : values) {
|
||||
colIndex++;
|
||||
if (value instanceof SqlParameterValue) {
|
||||
SqlParameterValue paramValue = (SqlParameterValue) value;
|
||||
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
|
||||
}
|
||||
else {
|
||||
int colType;
|
||||
if (columnTypes == null || columnTypes.length < colIndex) {
|
||||
colType = SqlTypeValue.TYPE_UNKNOWN;
|
||||
}
|
||||
else {
|
||||
colType = columnTypes[colIndex - 1];
|
||||
}
|
||||
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
|
||||
}
|
||||
}
|
||||
return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(sql, batchArgs, getJdbcOperations());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,90 @@
|
|||
package org.springframework.jdbc.core;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public abstract class BatchUpdateTestHelper {
|
||||
|
||||
public static void prepareBatchUpdateMocks(String sqlToUse, Object ids,
|
||||
int[] rowsAffected,
|
||||
MockControl ctrlDataSource, DataSource mockDataSource, MockControl ctrlConnection, Connection mockConnection,
|
||||
MockControl ctrlPreparedStatement,
|
||||
PreparedStatement mockPreparedStatement, MockControl ctrlDatabaseMetaData, DatabaseMetaData mockDatabaseMetaData)
|
||||
throws SQLException {
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setDefaultReturnValue(null);
|
||||
mockConnection.close();
|
||||
ctrlConnection.setDefaultVoidCallable();
|
||||
|
||||
mockDataSource.getConnection();
|
||||
ctrlDataSource.setDefaultReturnValue(mockConnection);
|
||||
|
||||
mockPreparedStatement.getConnection();
|
||||
ctrlPreparedStatement.setReturnValue(mockConnection);
|
||||
int idLength = 0;
|
||||
if (ids instanceof SqlParameterSource[]) {
|
||||
idLength = ((SqlParameterSource[])ids).length;
|
||||
}
|
||||
else {
|
||||
idLength = ((List)ids).size();
|
||||
}
|
||||
|
||||
for (int i = 0; i < idLength; i++) {
|
||||
if (ids instanceof SqlParameterSource[]) {
|
||||
mockPreparedStatement.setObject(1, ((SqlParameterSource[])ids)[i].getValue("id"));
|
||||
}
|
||||
else {
|
||||
mockPreparedStatement.setObject(1, ((Object[])((List)ids).get(i))[0]);
|
||||
}
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
}
|
||||
mockPreparedStatement.executeBatch();
|
||||
ctrlPreparedStatement.setReturnValue(rowsAffected);
|
||||
if (LogFactory.getLog(JdbcTemplate.class).isDebugEnabled()) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
mockDatabaseMetaData.getDatabaseProductName();
|
||||
ctrlDatabaseMetaData.setReturnValue("MySQL");
|
||||
mockDatabaseMetaData.supportsBatchUpdates();
|
||||
ctrlDatabaseMetaData.setReturnValue(true);
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
|
||||
}
|
||||
|
||||
public static void replayBatchUpdateMocks(MockControl ctrlDataSource,
|
||||
MockControl ctrlConnection,
|
||||
MockControl ctrlPreparedStatement,
|
||||
MockControl ctrlDatabaseMetaData) {
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlDatabaseMetaData.replay();
|
||||
ctrlDataSource.replay();
|
||||
ctrlConnection.replay();
|
||||
}
|
||||
|
||||
public static void verifyBatchUpdateMocks(MockControl ctrlPreparedStatement, MockControl ctrlDatabaseMetaData) {
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlDatabaseMetaData.verify();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -20,6 +20,8 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
|
@ -40,6 +42,7 @@ import org.springframework.jdbc.core.RowCallbackHandler;
|
|||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.SqlParameterValue;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.BatchUpdateTestHelper;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
|
|
@ -410,4 +413,41 @@ public class NamedParameterJdbcTemplateTests extends AbstractJdbcTests {
|
|||
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
|
||||
}
|
||||
|
||||
public void testBatchUpdateWithSqlParameterSource() throws Exception {
|
||||
|
||||
final String sqlToUse = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
|
||||
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id";
|
||||
final SqlParameterSource[] ids = new SqlParameterSource[2];
|
||||
ids[0] = new MapSqlParameterSource("id", 100);
|
||||
ids[1] = new MapSqlParameterSource("id", 200);
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
|
||||
MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
MockControl ctrlConnection = MockControl.createControl(Connection.class);
|
||||
Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
|
||||
|
||||
BatchUpdateTestHelper.prepareBatchUpdateMocks(sqlToUse, ids, rowsAffected, ctrlDataSource, mockDataSource, ctrlConnection,
|
||||
mockConnection, ctrlPreparedStatement, mockPreparedStatement, ctrlDatabaseMetaData,
|
||||
mockDatabaseMetaData);
|
||||
|
||||
BatchUpdateTestHelper.replayBatchUpdateMocks(ctrlDataSource, ctrlConnection, ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
|
||||
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(template);
|
||||
|
||||
int[] actualRowsAffected = namedParameterJdbcTemplate.batchUpdate(sql, ids);
|
||||
|
||||
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
|
||||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
|
||||
BatchUpdateTestHelper.verifyBatchUpdateMocks(ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.BatchUpdateTestHelper;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
|
|
@ -522,22 +523,6 @@ public class SimpleJdbcTemplateTests extends TestCase {
|
|||
}
|
||||
|
||||
public void testBatchUpdateWithSqlParameterSource() throws Exception {
|
||||
MockControl ctrlDataSource;
|
||||
DataSource mockDataSource;
|
||||
MockControl ctrlConnection;
|
||||
Connection mockConnection;
|
||||
|
||||
ctrlConnection = MockControl.createControl(Connection.class);
|
||||
mockConnection = (Connection) ctrlConnection.getMock();
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setDefaultReturnValue(null);
|
||||
mockConnection.close();
|
||||
ctrlConnection.setDefaultVoidCallable();
|
||||
|
||||
ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
mockDataSource.getConnection();
|
||||
ctrlDataSource.setDefaultReturnValue(mockConnection);
|
||||
|
||||
final String sqlToUse = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
|
||||
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id";
|
||||
|
|
@ -546,44 +531,20 @@ public class SimpleJdbcTemplateTests extends TestCase {
|
|||
ids[1] = new MapSqlParameterSource("id", 200);
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
|
||||
|
||||
MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
MockControl ctrlConnection = MockControl.createControl(Connection.class);
|
||||
Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
mockPreparedStatement.getConnection();
|
||||
ctrlPreparedStatement.setReturnValue(mockConnection);
|
||||
mockPreparedStatement.setObject(1, ((Integer)ids[0].getValue("id")).intValue());
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setObject(1, ((Integer)ids[1].getValue("id")).intValue());
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeBatch();
|
||||
ctrlPreparedStatement.setReturnValue(rowsAffected);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
|
||||
mockDatabaseMetaData.getDatabaseProductName();
|
||||
ctrlDatabaseMetaData.setReturnValue("MySQL");
|
||||
mockDatabaseMetaData.supportsBatchUpdates();
|
||||
ctrlDatabaseMetaData.setReturnValue(true);
|
||||
|
||||
mockConnection.prepareStatement(sqlToUse);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
|
||||
BatchUpdateTestHelper.prepareBatchUpdateMocks(sqlToUse, ids, rowsAffected, ctrlDataSource, mockDataSource, ctrlConnection,
|
||||
mockConnection, ctrlPreparedStatement, mockPreparedStatement, ctrlDatabaseMetaData,
|
||||
mockDatabaseMetaData);
|
||||
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlDatabaseMetaData.replay();
|
||||
ctrlDataSource.replay();
|
||||
ctrlConnection.replay();
|
||||
BatchUpdateTestHelper.replayBatchUpdateMocks(ctrlDataSource, ctrlConnection, ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
|
||||
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(template);
|
||||
|
|
@ -594,27 +555,10 @@ public class SimpleJdbcTemplateTests extends TestCase {
|
|||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlDatabaseMetaData.verify();
|
||||
BatchUpdateTestHelper.verifyBatchUpdateMocks(ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
}
|
||||
|
||||
public void testBatchUpdateWithListOfObjectArrays() throws Exception {
|
||||
MockControl ctrlDataSource;
|
||||
DataSource mockDataSource;
|
||||
MockControl ctrlConnection;
|
||||
Connection mockConnection;
|
||||
|
||||
ctrlConnection = MockControl.createControl(Connection.class);
|
||||
mockConnection = (Connection) ctrlConnection.getMock();
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setDefaultReturnValue(null);
|
||||
mockConnection.close();
|
||||
ctrlConnection.setDefaultVoidCallable();
|
||||
|
||||
ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
mockDataSource.getConnection();
|
||||
ctrlDataSource.setDefaultReturnValue(mockConnection);
|
||||
|
||||
final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
|
||||
final List<Object[]> ids = new ArrayList<Object[]>();
|
||||
|
|
@ -622,44 +566,20 @@ public class SimpleJdbcTemplateTests extends TestCase {
|
|||
ids.add(new Object[] {200});
|
||||
final int[] rowsAffected = new int[] { 1, 2 };
|
||||
|
||||
|
||||
MockControl ctrlDataSource = MockControl.createControl(DataSource.class);
|
||||
DataSource mockDataSource = (DataSource) ctrlDataSource.getMock();
|
||||
MockControl ctrlConnection = MockControl.createControl(Connection.class);
|
||||
Connection mockConnection = (Connection) ctrlConnection.getMock();
|
||||
MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
|
||||
PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
|
||||
mockPreparedStatement.getConnection();
|
||||
ctrlPreparedStatement.setReturnValue(mockConnection);
|
||||
mockPreparedStatement.setObject(1, ((Integer)ids.get(0)[0]).intValue());
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.setObject(1, ((Integer)ids.get(1)[0]).intValue());
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.addBatch();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
mockPreparedStatement.executeBatch();
|
||||
ctrlPreparedStatement.setReturnValue(rowsAffected);
|
||||
if (debugEnabled) {
|
||||
mockPreparedStatement.getWarnings();
|
||||
ctrlPreparedStatement.setReturnValue(null);
|
||||
}
|
||||
mockPreparedStatement.close();
|
||||
ctrlPreparedStatement.setVoidCallable();
|
||||
|
||||
MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData.class);
|
||||
DatabaseMetaData mockDatabaseMetaData = (DatabaseMetaData) ctrlDatabaseMetaData.getMock();
|
||||
mockDatabaseMetaData.getDatabaseProductName();
|
||||
ctrlDatabaseMetaData.setReturnValue("MySQL");
|
||||
mockDatabaseMetaData.supportsBatchUpdates();
|
||||
ctrlDatabaseMetaData.setReturnValue(true);
|
||||
|
||||
mockConnection.prepareStatement(sql);
|
||||
ctrlConnection.setReturnValue(mockPreparedStatement);
|
||||
mockConnection.getMetaData();
|
||||
ctrlConnection.setReturnValue(mockDatabaseMetaData, 2);
|
||||
BatchUpdateTestHelper.prepareBatchUpdateMocks(sql, ids, rowsAffected, ctrlDataSource, mockDataSource, ctrlConnection,
|
||||
mockConnection, ctrlPreparedStatement, mockPreparedStatement, ctrlDatabaseMetaData,
|
||||
mockDatabaseMetaData);
|
||||
|
||||
ctrlPreparedStatement.replay();
|
||||
ctrlDatabaseMetaData.replay();
|
||||
ctrlDataSource.replay();
|
||||
ctrlConnection.replay();
|
||||
BatchUpdateTestHelper.replayBatchUpdateMocks(ctrlDataSource, ctrlConnection, ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
|
||||
JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
|
||||
SimpleJdbcTemplate simpleJdbcTemplate = new SimpleJdbcTemplate(template);
|
||||
|
|
@ -670,8 +590,7 @@ public class SimpleJdbcTemplateTests extends TestCase {
|
|||
assertEquals(rowsAffected[0], actualRowsAffected[0]);
|
||||
assertEquals(rowsAffected[1], actualRowsAffected[1]);
|
||||
|
||||
ctrlPreparedStatement.verify();
|
||||
ctrlDatabaseMetaData.verify();
|
||||
BatchUpdateTestHelper.verifyBatchUpdateMocks(ctrlPreparedStatement, ctrlDatabaseMetaData);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue