/*
 * 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.shardingproxy.frontend.common.executor;

import io.netty.channel.ChannelId;
import io.netty.channel.EventLoopGroup;
import io.shardingsphere.core.constant.transaction.TransactionType;
import io.shardingsphere.shardingproxy.runtime.GlobalRegistry;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.concurrent.ExecutorService;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;

public final class ExecutorGroupTest {
    
    private TransactionType originalTransactionType = GlobalRegistry.getInstance().getTransactionType();
    
    @After
    public void tearDown() throws ReflectiveOperationException {
        setTransactionType(originalTransactionType);
    }
    
    @Test
    public void assertGetExecutorServiceWithLocal() throws ReflectiveOperationException {
        setTransactionType(TransactionType.LOCAL);
        EventLoopGroup eventLoopGroup = mock(EventLoopGroup.class);
        ChannelId channelId = mock(ChannelId.class);
        assertThat(new ExecutorGroup(eventLoopGroup, channelId).getExecutorService(), CoreMatchers.<ExecutorService>is(eventLoopGroup));
    }
    
    @Test
    public void assertGetExecutorServiceWithXA() throws ReflectiveOperationException {
        setTransactionType(TransactionType.XA);
        EventLoopGroup eventLoopGroup = mock(EventLoopGroup.class);
        ChannelId channelId = mock(ChannelId.class);
        ChannelThreadExecutorGroup.getInstance().register(channelId);
        assertThat(new ExecutorGroup(eventLoopGroup, channelId).getExecutorService(), Matchers.<ExecutorService>not(eventLoopGroup));
        assertNotNull(new ExecutorGroup(eventLoopGroup, channelId).getExecutorService());
        ChannelThreadExecutorGroup.getInstance().unregister(channelId);
    }
    
    private void setTransactionType(final TransactionType transactionType) throws ReflectiveOperationException {
        Field field = GlobalRegistry.getInstance().getClass().getDeclaredField("transactionType");
        field.setAccessible(true);
        field.set(GlobalRegistry.getInstance(), transactionType);
    }
}

@@ -755,16 +755,164 @@
 pGroup;%0A
+import io.shardingsphere.core.constant.properties.ShardingProperties;%0Aimport io.shardingsphere.core.constant.properties.ShardingPropertiesConstant;%0A
 import i
@@ -1178,16 +1178,45 @@
 .Field;%0A
+import java.util.Properties;%0A
 import j
@@ -2937,24 +2937,262 @@
 ld(%22
-transactionType%22
+shardingProperties%22);%0A        Properties props = new Properties();%0A        props.setProperty(ShardingPropertiesConstant.PROXY_TRANSACTION_TYPE.getKey(), transactionType.name());%0A        ShardingProperties shardingProperties = new ShardingProperties(props
 );%0A 

modify setTransactionType