ShardingDatabaseOnlyTest.java 5.5 KB
Newer Older
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>
 */

18
package com.dangdang.ddframe.rdb.common.sql;
19

20
import com.dangdang.ddframe.rdb.common.jaxb.SqlAssertData;
21
import com.dangdang.ddframe.rdb.common.sql.base.AbstractSqlAssertTest;
22 23
import com.dangdang.ddframe.rdb.common.sql.common.ShardingTestStrategy;
import com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm;
24 25 26 27 28
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
29
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
30
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
31
import com.dangdang.ddframe.rdb.sharding.constant.DatabaseType;
32
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
33
import com.dangdang.ddframe.rdb.sharding.keygen.fixture.IncrementKeyGenerator;
34
import org.junit.AfterClass;
35 36 37 38
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
39

40
import javax.sql.DataSource;
41
import java.util.Arrays;
42
import java.util.Collection;
43
import java.util.Collections;
44
import java.util.HashMap;
45
import java.util.List;
46
import java.util.Map;
47
import java.util.Set;
48

49 50 51
@RunWith(Parameterized.class)
@Ignore
public class ShardingDatabaseOnlyTest extends AbstractSqlAssertTest {
52 53 54
    
    private static boolean isShutdown;
    
55
    private static Map<DatabaseType, ShardingDataSource> shardingDataSources = new HashMap<>();
56
    
57
    public ShardingDatabaseOnlyTest(final String testCaseName, final String sql, final Set<DatabaseType> types, final List<SqlAssertData> data) {
58
        super(testCaseName, sql, types, data);
59 60
    }
    
61 62 63 64 65
    @Override
    protected ShardingTestStrategy getShardingStrategy() {
        return ShardingTestStrategy.tbl;
    }
    
66 67
    @Override
    protected List<String> getDataSetFiles() {
68 69 70 71 72 73 74 75 76 77 78
        return Arrays.asList(
                "integrate/dataset/db/init/db_0.xml",
                "integrate/dataset/db/init/db_1.xml",
                "integrate/dataset/db/init/db_2.xml",
                "integrate/dataset/db/init/db_3.xml",
                "integrate/dataset/db/init/db_4.xml",
                "integrate/dataset/db/init/db_5.xml",
                "integrate/dataset/db/init/db_6.xml",
                "integrate/dataset/db/init/db_7.xml",
                "integrate/dataset/db/init/db_8.xml",
                "integrate/dataset/db/init/db_9.xml");
79 80 81
    }
    
    @Override
82
    protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
83 84
        if (!shardingDataSources.isEmpty() && !isShutdown) {
            return shardingDataSources;
85 86
        }
        isShutdown = false;
87 88 89 90 91 92
        Map<String, Map<DatabaseType, DataSource>> dataSourceMap = createDataSourceMap();
        for (Map.Entry<String, Map<DatabaseType, DataSource>> each : dataSourceMap.entrySet()) {
            for (Map.Entry<DatabaseType, DataSource> dataSources : each.getValue().entrySet()) {
                Map<String, DataSource> dataSource = new HashMap<>();
                dataSource.put(each.getKey(), dataSources.getValue());
                DataSourceRule dataSourceRule = new DataSourceRule(dataSource);
93 94 95
                TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).generateKeyColumn("order_id", IncrementKeyGenerator.class).build();
                TableRule orderItemTableRule = TableRule.builder("t_order_item").dataSourceRule(dataSourceRule).build();
                ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule))
96
                        .bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
97 98
                        .databaseShardingStrategy(new DatabaseShardingStrategy(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm()))
                        .tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).build();
99
                shardingDataSources.put(dataSources.getKey(), new ShardingDataSource(shardingRule));
100 101 102
            }
        }
        return shardingDataSources;
103 104 105 106 107
    }
    
    @AfterClass
    public static void clear() {
        isShutdown = true;
108
        if (!shardingDataSources.isEmpty()) {
109
            for (ShardingDataSource each : shardingDataSources.values()) {
110 111
                each.close();
            }
112 113
        }
    }
114 115 116 117 118
    
    @Parameters(name = "{0}")
    public static Collection<Object[]> dataParameters() {
        return ShardingDatabaseOnlyTest.dataParameters("integrate/assert");
    }
119
}