From 734d113b11369dd2ca0b7cba09197374dba257f2 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 6 Sep 2023 11:27:30 +0200 Subject: [PATCH] Ensure configuration methods in SimpleJdbcInsert support method chaining Prior to this commit, the withoutTableColumnMetaDataAccess() and includeSynonymsForTableColumnMetaData() methods in SimpleJdbcInsert incorrectly declared a SimpleJdbcInsertOperations return type, which prevented method chaining when the instance was declared to be of type SimpleJdbcInsert. This commit changes the return types of those methods to SimpleJdbcInsert to benefit from covariant return types like the rest of the configuration methods in SimpleJdbcInsert. Closes gh-31177 --- .../jdbc/core/simple/SimpleJdbcInsert.java | 4 ++-- .../core/simple/SimpleJdbcInsertTests.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcInsert.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcInsert.java index 0284639bbaa..eb4413af450 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcInsert.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/SimpleJdbcInsert.java @@ -109,13 +109,13 @@ public class SimpleJdbcInsert extends AbstractJdbcInsert implements SimpleJdbcIn } @Override - public SimpleJdbcInsertOperations withoutTableColumnMetaDataAccess() { + public SimpleJdbcInsert withoutTableColumnMetaDataAccess() { setAccessTableColumnMetaData(false); return this; } @Override - public SimpleJdbcInsertOperations includeSynonymsForTableColumnMetaData() { + public SimpleJdbcInsert includeSynonymsForTableColumnMetaData() { setOverrideIncludeSynonymsDefault(true); return this; } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java index ae35ec172e1..94695745579 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/SimpleJdbcInsertTests.java @@ -64,6 +64,28 @@ class SimpleJdbcInsertTests { } + /** + * This method does not test any functionality but rather only that + * configuration methods can be chained without compiler errors. + */ + @Test // gh-31177 + void methodChaining() throws Exception { + SimpleJdbcInsert insert = new SimpleJdbcInsert(dataSource) + .withCatalogName("my_catalog") + .withSchemaName("my_schema") + .withTableName("my_table") + .usingColumns("col1", "col2") + .usingGeneratedKeyColumns("id") + .usingQuotedIdentifiers() + .withoutTableColumnMetaDataAccess() + .includeSynonymsForTableColumnMetaData(); + + assertThat(insert).isNotNull(); + + // Satisfy the @AfterEach mock verification. + connection.close(); + } + @Test void noSuchTable() throws Exception { ResultSet resultSet = mock();