From 0e557ef7657cae04d263daa6717ee34290338b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Fri, 18 Dec 2020 06:43:12 +0100 Subject: [PATCH] feat: allow unknown properties in connection url with lenient mode (#284) * feat: allow unknown properties in connection url with lenient mode * Update src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com> * fix: add credentials to prevent the use of env credentials Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com> --- .../google/cloud/spanner/jdbc/JdbcDriver.java | 7 ++++- .../cloud/spanner/jdbc/JdbcDriverTest.java | 28 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java index 65b944db..d2a2fea2 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java @@ -28,6 +28,7 @@ import java.sql.DriverPropertyInfo; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; +import java.sql.SQLWarning; import java.util.Map.Entry; import java.util.Properties; import java.util.logging.Logger; @@ -172,7 +173,11 @@ public Connection connect(String url, Properties info) throws SQLException { // Connection API String connectionUri = appendPropertiesToUrl(url.substring(5), info); ConnectionOptions options = ConnectionOptions.newBuilder().setUri(connectionUri).build(); - return new JdbcConnection(url, options); + JdbcConnection connection = new JdbcConnection(url, options); + if (options.getWarnings() != null) { + connection.pushWarning(new SQLWarning(options.getWarnings())); + } + return connection; } } catch (SpannerException e) { throw JdbcSqlExceptionFactory.of(e); diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java index 9eb10474..014d9bde 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java @@ -161,4 +161,32 @@ public String apply(DriverPropertyInfo input) { }); assertThat(driverPropertyNames).containsExactlyElementsIn(validConnectionPropertyNames); } + + @Test + public void testLenient() throws SQLException { + // With lenient=true the driver should accept unknown properties and only generate a warning. + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;lenient=true;foo=bar", + server.getPort(), TEST_KEY_PATH))) { + assertThat(connection.isClosed()).isFalse(); + assertThat((Throwable) connection.getWarnings()).isNotNull(); + assertThat(connection.getWarnings().getMessage()).contains("foo"); + } + + // Without lenient the driver should throw an exception for unknown properties. + try (Connection connection = + DriverManager.getConnection( + String.format( + "jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;foo=bar", + server.getPort(), TEST_KEY_PATH))) { + fail("missing expected exception"); + } catch (SQLException e) { + assertThat((Throwable) e).isInstanceOf(JdbcSqlException.class); + JdbcSqlException jdbc = (JdbcSqlException) e; + assertThat(jdbc.getMessage()).contains("foo"); + assertThat(jdbc.getCode()).isEqualTo(Code.INVALID_ARGUMENT); + } + } }