Skip to content

Commit 1efcff1

Browse files
committed
Refined SqlParameterSource batchUpdate tests (plus related polishing)
See gh-26071
1 parent 562d1df commit 1efcff1

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.lang.Nullable;
34-
import org.springframework.util.Assert;
3534

3635
/**
3736
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
@@ -193,9 +192,8 @@ public PreparedStatementCreatorImpl(List<?> parameters) {
193192

194193
public PreparedStatementCreatorImpl(String actualSql, List<?> parameters) {
195194
this.actualSql = actualSql;
196-
Assert.notNull(parameters, "Parameters List must not be null");
197195
this.parameters = parameters;
198-
if (this.parameters.size() != declaredParameters.size()) {
196+
if (parameters.size() != declaredParameters.size()) {
199197
// Account for named parameters being used multiple times
200198
Set<String> names = new HashSet<>();
201199
for (int i = 0; i < parameters.size(); i++) {

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

Lines changed: 3 additions & 8 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.
@@ -44,7 +44,6 @@ public abstract class SqlParameterSourceUtils {
4444
* @see BeanPropertySqlParameterSource
4545
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[])
4646
*/
47-
@SuppressWarnings("unchecked")
4847
public static SqlParameterSource[] createBatch(Object... candidates) {
4948
return createBatch(Arrays.asList(candidates));
5049
}
@@ -93,17 +92,13 @@ public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
9392
* @param source the source of parameter values and type information
9493
* @param parameterName the name of the parameter
9594
* @return the value object
95+
* @see SqlParameterValue
9696
*/
9797
@Nullable
9898
public static Object getTypedValue(SqlParameterSource source, String parameterName) {
9999
int sqlType = source.getSqlType(parameterName);
100100
if (sqlType != SqlParameterSource.TYPE_UNKNOWN) {
101-
if (source.getTypeName(parameterName) != null) {
102-
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
103-
}
104-
else {
105-
return new SqlParameterValue(sqlType, source.getValue(parameterName));
106-
}
101+
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
107102
}
108103
else {
109104
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.
@@ -470,24 +470,27 @@ public void testBatchUpdateWithSqlParameterSource() throws Exception {
470470

471471
@Test
472472
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
473-
SqlParameterSource[] ids = new SqlParameterSource[2];
474-
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
475-
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
476-
final int[] rowsAffected = new int[] {1, 2};
473+
SqlParameterSource[] ids = new SqlParameterSource[3];
474+
ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL);
475+
ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
476+
ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
477+
final int[] rowsAffected = new int[] {1, 2, 3};
477478

478479
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
479480
given(connection.getMetaData()).willReturn(databaseMetaData);
480481
namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false));
481482

482483
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
483484
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
484-
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
485+
assertTrue("executed 3 updates", actualRowsAffected.length == 3);
485486
assertEquals(rowsAffected[0], actualRowsAffected[0]);
486487
assertEquals(rowsAffected[1], actualRowsAffected[1]);
488+
assertEquals(rowsAffected[2], actualRowsAffected[2]);
487489
verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
490+
verify(preparedStatement).setNull(1, Types.NULL);
488491
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
489492
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
490-
verify(preparedStatement, times(2)).addBatch();
493+
verify(preparedStatement, times(3)).addBatch();
491494
verify(preparedStatement, atLeastOnce()).close();
492495
verify(connection, atLeastOnce()).close();
493496
}

0 commit comments

Comments
 (0)