Merge branch '2.6.x' into 2.7.x

Closes gh-29016
This commit is contained in:
Stephane Nicoll
2021-12-14 15:32:13 +01:00
22 changed files with 535 additions and 20 deletions

View File

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

View File

@@ -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");