From c0d98fcc7a45004e628a26015c24af5f4eb2e666 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 10 Oct 2023 22:58:13 +0200 Subject: [PATCH] Consistent Object result declarations for ResultQuerySpec Closes gh-31403 --- .../jdbc/core/simple/DefaultJdbcClient.java | 10 +- .../jdbc/core/simple/JdbcClient.java | 9 +- .../JdbcClientIndexedParameterTests.java | 22 +- .../core/simple/JdbcClientQueryTests.java | 216 ++++++++++-------- 4 files changed, 134 insertions(+), 123 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java index 753fac912fbc..b6c90a161f86 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/DefaultJdbcClient.java @@ -285,10 +285,9 @@ public Map singleRow() { return classicOps.queryForMap(sql, indexedParams.toArray()); } - @SuppressWarnings("unchecked") @Override - public List singleColumn() { - return (List) classicOps.queryForList(sql, Object.class, indexedParams.toArray()); + public List singleColumn() { + return classicOps.queryForList(sql, Object.class, indexedParams.toArray()); } } @@ -310,10 +309,9 @@ public Map singleRow() { return namedParamOps.queryForMap(sql, namedParamSource); } - @SuppressWarnings("unchecked") @Override - public List singleColumn() { - return (List) namedParamOps.queryForList(sql, namedParamSource, Object.class); + public List singleColumn() { + return namedParamOps.queryForList(sql, namedParamSource, Object.class); } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java index 57cde5d71d53..e42e67ab114c 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/JdbcClient.java @@ -323,17 +323,16 @@ interface ResultQuerySpec { * Retrieve a single column result, * retaining the order from the original database result. * @return a (potentially empty) list of rows, with each - * row represented as a column value of the given type + * row represented as its single column value */ - List singleColumn(); + List singleColumn(); /** * Retrieve a single value result. - * @return the single row represented as its single - * column value of the given type + * @return the single row represented as its single column value * @see DataAccessUtils#requiredSingleResult(Collection) */ - default T singleValue() { + default Object singleValue() { return DataAccessUtils.requiredSingleResult(singleColumn()); } } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java index f202ddab8259..9789e4bb462f 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIndexedParameterTests.java @@ -95,7 +95,7 @@ public void setup() throws Exception { @Test - public void testQueryWithResultSetExtractor() throws SQLException { + public void queryWithResultSetExtractor() throws SQLException { given(resultSet.next()).willReturn(true); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -122,7 +122,7 @@ public void testQueryWithResultSetExtractor() throws SQLException { } @Test - public void testQueryWithResultSetExtractorNoParameters() throws SQLException { + public void queryWithResultSetExtractorNoParameters() throws SQLException { given(resultSet.next()).willReturn(true); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -145,7 +145,7 @@ public void testQueryWithResultSetExtractorNoParameters() throws SQLException { } @Test - public void testQueryWithRowCallbackHandler() throws SQLException { + public void queryWithRowCallbackHandler() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -172,7 +172,7 @@ public void testQueryWithRowCallbackHandler() throws SQLException { } @Test - public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException { + public void queryWithRowCallbackHandlerNoParameters() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -195,7 +195,7 @@ public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException { } @Test - public void testQueryWithRowMapper() throws SQLException { + public void queryWithRowMapper() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -223,7 +223,7 @@ public void testQueryWithRowMapper() throws SQLException { } @Test - public void testQueryWithRowMapperNoParameters() throws SQLException { + public void queryWithRowMapperNoParameters() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -247,7 +247,7 @@ public void testQueryWithRowMapperNoParameters() throws SQLException { } @Test - public void testQueryForObjectWithRowMapper() throws SQLException { + public void queryForObjectWithRowMapper() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -274,7 +274,7 @@ public void testQueryForObjectWithRowMapper() throws SQLException { } @Test - public void testQueryForStreamWithRowMapper() throws SQLException { + public void queryForStreamWithRowMapper() throws SQLException { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt("id")).willReturn(1); given(resultSet.getString("forename")).willReturn("rod"); @@ -307,7 +307,7 @@ public void testQueryForStreamWithRowMapper() throws SQLException { } @Test - public void testUpdate() throws SQLException { + public void update() throws SQLException { given(preparedStatement.executeUpdate()).willReturn(1); params.add(1); @@ -323,7 +323,7 @@ public void testUpdate() throws SQLException { } @Test - public void testUpdateWithTypedParameters() throws SQLException { + public void updateWithTypedParameters() throws SQLException { given(preparedStatement.executeUpdate()).willReturn(1); params.add(new SqlParameterValue(Types.DECIMAL, 1)); @@ -339,7 +339,7 @@ public void testUpdateWithTypedParameters() throws SQLException { } @Test - public void testUpdateAndGeneratedKeys() throws SQLException { + public void updateAndGeneratedKeys() throws SQLException { given(resultSetMetaData.getColumnCount()).willReturn(1); given(resultSetMetaData.getColumnLabel(1)).willReturn("1"); given(resultSet.getMetaData()).willReturn(resultSetMetaData); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java index 24085826b829..6b913cd6cc00 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientQueryTests.java @@ -64,6 +64,7 @@ public void setup() throws Exception { given(dataSource.getConnection()).willReturn(connection); given(connection.prepareStatement(anyString())).willReturn(preparedStatement); given(preparedStatement.executeQuery()).willReturn(resultSet); + given(resultSet.getMetaData()).willReturn(resultSetMetaData); given(resultSetMetaData.getColumnCount()).willReturn(1); given(resultSetMetaData.getColumnLabel(1)).willReturn("age"); } @@ -72,8 +73,7 @@ public void setup() throws Exception { // Indexed parameters @Test - public void testQueryForListWithIndexedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForListWithIndexedParam() throws Exception { given(resultSet.next()).willReturn(true, true, false); given(resultSet.getObject(1)).willReturn(11, 12); @@ -92,7 +92,7 @@ public void testQueryForListWithIndexedParam() throws Exception { } @Test - public void testQueryForListWithIndexedParamAndEmptyResult() throws Exception { + public void queryForListWithIndexedParamAndEmptyResult() throws Exception { given(resultSet.next()).willReturn(false); List> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?") @@ -107,8 +107,7 @@ public void testQueryForListWithIndexedParamAndEmptyResult() throws Exception { } @Test - public void testQueryForListWithIndexedParamAndSingleRow() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForListWithIndexedParamAndSingleRow() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getObject(1)).willReturn(11); @@ -125,12 +124,28 @@ public void testQueryForListWithIndexedParamAndSingleRow() throws Exception { } @Test - public void testQueryForListWithIndexedParamAndSingleColumn() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForMapWithIndexedParamAndSingleRow() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getObject(1)).willReturn(11); - List li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?") + Map map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?") + .param(1, 3) + .query().singleRow(); + + assertThat(map.get("age")).as("Row is Integer").isEqualTo(11); + verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?"); + verify(preparedStatement).setObject(1, 3); + verify(resultSet).close(); + verify(preparedStatement).close(); + verify(connection).close(); + } + + @Test + public void queryForListWithIndexedParamAndSingleColumn() throws Exception { + given(resultSet.next()).willReturn(true, false); + given(resultSet.getObject(1)).willReturn(11); + + List li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?") .param(1, 3) .query().singleColumn(); @@ -144,17 +159,16 @@ public void testQueryForListWithIndexedParamAndSingleColumn() throws Exception { } @Test - public void testQueryForMapWithIndexedParamAndSingleRow() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithIndexedParamAndSingleValue() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(11); + given(resultSet.getObject(1)).willReturn(22); - Map map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < ?") + Integer value = (Integer) client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?") .param(1, 3) - .query().singleRow(); + .query().singleValue(); - assertThat(map.get("age")).as("Row is Integer").isEqualTo(11); - verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?"); + assertThat(value).isEqualTo(22); + verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); verify(preparedStatement).setObject(1, 3); verify(resultSet).close(); verify(preparedStatement).close(); @@ -162,13 +176,14 @@ public void testQueryForMapWithIndexedParamAndSingleRow() throws Exception { } @Test - public void testQueryForIntegerWithIndexedParamAndRowMapper() throws Exception { + public void queryForIntegerWithIndexedParamAndRowMapper() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(22); Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?") .param(1, 3) - .query((rs, rowNum) -> rs.getInt(1)).single(); + .query((rs, rowNum) -> rs.getInt(1)) + .single(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -179,13 +194,14 @@ public void testQueryForIntegerWithIndexedParamAndRowMapper() throws Exception { } @Test - public void testQueryForOptionalWithIndexedParamAndRowMapper() throws Exception { + public void queryForOptionalWithIndexedParamAndRowMapper() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(22); Optional value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?") .param(1, 3) - .query((rs, rowNum) -> rs.getInt(1)).optional(); + .query((rs, rowNum) -> rs.getInt(1)) + .optional(); assertThat(value.get()).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -196,14 +212,13 @@ public void testQueryForOptionalWithIndexedParamAndRowMapper() throws Exception } @Test - public void testQueryForIntegerWithIndexedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithIndexedParam() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); + given(resultSet.getInt(1)).willReturn(22); Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?") .param(1, 3) - .query().singleValue(); + .query(Integer.class).single(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -214,14 +229,13 @@ public void testQueryForIntegerWithIndexedParam() throws Exception { } @Test - public void testQueryForIntWithIndexedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntWithIndexedParam() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); + given(resultSet.getInt(1)).willReturn(22); int i = client.sql("SELECT AGE FROM CUSTMR WHERE ID = ?") .param(1, 3) - .query().singleValue(); + .query(Integer.class).single(); assertThat(i).as("Return of an int").isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -232,7 +246,7 @@ public void testQueryForIntWithIndexedParam() throws Exception { } @Test - public void testQueryForObjectWithIndexedParamAndList() { + public void queryForObjectWithIndexedParamAndList() { assertThatIllegalArgumentException().isThrownBy(() -> client.sql("SELECT AGE FROM CUSTMR WHERE ID IN (?)").param(Arrays.asList(3, 4)).query().singleValue()); } @@ -241,8 +255,7 @@ public void testQueryForObjectWithIndexedParamAndList() { // Named parameters @Test - public void testQueryForListWithNamedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForListWithNamedParam() throws Exception { given(resultSet.next()).willReturn(true, true, false); given(resultSet.getObject(1)).willReturn(11, 12); @@ -262,7 +275,7 @@ public void testQueryForListWithNamedParam() throws Exception { } @Test - public void testQueryForListWithNamedParamAndEmptyResult() throws Exception { + public void queryForListWithNamedParamAndEmptyResult() throws Exception { given(resultSet.next()).willReturn(false); List> li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id") @@ -278,8 +291,7 @@ public void testQueryForListWithNamedParamAndEmptyResult() throws Exception { } @Test - public void testQueryForListWithNamedParamAndSingleRow() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForListWithNamedParamAndSingleRow() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getObject(1)).willReturn(11); @@ -297,17 +309,15 @@ public void testQueryForListWithNamedParamAndSingleRow() throws Exception { } @Test - public void testQueryForListWithNamedParamAndSingleColumn() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForMapWithNamedParamAndSingleRow() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getObject(1)).willReturn(11); - List li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id") + Map map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id") .param("id", 3) - .query().singleColumn(); + .query().singleRow(); - assertThat(li.size()).as("All rows returned").isEqualTo(1); - assertThat(li.get(0)).as("First row is Integer").isEqualTo(11); + assertThat(map.get("age")).as("Row is Integer").isEqualTo(11); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?"); verify(preparedStatement).setObject(1, 3); verify(resultSet).close(); @@ -316,16 +326,16 @@ public void testQueryForListWithNamedParamAndSingleColumn() throws Exception { } @Test - public void testQueryForMapWithNamedParamAndSingleRow() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForListWithNamedParamAndSingleColumn() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getObject(1)).willReturn(11); - Map map = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id") + List li = client.sql("SELECT AGE FROM CUSTMR WHERE ID < :id") .param("id", 3) - .query().singleRow(); + .query().singleColumn(); - assertThat(map.get("age")).as("Row is Integer").isEqualTo(11); + assertThat(li.size()).as("All rows returned").isEqualTo(1); + assertThat(li.get(0)).as("First row is Integer").isEqualTo(11); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID < ?"); verify(preparedStatement).setObject(1, 3); verify(resultSet).close(); @@ -334,14 +344,13 @@ public void testQueryForMapWithNamedParamAndSingleRow() throws Exception { } @Test - public void testQueryForIntegerWithNamedParamAndRowMapper() throws Exception { + public void queryForIntegerWithNamedParamAndSingleValue() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getInt(1)).willReturn(22); + given(resultSet.getObject(1)).willReturn(22); - Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") + Integer value = (Integer) client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") .param("id", 3) - .query((rs, rowNum) -> rs.getInt(1)) - .single(); + .query().singleValue(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -352,14 +361,13 @@ public void testQueryForIntegerWithNamedParamAndRowMapper() throws Exception { } @Test - public void testQueryForIntegerWithNamedParamAndMappedSimpleValue() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithNamedParamAndRowMapper() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(22); Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") .param("id", 3) - .query(Integer.class) + .query((rs, rowNum) -> rs.getInt(1)) .single(); assertThat(value).isEqualTo(22); @@ -371,18 +379,16 @@ public void testQueryForIntegerWithNamedParamAndMappedSimpleValue() throws Excep } @Test - public void testQueryForMappedRecordWithNamedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); - given(resultSet.findColumn("age")).willReturn(1); + public void queryForOptionalWithNamedParamAndRowMapper() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(22); - AgeRecord value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") + Optional value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") .param("id", 3) - .query(AgeRecord.class) - .single(); + .query((rs, rowNum) -> rs.getInt(1)) + .optional(); - assertThat(value.age()).isEqualTo(22); + assertThat(value.get()).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); verify(preparedStatement).setObject(1, 3); verify(resultSet).close(); @@ -391,33 +397,13 @@ public void testQueryForMappedRecordWithNamedParam() throws Exception { } @Test - public void testQueryForMappedFieldHolderWithNamedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithNamedParam() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getInt(1)).willReturn(22); - AgeFieldHolder value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") - .param("id", 3) - .query(AgeFieldHolder.class) - .single(); - - assertThat(value.age).isEqualTo(22); - verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); - verify(preparedStatement).setObject(1, 3); - verify(resultSet).close(); - verify(preparedStatement).close(); - verify(connection).close(); - } - - @Test - public void testQueryForIntegerWithNamedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); - given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); - Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") .param("id", 3) - .query().singleValue(); + .query(Integer.class).single(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -428,14 +414,13 @@ public void testQueryForIntegerWithNamedParam() throws Exception { } @Test - public void testQueryForIntegerWithNamedParamAndList() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithNamedParamAndList() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); + given(resultSet.getInt(1)).willReturn(22); Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE ID IN (:ids)") .param("ids", Arrays.asList(3, 4)) - .query().singleValue(); + .query(Integer.class).single(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID IN (?, ?)"); @@ -447,17 +432,16 @@ public void testQueryForIntegerWithNamedParamAndList() throws Exception { } @Test - public void testQueryForIntegerWithNamedParamAndListOfExpressionLists() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntegerWithNamedParamAndListOfExpressionLists() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); + given(resultSet.getInt(1)).willReturn(22); List l1 = new ArrayList<>(); l1.add(new Object[] {3, "Rod"}); l1.add(new Object[] {4, "Juergen"}); Integer value = client.sql("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN (:multiExpressionList)") .param("multiExpressionList", l1) - .query().singleValue(); + .query(Integer.class).single(); assertThat(value).isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE (ID, NAME) IN ((?, ?), (?, ?))"); @@ -471,14 +455,13 @@ public void testQueryForIntegerWithNamedParamAndListOfExpressionLists() throws E } @Test - public void testQueryForIntWithNamedParam() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForIntWithNamedParam() throws Exception { given(resultSet.next()).willReturn(true, false); - given(resultSet.getObject(1)).willReturn(22); + given(resultSet.getInt(1)).willReturn(22); int i = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") .param("id", 3) - .query().singleValue(); + .query(Integer.class).single(); assertThat(i).as("Return of an int").isEqualTo(22); verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); @@ -489,8 +472,7 @@ public void testQueryForIntWithNamedParam() throws Exception { } @Test - public void testQueryForLongWithParamBean() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForLongWithParamBean() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); @@ -507,8 +489,7 @@ public void testQueryForLongWithParamBean() throws Exception { } @Test - public void testQueryForLongWithParamBeanWithCollection() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForLongWithParamBeanWithCollection() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); @@ -526,8 +507,7 @@ public void testQueryForLongWithParamBeanWithCollection() throws Exception { } @Test - public void testQueryForLongWithParamRecord() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForLongWithParamRecord() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); @@ -544,8 +524,7 @@ public void testQueryForLongWithParamRecord() throws Exception { } @Test - public void testQueryForLongWithParamFieldHolder() throws Exception { - given(resultSet.getMetaData()).willReturn(resultSetMetaData); + public void queryForLongWithParamFieldHolder() throws Exception { given(resultSet.next()).willReturn(true, false); given(resultSet.getLong(1)).willReturn(87L); @@ -561,6 +540,41 @@ public void testQueryForLongWithParamFieldHolder() throws Exception { verify(connection).close(); } + @Test + public void queryForMappedRecordWithNamedParam() throws Exception { + given(resultSet.findColumn("age")).willReturn(1); + given(resultSet.next()).willReturn(true, false); + given(resultSet.getInt(1)).willReturn(22); + + AgeRecord value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") + .param("id", 3) + .query(AgeRecord.class).single(); + + assertThat(value.age()).isEqualTo(22); + verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); + verify(preparedStatement).setObject(1, 3); + verify(resultSet).close(); + verify(preparedStatement).close(); + verify(connection).close(); + } + + @Test + public void queryForMappedFieldHolderWithNamedParam() throws Exception { + given(resultSet.next()).willReturn(true, false); + given(resultSet.getInt(1)).willReturn(22); + + AgeFieldHolder value = client.sql("SELECT AGE FROM CUSTMR WHERE ID = :id") + .param("id", 3) + .query(AgeFieldHolder.class).single(); + + assertThat(value.age).isEqualTo(22); + verify(connection).prepareStatement("SELECT AGE FROM CUSTMR WHERE ID = ?"); + verify(preparedStatement).setObject(1, 3); + verify(resultSet).close(); + verify(preparedStatement).close(); + verify(connection).close(); + } + static class ParameterBean {