From 608be54a58d4f4b94a23d37033877dfd0ce7b295 Mon Sep 17 00:00:00 2001 From: Jiayi Li <17785923961@163.com> Date: Tue, 17 May 2022 19:49:28 -0700 Subject: [PATCH 1/2] Improve test coverage of RdbmsOperation See gh-28472 --- .../jdbc/object/RdbmsOperationTests.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java index c91d7bd789..7f4da0666f 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java @@ -31,6 +31,7 @@ import org.springframework.jdbc.core.SqlOutParameter; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.datasource.DriverManagerDataSource; +import static java.util.Map.entry; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -50,6 +51,14 @@ public class RdbmsOperationTests { operation::compile); } + @Test + public void getSql() { + String sql = "select * from mytable"; + operation.setDataSource(new DriverManagerDataSource()); + operation.setSql(sql); + String strGotten = operation.getSql(); + assertThat(strGotten.equals(sql)); + } @Test public void setTypeAfterCompile() { operation.setDataSource(new DriverManagerDataSource()); @@ -98,6 +107,15 @@ public class RdbmsOperationTests { assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> operation.validateParameters(new Object[] { 1, 2 })); } + @Test + public void tooManyMapParameters() { + operation.setSql("select * from mytable"); + assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> + operation.validateNamedParameters(Map.ofEntries( + entry("a", "b"), + entry("c", "d") + ) )); + } @Test public void unspecifiedMapParameters() { From f9ee8a93ad06259fe6ef3b7ce54f6dbf2315a7df Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 29 Jul 2022 17:32:26 +0200 Subject: [PATCH 2/2] Polish "Improve test coverage of RdbmsOperation" See gh-28472 --- .../jdbc/object/RdbmsOperationTests.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java index 7f4da0666f..aff13d43a7 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/object/RdbmsOperationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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.jdbc.core.SqlOutParameter; import org.springframework.jdbc.core.SqlParameter; import org.springframework.jdbc.datasource.DriverManagerDataSource; -import static java.util.Map.entry; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -51,14 +50,6 @@ public class RdbmsOperationTests { operation::compile); } - @Test - public void getSql() { - String sql = "select * from mytable"; - operation.setDataSource(new DriverManagerDataSource()); - operation.setSql(sql); - String strGotten = operation.getSql(); - assertThat(strGotten.equals(sql)); - } @Test public void setTypeAfterCompile() { operation.setDataSource(new DriverManagerDataSource()); @@ -87,6 +78,7 @@ public class RdbmsOperationTests { @Test public void tooFewMapParameters() { + operation.setDataSource(new DriverManagerDataSource()); operation.setSql("select * from mytable"); operation.setTypes(new int[] { Types.INTEGER }); assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> @@ -94,31 +86,31 @@ public class RdbmsOperationTests { } @Test - public void operationConfiguredViaJdbcTemplateMustGetDataSource() throws Exception { + public void operationConfiguredViaJdbcTemplateMustGetDataSource() { operation.setSql("foo"); assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> operation.compile()) - .withMessageContaining("ataSource"); + .withMessageContaining("'dataSource'"); } @Test public void tooManyParameters() { + operation.setDataSource(new DriverManagerDataSource()); operation.setSql("select * from mytable"); assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> operation.validateParameters(new Object[] { 1, 2 })); } @Test public void tooManyMapParameters() { + operation.setDataSource(new DriverManagerDataSource()); operation.setSql("select * from mytable"); assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> - operation.validateNamedParameters(Map.ofEntries( - entry("a", "b"), - entry("c", "d") - ) )); + operation.validateNamedParameters(Map.of("a", "b", "c", "d"))); } @Test public void unspecifiedMapParameters() { + operation.setDataSource(new DriverManagerDataSource()); operation.setSql("select * from mytable"); Map params = new HashMap<>(); params.put("col1", "value");