Skip to content

Commit

Permalink
feat: allow unknown properties in connection url with lenient mode (#284
Browse files Browse the repository at this point in the history
)

* 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>
  • Loading branch information
olavloite and yoshi-code-bot committed Dec 18, 2020
1 parent 9f559d8 commit 0e557ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java
Expand Up @@ -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);
}
}
}

0 comments on commit 0e557ef

Please sign in to comment.