From d0d11546f92b476e0e95e30452072442d6f3e899 Mon Sep 17 00:00:00 2001 From: sfodje Date: Wed, 15 Apr 2020 12:20:32 -0700 Subject: [PATCH] Add support for multiple init scripts This is in response to issue testcontainers/testcontainers-java#2232 --- .../org/testcontainers/ext/ScriptUtils.java | 36 ++++++++++--------- .../junit/SimplePostgreSQLTest.java | 24 ++++++++++--- .../resources/somepath/init_postgresql_2.sql | 5 +++ .../containers/JdbcDatabaseContainer.java | 10 +++--- 4 files changed, 49 insertions(+), 26 deletions(-) create mode 100644 modules/jdbc-test/src/test/resources/somepath/init_postgresql_2.sql diff --git a/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java b/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java index d30064af28e..6deb018b46f 100644 --- a/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java +++ b/modules/database-commons/src/main/java/org/testcontainers/ext/ScriptUtils.java @@ -16,18 +16,17 @@ package org.testcontainers.ext; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testcontainers.delegate.DatabaseDelegate; - -import javax.script.ScriptException; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.LinkedList; import java.util.List; +import javax.script.ScriptException; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.delegate.DatabaseDelegate; /** * This is a modified version of the Spring-JDBC ScriptUtils class, adapted to reduce @@ -289,17 +288,22 @@ public static boolean containsSqlScriptDelimiters(String script, String delim) { * Load script from classpath and apply it to the given database * * @param databaseDelegate database delegate for script execution - * @param initScriptPath the resource to load the init script from + * @param initScriptPaths the resource to load the init script from */ - public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) { + public static void runInitScript(DatabaseDelegate databaseDelegate, String... initScriptPaths) { + String initScriptPath = initScriptPaths[0]; try { - URL resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath); - if (resource == null) { - LOGGER.warn("Could not load classpath init script: {}", initScriptPath); - throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath + ". Resource not found."); - } - String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8); - executeDatabaseScript(databaseDelegate, initScriptPath, scripts); + ClassLoader loader = ScriptUtils.class.getClassLoader(); + for (String path : initScriptPaths) { + initScriptPath = path; + URL resource = loader.getResource(path); + if (resource == null) { + LOGGER.warn("Could not load classpath init script: {}", path); + throw new ScriptLoadException("Could not load classpath init script: " + path + ". Resource not found."); + } + String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8); + executeDatabaseScript(databaseDelegate, path, scripts); + } } catch (IOException e) { LOGGER.warn("Could not load classpath init script: {}", initScriptPath); throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e); diff --git a/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimplePostgreSQLTest.java b/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimplePostgreSQLTest.java index 7189e05fb16..499141e53d8 100644 --- a/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimplePostgreSQLTest.java +++ b/modules/jdbc-test/src/test/java/org/testcontainers/junit/SimplePostgreSQLTest.java @@ -1,15 +1,14 @@ package org.testcontainers.junit; -import org.junit.Test; -import org.testcontainers.containers.PostgreSQLContainer; +import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; +import static org.rnorth.visibleassertions.VisibleAssertions.assertNotEquals; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.LogManager; - -import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; -import static org.rnorth.visibleassertions.VisibleAssertions.assertNotEquals; +import org.junit.Test; +import org.testcontainers.containers.PostgreSQLContainer; public class SimplePostgreSQLTest extends AbstractContainerDatabaseTest { @@ -62,4 +61,19 @@ public void testExplicitInitScript() throws SQLException { assertEquals("Value from init script should equal real value", "hello world", firstColumnValue); } } + @Test + public void testMultipleExplicitInitScript() throws SQLException { + try (PostgreSQLContainer postgres = new PostgreSQLContainer<>().withInitScript("somepath/init_postgresql.sql", "somepath/init_postgresql_2.sql")) { + postgres.start(); + + ResultSet resultSet = performQuery(postgres, "SELECT foo AS value FROM bar UNION SELECT bar AS value FROM foo"); + + String columnValue1 = resultSet.getString(1); + resultSet.next(); + String columnValue2 = resultSet.getString(1); + assertEquals("Values from init scripts should equal real values", "hello world", columnValue1); + assertEquals("Value to init script 2 shoudl equal real value", "hello world 2", columnValue2); + } + } + } diff --git a/modules/jdbc-test/src/test/resources/somepath/init_postgresql_2.sql b/modules/jdbc-test/src/test/resources/somepath/init_postgresql_2.sql new file mode 100644 index 00000000000..c7141478056 --- /dev/null +++ b/modules/jdbc-test/src/test/resources/somepath/init_postgresql_2.sql @@ -0,0 +1,5 @@ +CREATE TABLE foo ( + bar VARCHAR(255) +); + +INSERT INTO foo (bar) VALUES ('hello world 2'); diff --git a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java index 36a6461aa74..0564b8aae4d 100644 --- a/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java +++ b/modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java @@ -26,7 +26,7 @@ public abstract class JdbcDatabaseContainer parameters = new HashMap<>(); private int startupTimeoutSeconds = 120; @@ -107,8 +107,8 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) { return self(); } - public SELF withInitScript(String initScriptPath) { - this.initScriptPath = initScriptPath; + public SELF withInitScript(String... initScriptPath) { + this.initScriptPaths = initScriptPath; return self(); } @@ -236,8 +236,8 @@ protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName, * Load init script content and apply it to the database if initScriptPath is set */ protected void runInitScriptIfRequired() { - if (initScriptPath != null) { - ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath); + if (initScriptPaths != null) { + ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPaths); } }