AbstractSQLTest.java 3.6 KB
Newer Older
1
/*
2 3 4 5 6 7
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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
8 9 10 11 12 13 14 15 16 17
 *
 *     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.
 */

18
package org.apache.shardingsphere.driver.common.base;
19

ShardingSphere's avatar
ShardingSphere 已提交
20
import com.google.common.collect.Sets;
T
terrymanu 已提交
21
import org.apache.commons.dbcp2.BasicDataSource;
22
import org.apache.shardingsphere.driver.common.env.DatabaseEnvironment;
23
import org.apache.shardingsphere.infra.database.type.DatabaseType;
24
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
25
import org.h2.tools.RunScript;
T
tristaZero 已提交
26
import org.junit.BeforeClass;
27 28 29

import javax.sql.DataSource;
import java.io.InputStreamReader;
30 31
import java.sql.Connection;
import java.sql.SQLException;
T
tristaZero 已提交
32
import java.util.Arrays;
T
terrymanu 已提交
33
import java.util.HashMap;
34
import java.util.LinkedHashMap;
T
tristaZero 已提交
35
import java.util.List;
T
terrymanu 已提交
36
import java.util.Map;
L
Liang Zhang 已提交
37
import java.util.Objects;
T
terrymanu 已提交
38
import java.util.Set;
39

40
public abstract class AbstractSQLTest {
41
    
42
    private static final List<String> DB_NAMES = Arrays.asList("jdbc_0", "jdbc_1", "encrypt", "test_primary_ds", "test_replica_ds");
T
tristaZero 已提交
43
    
44
    private static final Set<DatabaseType> DATABASE_TYPES = Sets.newHashSet(DatabaseTypeRegistry.getActualDatabaseType("H2"));
45
    
L
Liang Zhang 已提交
46
    private static final Map<DatabaseType, Map<String, DataSource>> DATABASE_TYPE_MAP = new HashMap<>();
47
    
T
tristaZero 已提交
48 49
    @BeforeClass
    public static synchronized void initDataSource() {
50
        createDataSources();
H
haocao 已提交
51
    }
T
terrymanu 已提交
52
    
T
tristaZero 已提交
53 54
    private static void createDataSources() {
        for (String each : DB_NAMES) {
T
terrymanu 已提交
55
            for (DatabaseType type : DATABASE_TYPES) {
T
tristaZero 已提交
56
                createDataSources(each, type);
57 58 59 60
            }
        }
    }
    
T
terrymanu 已提交
61
    private static void createDataSources(final String dbName, final DatabaseType databaseType) {
L
Liang Zhang 已提交
62
        DATABASE_TYPE_MAP.computeIfAbsent(databaseType, key -> new LinkedHashMap<>()).put(dbName, buildDataSource(dbName, databaseType));
63
        buildSchema(dbName, databaseType);
64 65
    }
    
T
terrymanu 已提交
66
    private static BasicDataSource buildDataSource(final String dbName, final DatabaseType databaseType) {
67
        DatabaseEnvironment dbEnv = new DatabaseEnvironment(databaseType);
J
junxiong 已提交
68 69 70 71 72
        BasicDataSource result = new BasicDataSource();
        result.setDriverClassName(dbEnv.getDriverClassName());
        result.setUrl(dbEnv.getURL(dbName));
        result.setUsername(dbEnv.getUsername());
        result.setPassword(dbEnv.getPassword());
T
tristaZero 已提交
73
        result.setMaxTotal(50);
J
junxiong 已提交
74
        return result;
75 76
    }
    
77
    private static void buildSchema(final String dbName, final DatabaseType databaseType) {
T
tristaZero 已提交
78
        try {
L
Liang Zhang 已提交
79
            Connection conn = DATABASE_TYPE_MAP.get(databaseType).get(dbName).getConnection();
L
Liang Zhang 已提交
80
            RunScript.execute(conn, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream("jdbc_init.sql"))));
T
tristaZero 已提交
81 82
            conn.close();
        } catch (final SQLException ex) {
L
Liang Zhang 已提交
83
            throw new RuntimeException(ex);
84 85
        }
    }
86 87 88 89
    
    protected static Map<DatabaseType, Map<String, DataSource>> getDatabaseTypeMap() {
        return DATABASE_TYPE_MAP;
    }
90
}