Skip to content

Commit 251bca5

Browse files
Reza Morshedieddumelendez
andauthored
Fix missing null check for JDBC init script (#9118)
In 6a07650, a new feature was introduced to support multiple init scripts in JdbcDatabaseContainer. However, during this update, a critical null check was inadvertently removed from the `JdbcDatabaseContainer.runInitScriptIfRequired` method. This check was responsible for ignoring null values for the `initScript` parameter. As a result, when no init script is defined, a null value is passed to `ScriptUtils.runInitScript`, leading to an exception being thrown and preventing the database container from starting. This commit reinstates the missing null check, ensuring that undefined init scripts are properly handled. Additionally, a test has been added to verify that the issue is resolved and the database container can start without an init script. --------- Co-authored-by: Eddú Meléndez <[email protected]>
1 parent cb7a793 commit 251bca5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Objects;
2526
import java.util.Properties;
2627
import java.util.concurrent.Future;
2728
import java.util.concurrent.TimeUnit;
@@ -361,7 +362,10 @@ protected void optionallyMapResourceParameterAsVolume(
361362
* Load init script content and apply it to the database if initScriptPath is set
362363
*/
363364
protected void runInitScriptIfRequired() {
364-
initScriptPaths.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
365+
initScriptPaths
366+
.stream()
367+
.filter(Objects::nonNull)
368+
.forEach(path -> ScriptUtils.runInitScript(getDatabaseDelegate(), path));
365369
}
366370

367371
public void setParameters(Map<String, String> parameters) {

modules/postgresql/src/test/java/org/testcontainers/junit/postgresql/SimplePostgreSQLTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.logging.LogManager;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.assertThatNoException;
1415

1516
public class SimplePostgreSQLTest extends AbstractContainerDatabaseTest {
1617
static {
@@ -59,6 +60,16 @@ public void testUnsetCommand() throws SQLException {
5960
}
6061
}
6162

63+
@Test
64+
public void testMissingInitScript() {
65+
try (
66+
PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(PostgreSQLTestImages.POSTGRES_TEST_IMAGE)
67+
.withInitScript(null)
68+
) {
69+
assertThatNoException().isThrownBy(postgres::start);
70+
}
71+
}
72+
6273
@Test
6374
public void testExplicitInitScript() throws SQLException {
6475
try (

0 commit comments

Comments
 (0)