29
29
import java .util .Map ;
30
30
import java .util .Objects ;
31
31
import java .util .Optional ;
32
+ import java .util .OptionalInt ;
32
33
import java .util .UUID ;
33
34
34
35
import javax .sql .DataSource ;
47
48
* Core class of the library, providing a builder (with reasonable defaults) to wrap
48
49
* testcontainers and launch postgres container.
49
50
*/
51
+ @ SuppressWarnings ("OptionalUsedAsFieldOrParameterType" )
50
52
public class EmbeddedPostgres implements Closeable {
51
53
private static final Logger LOG = LoggerFactory .getLogger (EmbeddedPostgres .class );
52
54
@@ -74,25 +76,35 @@ public class EmbeddedPostgres implements Closeable {
74
76
Map <String , BindMount > bindMounts ,
75
77
Optional <Network > network ,
76
78
Optional <String > networkAlias ,
79
+ OptionalInt fixedPort ,
77
80
DockerImageName image ,
78
81
String databaseName
79
82
) throws IOException {
80
- this (postgresConfig , localeConfig , bindMounts , network , networkAlias , image , DEFAULT_PG_STARTUP_WAIT , databaseName );
83
+ this (postgresConfig , localeConfig , bindMounts , network , networkAlias , fixedPort , image , DEFAULT_PG_STARTUP_WAIT , databaseName );
81
84
}
82
85
83
86
EmbeddedPostgres (Map <String , String > postgresConfig ,
84
87
Map <String , String > localeConfig ,
85
88
Map <String , BindMount > bindMounts ,
86
89
Optional <Network > network ,
87
90
Optional <String > networkAlias ,
91
+ OptionalInt fixedPort ,
88
92
DockerImageName image ,
89
93
Duration pgStartupWait ,
90
94
String databaseName
91
- ) throws IOException {
95
+ ) {
92
96
LOG .trace ("Starting containers with image {}, pgConfig {}, localeConfig {}, bindMounts {}, pgStartupWait {}, dbName {} " , image ,
93
97
postgresConfig , localeConfig , bindMounts , pgStartupWait , databaseName );
94
98
image = image .asCompatibleSubstituteFor (POSTGRES );
95
- this .postgreDBContainer = new PostgreSQLContainer <>(image )
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 );
103
+ pgContainer .addFixedExposedPort (p , POSTGRESQL_PORT );
104
+ });
105
+
106
+ //TODO: generics are still mucked up
107
+ this .postgreDBContainer = ((PostgreSQLContainer <?>) pgContainer )
96
108
.withDatabaseName (databaseName )
97
109
.withUsername (POSTGRES )
98
110
.withPassword (POSTGRES )
@@ -225,6 +237,7 @@ public static class Builder {
225
237
private DockerImageName image = getDefaultImage ();
226
238
private String databaseName = POSTGRES ;
227
239
private Optional <String > networkAlias = Optional .empty ();
240
+ private OptionalInt fixedPort = OptionalInt .empty ();
228
241
229
242
// See comments at top for the logic.
230
243
DockerImageName getDefaultImage () {
@@ -345,6 +358,17 @@ public Builder setImage(DockerImageName image) {
345
358
return this ;
346
359
}
347
360
361
+ /**
362
+ * Force a fixed port on the local side of things. We strongly recommend
363
+ * against using this, using ephemeral ports, the default. We reserve the
364
+ * right to remove this support.
365
+ * @param fixedPort a integer port.
366
+ */
367
+ @ Deprecated
368
+ public void setFixedPort (Integer fixedPort ) {
369
+ this .fixedPort = OptionalInt .of (fixedPort );
370
+ }
371
+
348
372
/**
349
373
* Add the tag to an existing image
350
374
* @param tag Tag
@@ -360,7 +384,7 @@ DockerImageName getImage() {
360
384
}
361
385
362
386
public EmbeddedPostgres start () throws IOException {
363
- return new EmbeddedPostgres (config , localeConfig , bindMounts , network , networkAlias , image , pgStartupWait , databaseName );
387
+ return new EmbeddedPostgres (config , localeConfig , bindMounts , network , networkAlias , fixedPort , image , pgStartupWait , databaseName );
364
388
}
365
389
366
390
@ Override
0 commit comments