Search in sources :

Example 21 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteWithColumnNames.

@Test
public void assertExecuteWithColumnNames() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    when(statement.execute(DELETE_FROM_DUAL, new String[] { "col" })).thenReturn(false);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertFalse(actual.execute(new String[] { "col" }));
    verify(statement).execute(DELETE_FROM_DUAL, new String[] { "col" });
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 22 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteQueryForMultipleStatementsFailure.

@Test
public void assertExecuteQueryForMultipleStatementsFailure() throws SQLException {
    Statement statement1 = mock(Statement.class);
    Statement statement2 = mock(Statement.class);
    SQLException exp = new SQLException();
    when(statement1.executeQuery(SELECT_FROM_DUAL)).thenThrow(exp);
    when(statement2.executeQuery(SELECT_FROM_DUAL)).thenThrow(exp);
    when(statement1.getConnection()).thenReturn(mock(Connection.class));
    when(statement2.getConnection()).thenReturn(mock(Connection.class));
    StatementExecutor actual = new StatementExecutor(executorEngine);
    StatementExecutorWrapper wrapper1 = createStatementExecutorWrapperForDQL(statement1, "ds_0");
    actual.addStatement(wrapper1);
    StatementExecutorWrapper wrapper2 = createStatementExecutorWrapperForDQL(statement2, "ds_1");
    actual.addStatement(wrapper2);
    List<ResultSet> actualResultSets = actual.executeQuery();
    assertThat(actualResultSets, is(Arrays.asList((ResultSet) null, null)));
    verify(statement1).executeQuery(SELECT_FROM_DUAL);
    verify(statement2).executeQuery(SELECT_FROM_DUAL);
    verify(statement1).getConnection();
    verify(statement2).getConnection();
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifyDataSource("ds_1");
    verify(eventCaller, times(4)).verifySQL(SELECT_FROM_DUAL);
    verify(eventCaller, times(4)).verifyParameters(Collections.emptyList());
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller, times(2)).verifyEventExecutionType(EventExecutionType.EXECUTE_FAILURE);
    verify(eventCaller, times(2)).verifyException(exp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 23 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteUpdateForSingleStatementSuccess.

@Test
public void assertExecuteUpdateForSingleStatementSuccess() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    when(statement.executeUpdate(DELETE_FROM_DUAL)).thenReturn(10);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertThat(actual.executeUpdate(), is(10));
    verify(statement).executeUpdate(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Example 24 with StatementExecutorWrapper

use of com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper in project sharding-jdbc by dangdangdotcom.

the class StatementExecutorTest method assertExecuteForSingleStatementSuccessWithDML.

@Test
public void assertExecuteForSingleStatementSuccessWithDML() throws SQLException {
    Statement statement = mock(Statement.class);
    StatementExecutorWrapper wrapper = createStatementExecutorWrapperForDML(statement, "ds_0");
    when(statement.execute(DELETE_FROM_DUAL)).thenReturn(false);
    StatementExecutor actual = new StatementExecutor(executorEngine);
    actual.addStatement(wrapper);
    assertFalse(actual.execute());
    verify(statement).execute(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyDataSource("ds_0");
    verify(eventCaller, times(2)).verifySQL(DELETE_FROM_DUAL);
    verify(eventCaller, times(2)).verifyParameters(Collections.emptyList());
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.BEFORE_EXECUTE);
    verify(eventCaller).verifyEventExecutionType(EventExecutionType.EXECUTE_SUCCESS);
    verify(eventCaller, times(0)).verifyException(null);
}
Also used : Statement(java.sql.Statement) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper) Test(org.junit.Test)

Aggregations

StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)24 Statement (java.sql.Statement)21 Test (org.junit.Test)20 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)4 Context (com.codahale.metrics.Timer.Context)3 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)3 StatementExecutor (com.dangdang.ddframe.rdb.sharding.executor.StatementExecutor)1 SQLExecutionUnit (com.dangdang.ddframe.rdb.sharding.router.SQLExecutionUnit)1 SQLRouteResult (com.dangdang.ddframe.rdb.sharding.router.SQLRouteResult)1