use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor in project sharding-jdbc by shardingjdbc.
the class BatchPreparedStatementExecutorTest method assertExecuteBatchForSinglePreparedStatementSuccess.
@Test
public void assertExecuteBatchForSinglePreparedStatementSuccess() throws SQLException {
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(preparedStatement.executeBatch()).thenReturn(new int[] { 10, 20 });
when(preparedStatement.getConnection()).thenReturn(mock(Connection.class));
BatchPreparedStatementExecutor actual = new BatchPreparedStatementExecutor(getExecutorEngine(), DatabaseType.MySQL, SQLType.DML, createPreparedStatementUnits(SQL, preparedStatement, "ds_0", 2), Arrays.asList(Collections.<Object>singletonList(1), Collections.<Object>singletonList(2)));
assertThat(actual.executeBatch(), is(new int[] { 10, 20 }));
verify(preparedStatement).executeBatch();
verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
verify(getEventCaller(), times(4)).verifyDataSource("ds_0");
verify(getEventCaller(), times(4)).verifySQL(SQL);
verify(getEventCaller(), times(2)).verifyParameters(Collections.<Object>singletonList(1));
verify(getEventCaller(), times(2)).verifyParameters(Collections.<Object>singletonList(2));
verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
verify(getEventCaller(), times(0)).verifyException(null);
}
use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor in project sharding-jdbc by shardingjdbc.
the class BatchPreparedStatementExecutorTest method assertExecuteBatchForSinglePreparedStatementFailure.
@Test
public void assertExecuteBatchForSinglePreparedStatementFailure() throws SQLException {
PreparedStatement preparedStatement = mock(PreparedStatement.class);
SQLException exp = new SQLException();
when(preparedStatement.executeBatch()).thenThrow(exp);
when(preparedStatement.getConnection()).thenReturn(mock(Connection.class));
BatchPreparedStatementExecutor actual = new BatchPreparedStatementExecutor(getExecutorEngine(), DatabaseType.MySQL, SQLType.DML, createPreparedStatementUnits(SQL, preparedStatement, "ds_0", 2), Arrays.asList(Collections.<Object>singletonList(1), Collections.<Object>singletonList(2)));
assertThat(actual.executeBatch(), is(new int[] { 0, 0 }));
verify(preparedStatement).executeBatch();
verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
verify(getEventCaller(), times(4)).verifyDataSource("ds_0");
verify(getEventCaller(), times(4)).verifySQL(SQL);
verify(getEventCaller(), times(2)).verifyParameters(Collections.<Object>singletonList(1));
verify(getEventCaller(), times(2)).verifyParameters(Collections.<Object>singletonList(2));
verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(getEventCaller(), times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
verify(getEventCaller(), times(2)).verifyException(exp);
}
use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor in project sharding-jdbc by shardingjdbc.
the class BatchPreparedStatementExecutorTest method assertExecuteBatchForMultiplePreparedStatementsSuccess.
@Test
public void assertExecuteBatchForMultiplePreparedStatementsSuccess() throws SQLException {
PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
when(preparedStatement1.executeBatch()).thenReturn(new int[] { 10, 20 });
when(preparedStatement2.executeBatch()).thenReturn(new int[] { 20, 40 });
when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
BatchPreparedStatementExecutor actual = new BatchPreparedStatementExecutor(getExecutorEngine(), DatabaseType.MySQL, SQLType.DML, createPreparedStatementUnits(SQL, preparedStatement1, "ds_0", preparedStatement2, "ds_1", 2), Arrays.asList(Collections.<Object>singletonList(1), Collections.<Object>singletonList(2)));
assertThat(actual.executeBatch(), is(new int[] { 30, 60 }));
verify(preparedStatement1).executeBatch();
verify(preparedStatement2).executeBatch();
verify(preparedStatement1).getConnection();
verify(preparedStatement2).getConnection();
verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
verify(getEventCaller(), times(4)).verifyDataSource("ds_0");
verify(getEventCaller(), times(4)).verifyDataSource("ds_1");
verify(getEventCaller(), times(8)).verifySQL(SQL);
verify(getEventCaller(), times(4)).verifyParameters(Collections.<Object>singletonList(1));
verify(getEventCaller(), times(4)).verifyParameters(Collections.<Object>singletonList(2));
verify(getEventCaller(), times(4)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(getEventCaller(), times(4)).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
verify(getEventCaller(), times(0)).verifyException(null);
}
use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor in project sharding-jdbc by shardingjdbc.
the class BatchPreparedStatementExecutorTest method assertNoPreparedStatement.
@SuppressWarnings("unchecked")
@Test
public void assertNoPreparedStatement() throws SQLException {
BatchPreparedStatementExecutor actual = new BatchPreparedStatementExecutor(getExecutorEngine(), DatabaseType.MySQL, SQLType.DML, Collections.<BatchPreparedStatementUnit>emptyList(), Arrays.asList(Collections.<Object>singletonList(1), Collections.<Object>singletonList(2)));
assertThat(actual.executeBatch(), is(new int[] { 0, 0 }));
}
use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementExecutor in project sharding-jdbc by shardingjdbc.
the class BatchPreparedStatementExecutorTest method assertExecuteBatchForMultiplePreparedStatementsFailure.
@Test
public void assertExecuteBatchForMultiplePreparedStatementsFailure() throws SQLException {
PreparedStatement preparedStatement1 = mock(PreparedStatement.class);
PreparedStatement preparedStatement2 = mock(PreparedStatement.class);
SQLException exp = new SQLException();
when(preparedStatement1.executeBatch()).thenThrow(exp);
when(preparedStatement2.executeBatch()).thenThrow(exp);
when(preparedStatement1.getConnection()).thenReturn(mock(Connection.class));
when(preparedStatement2.getConnection()).thenReturn(mock(Connection.class));
BatchPreparedStatementExecutor actual = new BatchPreparedStatementExecutor(getExecutorEngine(), DatabaseType.MySQL, SQLType.DML, createPreparedStatementUnits(SQL, preparedStatement1, "ds_0", preparedStatement2, "ds_1", 2), Arrays.asList(Collections.<Object>singletonList(1), Collections.<Object>singletonList(2)));
assertThat(actual.executeBatch(), is(new int[] { 0, 0 }));
verify(preparedStatement1).executeBatch();
verify(preparedStatement2).executeBatch();
verify(preparedStatement1).getConnection();
verify(preparedStatement2).getConnection();
verify(getEventCaller(), times(2)).verifySQLType(SQLType.DML);
verify(getEventCaller(), times(4)).verifyDataSource("ds_0");
verify(getEventCaller(), times(4)).verifyDataSource("ds_1");
verify(getEventCaller(), times(8)).verifySQL(SQL);
verify(getEventCaller(), times(4)).verifyParameters(Collections.<Object>singletonList(1));
verify(getEventCaller(), times(4)).verifyParameters(Collections.<Object>singletonList(2));
verify(getEventCaller(), times(4)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
verify(getEventCaller(), times(4)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
verify(getEventCaller(), times(4)).verifyException(exp);
}
Aggregations