9
9
import org .testcontainers .utility .DockerImageName ;
10
10
import org .testcontainers .utility .MountableFile ;
11
11
12
- import java .io .File ;
13
12
import java .net .InetSocketAddress ;
14
- import java .net .URISyntaxException ;
15
- import java .net .URL ;
16
13
import java .util .Optional ;
17
14
18
15
/**
@@ -30,6 +27,8 @@ public class CassandraContainer extends GenericContainer<CassandraContainer> {
30
27
31
28
private static final String DEFAULT_LOCAL_DATACENTER = "datacenter1" ;
32
29
30
+ private static final String DEFAULT_INIT_SCRIPT_FILENAME = "init.cql" ;
31
+
33
32
private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra" ;
34
33
35
34
private static final String USERNAME = "cassandra" ;
@@ -80,26 +79,25 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
80
79
* Load init script content and apply it to the database if initScriptPath is set
81
80
*/
82
81
private void runInitScriptIfRequired () {
83
- if (initScriptPath != null ) {
82
+ if (this . initScriptPath != null ) {
84
83
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
+ );
99
97
} 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 );
101
99
throw new ScriptUtils .UncategorizedScriptException (
102
- "Error while executing init script: " + initScriptPath ,
100
+ "Error while executing init script: " + this . initScriptPath ,
103
101
e
104
102
);
105
103
}
0 commit comments