/*
 * 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.threadlocal.ExecutorExceptionHandler;
import io.shardingsphere.core.jdbc.core.ShardingContext;
import io.shardingsphere.core.jdbc.core.connection.ShardingConnection;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import lombok.AccessLevel;
import lombok.Getter;
import org.junit.After;
import org.junit.Before;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.sql.Connection;
import java.sql.SQLException;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@Getter(AccessLevel.PROTECTED)
public abstract class AbstractBaseExecutorTest {
    
    private ShardingExecuteEngine executeEngine;
    
    private ShardingConnection connection;
    
    @Mock
    private EventCaller eventCaller;
    
    private TestDQLExecutionEventListener dqlExecutionEventListener;
    
    private TestDMLExecutionEventListener dmlExecutionEventListener;
    
    private TestOverallExecutionEventListener overallExecutionEventListener;
    
    @Before
    public void setUp() throws SQLException {
        MockitoAnnotations.initMocks(this);
        ExecutorExceptionHandler.setExceptionThrown(false);
        executeEngine = new ShardingExecuteEngine(Runtime.getRuntime().availableProcessors());
        overallExecutionEventListener = new TestOverallExecutionEventListener(eventCaller);
        dqlExecutionEventListener = new TestDQLExecutionEventListener(eventCaller);
        dmlExecutionEventListener = new TestDMLExecutionEventListener(eventCaller);
        setConnection();
        register();
    }
    
    private void register() {
        ShardingEventBusInstance.getInstance().register(overallExecutionEventListener);
        ShardingEventBusInstance.getInstance().register(dqlExecutionEventListener);
        ShardingEventBusInstance.getInstance().register(dmlExecutionEventListener);
    }
    
    private void setConnection() throws SQLException {
        ShardingContext shardingContext = mock(ShardingContext.class);
        when(shardingContext.getExecuteEngine()).thenReturn(executeEngine);
        when(shardingContext.getMaxConnectionsSizePerQuery()).thenReturn(1);
        ShardingDataSource shardingDataSource = mock(ShardingDataSource.class);
        when(shardingDataSource.getShardingContext()).thenReturn(shardingContext);
        connection = new ShardingConnection(shardingDataSource);
        when(connection.getNewConnection(anyString())).thenReturn(mock(Connection.class));
    }
    
    @After
    public void tearDown() throws ReflectiveOperationException {
        ExecutorTestUtil.clear();
        ShardingEventBusInstance.getInstance().unregister(overallExecutionEventListener);
        ShardingEventBusInstance.getInstance().unregister(dqlExecutionEventListener);
        ShardingEventBusInstance.getInstance().unregister(dmlExecutionEventListener);
        executeEngine.close();
    }
}

@@ -655,16 +655,139 @@
 cutor;%0A%0A
+import io.shardingsphere.core.constant.DatabaseType;%0Aimport io.shardingsphere.core.constant.properties.ShardingProperties;%0A
 import i
@@ -1654,16 +1654,45 @@
 tions;%0A%0A
+import javax.sql.DataSource;%0A
 import j
@@ -1745,62 +1745,91 @@
 on;%0A
-%0A
 import 
-static org.mockito.ArgumentMatchers.anyString;
+java.util.LinkedHashMap;%0Aimport java.util.Map;%0Aimport java.util.Properties;%0A
 %0Aimp
@@ -2422,32 +2422,62 @@
 ows SQLException
+, ReflectiveOperationException
  %7B%0A        Mocki
@@ -3562,24 +3562,16 @@
         
-Sharding
 DataSour
@@ -3573,25 +3573,17 @@
 aSource 
-shardingD
+d
 ataSourc
@@ -3591,24 +3591,16 @@
  = mock(
-Sharding
 DataSour
@@ -3623,25 +3623,17 @@
    when(
-shardingD
+d
 ataSourc
@@ -3641,118 +3641,410 @@
 .get
-ShardingContext()).thenReturn(shardingContext);%0A        connection = new ShardingConnection(shardingDataSource
+Connection()).thenReturn(mock(Connection.class));%0A        Map%3CString, DataSource%3E dataSourceSourceMap = new LinkedHashMap%3C%3E();%0A        dataSourceSourceMap.put(%22ds_0%22, dataSource);%0A        dataSourceSourceMap.put(%22ds_1%22, dataSource);%0A        ShardingDataSource shardingDataSource = new ShardingDataSource(dataSourceSourceMap, shardingContext, new ShardingProperties(new Properties()), DatabaseType.MySQL
 );%0A 
@@ -4050,21 +4050,16 @@
         
-when(
 connecti
@@ -4064,73 +4064,52 @@
 tion
-.getNewConnection(anyString())).thenReturn(mock(Connection.class)
+ = new ShardingConnection(shardingDataSource
 );%0A 

use ds_0