ShardingDatabaseOnlyTest.java 5.0 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.SqlShardingRule;
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
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
38

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

47 48 49
@RunWith(Parameterized.class)
@Ignore
public class ShardingDatabaseOnlyTest extends AbstractSqlAssertTest {
50 51 52
    
    private static boolean isShutdown;
    
53
    private static Map<DatabaseType, ShardingDataSource> shardingDataSources = new HashMap<>();
54
    
55 56
    public ShardingDatabaseOnlyTest(final String testCaseName, final String sql, final Set<DatabaseType> types, final List<SqlShardingRule> sqlShardingRules) {
        super(testCaseName, sql, types, sqlShardingRules);
57 58
    }
    
59 60
    @Override
    protected ShardingTestStrategy getShardingStrategy() {
61
        return ShardingTestStrategy.db;
62 63
    }
    
64 65
    @Override
    protected List<String> getDataSetFiles() {
66 67 68 69 70 71 72 73 74 75 76
        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");
77 78 79
    }
    
    @Override
80
    protected final Map<DatabaseType, ShardingDataSource> getShardingDataSources() {
81 82
        if (!shardingDataSources.isEmpty() && !isShutdown) {
            return shardingDataSources;
83 84
        }
        isShutdown = false;
85 86 87 88 89 90 91 92 93 94
        Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
        for (Map.Entry<DatabaseType, Map<String, DataSource>> each : dataSourceMap.entrySet()) {
            DataSourceRule dataSourceRule = new DataSourceRule(each.getValue());
            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))
                    .bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
                    .databaseShardingStrategy(new DatabaseShardingStrategy(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm()))
                    .tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).build();
            shardingDataSources.put(each.getKey(), new ShardingDataSource(shardingRule));
95 96
        }
        return shardingDataSources;
97 98 99 100 101
    }
    
    @AfterClass
    public static void clear() {
        isShutdown = true;
102
        if (!shardingDataSources.isEmpty()) {
103
            for (ShardingDataSource each : shardingDataSources.values()) {
104 105
                each.close();
            }
106 107 108
        }
    }
}