From 4018b462f1e0573a321551eda1a12f1fe70e7611 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 22 May 2020 16:35:34 +0200 Subject: [PATCH] JdbcOperations polishing (backported from master) --- .../jdbc/core/JdbcOperations.java | 27 ++++++---- .../NamedParameterJdbcOperations.java | 50 ++++++++++--------- .../jdbc/core/JdbcTemplateQueryTests.java | 34 +++++-------- .../NamedParameterJdbcTemplateTests.java | 13 ++++- 4 files changed, 67 insertions(+), 57 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java index de6df06fc3..67ff03c8d7 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcOperations.java @@ -100,7 +100,7 @@ public interface JdbcOperations { * @param rse a callback that will extract all rows of results * @return an arbitrary result object, as returned by the ResultSetExtractor * @throws DataAccessException if there is any problem executing the query - * @see #query(String, Object[], ResultSetExtractor) + * @see #query(String, ResultSetExtractor, Object...) */ @Nullable T query(String sql, ResultSetExtractor rse) throws DataAccessException; @@ -114,7 +114,7 @@ public interface JdbcOperations { * @param sql the SQL query to execute * @param rch a callback that will extract results, one row at a time * @throws DataAccessException if there is any problem executing the query - * @see #query(String, Object[], RowCallbackHandler) + * @see #query(String, RowCallbackHandler, Object...) */ void query(String sql, RowCallbackHandler rch) throws DataAccessException; @@ -128,7 +128,7 @@ public interface JdbcOperations { * @param rowMapper a callback that will map one object per row * @return the result List, containing mapped objects * @throws DataAccessException if there is any problem executing the query - * @see #query(String, Object[], RowMapper) + * @see #query(String, RowMapper, Object...) */ List query(String sql, RowMapper rowMapper) throws DataAccessException; @@ -146,7 +146,7 @@ public interface JdbcOperations { * @throws IncorrectResultSizeDataAccessException if the query does not * return exactly one row * @throws DataAccessException if there is any problem executing the query - * @see #queryForObject(String, Object[], RowMapper) + * @see #queryForObject(String, RowMapper, Object...) */ @Nullable T queryForObject(String sql, RowMapper rowMapper) throws DataAccessException; @@ -166,7 +166,7 @@ public interface JdbcOperations { * @throws IncorrectResultSizeDataAccessException if the query does not return * exactly one row, or does not return exactly one column in that row * @throws DataAccessException if there is any problem executing the query - * @see #queryForObject(String, Object[], Class) + * @see #queryForObject(String, Class, Object...) */ @Nullable T queryForObject(String sql, Class requiredType) throws DataAccessException; @@ -184,7 +184,7 @@ public interface JdbcOperations { * @throws IncorrectResultSizeDataAccessException if the query does not * return exactly one row * @throws DataAccessException if there is any problem executing the query - * @see #queryForMap(String, Object[]) + * @see #queryForMap(String, Object...) * @see ColumnMapRowMapper */ Map queryForMap(String sql) throws DataAccessException; @@ -201,7 +201,7 @@ public interface JdbcOperations { * (for example, {@code Integer.class}) * @return a List of objects that match the specified element type * @throws DataAccessException if there is any problem executing the query - * @see #queryForList(String, Object[], Class) + * @see #queryForList(String, Class, Object...) * @see SingleColumnRowMapper */ List queryForList(String sql, Class elementType) throws DataAccessException; @@ -218,7 +218,7 @@ public interface JdbcOperations { * @param sql the SQL query to execute * @return an List that contains a Map per row * @throws DataAccessException if there is any problem executing the query - * @see #queryForList(String, Object[]) + * @see #queryForList(String, Object...) */ List> queryForList(String sql) throws DataAccessException; @@ -237,7 +237,7 @@ public interface JdbcOperations { * @return an SqlRowSet representation (possibly a wrapper around a * {@code javax.sql.rowset.CachedRowSet}) * @throws DataAccessException if there is any problem executing the query - * @see #queryForRowSet(String, Object[]) + * @see #queryForRowSet(String, Object...) * @see SqlRowSetResultSetExtractor * @see javax.sql.rowset.CachedRowSet */ @@ -323,7 +323,8 @@ public interface JdbcOperations { * @throws DataAccessException if there is any problem */ @Nullable - T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor rse) throws DataAccessException; + T query(String sql, @Nullable PreparedStatementSetter pss, ResultSetExtractor rse) + throws DataAccessException; /** * Query given SQL to create a prepared statement from SQL and a list of arguments @@ -466,7 +467,8 @@ public interface JdbcOperations { * @return the result List, containing mapped objects * @throws DataAccessException if the query fails */ - List query(String sql, @Nullable PreparedStatementSetter pss, RowMapper rowMapper) throws DataAccessException; + List query(String sql, @Nullable PreparedStatementSetter pss, RowMapper rowMapper) + throws DataAccessException; /** * Query given SQL to create a prepared statement from SQL and a list of @@ -903,6 +905,7 @@ public interface JdbcOperations { * @param sql the SQL statement to execute * @param batchArgs the List of Object arrays containing the batch of arguments for the query * @return an array containing the numbers of rows affected by each update in the batch + * @throws DataAccessException if there is any problem issuing the update */ int[] batchUpdate(String sql, List batchArgs) throws DataAccessException; @@ -913,6 +916,7 @@ public interface JdbcOperations { * @param argTypes the SQL types of the arguments * (constants from {@code java.sql.Types}) * @return an array containing the numbers of rows affected by each update in the batch + * @throws DataAccessException if there is any problem issuing the update */ int[] batchUpdate(String sql, List batchArgs, int[] argTypes) throws DataAccessException; @@ -926,6 +930,7 @@ public interface JdbcOperations { * @param pss the ParameterizedPreparedStatementSetter to use * @return an array containing for each batch another array containing the numbers of rows affected * by each update in the batch + * @throws DataAccessException if there is any problem issuing the update * @since 3.1 */ int[][] batchUpdate(String sql, Collection batchArgs, int batchSize, diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.java index eaf221a294..ad5c627eeb 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterJdbcOperations.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. @@ -130,7 +130,7 @@ public interface NamedParameterJdbcOperations { * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param rse object that will extract results * @return an arbitrary result object, as returned by the ResultSetExtractor - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ @Nullable T query(String sql, Map paramMap, ResultSetExtractor rse) @@ -145,7 +145,7 @@ public interface NamedParameterJdbcOperations { * @param sql the SQL query to execute * @param rse object that will extract results * @return an arbitrary result object, as returned by the ResultSetExtractor - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ @Nullable T query(String sql, ResultSetExtractor rse) throws DataAccessException; @@ -170,7 +170,7 @@ public interface NamedParameterJdbcOperations { * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param rch object that will extract results, one row at a time - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ void query(String sql, Map paramMap, RowCallbackHandler rch) throws DataAccessException; @@ -182,7 +182,7 @@ public interface NamedParameterJdbcOperations { * equivalent to a query call with an empty parameter Map. * @param sql the SQL query to execute * @param rch object that will extract results, one row at a time - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ void query(String sql, RowCallbackHandler rch) throws DataAccessException; @@ -194,7 +194,7 @@ public interface NamedParameterJdbcOperations { * @param paramSource container of arguments to bind to the query * @param rowMapper object that will map one object per row * @return the result List, containing mapped objects - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ List query(String sql, SqlParameterSource paramSource, RowMapper rowMapper) throws DataAccessException; @@ -208,7 +208,7 @@ public interface NamedParameterJdbcOperations { * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @param rowMapper object that will map one object per row * @return the result List, containing mapped objects - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ List query(String sql, Map paramMap, RowMapper rowMapper) throws DataAccessException; @@ -222,7 +222,7 @@ public interface NamedParameterJdbcOperations { * @param sql the SQL query to execute * @param rowMapper object that will map one object per row * @return the result List, containing mapped objects - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ List query(String sql, RowMapper rowMapper) throws DataAccessException; @@ -238,7 +238,7 @@ public interface NamedParameterJdbcOperations { * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row, or does not return exactly * one column in that row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ @Nullable T queryForObject(String sql, SqlParameterSource paramSource, RowMapper rowMapper) @@ -257,7 +257,7 @@ public interface NamedParameterJdbcOperations { * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row, or does not return exactly * one column in that row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails */ @Nullable T queryForObject(String sql, Map paramMap, RowMapper rowMapper) @@ -275,7 +275,7 @@ public interface NamedParameterJdbcOperations { * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row, or does not return exactly * one column in that row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForObject(String, Class) */ @Nullable @@ -295,7 +295,7 @@ public interface NamedParameterJdbcOperations { * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row, or does not return exactly * one column in that row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForObject(String, Class) */ @Nullable @@ -312,7 +312,7 @@ public interface NamedParameterJdbcOperations { * @return the result Map (one entry for each column, using the column name as the key) * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String) * @see org.springframework.jdbc.core.ColumnMapRowMapper */ @@ -332,7 +332,7 @@ public interface NamedParameterJdbcOperations { * @return the result Map (one entry for each column, using the column name as the key) * @throws org.springframework.dao.IncorrectResultSizeDataAccessException * if the query does not return exactly one row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForMap(String) * @see org.springframework.jdbc.core.ColumnMapRowMapper */ @@ -348,7 +348,7 @@ public interface NamedParameterJdbcOperations { * @param elementType the required type of element in the result list * (for example, {@code Integer.class}) * @return a List of objects that match the specified element type - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForList(String, Class) * @see org.springframework.jdbc.core.SingleColumnRowMapper */ @@ -366,7 +366,7 @@ public interface NamedParameterJdbcOperations { * @param elementType the required type of element in the result list * (for example, {@code Integer.class}) * @return a List of objects that match the specified element type - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForList(String, Class) * @see org.springframework.jdbc.core.SingleColumnRowMapper */ @@ -383,7 +383,7 @@ public interface NamedParameterJdbcOperations { * @param sql the SQL query to execute * @param paramSource container of arguments to bind to the query * @return a List that contains a Map per row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForList(String) */ List> queryForList(String sql, SqlParameterSource paramSource) throws DataAccessException; @@ -399,7 +399,7 @@ public interface NamedParameterJdbcOperations { * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @return a List that contains a Map per row - * @throws org.springframework.dao.DataAccessException if the query fails + * @throws DataAccessException if the query fails * @see org.springframework.jdbc.core.JdbcTemplate#queryForList(String) */ List> queryForList(String sql, Map paramMap) throws DataAccessException; @@ -417,7 +417,7 @@ public interface NamedParameterJdbcOperations { * @param paramSource container of arguments to bind to the query * @return an SqlRowSet representation (possibly a wrapper around a * {@code javax.sql.rowset.CachedRowSet}) - * @throws org.springframework.dao.DataAccessException if there is any problem executing the query + * @throws DataAccessException if there is any problem executing the query * @see org.springframework.jdbc.core.JdbcTemplate#queryForRowSet(String) * @see org.springframework.jdbc.core.SqlRowSetResultSetExtractor * @see javax.sql.rowset.CachedRowSet @@ -438,7 +438,7 @@ public interface NamedParameterJdbcOperations { * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @return an SqlRowSet representation (possibly a wrapper around a * {@code javax.sql.rowset.CachedRowSet}) - * @throws org.springframework.dao.DataAccessException if there is any problem executing the query + * @throws DataAccessException if there is any problem executing the query * @see org.springframework.jdbc.core.JdbcTemplate#queryForRowSet(String) * @see org.springframework.jdbc.core.SqlRowSetResultSetExtractor * @see javax.sql.rowset.CachedRowSet @@ -450,7 +450,7 @@ public interface NamedParameterJdbcOperations { * @param sql the SQL containing named parameters * @param paramSource container of arguments and SQL types to bind to the query * @return the number of rows affected - * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update + * @throws DataAccessException if there is any problem issuing the update */ int update(String sql, SqlParameterSource paramSource) throws DataAccessException; @@ -460,7 +460,7 @@ public interface NamedParameterJdbcOperations { * @param paramMap map of parameters to bind to the query * (leaving it to the PreparedStatement to guess the corresponding SQL type) * @return the number of rows affected - * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update + * @throws DataAccessException if there is any problem issuing the update */ int update(String sql, Map paramMap) throws DataAccessException; @@ -471,7 +471,7 @@ public interface NamedParameterJdbcOperations { * @param paramSource container of arguments and SQL types to bind to the query * @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys * @return the number of rows affected - * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update + * @throws DataAccessException if there is any problem issuing the update * @see MapSqlParameterSource * @see org.springframework.jdbc.support.GeneratedKeyHolder */ @@ -486,7 +486,7 @@ public interface NamedParameterJdbcOperations { * @param generatedKeyHolder a {@link KeyHolder} that will hold the generated keys * @param keyColumnNames names of the columns that will have keys generated for them * @return the number of rows affected - * @throws org.springframework.dao.DataAccessException if there is any problem issuing the update + * @throws DataAccessException if there is any problem issuing the update * @see MapSqlParameterSource * @see org.springframework.jdbc.support.GeneratedKeyHolder */ @@ -498,6 +498,7 @@ public interface NamedParameterJdbcOperations { * @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 + * @throws DataAccessException if there is any problem issuing the update */ int[] batchUpdate(String sql, Map[] batchValues); @@ -506,6 +507,7 @@ public interface NamedParameterJdbcOperations { * @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 + * @throws DataAccessException if there is any problem issuing the update */ int[] batchUpdate(String sql, SqlParameterSource[] batchArgs); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java index 76f54dca8f..a1c719b879 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 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. @@ -164,8 +164,7 @@ public class JdbcTemplateQueryTests { return rs.getInt(1); } }); - boolean condition = o instanceof Integer; - assertThat(condition).as("Correct result type").isTrue(); + assertThat(o instanceof Integer).as("Correct result type").isTrue(); verify(this.resultSet).close(); verify(this.statement).close(); } @@ -278,7 +277,7 @@ public class JdbcTemplateQueryTests { private void doTestQueryForListWithArgs(String sql) throws Exception { given(this.resultSet.next()).willReturn(true, true, false); given(this.resultSet.getObject(1)).willReturn(11, 12); - List> li = this.template.queryForList(sql, new Object[] {3}); + List> li = this.template.queryForList(sql, 3); assertThat(li.size()).as("All rows returned").isEqualTo(2); assertThat(((Integer) li.get(0).get("age")).intValue()).as("First row is Integer").isEqualTo(11); assertThat(((Integer) li.get(1).get("age")).intValue()).as("Second row is Integer").isEqualTo(12); @@ -291,7 +290,7 @@ public class JdbcTemplateQueryTests { public void testQueryForListWithArgsAndEmptyResult() throws Exception { String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?"; given(this.resultSet.next()).willReturn(false); - List> li = this.template.queryForList(sql, new Object[] {3}); + List> li = this.template.queryForList(sql, 3); assertThat(li.size()).as("All rows returned").isEqualTo(0); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); @@ -303,7 +302,7 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getObject(1)).willReturn(11); - List> li = this.template.queryForList(sql, new Object[] {3}); + List> li = this.template.queryForList(sql, 3); assertThat(li.size()).as("All rows returned").isEqualTo(1); assertThat(((Integer) li.get(0).get("age")).intValue()).as("First row is Integer").isEqualTo(11); verify(this.preparedStatement).setObject(1, 3); @@ -316,7 +315,7 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(11); - List li = this.template.queryForList(sql, new Object[] {3}, Integer.class); + List li = this.template.queryForList(sql, Integer.class, 3); assertThat(li.size()).as("All rows returned").isEqualTo(1); assertThat(li.get(0).intValue()).as("First row is Integer").isEqualTo(11); verify(this.preparedStatement).setObject(1, 3); @@ -329,7 +328,7 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID < ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getObject(1)).willReturn(11); - Map map = this.template.queryForMap(sql, new Object[] {3}); + Map map = this.template.queryForMap(sql, 3); assertThat(((Integer) map.get("age")).intValue()).as("Row is Integer").isEqualTo(11); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); @@ -341,14 +340,8 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(22); - Object o = this.template.queryForObject(sql, new Object[] {3}, new RowMapper() { - @Override - public Integer mapRow(ResultSet rs, int rowNum) throws SQLException { - return rs.getInt(1); - } - }); - boolean condition = o instanceof Integer; - assertThat(condition).as("Correct result type").isTrue(); + Object o = this.template.queryForObject(sql, (rs, rowNum) -> rs.getInt(1), 3); + assertThat(o instanceof Integer).as("Correct result type").isTrue(); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); verify(this.preparedStatement).close(); @@ -359,9 +352,8 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(22); - Object o = this.template.queryForObject(sql, new Object[] {3}, Integer.class); - boolean condition = o instanceof Integer; - assertThat(condition).as("Correct result type").isTrue(); + Object o = this.template.queryForObject(sql, Integer.class, 3); + assertThat(o instanceof Integer).as("Correct result type").isTrue(); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); verify(this.preparedStatement).close(); @@ -372,7 +364,7 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getInt(1)).willReturn(22); - int i = this.template.queryForObject(sql, new Object[] {3}, Integer.class).intValue(); + int i = this.template.queryForObject(sql, Integer.class, 3).intValue(); assertThat(i).as("Return of an int").isEqualTo(22); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); @@ -384,7 +376,7 @@ public class JdbcTemplateQueryTests { String sql = "SELECT AGE FROM CUSTMR WHERE ID = ?"; given(this.resultSet.next()).willReturn(true, false); given(this.resultSet.getLong(1)).willReturn(87L); - long l = this.template.queryForObject(sql, new Object[] {3}, Long.class).longValue(); + long l = this.template.queryForObject(sql, Long.class, 3).longValue(); assertThat(l).as("Return of a long").isEqualTo(87); verify(this.preparedStatement).setObject(1, 3); verify(this.resultSet).close(); 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 119c6865e3..48a0e52a89 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-2019 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. @@ -237,6 +237,7 @@ public class NamedParameterJdbcTemplateTests { verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED); verify(preparedStatement).setObject(1, 1, Types.DECIMAL); verify(preparedStatement).setString(2, "UK"); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -259,6 +260,7 @@ public class NamedParameterJdbcTemplateTests { assertThat(cust.getId() == 1).as("Customer id was assigned correctly").isTrue(); assertThat(cust.getForename().equals("rod")).as("Customer forename was assigned correctly").isTrue(); verify(connection).prepareStatement(SELECT_NO_PARAMETERS); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -285,6 +287,7 @@ public class NamedParameterJdbcTemplateTests { verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED); verify(preparedStatement).setObject(1, 1, Types.DECIMAL); verify(preparedStatement).setString(2, "UK"); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -307,6 +310,7 @@ public class NamedParameterJdbcTemplateTests { assertThat(customers.get(0).getId() == 1).as("Customer id was assigned correctly").isTrue(); assertThat(customers.get(0).getForename().equals("rod")).as("Customer forename was assigned correctly").isTrue(); verify(connection).prepareStatement(SELECT_NO_PARAMETERS); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -326,12 +330,14 @@ public class NamedParameterJdbcTemplateTests { cust.setForename(rs.getString(COLUMN_NAMES[1])); return cust; }); + assertThat(customers.size()).isEqualTo(1); assertThat(customers.get(0).getId() == 1).as("Customer id was assigned correctly").isTrue(); assertThat(customers.get(0).getForename().equals("rod")).as("Customer forename was assigned correctly").isTrue(); verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED); verify(preparedStatement).setObject(1, 1, Types.DECIMAL); verify(preparedStatement).setString(2, "UK"); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -349,10 +355,12 @@ public class NamedParameterJdbcTemplateTests { cust.setForename(rs.getString(COLUMN_NAMES[1])); return cust; }); + assertThat(customers.size()).isEqualTo(1); assertThat(customers.get(0).getId() == 1).as("Customer id was assigned correctly").isTrue(); assertThat(customers.get(0).getForename().equals("rod")).as("Customer forename was assigned correctly").isTrue(); verify(connection).prepareStatement(SELECT_NO_PARAMETERS); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); } @@ -365,6 +373,7 @@ public class NamedParameterJdbcTemplateTests { params.put("id", new SqlParameterValue(Types.DECIMAL, 1)); params.put("country", "UK"); + Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params, (rs, rownum) -> { Customer cust1 = new Customer(); @@ -372,11 +381,13 @@ public class NamedParameterJdbcTemplateTests { cust1.setForename(rs.getString(COLUMN_NAMES[1])); return cust1; }); + assertThat(cust.getId() == 1).as("Customer id was assigned correctly").isTrue(); assertThat(cust.getForename().equals("rod")).as("Customer forename was assigned correctly").isTrue(); verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED); verify(preparedStatement).setObject(1, 1, Types.DECIMAL); verify(preparedStatement).setString(2, "UK"); + verify(resultSet).close(); verify(preparedStatement).close(); verify(connection).close(); }