From ba637ba914e1731e4040db47ee85661359fd65e9 Mon Sep 17 00:00:00 2001 From: Dmitry Kaukov Date: Fri, 14 Jan 2022 13:32:43 +1100 Subject: [PATCH] Refactoring (URIBuilder) 1) timeout tweaks 2) removed URIBuilder usage --- pom.xml | 8 +---- .../db/postgres/embedded/ConnectionInfo.java | 1 - .../postgres/embedded/EmbeddedPostgres.java | 23 +++++++++---- .../postgres/embedded/PreparedDbProvider.java | 32 +++++++++++++++---- .../embedded/PreparedDbCustomizerTest.java | 7 ++-- 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 12fb00e6..edbdc300 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,7 @@ Embedded PostgreSQL driver - 1.8 + 11 1800 true false @@ -87,12 +87,6 @@ org.testcontainers testcontainers - - com.github.docker-java - docker-java-transport-zerodep - 3.2.12 - - junit diff --git a/src/main/java/com/opentable/db/postgres/embedded/ConnectionInfo.java b/src/main/java/com/opentable/db/postgres/embedded/ConnectionInfo.java index d4243738..9e0c38ab 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/ConnectionInfo.java +++ b/src/main/java/com/opentable/db/postgres/embedded/ConnectionInfo.java @@ -30,7 +30,6 @@ public String getUser() { return user; } - public String getUrl() { return url; } diff --git a/src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java b/src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java index e0683521..9858c090 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java +++ b/src/main/java/com/opentable/db/postgres/embedded/EmbeddedPostgres.java @@ -18,6 +18,7 @@ import java.io.Closeable; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.sql.SQLException; import java.time.Duration; @@ -32,8 +33,6 @@ import javax.sql.DataSource; -import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.net.URIBuilder; - import org.postgresql.ds.PGSimpleDataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,7 +43,7 @@ public class EmbeddedPostgres implements Closeable { private static final Logger LOG = LoggerFactory.getLogger(EmbeddedPostgres.class); - private static final Duration DEFAULT_PG_STARTUP_WAIT = Duration.ofSeconds(10); + static final Duration DEFAULT_PG_STARTUP_WAIT = Duration.ofSeconds(30); static final String POSTGRES = "postgres"; // There are 3 defaults. @@ -56,6 +55,7 @@ public class EmbeddedPostgres implements Closeable { // 3) Otherwise we'll just pull from docker hub with the DOCKER_DEFAULT_TAG static final DockerImageName DOCKER_DEFAULT_IMAGE_NAME = DockerImageName.parse(POSTGRES); static final String DOCKER_DEFAULT_TAG = "13-alpine"; + static final String JDBC_URL_PREFIX = "jdbc:"; // Note you can override any of these defaults explicitly in the builder. private final PostgreSQLContainer postgreDBContainer; @@ -151,12 +151,21 @@ public DataSource getDatabase(String userName, String dbName, Map params = new HashMap<>( + Optional.ofNullable(uri.getQuery()) + .stream() + .flatMap(i -> Arrays.stream(i.split("&"))) + .map(i -> i.split("=")) + .filter(i -> i.length > 1) + .collect(Collectors.toMap(i -> i[0], i -> i[1]))); + params.put("user", URLEncoder.encode(info.getUser(), StandardCharsets.UTF_8)); + params.put("password", URLEncoder.encode(info.getPassword(), StandardCharsets.UTF_8)); + return JDBC_URL_PREFIX + new URI(uri.getScheme(), + uri.getUserInfo(), + uri.getHost(), + uri.getPort(), + uri.getPath(), + params.entrySet().stream().map(i -> i.getKey() + "=" + i.getValue()).collect(Collectors.joining("&")), + uri.getFragment()); } catch (URISyntaxException e) { throw new SQLException(e); } diff --git a/src/test/java/com/opentable/db/postgres/embedded/PreparedDbCustomizerTest.java b/src/test/java/com/opentable/db/postgres/embedded/PreparedDbCustomizerTest.java index 3f2a1045..1978164f 100644 --- a/src/test/java/com/opentable/db/postgres/embedded/PreparedDbCustomizerTest.java +++ b/src/test/java/com/opentable/db/postgres/embedded/PreparedDbCustomizerTest.java @@ -20,6 +20,7 @@ import java.time.Duration; +import static com.opentable.db.postgres.embedded.EmbeddedPostgres.DEFAULT_PG_STARTUP_WAIT; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; @@ -32,11 +33,11 @@ public class PreparedDbCustomizerTest { @Rule public PreparedDbRule dbA2 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> {}); @Rule - public PreparedDbRule dbA3 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(Duration.ofSeconds(10))); + public PreparedDbRule dbA3 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(DEFAULT_PG_STARTUP_WAIT)); @Rule - public PreparedDbRule dbB1 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(Duration.ofSeconds(11))); + public PreparedDbRule dbB1 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(Duration.ofSeconds(DEFAULT_PG_STARTUP_WAIT.getSeconds() + 1))); @Rule - public PreparedDbRule dbB2 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(Duration.ofSeconds(11))); + public PreparedDbRule dbB2 = EmbeddedPostgresRules.preparedDatabase(EMPTY_PREPARER).customize(builder -> builder.setPGStartupWait(Duration.ofSeconds(DEFAULT_PG_STARTUP_WAIT.getSeconds() + 1))); @Test public void testCustomizers() {