Skip to content

Commit 9107aed

Browse files
committed
Refined SqlParameterSource batchUpdate tests (plus related polishing)
See gh-26071
1 parent 4ba1743 commit 9107aed

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
3131

3232
import org.springframework.dao.InvalidDataAccessApiUsageException;
3333
import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor;
34-
import org.springframework.util.Assert;
3534

3635
/**
3736
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
@@ -201,10 +200,9 @@ public PreparedStatementCreatorImpl(List<?> parameters) {
201200

202201
public PreparedStatementCreatorImpl(String actualSql, List<?> parameters) {
203202
this.actualSql = actualSql;
204-
Assert.notNull(parameters, "Parameters List must not be null");
205203
this.parameters = parameters;
206-
if (this.parameters.size() != declaredParameters.size()) {
207-
// account for named parameters being used multiple times
204+
if (parameters.size() != declaredParameters.size()) {
205+
// Account for named parameters being used multiple times
208206
Set<String> names = new HashSet<String>();
209207
for (int i = 0; i < parameters.size(); i++) {
210208
Object param = parameters.get(i);
@@ -278,7 +276,7 @@ public void setValues(PreparedStatement ps) throws SQLException {
278276
Collection<?> entries = (Collection<?>) in;
279277
for (Object entry : entries) {
280278
if (entry instanceof Object[]) {
281-
Object[] valueArray = ((Object[])entry);
279+
Object[] valueArray = ((Object[]) entry);
282280
for (Object argValue : valueArray) {
283281
StatementCreatorUtils.setParameterValue(psToUse, sqlColIndx++, declaredParameter, argValue);
284282
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
* in particular with {@link NamedParameterJdbcTemplate}.
2727
*
2828
* @author Thomas Risberg
29+
* @author Juergen Hoeller
2930
* @since 2.5
3031
*/
3132
public class SqlParameterSourceUtils {
@@ -67,16 +68,12 @@ public static SqlParameterSource[] createBatch(Object[] beans) {
6768
* @param source the source of parameter values and type information
6869
* @param parameterName the name of the parameter
6970
* @return the value object
71+
* @see SqlParameterValue
7072
*/
7173
public static Object getTypedValue(SqlParameterSource source, String parameterName) {
7274
int sqlType = source.getSqlType(parameterName);
7375
if (sqlType != SqlParameterSource.TYPE_UNKNOWN) {
74-
if (source.getTypeName(parameterName) != null) {
75-
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
76-
}
77-
else {
78-
return new SqlParameterValue(sqlType, source.getValue(parameterName));
79-
}
76+
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
8077
}
8178
else {
8279
return source.getValue(parameterName);

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -431,24 +431,27 @@ public void testBatchUpdateWithSqlParameterSource() throws Exception {
431431

432432
@Test
433433
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
434-
SqlParameterSource[] ids = new SqlParameterSource[2];
435-
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
436-
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
437-
final int[] rowsAffected = new int[] {1, 2};
434+
SqlParameterSource[] ids = new SqlParameterSource[3];
435+
ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL);
436+
ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
437+
ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
438+
final int[] rowsAffected = new int[] {1, 2, 3};
438439

439440
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
440441
given(connection.getMetaData()).willReturn(databaseMetaData);
441442
namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false));
442443

443444
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
444445
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
445-
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
446+
assertTrue("executed 3 updates", actualRowsAffected.length == 3);
446447
assertEquals(rowsAffected[0], actualRowsAffected[0]);
447448
assertEquals(rowsAffected[1], actualRowsAffected[1]);
449+
assertEquals(rowsAffected[2], actualRowsAffected[2]);
448450
verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
451+
verify(preparedStatement).setNull(1, Types.NULL);
449452
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
450453
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
451-
verify(preparedStatement, times(2)).addBatch();
454+
verify(preparedStatement, times(3)).addBatch();
452455
verify(preparedStatement, atLeastOnce()).close();
453456
verify(connection, atLeastOnce()).close();
454457
}

0 commit comments

Comments
 (0)