99import org .testcontainers .utility .DockerImageName ;
1010import org .testcontainers .utility .MountableFile ;
1111
12- import java .io .File ;
1312import java .net .InetSocketAddress ;
14- import java .net .URISyntaxException ;
15- import java .net .URL ;
1613import java .util .Optional ;
1714
1815/**
@@ -30,6 +27,8 @@ public class CassandraContainer extends GenericContainer<CassandraContainer> {
3027
3128 private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1" ;
3229
30+ private static final String DEFAULT_INIT_SCRIPT_FILENAME = "init.cql" ;
31+
3332 private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra" ;
3433
3534 private static final String USERNAME = "cassandra" ;
@@ -80,26 +79,25 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
8079 * Load init script content and apply it to the database if initScriptPath is set
8180 */
8281 private void runInitScriptIfRequired () {
83- if (initScriptPath != null ) {
82+ if (this . initScriptPath != null ) {
8483 try {
85- URL resource = Thread .currentThread ().getContextClassLoader ().getResource (initScriptPath );
86- if (resource == null ) {
87- logger ().warn ("Could not load classpath init script: {}" , initScriptPath );
88- throw new ScriptLoadException (
89- "Could not load classpath init script: " + initScriptPath + ". Resource not found."
90- );
91- }
92- // The init script is executed as is by the cqlsh command, so copy it into the container.
93- String targetInitScriptName = new File (resource .toURI ()).getName ();
94- copyFileToContainer (MountableFile .forClasspathResource (initScriptPath ), targetInitScriptName );
95- new CassandraDatabaseDelegate (this ).execute (null , targetInitScriptName , -1 , false , false );
96- } catch (URISyntaxException e ) {
97- logger ().warn ("Could not copy init script into container: {}" , initScriptPath );
98- throw new ScriptLoadException ("Could not copy init script into container: " + initScriptPath , e );
84+ final MountableFile originalInitScript = MountableFile .forClasspathResource (this .initScriptPath );
85+ // The init script is executed as is by the cqlsh command, so copy it into the container. The name
86+ // of the script is generic since it's not important to keep the original name.
87+ copyFileToContainer (originalInitScript , DEFAULT_INIT_SCRIPT_FILENAME );
88+ new CassandraDatabaseDelegate (this ).execute (null , DEFAULT_INIT_SCRIPT_FILENAME , -1 , false , false );
89+ } catch (IllegalArgumentException e ) {
90+ // MountableFile.forClasspathResource will throw an IllegalArgumentException if the resource cannot
91+ // be found.
92+ logger ().warn ("Could not load classpath init script: {}" , this .initScriptPath );
93+ throw new ScriptLoadException (
94+ "Could not load classpath init script: " + this .initScriptPath + ". Resource not found." ,
95+ e
96+ );
9997 } catch (ScriptUtils .ScriptStatementFailedException e ) {
100- logger ().error ("Error while executing init script: {}" , initScriptPath , e );
98+ logger ().error ("Error while executing init script: {}" , this . initScriptPath , e );
10199 throw new ScriptUtils .UncategorizedScriptException (
102- "Error while executing init script: " + initScriptPath ,
100+ "Error while executing init script: " + this . initScriptPath ,
103101 e
104102 );
105103 }
0 commit comments