From e2811ace726a01d522c0ae0217f4ddfa424f0e24 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 24 Mar 2021 12:33:56 +0000 Subject: [PATCH] Improve testing of optional schema and data scripts See gh-25487 --- .../ScriptDataSourceInitializerTests.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/ScriptDataSourceInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/ScriptDataSourceInitializerTests.java index 368d92cc8c..f235b85abe 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/ScriptDataSourceInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/init/ScriptDataSourceInitializerTests.java @@ -29,6 +29,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; /** * Tests for {@link ScriptDataSourceInitializer}. @@ -72,6 +73,40 @@ class ScriptDataSourceInitializerTests { assertThat(initializer.initializeDatabase()).isTrue(); } + @Test + void whenNoScriptsExistAtASchemaLocationThenInitializationFails() { + DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); + settings.setSchemaLocations(Arrays.asList("does-not-exist.sql")); + ScriptDataSourceInitializer initializer = createInitializer(settings); + assertThatIllegalStateException().isThrownBy(initializer::initializeDatabase) + .withMessage("No schema scripts found at location 'does-not-exist.sql'"); + } + + @Test + void whenNoScriptsExistAtADataLocationThenInitializationFails() { + DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); + settings.setDataLocations(Arrays.asList("does-not-exist.sql")); + ScriptDataSourceInitializer initializer = createInitializer(settings); + assertThatIllegalStateException().isThrownBy(initializer::initializeDatabase) + .withMessage("No data scripts found at location 'does-not-exist.sql'"); + } + + @Test + void whenNoScriptsExistAtAnOptionalSchemaLocationThenInitializationSucceeds() { + DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); + settings.setSchemaLocations(Arrays.asList("optional:does-not-exist.sql")); + ScriptDataSourceInitializer initializer = createInitializer(settings); + assertThat(initializer.initializeDatabase()).isFalse(); + } + + @Test + void whenNoScriptsExistAtAnOptionalDataLocationThenInitializationSucceeds() { + DataSourceInitializationSettings settings = new DataSourceInitializationSettings(); + settings.setDataLocations(Arrays.asList("optional:does-not-exist.sql")); + ScriptDataSourceInitializer initializer = createInitializer(settings); + assertThat(initializer.initializeDatabase()).isFalse(); + } + private ScriptDataSourceInitializer createInitializer(DataSourceInitializationSettings settings) { return new ScriptDataSourceInitializer(this.dataSource, settings); }