Skip to content

Commit 9f03423

Browse files
committed
Fix generics
1 parent fd1dc44 commit 9f03423

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class EmbeddedPostgres implements Closeable {
6666
static final String DOCKER_DEFAULT_TAG = "13-alpine";
6767
// Note you can override any of these defaults explicitly in the builder.
6868

69-
private final PostgreSQLContainer<?> postgreDBContainer;
69+
private final PostgreSQLContainer<?> postgreSQLContainer;
7070

7171
private final UUID instanceId = UUID.randomUUID();
7272

@@ -79,7 +79,7 @@ public class EmbeddedPostgres implements Closeable {
7979
OptionalInt fixedPort,
8080
DockerImageName image,
8181
String databaseName
82-
) throws IOException {
82+
) {
8383
this(postgresConfig, localeConfig, bindMounts, network, networkAlias, fixedPort, image, DEFAULT_PG_STARTUP_WAIT, databaseName);
8484
}
8585

@@ -93,18 +93,18 @@ public class EmbeddedPostgres implements Closeable {
9393
Duration pgStartupWait,
9494
String databaseName
9595
) {
96-
LOG.trace("Starting containers with image {}, pgConfig {}, localeConfig {}, bindMounts {}, pgStartupWait {}, dbName {} ", image,
97-
postgresConfig, localeConfig, bindMounts, pgStartupWait, databaseName);
96+
if (LOG.isTraceEnabled()) {
97+
LOG.trace("Starting containers with image {}, pgConfig {}, localeConfig {}, " +
98+
"bindMounts {}, pgStartupWait {}, dbName {} ", image,
99+
postgresConfig, localeConfig, bindMounts, pgStartupWait, databaseName);
100+
}
98101
image = image.asCompatibleSubstituteFor(POSTGRES);
99-
final FixedPostgresSQLContainer pgContainer = new FixedPostgresSQLContainer(image); //NOPMD
100-
fixedPort.ifPresent(p -> {
101-
// This would be exposed via a builder method
102-
LOG.warn("Exposing a fixed port {} which kind of sucks,", p);
102+
final FixedPostgresSQLContainer<?> pgContainer = new FixedPostgresSQLContainer<>(image); //NOPMD
103+
fixedPort.ifPresent(p -> {
104+
LOG.warn("Exposing a fixed port {} which is NOT recommended.", p);
103105
pgContainer.addFixedExposedPort(p, POSTGRESQL_PORT);
104106
});
105-
106-
//TODO: generics are still mucked up
107-
this.postgreDBContainer = ((PostgreSQLContainer<?>) pgContainer)
107+
this.postgreSQLContainer = pgContainer
108108
.withDatabaseName(databaseName)
109109
.withUsername(POSTGRES)
110110
.withPassword(POSTGRES)
@@ -115,11 +115,11 @@ public class EmbeddedPostgres implements Closeable {
115115
.withEnv("POSTGRES_HOST_AUTH_METHOD", "trust");
116116
final List<String> cmd = new ArrayList<>(Collections.singletonList(POSTGRES));
117117
cmd.addAll(createConfigOptions(postgresConfig));
118-
postgreDBContainer.setCommand(cmd.toArray(new String[0]));
119-
processBindMounts(postgreDBContainer, bindMounts);
120-
network.ifPresent(postgreDBContainer::withNetwork);
121-
networkAlias.ifPresent(postgreDBContainer::withNetworkAliases);
122-
postgreDBContainer.start();
118+
postgreSQLContainer.setCommand(cmd.toArray(new String[0]));
119+
processBindMounts(postgreSQLContainer, bindMounts);
120+
network.ifPresent(postgreSQLContainer::withNetwork);
121+
networkAlias.ifPresent(postgreSQLContainer::withNetworkAliases);
122+
postgreSQLContainer.start();
123123
}
124124

125125
private void processBindMounts(PostgreSQLContainer<?> postgreDBContainer, Map<String, BindMount> bindMounts) {
@@ -148,19 +148,19 @@ private List<String> createInitOptions(final Map<String, String> localeConfig) {
148148
}
149149

150150
public DataSource getTemplateDatabase() {
151-
return getDatabase(postgreDBContainer.getUsername(), "template1");
151+
return getDatabase(postgreSQLContainer.getUsername(), "template1");
152152
}
153153

154154
public DataSource getTemplateDatabase(Map<String, String> properties) {
155-
return getDatabase(postgreDBContainer.getUsername(), "template1", properties);
155+
return getDatabase(postgreSQLContainer.getUsername(), "template1", properties);
156156
}
157157

158158
public DataSource getPostgresDatabase() {
159-
return getDatabase(postgreDBContainer.getUsername(), postgreDBContainer.getDatabaseName());
159+
return getDatabase(postgreSQLContainer.getUsername(), postgreSQLContainer.getDatabaseName());
160160
}
161161

162162
public DataSource getPostgresDatabase(Map<String, String> properties) {
163-
return getDatabase(postgreDBContainer.getUsername(), postgreDBContainer.getDatabaseName(), properties);
163+
return getDatabase(postgreSQLContainer.getUsername(), postgreSQLContainer.getDatabaseName(), properties);
164164
}
165165

166166
public DataSource getDatabase(String userName, String dbName) {
@@ -170,10 +170,10 @@ public DataSource getDatabase(String userName, String dbName) {
170170
public DataSource getDatabase(String userName, String dbName, Map<String, String> properties) {
171171
final PGSimpleDataSource ds = new PGSimpleDataSource();
172172

173-
ds.setURL(postgreDBContainer.getJdbcUrl());
173+
ds.setURL(postgreSQLContainer.getJdbcUrl());
174174
ds.setDatabaseName(dbName);
175175
ds.setUser(userName);
176-
ds.setPassword(postgreDBContainer.getPassword());
176+
ds.setPassword(postgreSQLContainer.getPassword());
177177

178178
properties.forEach((propertyKey, propertyValue) -> {
179179
try {
@@ -192,22 +192,22 @@ public DataSource getDatabase(String userName, String dbName, Map<String, String
192192
*/
193193
public String getJdbcUrl(String dbName) {
194194
try {
195-
return JdbcUrlUtils.replaceDatabase(postgreDBContainer.getJdbcUrl(), dbName);
195+
return JdbcUrlUtils.replaceDatabase(postgreSQLContainer.getJdbcUrl(), dbName);
196196
} catch (URISyntaxException e) {
197197
return null;
198198
}
199199
}
200200

201201
public String getHost() {
202-
return postgreDBContainer.getContainerIpAddress();
202+
return postgreSQLContainer.getContainerIpAddress();
203203
}
204204
public int getPort() {
205-
return postgreDBContainer.getMappedPort(POSTGRESQL_PORT);
205+
return postgreSQLContainer.getMappedPort(POSTGRESQL_PORT);
206206
}
207207

208208
@Override
209209
public void close() throws IOException {
210-
postgreDBContainer.close();
210+
postgreSQLContainer.close();
211211
}
212212

213213
public static EmbeddedPostgres start() throws IOException {
@@ -219,11 +219,11 @@ public static EmbeddedPostgres.Builder builder() {
219219
}
220220

221221
public String getUserName() {
222-
return postgreDBContainer.getUsername();
222+
return postgreSQLContainer.getUsername();
223223
}
224224

225225
public String getPassword() {
226-
return postgreDBContainer.getPassword();
226+
return postgreSQLContainer.getPassword();
227227
}
228228

229229
public static class Builder {

src/main/java/com/opentable/db/postgres/embedded/FixedPostgresSQLContainer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
import org.testcontainers.containers.PostgreSQLContainer;
1717
import org.testcontainers.utility.DockerImageName;
1818

19-
public class FixedPostgresSQLContainer extends PostgreSQLContainer {
19+
/**
20+
* This class exists only to expose the addFixedExposedPort option.
21+
* Otherwise we'd use PostgreSQLContainer directly.
22+
* @param <SELF> boring generics for fluent api
23+
*/
24+
public class FixedPostgresSQLContainer<SELF extends PostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> {
2025
public FixedPostgresSQLContainer(DockerImageName dockerImageName) {
2126
super(dockerImageName);
2227
}

0 commit comments

Comments
 (0)