You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2020/12/24 16:19:08 UTC

[shardingsphere] branch master updated: Refactor IntegrateTestEnvironment (#8764)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 46927de  Refactor IntegrateTestEnvironment (#8764)
46927de is described below

commit 46927de4cc8609bd9d56aa5a51fd5a43ab7a23a3
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Dec 25 00:18:49 2020 +0800

    Refactor IntegrateTestEnvironment (#8764)
    
    * Minor change for IT
    
    * Add IntegrateTestEnvironmentType
    
    * Refactor IntegrateTestEnvironment
    
    * Remove IntegrateTestEnvironment.databaseTypes
---
 .../test/integration/engine/SingleIT.java          |   2 +-
 .../test/integration/engine/dql/BaseDQLIT.java     |   2 +-
 .../engine/util/IntegrateTestParameters.java       |   4 +-
 .../integration/env/IntegrateTestEnvironment.java  | 102 +++++++++------------
 .../env/IntegrateTestEnvironmentType.java          |  54 +++++++++++
 .../env/schema/SchemaEnvironmentManager.java       |   8 +-
 6 files changed, 106 insertions(+), 66 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/SingleIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/SingleIT.java
index be9befe..e7fb75e 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/SingleIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/SingleIT.java
@@ -69,7 +69,7 @@ public abstract class SingleIT extends BaseIT {
             .replace("%%", "%").replace("'%'", "'%%'");
     }
     
-    protected void printExceptionContext(final Exception ex) {
+    protected final void printExceptionContext(final Exception ex) {
         log.error("ruleType={}, databaseType={}, expectedDataFile={}, sql={}", getRuleType(), getDatabaseType().getName(), expectedDataFile, sql, ex);
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/dql/BaseDQLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/dql/BaseDQLIT.java
index b6aced4..e286fc7 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/dql/BaseDQLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/dql/BaseDQLIT.java
@@ -66,7 +66,7 @@ public abstract class BaseDQLIT extends SingleIT {
     @BeforeClass
     public static void insertData() throws IOException, JAXBException, SQLException, ParseException {
         createDatabasesAndTables();
-        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
+        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             insertData(each);
         }
     }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/util/IntegrateTestParameters.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/util/IntegrateTestParameters.java
index 630d464..8155d5a 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/util/IntegrateTestParameters.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/util/IntegrateTestParameters.java
@@ -63,7 +63,7 @@ public final class IntegrateTestParameters {
         Map<DatabaseType, Collection<Object[]>> availableCases = new LinkedHashMap<>();
         Map<DatabaseType, Collection<Object[]>> disabledCases = new LinkedHashMap<>();
         INTEGRATE_TEST_CASES_LOADER.getTestCases(sqlType).forEach(integrateTestCase -> getDatabaseTypes(integrateTestCase.getDbTypes()).forEach(databaseType -> {
-            if (IntegrateTestEnvironment.getInstance().getDatabaseTypes().contains(databaseType)) {
+            if (IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().containsKey(databaseType)) {
                 availableCases.putIfAbsent(databaseType, new LinkedList<>());
                 Arrays.stream(SQLCaseType.values()).forEach(sqlCaseType -> availableCases.get(databaseType).addAll(getParametersWithAssertion(databaseType, sqlCaseType, integrateTestCase)));
             } else {
@@ -114,7 +114,7 @@ public final class IntegrateTestParameters {
         Map<DatabaseType, Collection<Object[]>> disabledCases = new LinkedHashMap<>();
         INTEGRATE_TEST_CASES_LOADER.getTestCases(sqlType).forEach(integrateTestCase ->
             getDatabaseTypes(integrateTestCase.getDbTypes()).forEach(databaseType -> {
-                if (IntegrateTestEnvironment.getInstance().getDatabaseTypes().contains(databaseType)) {
+                if (IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().containsKey(databaseType)) {
                     availableCases.putIfAbsent(databaseType, new LinkedList<>());
                     availableCases.get(databaseType).addAll(getParametersWithCase(databaseType, integrateTestCase));
                 } else {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
index c2f91db..0dacc4f 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironment.java
@@ -19,16 +19,17 @@ package org.apache.shardingsphere.test.integration.env;
 
 import com.google.common.base.Splitter;
 import lombok.Getter;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.test.integration.env.datasource.DatabaseEnvironment;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 /**
  * Integrate test running environment.
@@ -38,82 +39,67 @@ public final class IntegrateTestEnvironment {
     
     private static final IntegrateTestEnvironment INSTANCE = new IntegrateTestEnvironment();
     
+    private final String activeProfile;
+    
     private final boolean runAdditionalTestCases;
     
     private final Collection<String> ruleTypes;
     
-    private final Collection<DatabaseType> databaseTypes;
-    
     private final Map<DatabaseType, DatabaseEnvironment> databaseEnvironments;
     
-    private final String activeProfile;
-    
     private IntegrateTestEnvironment() {
-        activeProfile = loadActiveProfile();
-        Properties prop = new Properties();
-        try {
-            prop.load(IntegrateTestEnvironment.class.getClassLoader().getResourceAsStream(getEnvironmentProperties()));
-        } catch (final IOException ex) {
-            throw new RuntimeException(ex);
-        }
-        runAdditionalTestCases = Boolean.parseBoolean(prop.getProperty("run.additional.cases"));
-        ruleTypes = Splitter.on(",").trimResults().splitToList(prop.getProperty("rule.types"));
-        databaseTypes = new LinkedList<>();
-        for (String each : prop.getProperty("databases", "H2").split(",")) {
-            databaseTypes.add(DatabaseTypeRegistry.getActualDatabaseType(each.trim()));
-        }
-        databaseEnvironments = new HashMap<>(databaseTypes.size(), 1);
-        for (DatabaseType each : databaseTypes) {
-            switch (each.getName()) {
-                case "H2":
-                    databaseEnvironments.put(each, new DatabaseEnvironment(each, "", 0, "sa", ""));
-                    break;
-                case "MySQL":
-                    databaseEnvironments.put(each, new DatabaseEnvironment(each, prop.getProperty("mysql.host", "127.0.0.1"), Integer.parseInt(prop.getProperty("mysql.port", "3306")),
-                        prop.getProperty("mysql.username", "root"), prop.getProperty("mysql.password", "")));
-                    break;
-                case "PostgreSQL":
-                    databaseEnvironments.put(each, new DatabaseEnvironment(each, prop.getProperty("postgresql.host", "127.0.0.1"), Integer.parseInt(prop.getProperty("postgresql.port", "5432")),
-                        prop.getProperty("postgresql.username", "postgres"), prop.getProperty("postgresql.password", "")));
-                    break;
-                case "SQLServer":
-                    databaseEnvironments.put(each, new DatabaseEnvironment(each, prop.getProperty("sqlserver.host", "127.0.0.1"), Integer.parseInt(prop.getProperty("sqlserver.port", "1433")),
-                        prop.getProperty("sqlserver.username", "sa"), prop.getProperty("sqlserver.password", "Jdbc1234")));
-                    break;
-                case "Oracle":
-                    databaseEnvironments.put(each, new DatabaseEnvironment(each, prop.getProperty("oracle.host", "127.0.0.1"), Integer.parseInt(prop.getProperty("oracle.port", "1521")),
-                        prop.getProperty("oracle.username", "jdbc"), prop.getProperty("oracle.password", "jdbc")));
-                    break;
-                default:
-                    break;
-            }
-        }
+        activeProfile = loadProperties("integrate/profile.properties").getProperty("mode");
+        Properties envProps = loadProperties(IntegrateTestEnvironmentType.valueFromProfileName(activeProfile).getEnvFileName());
+        runAdditionalTestCases = Boolean.parseBoolean(envProps.getProperty("run.additional.cases"));
+        ruleTypes = Splitter.on(",").trimResults().splitToList(envProps.getProperty("rule.types"));
+        databaseEnvironments = createDatabaseEnvironments(envProps);
     }
     
-    private String loadActiveProfile() {
-        Properties prop = new Properties();
+    private Properties loadProperties(final String fileName) {
+        Properties result = new Properties();
         try {
-            prop.load(IntegrateTestEnvironment.class.getClassLoader().getResourceAsStream("integrate/profile.properties"));
+            result.load(IntegrateTestEnvironment.class.getClassLoader().getResourceAsStream(fileName));
         } catch (final IOException ex) {
             throw new RuntimeException(ex);
         }
-        return prop.getProperty("mode");
+        return result;
     }
     
-    private String getEnvironmentProperties() {
-        if ("jdbc-ci".equals(activeProfile)) {
-            return "integrate/env-jdbc-ci.properties";
+    private Map<DatabaseType, DatabaseEnvironment> createDatabaseEnvironments(final Properties envProps) {
+        Collection<DatabaseType> databaseTypes = Arrays.stream(
+                envProps.getProperty("databases", "H2").split(",")).map(each -> DatabaseTypeRegistry.getActualDatabaseType(each.trim())).collect(Collectors.toList());
+        Map<DatabaseType, DatabaseEnvironment> result = new LinkedHashMap<>(databaseTypes.size(), 1);
+        for (DatabaseType each : databaseTypes) {
+            result.put(each, createDatabaseEnvironment(each, envProps));
         }
-        if ("proxy".equals(activeProfile)) {
-            return "integrate/env-proxy.properties";
+        return result;
+    }
+    
+    private DatabaseEnvironment createDatabaseEnvironment(final DatabaseType databaseType, final Properties envProps) {
+        switch (databaseType.getName()) {
+            case "H2":
+                return new DatabaseEnvironment(databaseType, "", 0, "sa", "");
+            case "MySQL":
+                return new DatabaseEnvironment(databaseType, envProps.getProperty("mysql.host", "127.0.0.1"), Integer.parseInt(envProps.getProperty("mysql.port", "3306")),
+                        envProps.getProperty("mysql.username", "root"), envProps.getProperty("mysql.password", ""));
+            case "PostgreSQL":
+                return new DatabaseEnvironment(databaseType, envProps.getProperty("postgresql.host", "127.0.0.1"), Integer.parseInt(envProps.getProperty("postgresql.port", "5432")),
+                        envProps.getProperty("postgresql.username", "postgres"), envProps.getProperty("postgresql.password", ""));
+            case "SQLServer":
+                return new DatabaseEnvironment(databaseType, envProps.getProperty("sqlserver.host", "127.0.0.1"), Integer.parseInt(envProps.getProperty("sqlserver.port", "1433")),
+                        envProps.getProperty("sqlserver.username", "sa"), envProps.getProperty("sqlserver.password", "Jdbc1234"));
+            case "Oracle":
+                return new DatabaseEnvironment(databaseType, envProps.getProperty("oracle.host", "127.0.0.1"), Integer.parseInt(envProps.getProperty("oracle.port", "1521")),
+                        envProps.getProperty("oracle.username", "jdbc"), envProps.getProperty("oracle.password", "jdbc"));
+            default:
+                throw new UnsupportedOperationException(databaseType.getName());
         }
-        return "integrate/env-jdbc-local.properties";
     }
     
     /**
-     * Is proxy environment.
+     * Judge whether proxy environment.
      *
-     * @return true or false
+     * @return is proxy environment or not 
      */
     public boolean isProxyEnvironment() {
         return "proxy".equals(activeProfile);
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironmentType.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironmentType.java
new file mode 100644
index 0000000..76b473e
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/IntegrateTestEnvironmentType.java
@@ -0,0 +1,54 @@
+/*
+ * 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.test.integration.env;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+/**
+ * Integrate test environment type.
+ */
+@RequiredArgsConstructor
+public enum IntegrateTestEnvironmentType {
+    
+    JDBC_LOCAL("jdbc-local", "integrate/env-jdbc-local.properties"),
+    
+    JDBC_CI("jdbc-ci", "integrate/env-jdbc-ci.properties"),
+    
+    PROXY("proxy", "integrate/env-proxy.properties");
+    
+    private final String profileName;
+    
+    @Getter
+    private final String envFileName;
+    
+    /**
+     * Get enum value from profile name.
+     * 
+     * @param profileName profile name
+     * @return enum value
+     */
+    public static IntegrateTestEnvironmentType valueFromProfileName(final String profileName) {
+        for (IntegrateTestEnvironmentType each : values()) {
+            if (each.profileName.equals(profileName)) {
+                return each;
+            }
+        }
+        return JDBC_LOCAL;
+    }
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
index efeb34a..c7799c7 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
@@ -67,7 +67,7 @@ public final class SchemaEnvironmentManager {
      */
     public static void createDatabase(final String ruleType) throws IOException, JAXBException, SQLException {
         SchemaEnvironment databaseInitialization = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
-        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
+        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             DataSource dataSource = DataSourceUtil.createDataSource(each, null);
             try (
                     Connection connection = dataSource.getConnection();
@@ -86,7 +86,7 @@ public final class SchemaEnvironmentManager {
      */
     public static void dropDatabase(final String ruleType) throws IOException, JAXBException {
         SchemaEnvironment databaseInitialization = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
-        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
+        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             DataSource dataSource = DataSourceUtil.createDataSource(each, null);
             if ("PostgreSQL".equals(each.getName())) {
                 try (
@@ -155,7 +155,7 @@ public final class SchemaEnvironmentManager {
      * @throws SQLException SQL exception
      */
     public static void createTable(final String ruleType) throws JAXBException, IOException, SQLException {
-        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
+        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             SchemaEnvironment databaseEnvironmentSchema = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
             createTable(databaseEnvironmentSchema, each);
         }
@@ -179,7 +179,7 @@ public final class SchemaEnvironmentManager {
      * @throws IOException IO exception
      */
     public static void dropTable(final String ruleType) throws JAXBException, IOException {
-        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
+        for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             SchemaEnvironment databaseEnvironmentSchema = unmarshal(EnvironmentPath.getDatabaseEnvironmentResourceFile(ruleType));
             dropTable(databaseEnvironmentSchema, each);
         }