You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/01/23 16:03:54 UTC
[shardingsphere] branch master updated: Use H2 test AbstractSQLTest
only (#9142)
This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8ef17ca Use H2 test AbstractSQLTest only (#9142)
8ef17ca is described below
commit 8ef17ca80623b471b870fd824c8cbdfb1be07476
Author: Liang Zhang <te...@163.com>
AuthorDate: Sun Jan 24 00:03:28 2021 +0800
Use H2 test AbstractSQLTest only (#9142)
* Remove useless codes of DatabaseEnvironment
* Remove DatabaseEnvironment
* Add DataSourceBuilder
* Use try with resource for connection with init data
* Simplify AbstractSQLTest
* Simplify AbstractSQLTest
* Simplify AbstractSQLCalciteTest
* Use H2 test AbstractSQLTest only
---
.../driver/common/base/AbstractSQLCalciteTest.java | 46 ++-----
.../driver/common/base/AbstractSQLTest.java | 58 ++------
...ractShardingSphereDataSourceForCalciteTest.java | 6 +-
...ractShardingSphereDataSourceForEncryptTest.java | 4 +-
...hardingSphereDataSourceForReplicaQueryTest.java | 4 +-
...tractShardingSphereDataSourceForShadowTest.java | 4 +-
...actShardingSphereDataSourceForShardingTest.java | 4 +-
.../driver/common/base/DataSourceBuilder.java | 47 +++++++
.../driver/common/env/DatabaseEnvironment.java | 148 ---------------------
.../statement/EncryptPreparedStatementTest.java | 5 +-
.../jdbc/core/statement/EncryptStatementTest.java | 5 +-
.../statement/ShadowPreparedStatementTest.java | 8 +-
.../jdbc/core/statement/ShadowStatementTest.java | 5 +-
13 files changed, 90 insertions(+), 254 deletions(-)
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLCalciteTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLCalciteTest.java
index b83b7e4..3eca1fd 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLCalciteTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLCalciteTest.java
@@ -17,10 +17,6 @@
package org.apache.shardingsphere.driver.common.base;
-import org.apache.commons.dbcp2.BasicDataSource;
-import org.apache.shardingsphere.driver.common.env.DatabaseEnvironment;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.h2.tools.RunScript;
import org.junit.BeforeClass;
@@ -29,55 +25,35 @@ import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
public abstract class AbstractSQLCalciteTest {
- private static final Map<DatabaseType, Map<String, DataSource>> DATABASE_TYPE_MAP = new HashMap<>();
+ private static final Map<String, DataSource> ACTUAL_DATA_SOURCES = new HashMap<>();
private static final String INIT_CALCITE_DATABASE_0 = "sql/jdbc_init_calcite_0.sql";
private static final String INIT_CALCITE_DATABASE_1 = "sql/jdbc_init_calcite_1.sql";
@BeforeClass
- public static synchronized void initDataSource() {
- createDataSources();
+ public static synchronized void initializeDataSource() throws SQLException {
+ createDataSources("jdbc_0", INIT_CALCITE_DATABASE_0);
+ createDataSources("jdbc_1", INIT_CALCITE_DATABASE_1);
}
- private static void createDataSources() {
- createDataSources("jdbc_0", DatabaseTypeRegistry.getActualDatabaseType("H2"), INIT_CALCITE_DATABASE_0);
- createDataSources("jdbc_1", DatabaseTypeRegistry.getActualDatabaseType("H2"), INIT_CALCITE_DATABASE_1);
+ private static void createDataSources(final String dataSourceName, final String initSql) throws SQLException {
+ ACTUAL_DATA_SOURCES.put(dataSourceName, DataSourceBuilder.build(dataSourceName));
+ initializeSchema(dataSourceName, initSql);
}
- private static void createDataSources(final String dbName, final DatabaseType databaseType, final String initSql) {
- DATABASE_TYPE_MAP.computeIfAbsent(databaseType, key -> new LinkedHashMap<>()).put(dbName, buildDataSource(dbName, databaseType));
- buildSchema(dbName, databaseType, initSql);
- }
-
- private static BasicDataSource buildDataSource(final String dbName, final DatabaseType databaseType) {
- DatabaseEnvironment dbEnv = new DatabaseEnvironment(databaseType);
- 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 buildSchema(final String dbName, final DatabaseType databaseType, final String initSql) {
- try {
- Connection conn = DATABASE_TYPE_MAP.get(databaseType).get(dbName).getConnection();
+ private static void initializeSchema(final String dataSourceName, final String initSql) throws SQLException {
+ try (Connection conn = ACTUAL_DATA_SOURCES.get(dataSourceName).getConnection()) {
RunScript.execute(conn, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream(initSql))));
- conn.close();
- } catch (final SQLException ex) {
- throw new RuntimeException(ex);
}
}
- protected static Map<DatabaseType, Map<String, DataSource>> getDatabaseTypeMap() {
- return DATABASE_TYPE_MAP;
+ protected static Map<String, DataSource> getActualDataSources() {
+ return ACTUAL_DATA_SOURCES;
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLTest.java
index 9ada8c1..68017e4 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractSQLTest.java
@@ -17,11 +17,6 @@
package org.apache.shardingsphere.driver.common.base;
-import com.google.common.collect.Sets;
-import org.apache.commons.dbcp2.BasicDataSource;
-import org.apache.shardingsphere.driver.common.env.DatabaseEnvironment;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.h2.tools.RunScript;
import org.junit.BeforeClass;
@@ -31,66 +26,41 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
-import java.util.Set;
public abstract class AbstractSQLTest {
- private static final List<String> DB_NAMES = Arrays.asList("jdbc_0", "jdbc_1", "shadow_jdbc_0", "shadow_jdbc_1", "encrypt", "test_primary_ds", "test_replica_ds");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Arrays.asList("jdbc_0", "jdbc_1", "shadow_jdbc_0", "shadow_jdbc_1", "encrypt", "test_primary_ds", "test_replica_ds");
- private static final Set<DatabaseType> DATABASE_TYPES = Sets.newHashSet(DatabaseTypeRegistry.getActualDatabaseType("H2"));
-
- private static final Map<DatabaseType, Map<String, DataSource>> DATABASE_TYPE_MAP = new HashMap<>();
+ private static final Map<String, DataSource> ACTUAL_DATA_SOURCES = new HashMap<>();
@BeforeClass
- public static synchronized void initDataSource() {
- createDataSources();
- }
-
- private static void createDataSources() {
- for (String each : DB_NAMES) {
- for (DatabaseType type : DATABASE_TYPES) {
- createDataSources(each, type);
- }
+ public static synchronized void initializeDataSource() throws SQLException {
+ for (String each : ACTUAL_DATA_SOURCE_NAMES) {
+ createDataSources(each);
}
}
- private static void createDataSources(final String dbName, final DatabaseType databaseType) {
- DATABASE_TYPE_MAP.computeIfAbsent(databaseType, key -> new LinkedHashMap<>()).put(dbName, buildDataSource(dbName, databaseType));
- buildSchema(dbName, databaseType);
- }
-
- private static BasicDataSource buildDataSource(final String dbName, final DatabaseType databaseType) {
- DatabaseEnvironment dbEnv = new DatabaseEnvironment(databaseType);
- 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 createDataSources(final String dataSourceName) throws SQLException {
+ ACTUAL_DATA_SOURCES.put(dataSourceName, DataSourceBuilder.build(dataSourceName));
+ initializeSchema(dataSourceName);
}
- private static void buildSchema(final String dbName, final DatabaseType databaseType) {
- try {
- Connection conn = DATABASE_TYPE_MAP.get(databaseType).get(dbName).getConnection();
- if ("encrypt".equals(dbName)) {
+ private static void initializeSchema(final String dataSourceName) throws SQLException {
+ try (Connection conn = ACTUAL_DATA_SOURCES.get(dataSourceName).getConnection()) {
+ if ("encrypt".equals(dataSourceName)) {
RunScript.execute(conn, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream("sql/jdbc_encrypt_init.sql"))));
- } else if ("shadow_jdbc_0".equals(dbName) || "shadow_jdbc_1".equals(dbName)) {
+ } else if ("shadow_jdbc_0".equals(dataSourceName) || "shadow_jdbc_1".equals(dataSourceName)) {
RunScript.execute(conn, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream("sql/jdbc_shadow_init.sql"))));
} else {
RunScript.execute(conn, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream("sql/jdbc_init.sql"))));
}
- conn.close();
- } catch (final SQLException ex) {
- throw new RuntimeException(ex);
}
}
- protected static Map<DatabaseType, Map<String, DataSource>> getDatabaseTypeMap() {
- return DATABASE_TYPE_MAP;
+ protected static Map<String, DataSource> getActualDataSources() {
+ return ACTUAL_DATA_SOURCES;
}
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForCalciteTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForCalciteTest.java
index 2f22a4b..d024f76 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForCalciteTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForCalciteTest.java
@@ -38,11 +38,11 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
-public class AbstractShardingSphereDataSourceForCalciteTest extends AbstractSQLCalciteTest {
+public abstract class AbstractShardingSphereDataSourceForCalciteTest extends AbstractSQLCalciteTest {
private static ShardingSphereDataSource dataSource;
- private static final List<String> CALCITE_DB_NAMES = Arrays.asList("jdbc_0", "jdbc_1");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Arrays.asList("jdbc_0", "jdbc_1");
private static final String CONFIG_CALCITE = "config/config-calcite.yaml";
@@ -55,7 +55,7 @@ public class AbstractShardingSphereDataSourceForCalciteTest extends AbstractSQLC
}
private static Map<String, DataSource> getDataSourceMap() {
- return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), CALCITE_DB_NAMES::contains);
+ return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
}
@Before
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForEncryptTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForEncryptTest.java
index c931e65..87570b58 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForEncryptTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForEncryptTest.java
@@ -48,7 +48,7 @@ public abstract class AbstractShardingSphereDataSourceForEncryptTest extends Abs
private static ShardingSphereDataSource encryptDataSourceWithProps;
- private static final List<String> ENCRYPT_DB_NAMES = Collections.singletonList("encrypt");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Collections.singletonList("encrypt");
private static final String ENCRYPT_CONFIG_FILE = "config/config-encrypt.yaml";
@@ -69,7 +69,7 @@ public abstract class AbstractShardingSphereDataSourceForEncryptTest extends Abs
}
private static Map<String, DataSource> getDataSources() {
- return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), ENCRYPT_DB_NAMES::contains);
+ return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
}
private static DataSource createDataSourceWithEmptyProps(final DataSource dataSource, final File yamlFile) throws IOException, SQLException {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForReplicaQueryTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForReplicaQueryTest.java
index 99d6c8f..015e4df 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForReplicaQueryTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForReplicaQueryTest.java
@@ -38,7 +38,7 @@ public abstract class AbstractShardingSphereDataSourceForReplicaQueryTest extend
private static final String CONFIG = "config/config-replica-query.yaml";
- private static final List<String> DATA_SOURCE_NAMES = Arrays.asList("test_primary_ds", "test_replica_ds");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Arrays.asList("test_primary_ds", "test_replica_ds");
@BeforeClass
public static void initReplicaQueryDataSources() throws SQLException, IOException {
@@ -49,7 +49,7 @@ public abstract class AbstractShardingSphereDataSourceForReplicaQueryTest extend
}
private static Map<String, DataSource> getDataSources() {
- return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), DATA_SOURCE_NAMES::contains);
+ return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
}
private static File getFile(final String fileName) {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShadowTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShadowTest.java
index ffcf077..0acdc9a 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShadowTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShadowTest.java
@@ -38,7 +38,7 @@ public abstract class AbstractShardingSphereDataSourceForShadowTest extends Abst
private static final String CONFIG_SHADOW = "config/config-shadow.yaml";
- private static final List<String> SHADOW_DB_NAMES = Arrays.asList("shadow_jdbc_0", "shadow_jdbc_1");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Arrays.asList("shadow_jdbc_0", "shadow_jdbc_1");
@BeforeClass
public static void initShadowDataSource() throws SQLException, IOException {
@@ -49,7 +49,7 @@ public abstract class AbstractShardingSphereDataSourceForShadowTest extends Abst
}
private static Map<String, DataSource> getDataSources() {
- return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), SHADOW_DB_NAMES::contains);
+ return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
}
private static File getFile(final String fileName) {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShardingTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShardingTest.java
index 0468e2c..c14ee43 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShardingTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/AbstractShardingSphereDataSourceForShardingTest.java
@@ -41,7 +41,7 @@ public abstract class AbstractShardingSphereDataSourceForShardingTest extends Ab
private static ShardingSphereDataSource dataSource;
- private static final List<String> SHARDING_DB_NAMES = Arrays.asList("jdbc_0", "jdbc_1");
+ private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Arrays.asList("jdbc_0", "jdbc_1");
private static final String CONFIG_SHARDING = "config/config-sharding.yaml";
@@ -54,7 +54,7 @@ public abstract class AbstractShardingSphereDataSourceForShardingTest extends Ab
}
private static Map<String, DataSource> getDataSourceMap() {
- return Maps.filterKeys(getDatabaseTypeMap().values().iterator().next(), SHARDING_DB_NAMES::contains);
+ return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
}
private static File getFile(final String fileName) {
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/DataSourceBuilder.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/DataSourceBuilder.java
new file mode 100644
index 0000000..10d032e
--- /dev/null
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/base/DataSourceBuilder.java
@@ -0,0 +1,47 @@
+/*
+ * 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.driver.common.base;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.commons.dbcp2.BasicDataSource;
+
+import javax.sql.DataSource;
+
+/**
+ * Data source builder.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DataSourceBuilder {
+
+ /**
+ * Build data source.
+ *
+ * @param dataSourceName data source name
+ * @return data source
+ */
+ public static DataSource build(final String dataSourceName) {
+ BasicDataSource result = new BasicDataSource();
+ result.setDriverClassName("org.h2.Driver");
+ result.setUrl(String.format("jdbc:h2:mem:%s;DATABASE_TO_UPPER=false;MODE=MySQL", dataSourceName));
+ result.setUsername("sa");
+ result.setPassword("");
+ result.setMaxTotal(50);
+ return result;
+ }
+}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/env/DatabaseEnvironment.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/env/DatabaseEnvironment.java
deleted file mode 100644
index 444ba79..0000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/common/env/DatabaseEnvironment.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * 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.driver.common.env;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public final class DatabaseEnvironment {
-
- private static final int INIT_CAPACITY = 5;
-
- private static final Map<DatabaseType, String> DRIVER_CLASS_NAME = new HashMap<>(INIT_CAPACITY, 1);
-
- private static final Map<DatabaseType, String> URL = new HashMap<>(INIT_CAPACITY, 1);
-
- private static final Map<DatabaseType, String> USERNAME = new HashMap<>(INIT_CAPACITY, 1);
-
- private static final Map<DatabaseType, String> PASSWORD = new HashMap<>(INIT_CAPACITY, 1);
-
- private static final Map<DatabaseType, String> SCHEMA = new HashMap<>(INIT_CAPACITY, 1);
-
- @Getter
- private final DatabaseType databaseType;
-
- public DatabaseEnvironment(final DatabaseType databaseType) {
- this.databaseType = databaseType;
- fillData();
- }
-
- private void fillData() {
- fillH2();
- fillMySQL();
- fillPostgreSQL();
- fillSQLServer();
- fillOracle();
- }
-
- private void fillH2() {
- DatabaseType databaseType = DatabaseTypeRegistry.getActualDatabaseType("H2");
- DRIVER_CLASS_NAME.put(databaseType, "org.h2.Driver");
- URL.put(databaseType, "jdbc:h2:mem:%s;DATABASE_TO_UPPER=false;MODE=MySQL");
- USERNAME.put(databaseType, "sa");
- PASSWORD.put(databaseType, "");
- SCHEMA.put(databaseType, null);
- }
-
- private void fillMySQL() {
- DatabaseType databaseType = DatabaseTypeRegistry.getActualDatabaseType("MySQL");
- DRIVER_CLASS_NAME.put(databaseType, "com.mysql.jdbc.Driver");
- URL.put(databaseType, "jdbc:mysql://db.mysql:3306/%s?serverTimezone=UTC&useSSL=false");
- USERNAME.put(databaseType, "root");
- PASSWORD.put(databaseType, "");
- SCHEMA.put(databaseType, null);
- }
-
- private void fillPostgreSQL() {
- DatabaseType databaseType = DatabaseTypeRegistry.getActualDatabaseType("PostgreSQL");
- DRIVER_CLASS_NAME.put(databaseType, "org.postgresql.Driver");
- URL.put(databaseType, "jdbc:postgresql://db.psql:5432/%s");
- USERNAME.put(databaseType, "postgres");
- PASSWORD.put(databaseType, "");
- SCHEMA.put(databaseType, null);
- }
-
- private void fillSQLServer() {
- DatabaseType databaseType = DatabaseTypeRegistry.getActualDatabaseType("SQLServer");
- DRIVER_CLASS_NAME.put(databaseType, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
- URL.put(databaseType, "jdbc:sqlserver://db.mssql:1433;DatabaseName=%s");
- USERNAME.put(databaseType, "sa");
- PASSWORD.put(databaseType, "Jdbc1234");
- SCHEMA.put(databaseType, null);
- }
-
- private void fillOracle() {
- DatabaseType databaseType = DatabaseTypeRegistry.getActualDatabaseType("Oracle");
- DRIVER_CLASS_NAME.put(databaseType, "oracle.jdbc.driver.OracleDriver");
- URL.put(databaseType, "jdbc:oracle:thin:@db.oracle:1521/test");
- USERNAME.put(databaseType, "jdbc");
- PASSWORD.put(databaseType, "jdbc");
- SCHEMA.put(databaseType, "%s");
- }
-
- /**
- * Get driver class name.
- *
- * @return driver class name
- */
- public String getDriverClassName() {
- return DRIVER_CLASS_NAME.get(databaseType);
- }
-
- /**
- * Get URL.
- *
- * @param dbName database name
- * @return database URL
- */
- public String getURL(final String dbName) {
- return String.format(URL.get(databaseType), dbName);
- }
-
- /**
- * Get username.
- *
- * @return username
- */
- public String getUsername() {
- return USERNAME.get(databaseType);
- }
-
- /**
- * Get password.
- *
- * @return password
- */
- public String getPassword() {
- return PASSWORD.get(databaseType);
- }
-
- /**
- * Get schema.
- *
- * @param dbName database name
- * @return schema
- */
- public String getSchema(final String dbName) {
- return null == SCHEMA.get(databaseType) ? null : String.format(SCHEMA.get(databaseType), dbName);
- }
-}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptPreparedStatementTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptPreparedStatementTest.java
index afbae2b..996420c 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptPreparedStatementTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptPreparedStatementTest.java
@@ -17,9 +17,8 @@
package org.apache.shardingsphere.driver.jdbc.core.statement;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.driver.common.base.AbstractShardingSphereDataSourceForEncryptTest;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.junit.Test;
import java.sql.Connection;
@@ -170,7 +169,7 @@ public final class EncryptPreparedStatementTest extends AbstractShardingSphereDa
}
private void assertResultSet(final int resultSetCount, final int id, final Object pwd, final Object assistPwd) throws SQLException {
- try (Connection conn = getDatabaseTypeMap().get(DatabaseTypeRegistry.getActualDatabaseType("H2")).get("encrypt").getConnection();
+ try (Connection conn = getActualDataSources().get("encrypt").getConnection();
Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery(SELECT_ALL_SQL);
int count = 1;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptStatementTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptStatementTest.java
index f591568..4e25477 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptStatementTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/EncryptStatementTest.java
@@ -17,9 +17,8 @@
package org.apache.shardingsphere.driver.jdbc.core.statement;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.driver.common.base.AbstractShardingSphereDataSourceForEncryptTest;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.junit.Test;
import java.sql.Connection;
@@ -168,7 +167,7 @@ public final class EncryptStatementTest extends AbstractShardingSphereDataSource
}
private void assertResultSet(final int resultSetCount, final int id, final Object pwd, final Object plain) throws SQLException {
- try (Connection conn = getDatabaseTypeMap().get(DatabaseTypeRegistry.getActualDatabaseType("H2")).get("encrypt").getConnection();
+ try (Connection conn = getActualDataSources().get("encrypt").getConnection();
Statement stmt = conn.createStatement()) {
ResultSet resultSet = stmt.executeQuery(SELECT_SQL_TO_ASSERT);
int count = 1;
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowPreparedStatementTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowPreparedStatementTest.java
index 5892586..a78fa3d 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowPreparedStatementTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowPreparedStatementTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.driver.jdbc.core.statement;
import org.apache.shardingsphere.driver.common.base.AbstractShardingSphereDataSourceForShadowTest;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -28,7 +27,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -136,8 +134,7 @@ public final class ShadowPreparedStatementTest extends AbstractShardingSphereDat
}
private void assertResultSet(final boolean isShadow, final int resultSetCount, final Object cipherPwd) throws SQLException {
- Map<String, DataSource> dataMaps = getDatabaseTypeMap().get(DatabaseTypeRegistry.getActualDatabaseType("H2"));
- DataSource dataSource = isShadow ? dataMaps.get("shadow_jdbc_1") : dataMaps.get("shadow_jdbc_0");
+ DataSource dataSource = isShadow ? getActualDataSources().get("shadow_jdbc_1") : getActualDataSources().get("shadow_jdbc_0");
try (Statement statement = dataSource.getConnection().createStatement()) {
ResultSet resultSet = statement.executeQuery(SELECT_SQL);
int count = 1;
@@ -150,8 +147,7 @@ public final class ShadowPreparedStatementTest extends AbstractShardingSphereDat
}
private void assertResultSet(final boolean isShadow, final int id, final int resultSetCount, final Object cipherPwd) throws SQLException {
- Map<String, DataSource> dataMaps = getDatabaseTypeMap().get(DatabaseTypeRegistry.getActualDatabaseType("H2"));
- DataSource dataSource = isShadow ? dataMaps.get("shadow_jdbc_1") : dataMaps.get("shadow_jdbc_0");
+ DataSource dataSource = isShadow ? getActualDataSources().get("shadow_jdbc_1") : getActualDataSources().get("shadow_jdbc_0");
try (PreparedStatement statement = dataSource.getConnection().prepareStatement(SELECT_SQL_BY_ID)) {
statement.setObject(1, id);
ResultSet resultSet = statement.executeQuery();
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowStatementTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowStatementTest.java
index c5b2612..1bd33ba 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowStatementTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShadowStatementTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.driver.jdbc.core.statement;
import org.apache.shardingsphere.driver.common.base.AbstractShardingSphereDataSourceForShadowTest;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.junit.After;
import org.junit.Test;
@@ -26,7 +25,6 @@ import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
@@ -86,8 +84,7 @@ public final class ShadowStatementTest extends AbstractShardingSphereDataSourceF
}
private void assertResultSet(final boolean isShadow, final int resultSetCount, final Object cipherPwd) throws SQLException {
- Map<String, DataSource> dataMaps = getDatabaseTypeMap().get(DatabaseTypeRegistry.getActualDatabaseType("H2"));
- DataSource dataSource = isShadow ? dataMaps.get("shadow_jdbc_1") : dataMaps.get("shadow_jdbc_0");
+ DataSource dataSource = isShadow ? getActualDataSources().get("shadow_jdbc_1") : getActualDataSources().get("shadow_jdbc_0");
try (Statement statement = dataSource.getConnection().createStatement()) {
ResultSet resultSet = statement.executeQuery(SELECT_SQL);
int count = 1;