From 157b3aa25fe6911295a18c3db4904b404103a8fb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 13 Dec 2021 14:42:02 +0100 Subject: [PATCH] Allow platform to be configured in DataSourceInitializers Closes gh-28932 --- .../batch/BatchDataSourceInitializer.java | 5 ++ .../autoconfigure/batch/BatchProperties.java | 14 ++++++ .../IntegrationDataSourceInitializer.java | 12 ++++- .../integration/IntegrationProperties.java | 14 ++++++ .../quartz/QuartzDataSourceInitializer.java | 5 ++ .../quartz/QuartzProperties.java | 16 ++++++- .../JdbcSessionDataSourceInitializer.java | 12 ++++- .../session/JdbcSessionProperties.java | 16 ++++++- .../BatchDataSourceInitializerTests.java | 47 +++++++++++++++++++ ...IntegrationDataSourceInitializerTests.java | 47 +++++++++++++++++++ .../QuartzDataSourceInitializerTests.java | 16 ++++++- ...JdbcSessionDataSourceInitializerTests.java | 47 +++++++++++++++++++ 12 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializerTests.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializerTests.java create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/test/kotlin/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializerTests.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java index b642fe2f44..007d825b3c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializer.java @@ -23,6 +23,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer; import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Initialize the Spring Batch schema (ignoring errors, so it should be idempotent). @@ -54,6 +55,10 @@ public class BatchDataSourceInitializer extends AbstractDataSourceInitializer { @Override protected String getDatabaseName() { + String platform = this.jdbcProperties.getPlatform(); + if (StringUtils.hasText(platform)) { + return platform; + } String databaseName = super.getDatabaseName(); if ("oracle".equals(databaseName)) { return "oracle10g"; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java index 15f6e7d739..2cfb4596ba 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchProperties.java @@ -122,6 +122,12 @@ public class BatchProperties { */ private String schema = DEFAULT_SCHEMA_LOCATION; + /** + * Platform to use in initialization scripts if the @@platform@@ placeholder is + * used. Auto-detected by default. + */ + private String platform; + /** * Table prefix for all the batch meta-data tables. */ @@ -140,6 +146,14 @@ public class BatchProperties { this.schema = schema; } + public String getPlatform() { + return this.platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + public String getTablePrefix() { return this.tablePrefix; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializer.java index b11f56bfb8..e3299e848c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -22,6 +22,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer; import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Initializer for Spring Integration schema. @@ -50,4 +51,13 @@ public class IntegrationDataSourceInitializer extends AbstractDataSourceInitiali return this.properties.getSchema(); } + @Override + protected String getDatabaseName() { + String platform = this.properties.getPlatform(); + if (StringUtils.hasText(platform)) { + return platform; + } + return super.getDatabaseName(); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationProperties.java index b5e10e9c20..0ec22ba339 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationProperties.java @@ -196,6 +196,12 @@ public class IntegrationProperties { */ private String schema = DEFAULT_SCHEMA_LOCATION; + /** + * Platform to use in initialization scripts if the @@platform@@ placeholder is + * used. Auto-detected by default. + */ + private String platform; + /** * Database schema initialization mode. */ @@ -209,6 +215,14 @@ public class IntegrationProperties { this.schema = schema; } + public String getPlatform() { + return this.platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + public DataSourceInitializationMode getInitializeSchema() { return this.initializeSchema; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java index aeab5908c4..9475723378 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializer.java @@ -23,6 +23,7 @@ import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Initialize the Quartz Scheduler schema. @@ -58,6 +59,10 @@ public class QuartzDataSourceInitializer extends AbstractDataSourceInitializer { @Override protected String getDatabaseName() { + String platform = this.properties.getJdbc().getPlatform(); + if (StringUtils.hasText(platform)) { + return platform; + } String databaseName = super.getDatabaseName(); if ("db2".equals(databaseName)) { return "db2_v95"; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java index 3509ddbb3f..7c0337e33c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -141,6 +141,12 @@ public class QuartzProperties { */ private String schema = DEFAULT_SCHEMA_LOCATION; + /** + * Platform to use in initialization scripts if the @@platform@@ placeholder is + * used. Auto-detected by default. + */ + private String platform; + /** * Database schema initialization mode. */ @@ -159,6 +165,14 @@ public class QuartzProperties { this.schema = schema; } + public String getPlatform() { + return this.platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + public DataSourceInitializationMode getInitializeSchema() { return this.initializeSchema; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializer.java index 4dd4c9f2d8..ea36a2865f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -22,6 +22,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer; import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.core.io.ResourceLoader; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Initializer for Spring Session schema. @@ -50,4 +51,13 @@ public class JdbcSessionDataSourceInitializer extends AbstractDataSourceInitiali return this.properties.getSchema(); } + @Override + protected String getDatabaseName() { + String platform = this.properties.getPlatform(); + if (StringUtils.hasText(platform)) { + return platform; + } + return super.getDatabaseName(); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java index 4a0dee835a..215b3881ec 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -42,6 +42,12 @@ public class JdbcSessionProperties { */ private String schema = DEFAULT_SCHEMA_LOCATION; + /** + * Platform to use in initialization scripts if the @@platform@@ placeholder is used. + * Auto-detected by default. + */ + private String platform; + /** * Name of the database table used to store sessions. */ @@ -77,6 +83,14 @@ public class JdbcSessionProperties { this.schema = schema; } + public String getPlatform() { + return this.platform; + } + + public void setPlatform(String platform) { + this.platform = platform; + } + public String getTableName() { return this.tableName; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializerTests.java new file mode 100644 index 0000000000..f4b9740b33 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchDataSourceInitializerTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.batch; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.DefaultResourceLoader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * Tests for {@link BatchDataSourceInitializer}. + * + * @author Stephane Nicoll + */ +class BatchDataSourceInitializerTests { + + @Test + void getDatabaseNameWithPlatformDoesNotTouchDataSource() { + DataSource dataSource = mock(DataSource.class); + BatchProperties properties = new BatchProperties(); + properties.getJdbc().setPlatform("test"); + BatchDataSourceInitializer initializer = new BatchDataSourceInitializer(dataSource, new DefaultResourceLoader(), + properties); + assertThat(initializer.getDatabaseName()).isEqualTo("test"); + verifyNoInteractions(dataSource); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializerTests.java new file mode 100644 index 0000000000..e2d35d5382 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationDataSourceInitializerTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.integration; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.DefaultResourceLoader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * Tests for {@link IntegrationDataSourceInitializer}. + * + * @author Stephane Nicoll + */ +class IntegrationDataSourceInitializerTests { + + @Test + void getDatabaseNameWithPlatformDoesNotTouchDataSource() { + DataSource dataSource = mock(DataSource.class); + IntegrationProperties properties = new IntegrationProperties(); + properties.getJdbc().setPlatform("test"); + IntegrationDataSourceInitializer initializer = new IntegrationDataSourceInitializer(dataSource, + new DefaultResourceLoader(), properties); + assertThat(initializer.getDatabaseName()).isEqualTo("test"); + verifyNoInteractions(dataSource); + } + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java index 6a4d980e02..ff52f46905 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzDataSourceInitializerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -30,10 +30,13 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.jdbc.core.JdbcTemplate; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; /** * Tests for {@link QuartzDataSourceInitializer}. @@ -48,6 +51,17 @@ class QuartzDataSourceInitializerTests { .withPropertyValues("spring.datasource.url=" + String.format( "jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", UUID.randomUUID().toString())); + @Test + void getDatabaseNameWithPlatformDoesNotTouchDataSource() { + DataSource dataSource = mock(DataSource.class); + QuartzProperties properties = new QuartzProperties(); + properties.getJdbc().setPlatform("test"); + QuartzDataSourceInitializer initializer = new QuartzDataSourceInitializer(dataSource, + new DefaultResourceLoader(), properties); + assertThat(initializer.getDatabaseName()).isEqualTo("test"); + verifyNoInteractions(dataSource); + } + @Test void hashIsUsedAsACommentPrefixByDefault() { this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues( diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/kotlin/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/kotlin/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializerTests.java new file mode 100644 index 0000000000..cbbb25981f --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/kotlin/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceInitializerTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.session; + +import javax.sql.DataSource; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.DefaultResourceLoader; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verifyNoInteractions; + +/** + * Tests for {@link JdbcSessionDataSourceInitializer}. + * + * @author Stephane Nicoll + */ +class JdbcSessionDataSourceInitializerTests { + + @Test + void getDatabaseNameWithPlatformDoesNotTouchDataSource() { + DataSource dataSource = mock(DataSource.class); + JdbcSessionProperties properties = new JdbcSessionProperties(); + properties.setPlatform("test"); + JdbcSessionDataSourceInitializer initializer = new JdbcSessionDataSourceInitializer(dataSource, + new DefaultResourceLoader(), properties); + assertThat(initializer.getDatabaseName()).isEqualTo("test"); + verifyNoInteractions(dataSource); + } + +}