/*
 * 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.jdbc.spring;

import io.shardingsphere.core.api.ConfigMapContext;
import io.shardingsphere.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm;
import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.jdbc.spring.datasource.SpringMasterSlaveDataSource;
import io.shardingsphere.jdbc.spring.util.FieldValueUtil;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@ContextConfiguration(locations = "classpath:META-INF/rdb/masterSlaveNamespace.xml")
public class MasterSlaveNamespaceTest extends AbstractJUnit4SpringContextTests {
    
    @Test
    public void assertDefaultMaserSlaveDataSource() {
        MasterSlaveRule masterSlaveRule = getMasterSlaveRule("defaultMasterSlaveDataSource");
        assertThat(masterSlaveRule.getMasterDataSourceName(), is("dbtbl_0_master"));
        assertTrue(masterSlaveRule.getSlaveDataSourceNames().contains("dbtbl_0_slave_0"));
        assertTrue(masterSlaveRule.getSlaveDataSourceNames().contains("dbtbl_0_slave_1"));
    }
    
    @Test
    public void assertTypeMasterSlaveDataSource() {
        MasterSlaveRule randomSlaveRule = getMasterSlaveRule("randomMasterSlaveDataSource");
        MasterSlaveRule roundRobinSlaveRule = getMasterSlaveRule("roundRobinMasterSlaveDataSource");
        assertTrue(randomSlaveRule.getLoadBalanceAlgorithm() instanceof RandomMasterSlaveLoadBalanceAlgorithm);
        assertTrue(roundRobinSlaveRule.getLoadBalanceAlgorithm() instanceof RoundRobinMasterSlaveLoadBalanceAlgorithm);
    }
    
    @Test
    public void assertRefMasterSlaveDataSource() {
        MasterSlaveLoadBalanceAlgorithm randomStrategy = this.applicationContext.getBean("randomStrategy", MasterSlaveLoadBalanceAlgorithm.class);
        MasterSlaveRule masterSlaveRule = getMasterSlaveRule("refMasterSlaveDataSource");
        assertTrue(masterSlaveRule.getLoadBalanceAlgorithm() == randomStrategy);
    }
    
    @Test
    public void assertDefaultShardingDataSource() {
        Map<String, DataSource> dataSourceMap = getDataSourceMap("defaultShardingDataSource");
        assertNotNull(dataSourceMap.get("dbtbl_0_master"));
        assertNotNull(dataSourceMap.get("dbtbl_0_slave_0"));
        assertNotNull(dataSourceMap.get("dbtbl_0_slave_1"));
        assertNotNull(dataSourceMap.get("dbtbl_1_master"));
        assertNotNull(dataSourceMap.get("dbtbl_1_slave_0"));
        assertNotNull(dataSourceMap.get("dbtbl_1_slave_1"));
        ShardingRule shardingRule = getShardingRule("defaultShardingDataSource");
        assertThat(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName(), is("randomMasterSlaveDataSource"));
        assertThat(shardingRule.getTableRules().size(), is(1));
        assertThat(shardingRule.getTableRules().iterator().next().getLogicTable(), is("t_order"));
    }
    
    @Test
    public void assertShardingDataSourceType() {
        assertTrue(this.applicationContext.getBean("defaultMasterSlaveDataSource", MasterSlaveDataSource.class) instanceof SpringMasterSlaveDataSource);
    }
    
    @SuppressWarnings("unchecked")
    private Map<String, DataSource> getDataSourceMap(final String shardingDataSourceName) {
        ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);
        return shardingDataSource.getDataSourceMap();
    }
    
    private ShardingRule getShardingRule(final String shardingDataSourceName) {
        ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);
        Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
        return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
    }
    
    private MasterSlaveRule getMasterSlaveRule(final String masterSlaveDataSourceName) {
        MasterSlaveDataSource masterSlaveDataSource = this.applicationContext.getBean(masterSlaveDataSourceName, MasterSlaveDataSource.class);
        return (MasterSlaveRule) FieldValueUtil.getFieldValue(masterSlaveDataSource, "masterSlaveRule", true);
    }
    
    @Test
    public void assertConfigMapDataSource() {
        Object masterSlaveDataSource = this.applicationContext.getBean("configMapDataSource");
        Map<String, Object> configMap = new HashMap<>();
        configMap.put("key1", "value1");
        assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
        assertThat(masterSlaveDataSource, instanceOf(MasterSlaveDataSource.class));
    }
}

@@ -1096,224 +1096,27 @@
 ore.
-jdbc.core.datasource.ShardingDataSource;%0Aimport io.shardingsphere.core.rule.MasterSlaveRule;%0Aimport io.shardingsphere.core.rule.ShardingRule;%0Aimport io.shardingsphere.jdbc.spring.datasource.SpringMasterSlaveDataSourc
+rule.MasterSlaveRul
 e;%0Ai
@@ -1343,37 +1343,8 @@
 s;%0A%0A
-import javax.sql.DataSource;%0A
 impo
@@ -1488,54 +1488,8 @@
 is;%0A
-import static org.junit.Assert.assertNotNull;%0A
 impo
@@ -2967,32 +2967,32 @@
 veDataSource%22);%0A
+
         assertTr
@@ -3071,1879 +3071,8 @@
    %0A
-    @Test%0A    public void assertDefaultShardingDataSource() %7B%0A        Map%3CString, DataSource%3E dataSourceMap = getDataSourceMap(%22defaultShardingDataSource%22);%0A        assertNotNull(dataSourceMap.get(%22dbtbl_0_master%22));%0A        assertNotNull(dataSourceMap.get(%22dbtbl_0_slave_0%22));%0A        assertNotNull(dataSourceMap.get(%22dbtbl_0_slave_1%22));%0A        assertNotNull(dataSourceMap.get(%22dbtbl_1_master%22));%0A        assertNotNull(dataSourceMap.get(%22dbtbl_1_slave_0%22));%0A        assertNotNull(dataSourceMap.get(%22dbtbl_1_slave_1%22));%0A        ShardingRule shardingRule = getShardingRule(%22defaultShardingDataSource%22);%0A        assertThat(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName(), is(%22randomMasterSlaveDataSource%22));%0A        assertThat(shardingRule.getTableRules().size(), is(1));%0A        assertThat(shardingRule.getTableRules().iterator().next().getLogicTable(), is(%22t_order%22));%0A    %7D%0A    %0A    @Test%0A    public void assertShardingDataSourceType() %7B%0A        assertTrue(this.applicationContext.getBean(%22defaultMasterSlaveDataSource%22, MasterSlaveDataSource.class) instanceof SpringMasterSlaveDataSource);%0A    %7D%0A    %0A    @SuppressWarnings(%22unchecked%22)%0A    private Map%3CString, DataSource%3E getDataSourceMap(final String shardingDataSourceName) %7B%0A        ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);%0A        return shardingDataSource.getDataSourceMap();%0A    %7D%0A    %0A    private ShardingRule getShardingRule(final String shardingDataSourceName) %7B%0A        ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);%0A        Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, %22shardingContext%22, true);%0A        return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, %22shardingRule%22);%0A    %7D%0A    %0A
     

modify test case