Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@

package org.testcontainers.ext;

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;

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 static org.apache.commons.lang.ArrayUtils.isEmpty;

/**
* This is a modified version of the Spring-JDBC ScriptUtils class, adapted to reduce
Expand Down Expand Up @@ -285,27 +286,47 @@ public static boolean containsSqlScriptDelimiters(String script, String delim) {
return false;
}

/**
* 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
*/
public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) {
runMultiInitScript(databaseDelegate, initScriptPath);
}

/**
* Load script from classpath and apply it to the given database
* Loads multiple scripts from classpath and applies 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 resources to load the init scripts from
*/
public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) {
public static void runMultiInitScript(DatabaseDelegate databaseDelegate, String... initScriptPaths) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need such big changes to ScriptUtils for this? It looks like this other PR has a simpler approach, and I can't see why that wouldn't work...

if (isEmpty(initScriptPaths)) {
LOGGER.warn("No initScriptPath provided. Skipping...");
return;
}

String initScriptPathCursor = "";
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) {
initScriptPathCursor = 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);
LOGGER.warn("Could not load classpath init script: {}", initScriptPathCursor);
throw new ScriptLoadException("Could not load classpath init script: " + initScriptPathCursor, e);
} catch (ScriptException e) {
LOGGER.error("Error while executing init script: {}", initScriptPath, e);
throw new UncategorizedScriptException("Error while executing init script: " + initScriptPath, e);
LOGGER.error("Error while executing init script: {}", initScriptPathCursor, e);
throw new UncategorizedScriptException("Error while executing init script: " + initScriptPathCursor, 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<>().withMultiInitScript("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 @@ -108,7 +108,11 @@ public SELF withConnectTimeoutSeconds(int connectTimeoutSeconds) {
}

public SELF withInitScript(String initScriptPath) {
this.initScriptPath = initScriptPath;
return withMultiInitScript(initScriptPath);
}

public SELF withMultiInitScript(String... initScriptPaths) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought but... If the parameters were String, String... couldn't we keep the original method name and avoid problems with overloading/binary incompatibility? 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought, but assumed the purpose was to keep the API visually and functionally the same. Now that you raise it up, it is a good question. The previous iteration was a lot simpler and less complicated.

this.initScriptPaths = initScriptPaths;
return self();
}

Expand Down Expand Up @@ -236,8 +240,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.runMultiInitScript(getDatabaseDelegate(), initScriptPaths);
}
}

Expand Down