YamlShardingWithMasterSlaveIntegrateTest.java 3.5 KB
Newer Older
G
gaohongtao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>
 */

T
terrymanu 已提交
18
package io.shardingjdbc.core.yaml.sharding;
G
gaohongtao 已提交
19 20 21 22

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
H
haocao 已提交
23
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
T
terrymanu 已提交
24
import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest;
G
gaohongtao 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
39 40
import java.util.HashMap;
import java.util.Map;
G
gaohongtao 已提交
41 42 43

@RunWith(Parameterized.class)
@RequiredArgsConstructor
44
public class YamlShardingWithMasterSlaveIntegrateTest extends AbstractYamlDataSourceTest {
G
gaohongtao 已提交
45 46 47 48 49 50 51 52
    
    private final String filePath;
    
    private final boolean hasDataSource;
    
    @Parameterized.Parameters(name = "{index}:{0}-{1}")
    public static Collection init() {
        return Arrays.asList(new Object[][]{
T
terrymanu 已提交
53 54 55 56
                {"/yaml/integrate/sharding_ms/configWithDataSourceWithoutProps.yaml", true},
                {"/yaml/integrate/sharding_ms/configWithoutDataSourceWithoutProps.yaml", false},
                {"/yaml/integrate/sharding_ms/configWithDataSourceWithProps.yaml", true},
                {"/yaml/integrate/sharding_ms/configWithoutDataSourceWithProps.yaml", false},
G
gaohongtao 已提交
57 58 59 60 61
        });
    }
    
    @Test
    public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
62
        File yamlFile = new File(YamlShardingWithMasterSlaveIntegrateTest.class.getResource(filePath).toURI());
G
gaohongtao 已提交
63 64
        DataSource dataSource;
        if (hasDataSource) {
H
haocao 已提交
65
            dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);
G
gaohongtao 已提交
66
        } else {
67
            Map<String, DataSource> dataSourceMap = Maps.asMap(Sets.newHashSet("db0_master", "db0_slave", "db1_master", "db1_slave"), new Function<String, DataSource>() {
G
gaohongtao 已提交
68 69 70 71
                @Override
                public DataSource apply(final String key) {
                    return createDataSource(key);
                }
72 73 74 75 76
            });
            Map<String, DataSource> result = new HashMap<>();
            for (Map.Entry<String, DataSource> each : dataSourceMap.entrySet()) {
                result.put(each.getKey(), each.getValue());
            }
H
haocao 已提交
77
            dataSource = ShardingDataSourceFactory.createDataSource(result, yamlFile);
G
gaohongtao 已提交
78 79 80
        }
        try (Connection conn = dataSource.getConnection();
             Statement stm = conn.createStatement()) {
81
            stm.execute(String.format("INSERT INTO t_order(user_id,status) values(%d, %s)", 10, "'insert'"));
G
gaohongtao 已提交
82 83 84 85 86 87
            stm.executeQuery("SELECT * FROM t_order");
            stm.executeQuery("SELECT * FROM t_order_item");
            stm.executeQuery("SELECT * FROM config");
        }
    }
}