/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.core.executor;

import io.shardingsphere.core.event.ShardingEventBusInstance;
import io.shardingsphere.core.executor.fixture.EventCaller;
import io.shardingsphere.core.executor.fixture.ExecutorTestUtil;
import io.shardingsphere.core.executor.fixture.TestDMLExecutionEventListener;
import io.shardingsphere.core.executor.fixture.TestDQLExecutionEventListener;
import io.shardingsphere.core.executor.fixture.TestOverallExecutionEventListener;
import io.shardingsphere.core.executor.sql.execute.SQLExecuteTemplate;
import io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorExceptionHandler;
import lombok.AccessLevel;
import lombok.Getter;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@Getter(AccessLevel.PROTECTED)
public abstract class AbstractBaseExecutorTest {
    
    private ShardingExecuteEngine executeEngine;
    
    private SQLExecuteTemplate executeTemplate;
    
    @Mock
    private EventCaller eventCaller;
    
    private TestDQLExecutionEventListener dqlExecutionEventListener;
    
    private TestDMLExecutionEventListener dmlExecutionEventListener;
    
    private TestOverallExecutionEventListener overallExecutionEventListener;
    
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        ExecutorExceptionHandler.setExceptionThrown(false);
        executeEngine = new ShardingExecuteEngine(Runtime.getRuntime().availableProcessors());
        executeTemplate = new SQLExecuteTemplate(executeEngine);
        overallExecutionEventListener = new TestOverallExecutionEventListener(eventCaller);
        dqlExecutionEventListener = new TestDQLExecutionEventListener(eventCaller);
        dmlExecutionEventListener = new TestDMLExecutionEventListener(eventCaller);
        ShardingEventBusInstance.getInstance().register(overallExecutionEventListener);
        ShardingEventBusInstance.getInstance().register(dqlExecutionEventListener);
        ShardingEventBusInstance.getInstance().register(dmlExecutionEventListener);
    }
    
    @After
    public void tearDown() throws ReflectiveOperationException {
        ExecutorTestUtil.clear();
        ShardingEventBusInstance.getInstance().unregister(overallExecutionEventListener);
        ShardingEventBusInstance.getInstance().unregister(dqlExecutionEventListener);
        ShardingEventBusInstance.getInstance().unregister(dmlExecutionEventListener);
        executeEngine.close();
    }
}

@@ -1135,277 +1135,599 @@
 ute.
-SQLExecuteTemplate;%0Aimport io.shardingsphere.core.executor.sql.execute.threadlocal.ExecutorExceptionHandler;%0Aimport lombok.AccessLevel;%0Aimport lombok.Getter;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations
+threadlocal.ExecutorExceptionHandler;%0Aimport io.shardingsphere.core.jdbc.core.ShardingContext;%0Aimport io.shardingsphere.core.jdbc.core.connection.ShardingConnection;%0Aimport io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;%0Aimport lombok.AccessLevel;%0Aimport lombok.Getter;%0Aimport org.junit.After;%0Aimport org.junit.Before;%0Aimport org.mockito.Mock;%0Aimport org.mockito.MockitoAnnotations;%0A%0Aimport java.sql.Connection;%0Aimport java.sql.SQLException;%0A%0Aimport static org.mockito.ArgumentMatchers.anyString;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when
 ;%0A%0A@
@@ -1880,42 +1880,44 @@
 ate 
-SQLExecuteTemplate executeTemplate
+static ShardingConnection CONNECTION
 ;%0A  
@@ -2236,16 +2236,36 @@
  setUp()
+ throws SQLException
  %7B%0A     
@@ -2462,73 +2462,8 @@
 ));%0A
-        executeTemplate = new SQLExecuteTemplate(executeEngine);%0A
     
@@ -2710,32 +2710,118 @@
 r(eventCaller);%0A
+        setConnection();%0A        register();%0A    %7D%0A    %0A    private void register() %7B%0A
         Sharding
@@ -3067,24 +3067,633 @@
 %0A    %7D%0A    %0A
+    private void setConnection() throws SQLException %7B%0A        ShardingContext shardingContext = mock(ShardingContext.class);%0A        when(shardingContext.getExecuteEngine()).thenReturn(executeEngine);%0A        when(shardingContext.getMaxConnectionsSizePerQuery()).thenReturn(1);%0A        ShardingDataSource shardingDataSource = mock(ShardingDataSource.class);%0A        when(shardingDataSource.getShardingContext()).thenReturn(shardingContext);%0A        CONNECTION = new ShardingConnection(shardingDataSource);%0A        when(CONNECTION.getNewConnection(anyString())).thenReturn(mock(Connection.class));%0A    %7D%0A    %0A
     @After%0A 

add setConnection()