use of org.apache.gobblin.writer.JdbcWriter in project incubator-gobblin by apache.
the class JdbcWriterTest method writeFailRollbackTest.
@Test
public void writeFailRollbackTest() throws SQLException, IOException {
final String database = "db";
final String table = "users";
JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class);
Connection conn = mock(Connection.class);
doThrow(RuntimeException.class).when(writerCommands).insert(anyString(), anyString(), any(JdbcEntryData.class));
JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn);
try {
writer.write(null);
Assert.fail("Test case didn't throw Exception.");
} catch (RuntimeException e) {
Assert.assertTrue(e instanceof RuntimeException);
}
writer.close();
verify(writerCommands, times(1)).insert(anyString(), anyString(), any(JdbcEntryData.class));
verify(conn, times(1)).rollback();
verify(conn, never()).commit();
verify(conn, times(1)).close();
Assert.assertEquals(writer.recordsWritten(), 0L);
}
use of org.apache.gobblin.writer.JdbcWriter in project incubator-gobblin by apache.
the class JdbcWriterTest method writeAndCommitTest.
@Test
public void writeAndCommitTest() throws SQLException, IOException {
final String database = "db";
final String table = "users";
final int writeCount = 25;
JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class);
Connection conn = mock(Connection.class);
try (JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn)) {
for (int i = 0; i < writeCount; i++) {
writer.write(null);
}
writer.commit();
Assert.assertEquals(writer.recordsWritten(), writeCount);
}
verify(writerCommands, times(writeCount)).insert(anyString(), anyString(), any(JdbcEntryData.class));
verify(conn, times(1)).commit();
verify(conn, never()).rollback();
verify(writerCommands, times(1)).flush();
verify(conn, times(1)).close();
}
Aggregations