Skip to content

Commit b675a38

Browse files
committed
Add TemporaryNetwork Rule
1 parent 14ba37a commit b675a38

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

core/src/test/java/org/testcontainers/containers/NetworkTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.testcontainers.DockerClientFactory;
88
import org.testcontainers.TestImages;
99
import org.testcontainers.junit.vintage.Container;
10+
import org.testcontainers.junit.vintage.TemporaryNetwork;
1011
import org.testcontainers.junit.vintage.Testcontainers;
1112

1213
import static org.assertj.core.api.Assertions.assertThat;
@@ -17,7 +18,7 @@ public class NetworkTest {
1718
public static class WithRules {
1819

1920
@Rule
20-
public Network network = Network.newNetwork();
21+
public TemporaryNetwork network = new TemporaryNetwork(Network.newNetwork());
2122

2223
@Rule
2324
public Testcontainers containers = new Testcontainers(this);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.testcontainers.junit.vintage;
2+
3+
import org.junit.rules.ExternalResource;
4+
import org.testcontainers.containers.Network;
5+
6+
/**
7+
* Integrates {@link Network} with the JUnit4 lifecycle.
8+
*/
9+
public final class TemporaryNetwork extends ExternalResource implements Network {
10+
11+
private final Network network;
12+
13+
private volatile State state = State.BEFORE_RULE;
14+
15+
/**
16+
* Creates an instance.
17+
*
18+
* <p>The passed-in network will be closed when the current test completes.
19+
*
20+
* @param network Network that the rule will delegate to.
21+
*/
22+
public TemporaryNetwork(Network network) {
23+
this.network = network;
24+
}
25+
26+
@Override
27+
public String getId() {
28+
if (state == State.AFTER_RULE) {
29+
throw new IllegalStateException("Cannot reference the network after the test completes");
30+
}
31+
return network.getId();
32+
}
33+
34+
@Override
35+
public void close() {
36+
switch (state) {
37+
case BEFORE_RULE:
38+
throw new IllegalStateException("Cannot close the network before the test starts");
39+
case INSIDE_RULE:
40+
break;
41+
case AFTER_RULE:
42+
throw new IllegalStateException("Cannot reference the network after the test completes");
43+
}
44+
network.close();
45+
}
46+
47+
@Override
48+
protected void before() throws Throwable {
49+
state = State.AFTER_RULE; // Just in case an exception is thrown below.
50+
network.getId(); // This has the side-effect of creating the network.
51+
52+
state = State.INSIDE_RULE;
53+
}
54+
55+
@Override
56+
protected void after() {
57+
state = State.AFTER_RULE;
58+
network.close();
59+
}
60+
61+
private enum State {
62+
BEFORE_RULE,
63+
INSIDE_RULE,
64+
AFTER_RULE,
65+
}
66+
}

0 commit comments

Comments
 (0)