Merge branch '2.6.x' into 2.7.x

Closes gh-30795
This commit is contained in:
Andy Wilkinson 2022-04-25 19:31:47 +01:00
commit facbc7b47b
1 changed files with 48 additions and 0 deletions

View File

@ -16,13 +16,29 @@
package org.springframework.boot.autoconfigure.batch; package org.springframework.boot.autoconfigure.batch;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.EnumSource.Mode;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.sql.init.DatabaseInitializationSettings; import org.springframework.boot.sql.init.DatabaseInitializationSettings;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -45,4 +61,36 @@ class BatchDataSourceScriptDatabaseInitializerTests {
then(dataSource).shouldHaveNoInteractions(); then(dataSource).shouldHaveNoInteractions();
} }
@ParameterizedTest
@EnumSource(value = DatabaseDriver.class, mode = Mode.EXCLUDE, names = { "FIREBIRD", "GAE", "HANA", "INFORMIX",
"JTDS", "PHOENIX", "REDSHIFT", "TERADATA", "TESTCONTAINERS", "UNKNOWN" })
void batchSchemaCanBeLocated(DatabaseDriver driver) throws IOException, SQLException {
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
BatchProperties properties = new BatchProperties();
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
given(dataSource.getConnection()).willReturn(connection);
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
given(connection.getMetaData()).willReturn(metadata);
String productName = (String) ReflectionTestUtils.getField(driver, "productName");
given(metadata.getDatabaseProductName()).willReturn(productName);
DatabaseInitializationSettings settings = BatchDataSourceScriptDatabaseInitializer.getSettings(dataSource,
properties.getJdbc());
List<String> schemaLocations = settings.getSchemaLocations();
assertThat(schemaLocations)
.allSatisfy((location) -> assertThat(resourceLoader.getResource(location).exists()).isTrue());
}
@Test
void batchHasExpectedBuiltInSchemas() throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
List<String> schemaNames = Stream
.of(resolver.getResources("classpath:org/springframework/batch/core/schema-*.sql"))
.map((resource) -> resource.getFilename()).filter((resourceName) -> !resourceName.contains("-drop-"))
.collect(Collectors.toList());
assertThat(schemaNames).containsExactlyInAnyOrder("schema-derby.sql", "schema-sqlserver.sql",
"schema-mysql.sql", "schema-sqlite.sql", "schema-postgresql.sql", "schema-oracle10g.sql",
"schema-db2.sql", "schema-sqlf.sql", "schema-hsqldb.sql", "schema-sybase.sql", "schema-h2.sql");
}
} }