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/05/11 13:33:30 UTC

[shardingsphere] branch master updated: Refactor AbstractShardingSphereDataSourceForEncryptTest (#10308)

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 c78723e  Refactor AbstractShardingSphereDataSourceForEncryptTest (#10308)
c78723e is described below

commit c78723ed8b01207eec975a163b374e47585fa8be
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue May 11 21:32:46 2021 +0800

    Refactor AbstractShardingSphereDataSourceForEncryptTest (#10308)
---
 ...ractShardingSphereDataSourceForEncryptTest.java | 43 +++++++++-------------
 ....yaml => config-encrypt-query-with-cipher.yaml} |  1 -
 ...t.yaml => config-encrypt-query-with-plain.yaml} |  0
 3 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/base/AbstractShardingSphereDataSourceForEncryptTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/base/AbstractShardingSphereDataSourceForEncryptTest.java
index f4cc009..00ed597 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/base/AbstractShardingSphereDataSourceForEncryptTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/base/AbstractShardingSphereDataSourceForEncryptTest.java
@@ -19,13 +19,9 @@ package org.apache.shardingsphere.driver.jdbc.base;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
-import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
 import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
 import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
-import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
 import org.h2.tools.RunScript;
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -40,27 +36,27 @@ import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Properties;
 
 public abstract class AbstractShardingSphereDataSourceForEncryptTest extends AbstractSQLTest {
     
-    private static ShardingSphereDataSource dataSource;
+    private static ShardingSphereDataSource queryWithPlainDataSource;
     
-    private static ShardingSphereDataSource encryptDataSourceWithProps;
+    private static ShardingSphereDataSource queryWithCipherDataSource;
     
     private static final List<String> ACTUAL_DATA_SOURCE_NAMES = Collections.singletonList("encrypt");
     
-    private static final String ENCRYPT_CONFIG_FILE = "config/config-encrypt.yaml";
+    private static final String ENCRYPT_CONFIG_QUERY_WITH_PLAIN_FILE = "config/config-encrypt-query-with-plain.yaml";
+    
+    private static final String ENCRYPT_CONFIG_QUERY_WITH_CIPHER_FILE = "config/config-encrypt-query-with-cipher.yaml";
     
     @BeforeClass
     public static void initEncryptDataSource() throws SQLException, IOException {
-        if (null != dataSource && null != encryptDataSourceWithProps) {
+        if (null != queryWithPlainDataSource && null != queryWithCipherDataSource) {
             return;
         }
-        File encryptFile = getFile(ENCRYPT_CONFIG_FILE);
         DataSource dataSource = getDataSources().values().iterator().next();
-        AbstractShardingSphereDataSourceForEncryptTest.dataSource = (ShardingSphereDataSource) createDataSourceWithEmptyProps(dataSource, encryptFile);
-        encryptDataSourceWithProps = (ShardingSphereDataSource) YamlShardingSphereDataSourceFactory.createDataSource(dataSource, encryptFile);
+        queryWithPlainDataSource = (ShardingSphereDataSource) YamlShardingSphereDataSourceFactory.createDataSource(dataSource, getFile(ENCRYPT_CONFIG_QUERY_WITH_CIPHER_FILE));
+        queryWithCipherDataSource = (ShardingSphereDataSource) YamlShardingSphereDataSourceFactory.createDataSource(dataSource, getFile(ENCRYPT_CONFIG_QUERY_WITH_PLAIN_FILE));
     }
     
     private static File getFile(final String fileName) {
@@ -72,14 +68,9 @@ public abstract class AbstractShardingSphereDataSourceForEncryptTest extends Abs
         return Maps.filterKeys(getActualDataSources(), ACTUAL_DATA_SOURCE_NAMES::contains);
     }
     
-    private static DataSource createDataSourceWithEmptyProps(final DataSource dataSource, final File yamlFile) throws IOException, SQLException {
-        YamlRootRuleConfigurations configurations = YamlEngine.unmarshal(yamlFile, YamlRootRuleConfigurations.class);
-        return ShardingSphereDataSourceFactory.createDataSource(dataSource, new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(configurations.getRules()), new Properties());
-    }
-    
     @Before
     public void initTable() {
-        try (ShardingSphereConnection connection = dataSource.getConnection()) {
+        try (ShardingSphereConnection connection = queryWithPlainDataSource.getConnection()) {
             RunScript.execute(connection, new InputStreamReader(Objects.requireNonNull(AbstractSQLTest.class.getClassLoader().getResourceAsStream("sql/encrypt_data.sql"))));
         } catch (final SQLException ex) {
             throw new RuntimeException(ex);
@@ -87,24 +78,24 @@ public abstract class AbstractShardingSphereDataSourceForEncryptTest extends Abs
     }
     
     protected final ShardingSphereConnection getEncryptConnection() {
-        return dataSource.getConnection();
+        return queryWithPlainDataSource.getConnection();
     }
     
     protected final ShardingSphereConnection getEncryptConnectionWithProps() {
-        return encryptDataSourceWithProps.getConnection();
+        return queryWithCipherDataSource.getConnection();
     }
     
     @AfterClass
     public static void close() throws Exception {
-        if (null == dataSource) {
+        if (null == queryWithPlainDataSource) {
             return;
         }
-        dataSource.close();
-        dataSource = null;
-        if (null == encryptDataSourceWithProps) {
+        queryWithPlainDataSource.close();
+        queryWithPlainDataSource = null;
+        if (null == queryWithCipherDataSource) {
             return;
         }
-        encryptDataSourceWithProps.close();
-        encryptDataSourceWithProps = null;
+        queryWithCipherDataSource.close();
+        queryWithCipherDataSource = null;
     }
 }
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt.yaml b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt-query-with-cipher.yaml
similarity index 97%
copy from shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt.yaml
copy to shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt-query-with-cipher.yaml
index 24ffa4d..c804000 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt.yaml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt-query-with-cipher.yaml
@@ -48,4 +48,3 @@ rules:
 
 props:
   sql-show: true
-  query-with-cipher-column: false
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt.yaml b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt-query-with-plain.yaml
similarity index 100%
rename from shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt.yaml
rename to shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/config-encrypt-query-with-plain.yaml