use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.
the class CloudSpannerIT method performJdbcTests.
private void performJdbcTests() throws Exception {
// Get a JDBC connection
try (Connection connection = createConnection()) {
connection.setAutoCommit(false);
// Test connection validity
assertTrue(connection.isValid(0));
assertTrue(connection.isValid(1));
assertTrue(connection.isValid(1000));
// Check node count
assertEquals(1, ((CloudSpannerConnection) connection).getNodeCount());
// Test connection pooling
ConnectionPoolingTester poolingTester = new ConnectionPoolingTester();
poolingTester.testPooling((CloudSpannerConnection) connection);
// Test Table DDL statements
TableDDLTester tableDDLTester = new TableDDLTester(connection);
tableDDLTester.runCreateTests();
// Test DML statements
DMLTester dmlTester = new DMLTester(connection);
dmlTester.runDMLTests();
// Test meta data functions
MetaDataTester metaDataTester = new MetaDataTester((CloudSpannerConnection) connection);
metaDataTester.runMetaDataTests();
// Test transaction functions
TransactionTester txTester = new TransactionTester(connection);
txTester.runTransactionTests();
// Test select statements
SelectStatementsTester selectTester = new SelectStatementsTester(connection);
selectTester.runSelectTests();
// Test XA transactions
XATester xaTester = new XATester();
xaTester.testXA(getProject(), instanceId, DATABASE_ID, credentialsPath);
// Test drop statements
tableDDLTester.runDropTests();
} catch (SQLException | PropertyVetoException | AssertionError e) {
log.log(Level.WARNING, "Error during JDBC tests", e);
throw e;
}
}
use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.
the class InsertWorkerTest method createMocks.
private void createMocks(CloudSpannerConnection connection, String selectSQL, long count, String updateSQL, boolean throwExceptionOnUpdate) throws SQLException {
when(connection.createCopyConnection()).thenAnswer(new Answer<CloudSpannerConnection>() {
@Override
public CloudSpannerConnection answer(InvocationOnMock invocation) throws Throwable {
CloudSpannerConnection copy = CloudSpannerTestObjects.createConnection();
createMocks(copy, selectSQL, count, updateSQL);
return copy;
}
});
CloudSpannerPreparedStatement countStatement = mock(CloudSpannerPreparedStatement.class);
CloudSpannerResultSet countResultSet = mock(CloudSpannerResultSet.class);
when(countResultSet.next()).thenReturn(true, false);
when(countResultSet.getLong(1)).thenReturn(count);
when(countStatement.executeQuery()).thenReturn(countResultSet);
when(connection.prepareStatement("SELECT COUNT(*) AS C FROM ((" + selectSQL + ") LIMIT 5000) Q")).thenReturn(countStatement);
CloudSpannerPreparedStatement selectStatement = mock(CloudSpannerPreparedStatement.class);
CloudSpannerResultSet selectResultSet = mock(CloudSpannerResultSet.class);
CloudSpannerResultSetMetaData metadata = mock(CloudSpannerResultSetMetaData.class);
when(metadata.getColumnCount()).thenReturn(3);
when(selectResultSet.next()).then(new Answer<Boolean>() {
private long called = 0;
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
called++;
if (called <= count)
return true;
return false;
}
});
when(selectResultSet.getObject(1)).then(new Returns(1L));
when(selectResultSet.getObject(2)).then(new Returns("TWO"));
when(selectResultSet.getObject(3)).then(new Returns("TO"));
when(selectResultSet.getMetaData()).thenReturn(metadata);
when(selectStatement.executeQuery()).thenReturn(selectResultSet);
when(connection.prepareStatement(selectSQL)).thenReturn(selectStatement);
CloudSpannerPreparedStatement updateStatement = mock(CloudSpannerPreparedStatement.class);
if (throwExceptionOnUpdate)
when(updateStatement.executeUpdate()).thenThrow(SQLException.class);
else
when(updateStatement.executeUpdate()).thenReturn(1);
when(connection.prepareStatement(updateSQL)).thenReturn(updateStatement);
}
use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.
the class InsertWorkerTest method prepareAutoCommitSimpleInsert.
private CloudSpannerPreparedStatement prepareAutoCommitSimpleInsert(boolean exception) throws SQLException {
CloudSpannerPreparedStatement statement = prepareSimpleInsert(exception);
CloudSpannerConnection connection = statement.getConnection();
assertFalse(connection.getAutoCommit());
connection.setAutoCommit(true);
assertTrue(connection.getAutoCommit());
return statement;
}
use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.
the class ExtendedModeIT method test1_ExtendedModeUpdate.
@Test
public void test1_ExtendedModeUpdate() throws SQLException {
((CloudSpannerConnection) getConnection()).setAllowExtendedMode(true);
try (ResultSet rs = getConnection().createStatement().executeQuery("select * from test order by id")) {
int i = 0;
while (rs.next()) {
assertEquals(String.valueOf(i), rs.getString("name"));
i++;
}
}
PreparedStatement ps = getConnection().prepareStatement("update test set name=concat('one', name) where id < ?");
ps.setLong(1, 10L);
ps.executeUpdate();
getConnection().commit();
try (ResultSet rs = getConnection().createStatement().executeQuery("select * from test order by id")) {
int i = 0;
while (rs.next()) {
if (i < 10) {
assertEquals("one" + String.valueOf(i), rs.getString("name"));
} else {
assertEquals(String.valueOf(i), rs.getString("name"));
}
i++;
}
}
}
use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.
the class ExtendedModeIT method test5_ExtendedModeUpdateWithScaleOrLength.
@Test
public void test5_ExtendedModeUpdateWithScaleOrLength() throws SQLException {
((CloudSpannerConnection) getConnection()).setAllowExtendedMode(true);
try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=40 and name like '4%'")) {
while (rs.next()) {
assertEquals(1L, rs.getLong(1));
}
}
PreparedStatement ps = getConnection().prepareStatement("delete from test where id=? and name like coalesce(?, '5%')");
ps.setLong(1, 40L);
ps.setObject(2, "4%", Types.NVARCHAR, 100);
ps.executeUpdate();
getConnection().commit();
try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=40 and name like '4%'")) {
while (rs.next()) {
assertEquals(0L, rs.getLong(1));
}
}
}
Aggregations