From 33cf006288991c5cb2ddb4ca833dd90869a39e16 Mon Sep 17 00:00:00 2001 From: Michael Bell Date: Wed, 19 Jan 2022 07:30:48 -0800 Subject: [PATCH] A few miscellaneous fixes: 1. Java 8 compatibility for now - see changes in JdbcUrlUtils 2. Change POM version to 1.00.RC2-SNAPSHOT --- README.md | 2 +- pom.xml | 3 +-- .../opentable/db/postgres/embedded/JdbcUrlUtils.java | 11 ++++++----- .../db/postgres/embedded/PreparedDbProvider.java | 3 ++- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e53b851f..516ecbcd 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ OpenTable Embedded PostgreSQL Component ======================================= -Note: This library requires Java 11+. +Note: This library requires Java 8+. Allows embedding PostgreSQL into Java application code, using Docker containers. Excellent for allowing you to unit diff --git a/pom.xml b/pom.xml index a187d3af..8c280fed 100644 --- a/pom.xml +++ b/pom.xml @@ -32,11 +32,10 @@ com.opentable.components otj-pg-embedded - 1.0.1.RC1-SNAPSHOT + 1.0.0.RC2-SNAPSHOT Embedded PostgreSQL driver - 11 1800 true false diff --git a/src/main/java/com/opentable/db/postgres/embedded/JdbcUrlUtils.java b/src/main/java/com/opentable/db/postgres/embedded/JdbcUrlUtils.java index 1d3fa594..346ec670 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/JdbcUrlUtils.java +++ b/src/main/java/com/opentable/db/postgres/embedded/JdbcUrlUtils.java @@ -13,15 +13,16 @@ */ package com.opentable.db.postgres.embedded; +import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import java.util.stream.Stream; final class JdbcUrlUtils { @@ -50,17 +51,17 @@ static int getPort(final String url) { * @return Modified Url * @throws URISyntaxException If Url violates RFC 2396 */ - static String addUsernamePassword(final String url, final String userName, final String password) throws URISyntaxException { + static String addUsernamePassword(final String url, final String userName, final String password) throws URISyntaxException, UnsupportedEncodingException { final URI uri = URI.create(url.substring(JDBC_URL_PREFIX.length())); final Map params = new HashMap<>( Optional.ofNullable(uri.getQuery()) - .stream() + .map(Stream::of).orElse(Stream.empty())// Hack around the fact Optional.Stream requires Java 9+. .flatMap(par -> Arrays.stream(par.split("&"))) .map(part -> part.split("=")) .filter(part -> part.length > 1) .collect(Collectors.toMap(part -> part[0], part -> part[1]))); - params.put("user", URLEncoder.encode(userName, StandardCharsets.UTF_8)); - params.put("password", URLEncoder.encode(password, StandardCharsets.UTF_8)); + params.put("user", URLEncoder.encode(userName, "UTF-8")); // Use the old form for Java 8 compatibility. + params.put("password", URLEncoder.encode(password, "UTF-8")); return JDBC_URL_PREFIX + new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), diff --git a/src/main/java/com/opentable/db/postgres/embedded/PreparedDbProvider.java b/src/main/java/com/opentable/db/postgres/embedded/PreparedDbProvider.java index 8427fb14..f63faed4 100644 --- a/src/main/java/com/opentable/db/postgres/embedded/PreparedDbProvider.java +++ b/src/main/java/com/opentable/db/postgres/embedded/PreparedDbProvider.java @@ -14,6 +14,7 @@ package com.opentable.db.postgres.embedded; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; import java.sql.Connection; import java.sql.PreparedStatement; @@ -100,7 +101,7 @@ public String createDatabase() throws SQLException { } try { return JdbcUrlUtils.addUsernamePassword(info.getUrl(), info.getUser(), info.getPassword()); - } catch (URISyntaxException e) { + } catch (URISyntaxException | UnsupportedEncodingException e) { throw new SQLException(e); } }