29
29
30
30
import javax .sql .DataSource ;
31
31
import java .sql .DriverManager ;
32
+ import java .util .Arrays ;
32
33
import java .util .Collection ;
33
34
import java .util .HashMap ;
34
35
import java .util .LinkedHashMap ;
@@ -47,6 +48,8 @@ public abstract class DockerStorageContainer extends DockerITContainer implement
47
48
48
49
private static final String READY_USER_PASSWORD = "Ready@123" ;
49
50
51
+ private static final Collection <String > TO_BE_MOUNTED_SQL_FILES = Arrays .asList ("00-init-authority.sql" , "99-be-ready.sql" );
52
+
50
53
private final DatabaseType databaseType ;
51
54
52
55
private final Map <String , DataSource > actualDataSourceMap = new LinkedHashMap <>();
@@ -60,20 +63,24 @@ protected DockerStorageContainer(final DatabaseType databaseType, final String c
60
63
61
64
@ Override
62
65
protected void configure () {
63
- withClasspathResourceMapping ("/container/" + databaseType .getType ().toLowerCase () + "/init-sql/00-init-authority.sql" , "/docker-entrypoint-initdb.d/00-init-authority.sql" , BindMode .READ_ONLY );
64
- withClasspathResourceMapping ("/container/" + databaseType .getType ().toLowerCase () + "/init-sql/99-be-ready.sql" , "/docker-entrypoint-initdb.d/99-be-ready.sql" , BindMode .READ_ONLY );
66
+ for (String each : TO_BE_MOUNTED_SQL_FILES ) {
67
+ findToBeMountedSQLFile (each ).ifPresent (optional -> withClasspathResourceMapping (optional , "/docker-entrypoint-initdb.d/" + each , BindMode .READ_ONLY ));
68
+ }
65
69
withExposedPorts (getExposedPort ());
66
- setWaitStrategy (new JdbcConnectionWaitStrategy (
67
- () -> DriverManager .getConnection (getDefaultDatabaseName ().isPresent ()
68
- ? DataSourceEnvironment .getURL (databaseType , "localhost" , getFirstMappedPort (), getDefaultDatabaseName ().get ())
69
- : DataSourceEnvironment .getURL (databaseType , "localhost" , getFirstMappedPort ()), READY_USER , READY_USER_PASSWORD )));
70
+ setWaitStrategy (new JdbcConnectionWaitStrategy (() -> DriverManager .getConnection (getDefaultDatabaseName ().isPresent ()
71
+ ? DataSourceEnvironment .getURL (databaseType , "localhost" , getFirstMappedPort (), getDefaultDatabaseName ().get ())
72
+ : DataSourceEnvironment .getURL (databaseType , "localhost" , getFirstMappedPort ()), READY_USER , READY_USER_PASSWORD )));
73
+ }
74
+
75
+ private Optional <String > findToBeMountedSQLFile (final String toBeMountedSQLFile ) {
76
+ String toBeMountedSQLFilePath = String .format ("container/%s/init-sql/%s" , databaseType .getType ().toLowerCase (), toBeMountedSQLFile );
77
+ return null == Thread .currentThread ().getContextClassLoader ().getResource (toBeMountedSQLFilePath ) ? Optional .empty () : Optional .of ("/" + toBeMountedSQLFilePath );
70
78
}
71
79
72
80
protected final void setCommands (final String command ) {
73
- if (Strings .isNullOrEmpty (command )) {
74
- return ;
81
+ if (! Strings .isNullOrEmpty (command )) {
82
+ setCommand ( command ) ;
75
83
}
76
- setCommand (command );
77
84
}
78
85
79
86
protected final void addEnvs (final Map <String , String > envs ) {
@@ -165,12 +172,12 @@ public final String getAbbreviation() {
165
172
166
173
@ Override
167
174
public Map <String , String > getLinkReplacements () {
168
- Map <String , String > replacements = new HashMap <>();
175
+ Map <String , String > result = new HashMap <>();
169
176
for (String each : getNetworkAliases ()) {
170
177
for (Integer exposedPort : getExposedPorts ()) {
171
- replacements .put (each + ":" + exposedPort , getHost () + ":" + getMappedPort (exposedPort ));
178
+ result .put (each + ":" + exposedPort , getHost () + ":" + getMappedPort (exposedPort ));
172
179
}
173
180
}
174
- return replacements ;
181
+ return result ;
175
182
}
176
183
}
0 commit comments