/*
 * Copyright 1999-2015 dangdang.com.
 * <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.shardingjdbc.core.integrate.type.ms;

import io.shardingjdbc.core.common.base.AbstractSQLAssertTest;
import io.shardingjdbc.core.common.env.ShardingTestStrategy;
import io.shardingjdbc.core.integrate.jaxb.SQLShardingRule;
import io.shardingjdbc.core.rule.MasterSlaveRule;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.hint.HintManagerHolder;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import org.junit.After;

import javax.sql.DataSource;
import java.io.File;
import java.net.URL;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class MasterSlaveOnlyTest extends AbstractSQLAssertTest {
    
    private static Map<DatabaseType, MasterSlaveDataSource> masterSlaveDataSources = new HashMap<>();
    
    public MasterSlaveOnlyTest(final String testCaseName, final String sql, final DatabaseType type, final List<SQLShardingRule> sqlShardingRules) {
        super(testCaseName, sql, type, sqlShardingRules);
    }
    
    @Override
    protected File getExpectedFile(final String expected) {
        String expectedFile = null == expected ? "integrate/dataset/EmptyTable.xml"
                : String.format("integrate/dataset/masterslave/expect/" + expected, getShardingStrategy());
        URL url = AbstractSQLAssertTest.class.getClassLoader().getResource(expectedFile);
        if (null == url) {
            throw new RuntimeException("Wrong expected file:" + expectedFile);
        }
        return new File(url.getPath());
    }
    
    @Override
    protected ShardingTestStrategy getShardingStrategy() {
        return ShardingTestStrategy.masterslaveonly;
    }
    
    @Override
    protected List<String> getInitDataSetFiles() {
        return Arrays.asList("integrate/dataset/masterslave/init/master_only.xml", "integrate/dataset/masterslave/init/slave_only.xml");
    }
    
    @Override
    protected final Map<DatabaseType, MasterSlaveDataSource> getDataSources() throws SQLException {
        Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
        for (Entry<DatabaseType, Map<String, DataSource>> each : dataSourceMap.entrySet()) {
            masterSlaveDataSources.put(each.getKey(), getMasterSlaveDataSource(each.getValue()));
        }
        return masterSlaveDataSources;
    }
    
    private MasterSlaveDataSource getMasterSlaveDataSource(final Map<String, DataSource> masterSlaveDataSourceMap) throws SQLException {
        Map<String, DataSource> slaveDs = new HashMap<>(1, 1);
        slaveDs.put("dataSource_slave_only", masterSlaveDataSourceMap.get("dataSource_slave_only"));
        return new MasterSlaveDataSource(new MasterSlaveRule("ms_only", "dataSource_master_only", masterSlaveDataSourceMap.get("dataSource_master_only"), slaveDs));
    }
    
    @After
    public final void clearFlag() {
        HintManagerHolder.clear();
        MasterSlaveDataSource.resetDMLFlag();
    }
}

@@ -689,212 +689,233 @@
 ore.
-common.base.AbstractSQLAssertTest;%0Aimport io.shardingjdbc.core.common.env.ShardingTestStrategy;%0Aimport io.shardingjdbc.core.integrate.jaxb.SQLShardingRule;%0Aimport io.shardingjdbc.core.rule.MasterSlaveRule
+api.MasterSlaveDataSourceFactory;%0Aimport io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;%0Aimport io.shardingjdbc.core.common.base.AbstractSQLAssertTest;%0Aimport io.shardingjdbc.core.common.env.ShardingTestStrategy
 ;%0Aim
@@ -1015,16 +1015,76 @@
 Holder;%0A
+import io.shardingjdbc.core.integrate.jaxb.SQLShardingRule;%0A
 import i
@@ -1298,16 +1298,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -3302,133 +3302,302 @@
   Ma
-p%3CString, DataSource%3E slaveDs
+sterSlaveRuleConfiguration masterSlaveRuleConfig
  = new 
-HashMap%3C%3E(1, 1);%0A        slaveDs.put(%22dataSource_slave_only%22, masterSlaveDataSourceMap.ge
+MasterSlaveRuleConfiguration();%0A        masterSlaveRuleConfig.setName(%22ds_ms%22);%0A        masterSlaveRuleConfig.setMasterDataSourceName(%22dataSource_master_only%22);%0A        masterSlaveRuleConfig.setSlaveDataSourceNames(Collections.singletonLis
 t(%22d
@@ -3636,20 +3636,17 @@
  return 
-new 
+(
 MasterSl
@@ -3662,13 +3662,9 @@
 urce
-(new 
+)
 Mast
@@ -3674,81 +3674,55 @@
 lave
-Rule(%22ms_only%22, %22dataSource_master_only%22, masterSlaveDataSourceMap.get(%22d
+DataSourceFactory.createDataSource(masterSlaveD
 ataS
@@ -3730,32 +3730,34 @@
 urce
-_
+Map, 
 master
-_only%22), slaveDs)
+SlaveRuleConfig
 );%0A 

Update test cases.