Merge branch '2.5.x'

Closes gh-29002
pull/29089/head
Stephane Nicoll 3 years ago
commit ba60e3d358

@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.batch.BatchProperties.Jdbc;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
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).
@ -65,6 +66,10 @@ public class BatchDataSourceInitializer extends org.springframework.boot.jdbc.Ab
@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";

@ -16,12 +16,15 @@
package org.springframework.boot.autoconfigure.batch;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.util.StringUtils;
/**
* {@link DataSourceScriptDatabaseInitializer} for the Spring Batch database. May be
@ -67,13 +70,20 @@ public class BatchDataSourceScriptDatabaseInitializer extends DataSourceScriptDa
*/
public static DatabaseInitializationSettings getSettings(DataSource dataSource, BatchProperties.Jdbc properties) {
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.ORACLE, "oracle10g");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.MARIADB, "mysql");
settings.setSchemaLocations(platformResolver.resolveAll(dataSource, properties.getSchema()));
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties));
settings.setMode(properties.getInitializeSchema());
settings.setContinueOnError(true);
return settings;
}
private static List<String> resolveSchemaLocations(DataSource dataSource, BatchProperties.Jdbc properties) {
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.ORACLE, "oracle10g");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.MARIADB, "mysql");
if (StringUtils.hasText(properties.getPlatform())) {
return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
}
return platformResolver.resolveAll(dataSource, properties.getSchema());
}
}

@ -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;
}

@ -21,6 +21,7 @@ import javax.sql.DataSource;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Initializer for Spring Integration schema.
@ -61,4 +62,13 @@ public class IntegrationDataSourceInitializer extends org.springframework.boot.j
return this.properties.getSchema();
}
@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}
}

@ -16,11 +16,14 @@
package org.springframework.boot.autoconfigure.integration;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.util.StringUtils;
/**
* {@link DataSourceScriptDatabaseInitializer} for the Spring Integration database. May be
@ -66,11 +69,18 @@ public class IntegrationDataSourceScriptDatabaseInitializer extends DataSourceSc
*/
static DatabaseInitializationSettings getSettings(DataSource dataSource, IntegrationProperties.Jdbc properties) {
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
settings.setSchemaLocations(platformResolver.resolveAll(dataSource, properties.getSchema()));
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties));
settings.setMode(properties.getInitializeSchema());
settings.setContinueOnError(true);
return settings;
}
private static List<String> resolveSchemaLocations(DataSource dataSource, IntegrationProperties.Jdbc properties) {
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
if (StringUtils.hasText(properties.getPlatform())) {
return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
}
return platformResolver.resolveAll(dataSource, properties.getSchema());
}
}

@ -209,6 +209,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.
*/
@ -222,6 +228,14 @@ public class IntegrationProperties {
this.schema = schema;
}
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}

@ -22,6 +22,7 @@ import org.springframework.boot.sql.init.DatabaseInitializationMode;
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.
@ -69,6 +70,10 @@ public class QuartzDataSourceInitializer extends org.springframework.boot.jdbc.A
@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";

@ -16,12 +16,15 @@
package org.springframework.boot.autoconfigure.quartz;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.util.StringUtils;
/**
* {@link DataSourceScriptDatabaseInitializer} for the Quartz Scheduler database. May be
@ -66,16 +69,23 @@ public class QuartzDataSourceScriptDatabaseInitializer extends DataSourceScriptD
*/
public static DatabaseInitializationSettings getSettings(DataSource dataSource, QuartzProperties properties) {
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties.getJdbc()));
settings.setMode(properties.getJdbc().getInitializeSchema());
settings.setContinueOnError(true);
return settings;
}
private static List<String> resolveSchemaLocations(DataSource dataSource, QuartzProperties.Jdbc properties) {
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.DB2, "db2_v95");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.MYSQL, "mysql_innodb");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.MARIADB, "mysql_innodb");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.POSTGRESQL, "postgres");
platformResolver = platformResolver.withDriverPlatform(DatabaseDriver.SQLSERVER, "sqlServer");
settings.setSchemaLocations(platformResolver.resolveAll(dataSource, properties.getJdbc().getSchema()));
settings.setMode(properties.getJdbc().getInitializeSchema());
settings.setContinueOnError(true);
return settings;
if (StringUtils.hasText(properties.getPlatform())) {
return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
}
return platformResolver.resolveAll(dataSource, properties.getSchema());
}
}

@ -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 DatabaseInitializationMode getInitializeSchema() {
return this.initializeSchema;
}

@ -21,6 +21,7 @@ import javax.sql.DataSource;
import org.springframework.boot.sql.init.DatabaseInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Initializer for Spring Session schema.
@ -61,4 +62,13 @@ public class JdbcSessionDataSourceInitializer extends org.springframework.boot.j
return this.properties.getSchema();
}
@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}
}

@ -16,11 +16,14 @@
package org.springframework.boot.autoconfigure.session;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.util.StringUtils;
/**
* {@link DataSourceScriptDatabaseInitializer} for the Spring Session JDBC database. May
@ -67,11 +70,18 @@ public class JdbcSessionDataSourceScriptDatabaseInitializer extends DataSourceSc
*/
static DatabaseInitializationSettings getSettings(DataSource dataSource, JdbcSessionProperties properties) {
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
settings.setSchemaLocations(platformResolver.resolveAll(dataSource, properties.getSchema()));
settings.setSchemaLocations(resolveSchemaLocations(dataSource, properties));
settings.setMode(properties.getInitializeSchema());
settings.setContinueOnError(true);
return settings;
}
private static List<String> resolveSchemaLocations(DataSource dataSource, JdbcSessionProperties properties) {
PlatformPlaceholderDatabaseDriverResolver platformResolver = new PlatformPlaceholderDatabaseDriverResolver();
if (StringUtils.hasText(properties.getPlatform())) {
return platformResolver.resolveAll(properties.getPlatform(), properties.getSchema());
}
return platformResolver.resolveAll(dataSource, properties.getSchema());
}
}

@ -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;
}

@ -0,0 +1,48 @@
/*
* 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
*/
@Deprecated
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);
}
}

@ -0,0 +1,48 @@
/*
* 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.boot.sql.init.DatabaseInitializationSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Tests for {@link BatchDataSourceScriptDatabaseInitializer}.
*
* @author Stephane Nicoll
*/
class BatchDataSourceScriptDatabaseInitializerTests {
@Test
void getSettingsWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
BatchProperties properties = new BatchProperties();
properties.getJdbc().setPlatform("test");
DatabaseInitializationSettings settings = BatchDataSourceScriptDatabaseInitializer.getSettings(dataSource,
properties.getJdbc());
assertThat(settings.getSchemaLocations())
.containsOnly("classpath:org/springframework/batch/core/schema-test.sql");
verifyNoInteractions(dataSource);
}
}

@ -0,0 +1,48 @@
/*
* 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
*/
@Deprecated
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);
}
}

@ -0,0 +1,48 @@
/*
* 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.boot.sql.init.DatabaseInitializationSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Tests for {@link IntegrationDataSourceScriptDatabaseInitializer}.
*
* @author Stephane Nicoll
*/
class IntegrationDataSourceScriptDatabaseInitializerTests {
@Test
void getSettingsWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
IntegrationProperties properties = new IntegrationProperties();
properties.getJdbc().setPlatform("test");
DatabaseInitializationSettings settings = IntegrationDataSourceScriptDatabaseInitializer.getSettings(dataSource,
properties.getJdbc());
assertThat(settings.getSchemaLocations())
.containsOnly("classpath:org/springframework/integration/jdbc/schema-test.sql");
verifyNoInteractions(dataSource);
}
}

@ -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}.
@ -49,6 +52,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(

@ -0,0 +1,48 @@
/*
* 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.quartz;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Tests for {@link QuartzDataSourceScriptDatabaseInitializer}.
*
* @author Stephane Nicoll
*/
class QuartzDataSourceScriptDatabaseInitializerTests {
@Test
void getSettingsWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
QuartzProperties properties = new QuartzProperties();
properties.getJdbc().setPlatform("test");
DatabaseInitializationSettings settings = QuartzDataSourceScriptDatabaseInitializer.getSettings(dataSource,
properties);
assertThat(settings.getSchemaLocations())
.containsOnly("classpath:org/quartz/impl/jdbcjobstore/tables_test.sql");
verifyNoInteractions(dataSource);
}
}

@ -0,0 +1,48 @@
/*
* 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.boot.sql.init.DatabaseInitializationSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
/**
* Tests for {@link JdbcSessionDataSourceScriptDatabaseInitializer}.
*
* @author Stephane Nicoll
*/
class JdbcSessionDataSourceScriptDatabaseInitializerTests {
@Test
void getSettingsWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
JdbcSessionProperties properties = new JdbcSessionProperties();
properties.setPlatform("test");
DatabaseInitializationSettings settings = JdbcSessionDataSourceScriptDatabaseInitializer.getSettings(dataSource,
properties);
assertThat(settings.getSchemaLocations())
.containsOnly("classpath:org/springframework/session/jdbc/schema-test.sql");
verifyNoInteractions(dataSource);
}
}

@ -0,0 +1,48 @@
/*
* 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
*/
@Deprecated
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);
}
}

@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import javax.sql.DataSource;
@ -92,6 +93,23 @@ public class PlatformPlaceholderDatabaseDriverResolver {
*/
public List<String> resolveAll(DataSource dataSource, String... values) {
Assert.notNull(dataSource, "DataSource must not be null");
return resolveAll(() -> determinePlatform(dataSource), values);
}
/**
* Resolves the placeholders in the given {@code values}, replacing them with the
* given platform.
* @param platform the platform to use
* @param values the values in which placeholders are resolved
* @return the values with their placeholders resolved
* @since 2.6.2
*/
public List<String> resolveAll(String platform, String... values) {
Assert.notNull(platform, "Platform must not be null");
return resolveAll(() -> platform, values);
}
private List<String> resolveAll(Supplier<String> platformProvider, String... values) {
if (ObjectUtils.isEmpty(values)) {
return Collections.emptyList();
}
@ -100,7 +118,7 @@ public class PlatformPlaceholderDatabaseDriverResolver {
for (String value : values) {
if (StringUtils.hasLength(value)) {
if (value.contains(this.placeholder)) {
platform = (platform != null) ? platform : determinePlatform(dataSource);
platform = (platform != null) ? platform : platformProvider.get();
value = value.replace(this.placeholder, platform);
}
}

@ -35,29 +35,50 @@ import static org.mockito.Mockito.mock;
* Tests for {@link PlatformPlaceholderDatabaseDriverResolver}
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
class PlatformPlaceholderDatabaseDriverResolverTests {
@Test
void resolveAllWhenThereAreNoValuesShouldReturnEmptyList() {
void resolveAllWithPlatformWhenThereAreNoValuesShouldReturnEmptyList() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll("test")).isEmpty();
}
@Test
void resolveAllWithPlatformWhenValueDoesNotContainPlaceholderShouldReturnValueUnchanged() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll("test", "schema.sql"))
.containsExactly("schema.sql");
}
@Test
void resolveAllWithPlatformWhenValuesContainPlaceholdersShouldReturnValuesWithPlaceholdersReplaced() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll("postgresql", "schema.sql",
"schema-@@platform@@.sql", "data-@@platform@@.sql")).containsExactly("schema.sql",
"schema-postgresql.sql", "data-postgresql.sql");
}
@Test
void resolveAllWithDataSourceWhenThereAreNoValuesShouldReturnEmptyList() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll(mock(DataSource.class))).isEmpty();
}
@Test
void resolveAllWhenValueDoesNotContainPlaceholderShouldReturnValueUnchanged() {
void resolveAllWithDataSourceWhenValueDoesNotContainPlaceholderShouldReturnValueUnchanged() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll(mock(DataSource.class), "schema.sql"))
.containsExactly("schema.sql");
}
@Test
void resolveAllWhenValuesContainPlaceholdersShouldReturnValuesWithPlaceholdersReplaced() throws SQLException {
void resolveAllWithDataSourceWhenValuesContainPlaceholdersShouldReturnValuesWithPlaceholdersReplaced()
throws SQLException {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll(dataSourceWithProductName("PostgreSQL"),
"schema.sql", "schema-@@platform@@.sql", "data-@@platform@@.sql")).containsExactly("schema.sql",
"schema-postgresql.sql", "data-postgresql.sql");
}
@Test
void resolveAllWhenDriverMappingsAreCustomizedShouldResolvePlaceholderUsingCustomMapping() throws SQLException {
void resolveAllWithDataSourceWhenDriverMappingsAreCustomizedShouldResolvePlaceholderUsingCustomMapping()
throws SQLException {
assertThat(new PlatformPlaceholderDatabaseDriverResolver()
.withDriverPlatform(DatabaseDriver.POSTGRESQL, "postgres")
.resolveAll(dataSourceWithProductName("PostgreSQL"), "schema-@@platform@@.sql"))
@ -65,19 +86,19 @@ class PlatformPlaceholderDatabaseDriverResolverTests {
}
@Test
void resolveAllWhenValueIsAnEmptyStringShouldReturnValueUnchanged() {
void resolveAllWithDataSourceWhenValueIsAnEmptyStringShouldReturnValueUnchanged() {
assertThat(new PlatformPlaceholderDatabaseDriverResolver().resolveAll(mock(DataSource.class), ""))
.containsExactly("");
}
@Test
void resolveAllWhenDriverIsUnknownShouldThrow() {
void resolveAllWithDataSourceWhenDriverIsUnknownShouldThrow() {
assertThatIllegalStateException().isThrownBy(() -> new PlatformPlaceholderDatabaseDriverResolver()
.resolveAll(dataSourceWithProductName("CustomDB"), "schema-@@platform@@.sql"));
}
@Test
void resolveAllWhenPlaceholderIsCustomizedShouldResolvePlaceholders() throws SQLException {
void resolveAllWithDataSourceWhenPlaceholderIsCustomizedShouldResolvePlaceholders() throws SQLException {
assertThat(new PlatformPlaceholderDatabaseDriverResolver("##platform##").resolveAll(
dataSourceWithProductName("PostgreSQL"), "schema-##platform##.sql", "schema-@@platform@@.sql"))
.containsExactly("schema-postgresql.sql", "schema-@@platform@@.sql");

Loading…
Cancel
Save