Skip to content

Commit

Permalink
Add support for multiple init scripts
Browse files Browse the repository at this point in the history
This is in response to issue testcontainers#2232
  • Loading branch information
sfodje committed Apr 15, 2020
1 parent 207d62d commit d0d1154
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE foo (
bar VARCHAR(255)
);

INSERT INTO foo (bar) VALUES ('hello world 2');
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class JdbcDatabaseContainer<SELF extends JdbcDatabaseContainer<S

private static final Object DRIVER_LOAD_MUTEX = new Object();
private Driver driver;
private String initScriptPath;
private String[] initScriptPaths;
protected Map<String, String> parameters = new HashMap<>();

private int startupTimeoutSeconds = 120;
Expand Down Expand Up @@ -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();
}

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

Expand Down

0 comments on commit d0d1154

Please sign in to comment.