From 9107aed878b588ceb097dda19bd3e11e3e0b2344 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 12 Nov 2020 14:43:11 +0100 Subject: [PATCH] Refined SqlParameterSource batchUpdate tests (plus related polishing) See gh-26071 --- .../core/PreparedStatementCreatorFactory.java | 10 ++++------ .../namedparam/SqlParameterSourceUtils.java | 11 ++++------- .../NamedParameterJdbcTemplateTests.java | 17 ++++++++++------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java index c4ad4ff31297..5c11d7cd2f07 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -31,7 +31,6 @@ import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor; -import org.springframework.util.Assert; /** * Helper class that efficiently creates multiple {@link PreparedStatementCreator} @@ -201,10 +200,9 @@ public PreparedStatementCreatorImpl(List parameters) { public PreparedStatementCreatorImpl(String actualSql, List parameters) { this.actualSql = actualSql; - Assert.notNull(parameters, "Parameters List must not be null"); this.parameters = parameters; - if (this.parameters.size() != declaredParameters.size()) { - // account for named parameters being used multiple times + if (parameters.size() != declaredParameters.size()) { + // Account for named parameters being used multiple times Set names = new HashSet(); for (int i = 0; i < parameters.size(); i++) { Object param = parameters.get(i); @@ -278,7 +276,7 @@ public void setValues(PreparedStatement ps) throws SQLException { Collection entries = (Collection) in; for (Object entry : entries) { if (entry instanceof Object[]) { - Object[] valueArray = ((Object[])entry); + Object[] valueArray = ((Object[]) entry); for (Object argValue : valueArray) { StatementCreatorUtils.setParameterValue(psToUse, sqlColIndx++, declaredParameter, argValue); } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java index 66eb1b1c37b5..201ace2fbce9 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/SqlParameterSourceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -26,6 +26,7 @@ * in particular with {@link NamedParameterJdbcTemplate}. * * @author Thomas Risberg + * @author Juergen Hoeller * @since 2.5 */ public class SqlParameterSourceUtils { @@ -67,16 +68,12 @@ public static SqlParameterSource[] createBatch(Object[] beans) { * @param source the source of parameter values and type information * @param parameterName the name of the parameter * @return the value object + * @see SqlParameterValue */ public static Object getTypedValue(SqlParameterSource source, String parameterName) { int sqlType = source.getSqlType(parameterName); if (sqlType != SqlParameterSource.TYPE_UNKNOWN) { - if (source.getTypeName(parameterName) != null) { - return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName)); - } - else { - return new SqlParameterValue(sqlType, source.getValue(parameterName)); - } + return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName)); } else { return source.getValue(parameterName); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java index 446ba98e4e89..dd976b98eb53 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -431,10 +431,11 @@ public void testBatchUpdateWithSqlParameterSource() throws Exception { @Test public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception { - SqlParameterSource[] ids = new SqlParameterSource[2]; - ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC); - ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC); - final int[] rowsAffected = new int[] {1, 2}; + SqlParameterSource[] ids = new SqlParameterSource[3]; + ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL); + ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC); + ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC); + final int[] rowsAffected = new int[] {1, 2, 3}; given(preparedStatement.executeBatch()).willReturn(rowsAffected); given(connection.getMetaData()).willReturn(databaseMetaData); @@ -442,13 +443,15 @@ public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception int[] actualRowsAffected = namedParameterTemplate.batchUpdate( "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids); - assertTrue("executed 2 updates", actualRowsAffected.length == 2); + assertTrue("executed 3 updates", actualRowsAffected.length == 3); assertEquals(rowsAffected[0], actualRowsAffected[0]); assertEquals(rowsAffected[1], actualRowsAffected[1]); + assertEquals(rowsAffected[2], actualRowsAffected[2]); verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?"); + verify(preparedStatement).setNull(1, Types.NULL); verify(preparedStatement).setObject(1, 100, Types.NUMERIC); verify(preparedStatement).setObject(1, 200, Types.NUMERIC); - verify(preparedStatement, times(2)).addBatch(); + verify(preparedStatement, times(3)).addBatch(); verify(preparedStatement, atLeastOnce()).close(); verify(connection, atLeastOnce()).close(); }