Skip to content

Commit 38d6831

Browse files
author
raju.gupta
committed
Refactor test containers to use final and try-with-resources for better resource management
1 parent 47d5597 commit 38d6831

File tree

107 files changed

+2384
-2018
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+2384
-2018
lines changed

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.util.concurrent.Uninterruptibles;
44
import org.apache.commons.lang3.SystemUtils;
5+
import org.assertj.core.api.AssertionsForClassTypes;
56
import org.assertj.core.api.Assumptions;
67
import org.junit.jupiter.params.ParameterizedTest;
78
import org.junit.jupiter.params.provider.MethodSource;
@@ -60,34 +61,40 @@ void test(boolean localMode, String expectedEnvVar, File... composeFiles) {
6061
) {
6162
compose.start();
6263

63-
BufferedReader br = Unreliables.retryUntilSuccess(
64-
10,
65-
TimeUnit.SECONDS,
66-
() -> {
67-
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
64+
AssertionsForClassTypes
65+
.assertThatNoException()
66+
.isThrownBy(() -> {
67+
try (
68+
Socket socket = new Socket(
69+
compose.getServiceHost(SERVICE_NAME, SERVICE_PORT),
70+
compose.getServicePort(SERVICE_NAME, SERVICE_PORT)
71+
);
72+
BufferedReader br = Unreliables.retryUntilSuccess(
73+
10,
74+
TimeUnit.SECONDS,
75+
() -> {
76+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
6877

69-
Socket socket = new Socket(
70-
compose.getServiceHost(SERVICE_NAME, SERVICE_PORT),
71-
compose.getServicePort(SERVICE_NAME, SERVICE_PORT)
72-
);
73-
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
74-
}
75-
);
76-
77-
Unreliables.retryUntilTrue(
78-
10,
79-
TimeUnit.SECONDS,
80-
() -> {
81-
while (br.ready()) {
82-
String line = br.readLine();
83-
if (line.contains(expectedEnvVar)) {
84-
return true;
85-
}
78+
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
79+
}
80+
)
81+
) {
82+
Unreliables.retryUntilTrue(
83+
10,
84+
TimeUnit.SECONDS,
85+
() -> {
86+
while (br.ready()) {
87+
String line = br.readLine();
88+
if (line.contains(expectedEnvVar)) {
89+
return true;
90+
}
91+
}
92+
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
93+
return false;
94+
}
95+
);
8696
}
87-
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
88-
return false;
89-
}
90-
);
97+
});
9198
}
9299
}
93100
}

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

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static Iterable<Object[]> data() {
4343

4444
@ParameterizedTest(name = "{index}: local[{0}], composeFiles[{2}], expectedEnvVar[{1}]")
4545
@MethodSource("data")
46-
void test(boolean localMode, String expectedEnvVar, File... composeFiles) {
46+
void test(boolean localMode, String expectedEnvVar, File... composeFiles) throws Exception {
4747
if (localMode) {
4848
Assumptions
4949
.assumeThat(CommandLine.executableExists(DockerComposeContainer.COMPOSE_EXECUTABLE))
@@ -60,34 +60,36 @@ void test(boolean localMode, String expectedEnvVar, File... composeFiles) {
6060
) {
6161
compose.start();
6262

63-
BufferedReader br = Unreliables.retryUntilSuccess(
64-
10,
65-
TimeUnit.SECONDS,
66-
() -> {
67-
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
63+
try (
64+
Socket socket = new Socket(
65+
compose.getServiceHost(SERVICE_NAME, SERVICE_PORT),
66+
compose.getServicePort(SERVICE_NAME, SERVICE_PORT)
67+
);
68+
BufferedReader br = Unreliables.retryUntilSuccess(
69+
10,
70+
TimeUnit.SECONDS,
71+
() -> {
72+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
6873

69-
Socket socket = new Socket(
70-
compose.getServiceHost(SERVICE_NAME, SERVICE_PORT),
71-
compose.getServicePort(SERVICE_NAME, SERVICE_PORT)
72-
);
73-
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
74-
}
75-
);
76-
77-
Unreliables.retryUntilTrue(
78-
10,
79-
TimeUnit.SECONDS,
80-
() -> {
81-
while (br.ready()) {
82-
String line = br.readLine();
83-
if (line.contains(expectedEnvVar)) {
84-
return true;
74+
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
75+
}
76+
)
77+
) {
78+
Unreliables.retryUntilTrue(
79+
10,
80+
TimeUnit.SECONDS,
81+
() -> {
82+
while (br.ready()) {
83+
String line = br.readLine();
84+
if (line.contains(expectedEnvVar)) {
85+
return true;
86+
}
8587
}
88+
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
89+
return false;
8690
}
87-
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
88-
return false;
89-
}
90-
);
91+
);
92+
}
9193
}
9294
}
9395
}

core/src/test/java/org/testcontainers/images/builder/dockerfile/statement/AbstractStatementTest.java

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import org.apache.commons.io.IOUtils;
44
import org.apache.commons.lang3.StringUtils;
5-
import org.apache.commons.lang3.exception.ExceptionUtils;
5+
import org.assertj.core.api.Assertions;
6+
import org.assertj.core.api.AssertionsForClassTypes;
67
import org.junit.jupiter.api.TestInfo;
78
import org.rnorth.ducttape.Preconditions;
89

@@ -21,50 +22,46 @@ public abstract class AbstractStatementTest {
2122

2223
protected void assertStatement(Statement statement) {
2324
String testName = testInfo.getTestMethod().get().getName();
24-
String[] expectedLines = new String[0];
25-
try {
26-
String path = "fixtures/statements/" + getClass().getSimpleName() + "/" + testName;
27-
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path);
25+
String path = "fixtures/statements/" + getClass().getSimpleName() + "/" + testName;
2826

29-
Preconditions.check("inputStream is null for path " + path, inputStream != null);
27+
AssertionsForClassTypes
28+
.assertThatNoException()
29+
.isThrownBy(() -> {
30+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(path)) {
31+
String[] expectedLines;
32+
Preconditions.check("inputStream is null for path " + path, inputStream != null);
3033

31-
String content = IOUtils.toString(inputStream);
32-
IOUtils.closeQuietly(inputStream);
33-
expectedLines = StringUtils.chomp(content.replaceAll("\r\n", "\n").trim()).split("\n");
34-
} catch (Exception e) {
35-
fail("can't load fixture '" + testName + "'\n" + ExceptionUtils.getStackTrace(e));
36-
}
34+
String content = IOUtils.toString(inputStream);
35+
IOUtils.closeQuietly(inputStream);
36+
expectedLines = StringUtils.chomp(content.replaceAll("\r\n", "\n").trim()).split("\n");
37+
StringBuilder builder = new StringBuilder();
38+
statement.appendArguments(builder);
39+
String[] resultLines = StringUtils.chomp(builder.toString().trim()).split("\n");
3740

38-
StringBuilder builder = new StringBuilder();
39-
statement.appendArguments(builder);
40-
String[] resultLines = StringUtils.chomp(builder.toString().trim()).split("\n");
41+
Assertions.assertThat(expectedLines.length).isSameAs(resultLines.length);
4142

42-
if (expectedLines.length != resultLines.length) {
43-
fail(
44-
"number of lines is not the same. Expected " + expectedLines.length + " but got " + resultLines.length
45-
);
46-
}
43+
if (!Arrays.equals(expectedLines, resultLines)) {
44+
StringBuilder failureBuilder = new StringBuilder();
45+
failureBuilder.append("Invalid statement!\n");
4746

48-
if (!Arrays.equals(expectedLines, resultLines)) {
49-
StringBuilder failureBuilder = new StringBuilder();
50-
failureBuilder.append("Invalid statement!\n");
47+
for (int i = 0; i < expectedLines.length; i++) {
48+
String expectedLine = expectedLines[i];
49+
String actualLine = resultLines[i];
5150

52-
for (int i = 0; i < expectedLines.length; i++) {
53-
String expectedLine = expectedLines[i];
54-
String actualLine = resultLines[i];
51+
if (!expectedLine.equals(actualLine)) {
52+
failureBuilder.append("Invalid line #");
53+
failureBuilder.append(i);
54+
failureBuilder.append(":\n\tActual: <");
55+
failureBuilder.append(actualLine);
56+
failureBuilder.append(">\n\tExpected: <");
57+
failureBuilder.append(expectedLine);
58+
failureBuilder.append(">\n");
59+
}
60+
}
5561

56-
if (!expectedLine.equals(actualLine)) {
57-
failureBuilder.append("Invalid line #");
58-
failureBuilder.append(i);
59-
failureBuilder.append(":\n\tActual: <");
60-
failureBuilder.append(actualLine);
61-
failureBuilder.append(">\n\tExpected: <");
62-
failureBuilder.append(expectedLine);
63-
failureBuilder.append(">\n");
62+
fail(failureBuilder.toString());
63+
}
6464
}
65-
}
66-
67-
fail(failureBuilder.toString());
68-
}
65+
});
6966
}
7067
}

core/src/test/java/org/testcontainers/junit/BaseComposeTest.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
16-
import java.util.stream.Collectors;
1716

1817
import static org.assertj.core.api.Assertions.assertThat;
1918

@@ -23,7 +22,7 @@ public abstract class BaseComposeTest {
2322

2423
protected abstract ComposeContainer getEnvironment();
2524

26-
private List<String> existingNetworks = new ArrayList<>();
25+
private final List<String> existingNetworks = new ArrayList<>();
2726

2827
@BeforeAll
2928
public static void checkVersion() {
@@ -32,33 +31,39 @@ public static void checkVersion() {
3231

3332
@Test
3433
void simpleTest() {
35-
Jedis jedis = new Jedis(
36-
getEnvironment().getServiceHost("redis-1", REDIS_PORT),
37-
getEnvironment().getServicePort("redis-1", REDIS_PORT)
38-
);
39-
40-
jedis.incr("test");
41-
jedis.incr("test");
42-
jedis.incr("test");
43-
44-
assertThat(jedis.get("test")).as("A redis instance defined in compose can be used in isolation").isEqualTo("3");
34+
try (
35+
Jedis jedis = new Jedis(
36+
getEnvironment().getServiceHost("redis-1", REDIS_PORT),
37+
getEnvironment().getServicePort("redis-1", REDIS_PORT)
38+
)
39+
) {
40+
jedis.incr("test");
41+
jedis.incr("test");
42+
jedis.incr("test");
43+
44+
assertThat(jedis.get("test"))
45+
.as("A redis instance defined in compose can be used in isolation")
46+
.isEqualTo("3");
47+
}
4548
}
4649

4750
@Test
4851
void secondTest() {
4952
// used in manual checking for cleanup in between tests
50-
Jedis jedis = new Jedis(
51-
getEnvironment().getServiceHost("redis-1", REDIS_PORT),
52-
getEnvironment().getServicePort("redis-1", REDIS_PORT)
53-
);
54-
55-
jedis.incr("test");
56-
jedis.incr("test");
57-
jedis.incr("test");
58-
59-
assertThat(jedis.get("test")).as("Tests use fresh container instances").isEqualTo("3");
60-
// if these end up using the same container one of the test methods will fail.
61-
// However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
53+
try (
54+
Jedis jedis = new Jedis(
55+
getEnvironment().getServiceHost("redis-1", REDIS_PORT),
56+
getEnvironment().getServicePort("redis-1", REDIS_PORT)
57+
)
58+
) {
59+
jedis.incr("test");
60+
jedis.incr("test");
61+
jedis.incr("test");
62+
63+
assertThat(jedis.get("test")).as("Tests use fresh container instances").isEqualTo("3");
64+
// if these end up using the same container one of the test methods will fail.
65+
// However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
66+
}
6267
}
6368

6469
@BeforeEach
@@ -80,6 +85,6 @@ private List<String> findAllNetworks() {
8085
.stream()
8186
.map(Network::getName)
8287
.sorted()
83-
.collect(Collectors.toList());
88+
.toList();
8489
}
8590
}

core/src/test/java/org/testcontainers/junit/BaseDockerComposeTest.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,39 @@ public static void checkVersion() {
3535

3636
@Test
3737
void simpleTest() {
38-
Jedis jedis = new Jedis(
39-
getEnvironment().getServiceHost("redis_1", REDIS_PORT),
40-
getEnvironment().getServicePort("redis_1", REDIS_PORT)
41-
);
42-
43-
jedis.incr("test");
44-
jedis.incr("test");
45-
jedis.incr("test");
46-
47-
assertThat(jedis.get("test")).as("A redis instance defined in compose can be used in isolation").isEqualTo("3");
38+
try (
39+
Jedis jedis = new Jedis(
40+
getEnvironment().getServiceHost("redis_1", REDIS_PORT),
41+
getEnvironment().getServicePort("redis_1", REDIS_PORT)
42+
)
43+
) {
44+
jedis.incr("test");
45+
jedis.incr("test");
46+
jedis.incr("test");
47+
48+
assertThat(jedis.get("test"))
49+
.as("A redis instance defined in compose can be used in isolation")
50+
.isEqualTo("3");
51+
}
4852
}
4953

5054
@Test
5155
void secondTest() {
5256
// used in manual checking for cleanup in between tests
53-
Jedis jedis = new Jedis(
54-
getEnvironment().getServiceHost("redis_1", REDIS_PORT),
55-
getEnvironment().getServicePort("redis_1", REDIS_PORT)
56-
);
57-
58-
jedis.incr("test");
59-
jedis.incr("test");
60-
jedis.incr("test");
61-
62-
assertThat(jedis.get("test")).as("Tests use fresh container instances").isEqualTo("3");
63-
// if these end up using the same container one of the test methods will fail.
64-
// However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
57+
try (
58+
Jedis jedis = new Jedis(
59+
getEnvironment().getServiceHost("redis_1", REDIS_PORT),
60+
getEnvironment().getServicePort("redis_1", REDIS_PORT)
61+
)
62+
) {
63+
jedis.incr("test");
64+
jedis.incr("test");
65+
jedis.incr("test");
66+
67+
assertThat(jedis.get("test")).as("Tests use fresh container instances").isEqualTo("3");
68+
// if these end up using the same container one of the test methods will fail.
69+
// However, @Rule creates a separate DockerComposeContainer instance per test, so this just shouldn't happen
70+
}
6571
}
6672

6773
@BeforeEach

core/src/test/java/org/testcontainers/junit/DockerComposeContainerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
class DockerComposeContainerTest extends BaseDockerComposeTest {
2121

2222
@AutoClose
23-
public DockerComposeContainer environment = new DockerComposeContainer(
23+
public DockerComposeContainer<?> environment = new DockerComposeContainer<>(
2424
new File("src/test/resources/compose-test.yml")
2525
)
2626
.withExposedService("redis_1", REDIS_PORT)

0 commit comments

Comments
 (0)