Skip to content

Commit

Permalink
Refactoring (URIBuilder)
Browse files Browse the repository at this point in the history
1) timeout tweaks
2) removed URIBuilder usage
  • Loading branch information
dkaukov committed Jan 14, 2022
1 parent c3d5d7c commit ba637ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<description>Embedded PostgreSQL driver</description>

<properties>
<project.build.targetJdk>1.8</project.build.targetJdk>
<project.build.targetJdk>11</project.build.targetJdk>
<basepom.test.timeout>1800</basepom.test.timeout>
<basepom.oss.skip-scala-doc>true</basepom.oss.skip-scala-doc>
<basepom.check.skip-javadoc>false</basepom.check.skip-javadoc>
Expand Down Expand Up @@ -87,12 +87,6 @@
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-zerodep</artifactId>
<version>3.2.12</version>
</dependency>


<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public String getUser() {
return user;
}


public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -151,12 +151,21 @@ public DataSource getDatabase(String userName, String dbName, Map<String, String
return ds;
}

/**
* Returns JDBC connection string for specified database
* @param dbName Database name
* @return URL
*/
public String getJdbcUrl(String dbName) {
try {
return "jdbc:" + new URIBuilder(postgreDBContainer.getJdbcUrl().substring(5))
.setPath("/" + dbName)
.build()
.toASCIIString();
final URI uri = URI.create(postgreDBContainer.getJdbcUrl().substring(JDBC_URL_PREFIX.length()));
return JDBC_URL_PREFIX + new URI(uri.getScheme(),
uri.getUserInfo(),
uri.getHost(),
uri.getPort(),
"/" + dbName,
uri.getQuery(),
uri.getFragment());
} catch (URISyntaxException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,31 @@
*/
package com.opentable.db.postgres.embedded;

import static com.opentable.db.postgres.embedded.EmbeddedPostgres.JDBC_URL_PREFIX;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.net.URIBuilder;

import org.apache.commons.lang3.RandomStringUtils;
import org.postgresql.ds.PGSimpleDataSource;
import org.slf4j.Logger;
Expand Down Expand Up @@ -102,11 +108,23 @@ public String createDatabase() throws SQLException {
return null;
}
try {
return "jdbc:" + new URIBuilder(info.getUrl().substring(5))
.addParameter("user", info.getUser())
.addParameter("password", info.getPassword())
.build()
.toASCIIString();
final URI uri = URI.create(info.getUrl().substring(JDBC_URL_PREFIX.length()));
final Map<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand Down

2 comments on commit ba637ba

@opentable-devops
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Architecture Team / Java TC Platform / otj-pg-embedded / PR Build Build ???.164/merge.114 is now running

@opentable-devops
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Architecture Team / Java TC Platform / otj-pg-embedded / PR Build Build 1.0.0.RC1-SNAPSHOT.164/merge.114 outcome was SUCCESS
Summary: Tests passed: 13, ignored: 2 Build time: 00:01:05

Please sign in to comment.