Skip to content

Commit 6665ad7

Browse files
committed
fix(module/mongodb):move to org.testcontainers.mongodb.MongoDBContainer
1 parent e2adfb1 commit 6665ad7

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

modules/mongodb/src/main/java/org/testcontainers/containers/MongoDBContainer.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,4 @@ void withSharding() {
224224
}
225225
}
226226

227-
/**
228-
* Executes a MongoDB initialization script from the classpath during startup.
229-
* <p>
230-
* The script will be copied to {@code /docker-entrypoint-initdb.d/init.js}.
231-
* This method also adjusts the {@link org.testcontainers.containers.wait.strategy.WaitStrategy}
232-
* to expect the "waiting for connections" log message twice, as the execution of an init script
233-
* causes MongoDB to restart.
234-
*
235-
* @param scriptPath the path to the init script file on the classpath
236-
* @return this container instance
237-
*/
238-
public MongoDBContainer withInitScript(String scriptPath) {
239-
withCopyFileToContainer(MountableFile.forClasspathResource(scriptPath), "/docker-entrypoint-initdb.d/init.js");
240-
241-
this.waitStrategy = Wait.forLogMessage("(?i).*waiting for connections.*", 2);
242-
243-
return this;
244-
}
245227
}

modules/mongodb/src/main/java/org/testcontainers/mongodb/MongoDBContainer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,23 @@ public String getReplicaSetUrl(String databaseName) {
204204
}
205205
return getConnectionString() + "/" + databaseName;
206206
}
207+
208+
/**
209+
* Executes a MongoDB initialization script from the classpath during startup.
210+
* <p>
211+
* The script will be copied to {@code /docker-entrypoint-initdb.d/init.js}.
212+
* This method also adjusts the {@link org.testcontainers.containers.wait.strategy.WaitStrategy}
213+
* to expect the "waiting for connections" log message twice, as the execution of an init script
214+
* causes MongoDB to restart.
215+
*
216+
* @param scriptPath the path to the init script file on the classpath
217+
* @return this container instance
218+
*/
219+
public MongoDBContainer withInitScript(String scriptPath) {
220+
withCopyFileToContainer(MountableFile.forClasspathResource(scriptPath), "/docker-entrypoint-initdb.d/init.js");
221+
222+
this.waitStrategy = Wait.forLogMessage("(?i).*waiting for connections.*", 2);
223+
224+
return this;
225+
}
207226
}

modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBContainerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import static org.assertj.core.api.Assertions.assertThat;
66

7+
import java.time.Duration;
8+
79
class MongoDBContainerTest extends AbstractMongo {
810

911
/**
@@ -38,4 +40,17 @@ void shouldTestDatabaseName() {
3840
assertThat(mongoDBContainer.getReplicaSetUrl(databaseName)).endsWith(databaseName);
3941
}
4042
}
43+
44+
@Test
45+
void testWithInitScript() {
46+
try (
47+
MongoDBContainer mongoDB = new MongoDBContainer("mongo:4.0.10")
48+
.withInitScript("init.js")
49+
.withStartupTimeout(Duration.ofSeconds(30))
50+
) {
51+
mongoDB.start();
52+
53+
assertThat(mongoDB.isRunning()).isTrue();
54+
}
55+
}
4156
}

modules/mongodb/src/test/java/org/testcontainers/mongodb/MongoDBInitScriptTest.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)