/*
 * 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
 *
 *     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.
 */

package org.apache.shardingsphere.shardingjdbc.common.base;

import com.google.common.collect.Sets;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.shardingjdbc.common.env.DatabaseEnvironment;
import org.h2.tools.RunScript;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public abstract class AbstractSQLTest {
    
    private static final List<String> DB_NAMES = Arrays.asList("jdbc_0", "jdbc_1", "encrypt");
    
    private static Set<DatabaseType> databaseTypes = Sets.newHashSet(DatabaseType.H2);
    
    private static Map<DatabaseType, Map<String, DataSource>> databaseTypeMap = new HashMap<>();
    
    static {
        init();
    }
    
    private static synchronized void init() {
        try {
            Properties prop = new Properties();
            prop.load(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/env.properties"));
            createDataSources();
        } catch (final IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private static void createDataSources() {
        for (String each : DB_NAMES) {
            for (DatabaseType type : databaseTypes) {
                createDataSources(each, type);
                
            }
        }
    }
    
    private static void createDataSources(final String dbName, final DatabaseType type) {
        String dataSource = "dataSource_" + dbName;
        Map<String, DataSource> dataSourceMap = databaseTypeMap.get(type);
        if (null == dataSourceMap) {
            dataSourceMap = new HashMap<>();
            databaseTypeMap.put(type, dataSourceMap);
        }
        BasicDataSource result = buildDataSource(dbName, type);
        dataSourceMap.put(dataSource, result);
        createSchema(dbName, type);
    }
    
    private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) {
        DatabaseEnvironment dbEnv = new DatabaseEnvironment(type);
        BasicDataSource result = new BasicDataSource();
        result.setDriverClassName(dbEnv.getDriverClassName());
        result.setUrl(dbEnv.getURL(dbName));
        result.setUsername(dbEnv.getUsername());
        result.setPassword(dbEnv.getPassword());
        result.setMaxTotal(50);
        return result;
    }
    
    private static void createSchema(final String dbName, final DatabaseType dbType) {
        try {
            Connection conn = databaseTypeMap.get(dbType).get(dbName).getConnection();
            RunScript.execute(conn, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("integrate/cases/jdbc/jdbc_init.sql")));
            conn.close();
        } catch (final SQLException ex) {
            ex.printStackTrace();
        }
    }
}

@@ -896,16 +896,65 @@
 t.Sets;%0A
+import lombok.AccessLevel;%0Aimport lombok.Getter;%0A
 import o
@@ -990,24 +990,24 @@
 DataSource;%0A
-
 import org.a
@@ -1163,16 +1163,46 @@
 nScript;
+%0Aimport org.junit.BeforeClass;
 %0A%0Aimport
@@ -1721,32 +1721,67 @@
 eType.H2);%0A    %0A
+    @Getter(AccessLevel.PROTECTED)%0A
     private stat
@@ -1874,55 +1874,31 @@
     
-static %7B%0A        init();%0A    %7D%0A    %0A    private
+@BeforeClass%0A    public
  sta
@@ -1923,16 +1923,26 @@
 oid init
+DataSource
 () %7B%0A   
@@ -2430,25 +2430,8 @@
 e);%0A
-                %0A
     
@@ -2555,60 +2555,8 @@
 ) %7B%0A
-        String dataSource = %22dataSource_%22 + dbName;%0A
     
@@ -2863,24 +2863,20 @@
 ap.put(d
-ataSourc
+bNam
 e, resul

reconstruct it