diff --git a/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java b/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java index c687c63eaa3..73a0c83443e 100644 --- a/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java +++ b/modules/cassandra/src/main/java/org/testcontainers/cassandra/CassandraContainer.java @@ -9,10 +9,7 @@ import org.testcontainers.utility.DockerImageName; import org.testcontainers.utility.MountableFile; -import java.io.File; import java.net.InetSocketAddress; -import java.net.URISyntaxException; -import java.net.URL; import java.util.Optional; /** @@ -30,6 +27,8 @@ public class CassandraContainer extends GenericContainer { private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1"; + private static final String DEFAULT_INIT_SCRIPT_FILENAME = "init.cql"; + private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra"; private static final String USERNAME = "cassandra"; @@ -80,26 +79,25 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) { * Load init script content and apply it to the database if initScriptPath is set */ private void runInitScriptIfRequired() { - if (initScriptPath != null) { + if (this.initScriptPath != null) { try { - URL resource = Thread.currentThread().getContextClassLoader().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." - ); - } - // The init script is executed as is by the cqlsh command, so copy it into the container. - String targetInitScriptName = new File(resource.toURI()).getName(); - copyFileToContainer(MountableFile.forClasspathResource(initScriptPath), targetInitScriptName); - new CassandraDatabaseDelegate(this).execute(null, targetInitScriptName, -1, false, false); - } catch (URISyntaxException e) { - logger().warn("Could not copy init script into container: {}", initScriptPath); - throw new ScriptLoadException("Could not copy init script into container: " + initScriptPath, e); + final MountableFile originalInitScript = MountableFile.forClasspathResource(this.initScriptPath); + // The init script is executed as is by the cqlsh command, so copy it into the container. The name + // of the script is generic since it's not important to keep the original name. + copyFileToContainer(originalInitScript, DEFAULT_INIT_SCRIPT_FILENAME); + new CassandraDatabaseDelegate(this).execute(null, DEFAULT_INIT_SCRIPT_FILENAME, -1, false, false); + } catch (IllegalArgumentException e) { + // MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot + // be found. + logger().warn("Could not load classpath init script: {}", this.initScriptPath); + throw new ScriptLoadException( + "Could not load classpath init script: " + this.initScriptPath + ". Resource not found.", + e + ); } catch (ScriptUtils.ScriptStatementFailedException e) { - logger().error("Error while executing init script: {}", initScriptPath, e); + logger().error("Error while executing init script: {}", this.initScriptPath, e); throw new ScriptUtils.UncategorizedScriptException( - "Error while executing init script: " + initScriptPath, + "Error while executing init script: " + this.initScriptPath, e ); } diff --git a/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java b/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java index 632966d1bb3..d156a28ef0c 100644 --- a/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java +++ b/modules/cassandra/src/test/java/org/testcontainers/cassandra/CassandraContainerTest.java @@ -8,6 +8,7 @@ import org.testcontainers.utility.DockerImageName; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.catchThrowable; public class CassandraContainerTest { @@ -81,6 +82,16 @@ public void testInitScript() { } } + @Test + public void testNonexistentInitScript() { + try ( + CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE) + .withInitScript("unknown_script.cql") + ) { + assertThat(catchThrowable(cassandraContainer::start)).isInstanceOf(ContainerLaunchException.class); + } + } + @Test public void testInitScriptWithRequiredAuthentication() { try (