Skip to content

Commit 14be0bb

Browse files
committed
Polish
1 parent 5ef8e2a commit 14be0bb

12 files changed

+112
-196
lines changed

core/src/main/java/org/testcontainers/containers/ComposeContainer.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.time.Duration;
1717
import java.util.ArrayList;
1818
import java.util.Arrays;
19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.List;
@@ -68,7 +69,7 @@ public class ComposeContainer implements Startable {
6869
private List<String> filesInDirectory = new ArrayList<>();
6970

7071
/**
71-
* Creates a new ComposeContainer with a random identifier using the specified Docker image and compose files.
72+
* Creates a new ComposeContainer using the specified Docker image and compose files.
7273
*
7374
* @param image The Docker image to use for the container
7475
* @param composeFiles One or more Docker Compose configuration files
@@ -78,7 +79,7 @@ public ComposeContainer(DockerImageName image, File... composeFiles) {
7879
}
7980

8081
/**
81-
* Creates a new ComposeContainer with a random identifier using the specified Docker image and compose files.
82+
* Creates a new ComposeContainer using the specified Docker image and compose files.
8283
*
8384
* @param image The Docker image to use for the container
8485
* @param composeFiles A list of Docker Compose configuration files
@@ -98,9 +99,19 @@ public ComposeContainer(DockerImageName image, String identifier, File... compos
9899
this(image, identifier, Arrays.asList(composeFiles));
99100
}
100101

102+
/**
103+
* Creates a new ComposeContainer with the specified Docker image, identifier, and a single compose file.
104+
*
105+
* @param image The Docker image to use for the container
106+
* @param identifier A unique identifier for this compose environment
107+
* @param composeFile A Docker Compose configuration file
108+
*/
109+
public ComposeContainer(DockerImageName image, String identifier, File composeFile) {
110+
this(image, identifier, Collections.singletonList(composeFile));
111+
}
112+
101113
/**
102114
* Creates a new ComposeContainer with the specified Docker image, identifier, and compose files.
103-
* This is the primary constructor that all other constructors delegate to.
104115
*
105116
* @param image The Docker image to use for the container
106117
* @param identifier A unique identifier for this compose environment
@@ -113,39 +124,35 @@ public ComposeContainer(DockerImageName image, String identifier, List<File> com
113124
}
114125

115126
/**
116-
* @deprecated
117-
* Use the new constructor ComposeContainer(DockerImageName image, File... composeFiles)
127+
* Use the new constructor {@link #ComposeContainer(DockerImageName image, File... composeFiles)}
118128
*/
119-
@Deprecated
120129
public ComposeContainer(File... composeFiles) {
121130
this(DEFAULT_IMAGE_NAME, Arrays.asList(composeFiles));
131+
this.localCompose = true;
122132
}
123133

124134
/**
125-
* @deprecated
126-
* Use the new constructor ComposeContainer(DockerImageName image,List composeFiles)
135+
* Use the new constructor {@link #ComposeContainer(DockerImageName image, List composeFiles)}
127136
*/
128-
@Deprecated
129137
public ComposeContainer(List<File> composeFiles) {
130138
this(DEFAULT_IMAGE_NAME, composeFiles);
139+
this.localCompose = true;
131140
}
132141

133142
/**
134-
* @deprecated
135-
* Use the new constructor ComposeContainer(DockerImageName image, String identifier, File... composeFile)
143+
* Use the new constructor {@link #ComposeContainer(DockerImageName image, String identifier, File... composeFile)}
136144
*/
137-
@Deprecated
138145
public ComposeContainer(String identifier, File... composeFiles) {
139146
this(DEFAULT_IMAGE_NAME, identifier, Arrays.asList(composeFiles));
147+
this.localCompose = true;
140148
}
141149

142150
/**
143-
* @deprecated
144-
* Use the new constructor ComposeContainer(DockerImageName image,String identifier, List composeFiles)
151+
* Use the new constructor {@link #ComposeContainer(DockerImageName image, String identifier, List composeFiles)}
145152
*/
146-
@Deprecated
147153
public ComposeContainer(String identifier, List<File> composeFiles) {
148154
this(DEFAULT_IMAGE_NAME, identifier, composeFiles);
155+
this.localCompose = true;
149156
}
150157

151158
@Override
@@ -291,16 +298,6 @@ public ComposeContainer withEnv(Map<String, String> env) {
291298
return this;
292299
}
293300

294-
/**
295-
* Use a local Docker Compose binary instead of a container.
296-
*
297-
* @return this instance, for chaining
298-
*/
299-
public ComposeContainer withLocalCompose(boolean localCompose) {
300-
this.localCompose = localCompose;
301-
return this;
302-
}
303-
304301
/**
305302
* Whether to pull images first.
306303
*

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,17 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>> i
6666
private List<String> filesInDirectory = new ArrayList<>();
6767

6868
/**
69-
* Creates a new DockerComposeContainer with the specified Docker image, identifier, and a single compose file.
69+
* Creates a new DockerComposeContainer using the specified Docker image and compose files.
7070
*
7171
* @param image The Docker image to use for the container
72-
* @param identifier A unique identifier for this compose environment
73-
* @param composeFile A Docker Compose configuration file
72+
* @param composeFiles One or more Docker Compose configuration files
7473
*/
75-
public DockerComposeContainer(DockerImageName image, String identifier, File composeFile) {
76-
this(image, identifier, Collections.singletonList(composeFile));
74+
public DockerComposeContainer(DockerImageName image, File... composeFiles) {
75+
this(image, Arrays.asList(composeFiles));
7776
}
7877

7978
/**
80-
* Creates a new DockerComposeContainer with a random identifier using the specified Docker image and compose files.
79+
* Creates a new DockerComposeContainer using the specified Docker image and compose files.
8180
*
8281
* @param image The Docker image to use for the container
8382
* @param composeFiles A list of Docker Compose configuration files
@@ -97,9 +96,19 @@ public DockerComposeContainer(DockerImageName image, String identifier, File...
9796
this(image, identifier, Arrays.asList(composeFiles));
9897
}
9998

99+
/**
100+
* Creates a new DockerComposeContainer with the specified Docker image, identifier, and a single compose file.
101+
*
102+
* @param image The Docker image to use for the container
103+
* @param identifier A unique identifier for this compose environment
104+
* @param composeFile A Docker Compose configuration file
105+
*/
106+
public DockerComposeContainer(DockerImageName image, String identifier, File composeFile) {
107+
this(image, identifier, Collections.singletonList(composeFile));
108+
}
109+
100110
/**
101111
* Creates a new DockerComposeContainer with the specified Docker image, identifier, and compose files.
102-
* This is the primary constructor that all other constructors delegate to.
103112
*
104113
* @param image The Docker image to use for the container
105114
* @param identifier A unique identifier for this compose environment
@@ -112,46 +121,41 @@ public DockerComposeContainer(DockerImageName image, String identifier, List<Fil
112121
}
113122

114123
/**
115-
* @deprecated
116-
* Use the new constructor DockerComposeContainer(File composeFile, String identifier)
124+
* Use the new constructor {@link #DockerComposeContainer(DockerImageName image, String identifier, File composeFile)}
117125
*/
118-
@Deprecated
119126
public DockerComposeContainer(File composeFile, String identifier) {
120127
this(identifier, composeFile);
128+
this.localCompose = true;
121129
}
122130

123131
/**
124-
* @deprecated
125-
* Use the new constructor DockerComposeContainer(File... composeFiles)
132+
* Use the new constructor {@link #DockerComposeContainer(DockerImageName image, List composeFiles)}
126133
*/
127-
@Deprecated
128134
public DockerComposeContainer(File... composeFiles) {
129135
this(Arrays.asList(composeFiles));
136+
this.localCompose = true;
130137
}
131138

132139
/**
133-
* @deprecated
134-
* Use the new constructor DockerComposeContainer(List composeFiles)
140+
* Use the new constructor {@link #DockerComposeContainer(DockerImageName image, List composeFiles)}
135141
*/
136142
@Deprecated
137143
public DockerComposeContainer(List<File> composeFiles) {
138144
this(Base58.randomString(6).toLowerCase(), composeFiles);
145+
this.localCompose = true;
139146
}
140147

141148
/**
142-
* @deprecated
143-
* Use the new constructor DockerComposeContainer(String identifier,File... composeFiles)
149+
* Use the new constructor {@link #DockerComposeContainer(DockerImageName image, String identifier, File... composeFiles)}
144150
*/
145-
@Deprecated
146151
public DockerComposeContainer(String identifier, File... composeFiles) {
147152
this(identifier, Arrays.asList(composeFiles));
153+
this.localCompose = true;
148154
}
149155

150156
/**
151-
* @deprecated
152-
* Use the new constructor DockerComposeContainer(String identifier,List composeFiles)
157+
* Use the new constructor {@link #DockerComposeContainer(DockerImageName image, String identifier, List composeFiles)}
153158
*/
154-
@Deprecated
155159
public DockerComposeContainer(String identifier, List<File> composeFiles) {
156160
this.composeDelegate =
157161
new ComposeDelegate(
@@ -162,6 +166,7 @@ public DockerComposeContainer(String identifier, List<File> composeFiles) {
162166
DockerImageName.parse("docker/compose:1.29.2")
163167
);
164168
this.project = this.composeDelegate.getProject();
169+
this.localCompose = true;
165170
}
166171

167172
@Override
@@ -303,16 +308,6 @@ public SELF withEnv(Map<String, String> env) {
303308
return self();
304309
}
305310

306-
/**
307-
* Use a local Docker Compose binary instead of a container.
308-
*
309-
* @return this instance, for chaining
310-
*/
311-
public SELF withLocalCompose(boolean localCompose) {
312-
this.localCompose = localCompose;
313-
return self();
314-
}
315-
316311
/**
317312
* Whether to pull images first.
318313
*

core/src/main/java/org/testcontainers/utility/TestcontainersConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private String getConfigurable(
244244
}
245245

246246
for (final Properties properties : propertiesSources) {
247-
if (properties.get(propertyName) != null && !properties.get(propertyName).toString().trim().isEmpty()) {
247+
if (properties.get(propertyName) != null) {
248248
return (String) properties.get(propertyName);
249249
}
250250
}
Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,49 @@
11
package org.testcontainers.containers;
22

3-
import ch.qos.logback.classic.Logger;
4-
import ch.qos.logback.classic.spi.ILoggingEvent;
5-
import ch.qos.logback.core.AppenderBase;
6-
import org.junit.After;
7-
import org.junit.Before;
8-
import org.junit.Test;
9-
import org.slf4j.LoggerFactory;
3+
import org.junit.jupiter.api.Test;
104
import org.testcontainers.utility.DockerImageName;
11-
import org.testcontainers.utility.TestcontainersConfiguration;
125

136
import java.io.File;
14-
import java.util.ArrayList;
15-
import java.util.List;
7+
import java.util.Optional;
168

179
import static org.assertj.core.api.Assertions.assertThat;
1810

19-
public class ComposeContainerTest {
11+
class ComposeContainerTest {
2012

2113
public static final String DOCKER_IMAGE = "docker:25.0.2";
2214

23-
private static final String COMPOSE_FILE_PATH = "src/test/resources/docker-compose-imagename-parsing-v2.yml";
24-
25-
private ComposeContainer composeContainer;
26-
27-
private TestLogAppender testLogAppender;
28-
29-
private Logger rootLogger;
30-
31-
@Before
32-
public void setup() {
33-
testLogAppender = new TestLogAppender();
34-
testLogAppender.start();
35-
rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
36-
rootLogger.addAppender(testLogAppender);
37-
TestcontainersConfiguration.getInstance().updateUserConfig("compose.container.image", DOCKER_IMAGE);
38-
}
39-
40-
@After
41-
public void tearDown() {
42-
composeContainer.stop();
43-
rootLogger.detachAppender(testLogAppender);
44-
TestcontainersConfiguration.getInstance().updateUserConfig("compose.container.image", "");
45-
System.clearProperty("compose.container.image");
46-
}
15+
private static final String COMPOSE_FILE_PATH = "src/test/resources/v2-compose-test.yml";
4716

4817
@Test
49-
public void testWithCustomDockerImage() {
50-
composeContainer = new ComposeContainer(DockerImageName.parse(DOCKER_IMAGE), new File(COMPOSE_FILE_PATH));
18+
void testWithCustomDockerImage() {
19+
ComposeContainer composeContainer = new ComposeContainer(
20+
DockerImageName.parse(DOCKER_IMAGE),
21+
new File(COMPOSE_FILE_PATH)
22+
);
5123
composeContainer.start();
52-
verifyContainerCreation();
24+
verifyContainerCreation(composeContainer);
25+
composeContainer.stop();
5326
}
5427

5528
@Test
56-
public void testWithCustomDockerImageAndIdentifier() {
57-
composeContainer =
58-
new ComposeContainer(DockerImageName.parse(DOCKER_IMAGE), "myidentifier", new File(COMPOSE_FILE_PATH));
29+
void testWithCustomDockerImageAndIdentifier() {
30+
ComposeContainer composeContainer = new ComposeContainer(
31+
DockerImageName.parse(DOCKER_IMAGE),
32+
"myidentifier",
33+
new File(COMPOSE_FILE_PATH)
34+
);
5935
composeContainer.start();
60-
verifyContainerCreation();
61-
}
62-
63-
private void verifyContainerCreation() {
64-
List<String> logs = testLogAppender.getLogs();
65-
66-
assertThat(logs).isNotNull().anyMatch(line -> line.contains("Creating container for image: " + DOCKER_IMAGE));
36+
verifyContainerCreation(composeContainer);
37+
composeContainer.stop();
6738
}
6839

69-
private static class TestLogAppender extends AppenderBase<ILoggingEvent> {
70-
71-
private final List<String> logs = new ArrayList<>();
72-
73-
@Override
74-
protected void append(ILoggingEvent eventObject) {
75-
logs.add(eventObject.getFormattedMessage());
76-
}
77-
78-
public List<String> getLogs() {
79-
return logs;
80-
}
40+
private void verifyContainerCreation(ComposeContainer composeContainer) {
41+
Optional<ContainerState> redis = composeContainer.getContainerByServiceName("redis");
42+
assertThat(redis)
43+
.hasValueSatisfying(container -> {
44+
assertThat(container.isRunning()).isTrue();
45+
assertThat(container.getContainerInfo().getConfig().getLabels())
46+
.containsEntry("com.docker.compose.version", "2.24.5");
47+
});
8148
}
8249
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ void test(boolean localMode, String expectedEnvVar, File... composeFiles) {
5454
.isTrue();
5555
}
5656
try (
57-
ComposeContainer compose = new ComposeContainer(composeFiles)
58-
.withLocalCompose(localMode)
59-
.withExposedService(SERVICE_NAME, SERVICE_PORT)
57+
ComposeContainer compose = new ComposeContainer(composeFiles).withExposedService(SERVICE_NAME, SERVICE_PORT)
6058
) {
6159
compose.start();
6260

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ void testProfileOption(boolean localMode) {
2929
try (
3030
// composeContainerWithLocalCompose {
3131
ComposeContainer compose = new ComposeContainer(COMPOSE_FILE)
32-
.withLocalCompose(true)
3332
// }
3433
.withOptions("--profile=cache")
3534
) {

0 commit comments

Comments
 (0)