use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testInvokeMethodOnClosedConnection.
private void testInvokeMethodOnClosedConnection(Method method, Object... args) throws SQLException, IllegalAccessException, IllegalArgumentException {
ConnectionOptions options = mockOptions();
JdbcConnection connection = createConnection(options);
connection.close();
boolean valid = false;
try {
method.invoke(connection, args);
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof JdbcSqlException && ((JdbcSqlException) e.getTargetException()).getCode() == Code.FAILED_PRECONDITION && ((JdbcSqlException) e.getTargetException()).getMessage().endsWith("has been closed")) {
// this is the expected exception
valid = true;
}
}
assertWithMessage("Method did not throw exception on closed connection: " + method.getName()).that(valid).isTrue();
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testCatalog.
@Test
public void testCatalog() throws SQLException {
ConnectionOptions options = mockOptions();
when(options.getDatabaseName()).thenReturn("test");
try (JdbcConnection connection = createConnection(options)) {
// The connection should always return the empty string as the current catalog, as no other
// catalogs exist in the INFORMATION_SCHEMA.
assertThat(connection.getCatalog()).isEqualTo("");
// This should be allowed.
connection.setCatalog("");
try {
// This should cause an exception.
connection.setCatalog("other");
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testSetClientInfo.
@Test
public void testSetClientInfo() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
try (ResultSet validProperties = connection.getMetaData().getClientInfoProperties()) {
while (validProperties.next()) {
assertThat((Object) connection.getWarnings()).isNull();
String name = validProperties.getString("NAME");
connection.setClientInfo(name, "new-client-info-value");
assertThat((Object) connection.getWarnings()).isNull();
assertThat(connection.getClientInfo(name)).isEqualTo("new-client-info-value");
Properties props = new Properties();
props.setProperty(name.toLowerCase(), "some-other-value");
connection.setClientInfo(props);
assertThat((Object) connection.getWarnings()).isNull();
assertThat(connection.getClientInfo(name)).isEqualTo("some-other-value");
assertThat(connection.getClientInfo().keySet()).hasSize(1);
for (String key : connection.getClientInfo().stringPropertyNames()) {
if (key.equals(name)) {
assertThat(connection.getClientInfo().getProperty(key)).isEqualTo("some-other-value");
} else {
assertThat(connection.getClientInfo().getProperty(key)).isEqualTo("");
}
}
}
}
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testGetCommitResponse_throwsSqlException.
@Test
public void testGetCommitResponse_throwsSqlException() {
ConnectionOptions options = mockOptions();
com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
when(options.getConnection()).thenReturn(spannerConnection);
Mockito.doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "test exception")).when(spannerConnection).setReturnCommitStats(any(boolean.class));
try (JdbcConnection connection = new JdbcConnection("jdbc:cloudspanner://localhost/projects/project/instances/instance/databases/database;credentialsUrl=url", options)) {
connection.setReturnCommitStats(true);
fail("missing expected exception");
} catch (SQLException e) {
assertTrue(e instanceof JdbcSqlException);
assertEquals(Code.FAILED_PRECONDITION, ((JdbcSqlException) e).getCode());
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testWarnings.
@Test
public void testWarnings() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
assertThat((Object) connection.getWarnings()).isNull();
// Push one warning and get it twice.
connection.pushWarning(new SQLWarning("test"));
assertThat(connection.getWarnings().getMessage()).isEqualTo("test");
assertThat(connection.getWarnings().getMessage()).isEqualTo("test");
// Clear warnings and push two warnings and get them both.
connection.clearWarnings();
connection.pushWarning(new SQLWarning("test 1"));
connection.pushWarning(new SQLWarning("test 2"));
assertThat(connection.getWarnings().getMessage()).isEqualTo("test 1");
assertThat(connection.getWarnings().getMessage()).isEqualTo("test 1");
assertThat(connection.getWarnings().getNextWarning().getMessage()).isEqualTo("test 2");
// Clear warnings.
connection.clearWarnings();
assertThat((Object) connection.getWarnings()).isNull();
}
}
Aggregations