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

menghaoran 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 15ddcf2  Minor changes for H2Container (#15410)
15ddcf2 is described below

commit 15ddcf21e46c2a4e425eed84a68da4b003a0ade7
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 15 00:22:14 2022 +0800

    Minor changes for H2Container (#15410)
    
    * Refactor H2Container
    
    * Refactor StorageContainer
---
 .../container/atomic/storage/StorageContainer.java | 12 +++++------
 .../container/atomic/storage/impl/H2Container.java | 24 +++++++++++-----------
 2 files changed, 17 insertions(+), 19 deletions(-)

diff --git 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/StorageContainer.java
 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/StorageContainer.java
index 2846ee1..32e9826 100644
--- 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/StorageContainer.java
+++ 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/StorageContainer.java
@@ -26,13 +26,12 @@ import 
org.apache.shardingsphere.test.integration.env.EnvironmentPath;
 import 
org.apache.shardingsphere.test.integration.env.database.DatabaseEnvironmentManager;
 import 
org.apache.shardingsphere.test.integration.framework.container.atomic.AtomicContainer;
 import org.testcontainers.containers.BindMode;
-import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
-import 
org.testcontainers.shaded.com.google.common.collect.ImmutableMap.Builder;
 
 import javax.sql.DataSource;
 import javax.xml.bind.JAXBException;
 import java.io.IOException;
 import java.util.Collection;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Optional;
 
@@ -41,14 +40,14 @@ import java.util.Optional;
  */
 public abstract class StorageContainer extends AtomicContainer {
     
-    private Map<String, DataSource> actualDataSourceMap;
-    
     @Getter
     private final DatabaseType databaseType;
     
     @Getter
     private final String scenario;
     
+    private Map<String, DataSource> actualDataSourceMap;
+    
     public StorageContainer(final DatabaseType databaseType, final String 
dockerImageName, final boolean isFakedContainer, final String scenario) {
         super(databaseType.getName().toLowerCase(), dockerImageName, 
isFakedContainer);
         this.databaseType = databaseType;
@@ -69,9 +68,8 @@ public abstract class StorageContainer extends 
AtomicContainer {
     public synchronized Map<String, DataSource> getActualDataSourceMap() {
         if (null == actualDataSourceMap) {
             Collection<String> dataSourceNames = 
DatabaseEnvironmentManager.getDatabaseNames(scenario);
-            Builder<String, DataSource> builder = ImmutableMap.builder();
-            dataSourceNames.forEach(each -> builder.put(each, 
createDataSource(each)));
-            actualDataSourceMap = builder.build();
+            actualDataSourceMap = new LinkedHashMap<>(dataSourceNames.size(), 
1);
+            dataSourceNames.forEach(each -> actualDataSourceMap.put(each, 
createDataSource(each)));
         }
         return actualDataSourceMap;
     }
diff --git 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/H2Container.java
 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/H2Container.java
index 3d5f639..609e821 100644
--- 
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/H2Container.java
+++ 
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/framework/container/atomic/storage/impl/H2Container.java
@@ -18,7 +18,6 @@
 package 
org.apache.shardingsphere.test.integration.framework.container.atomic.storage.impl;
 
 import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.test.integration.env.EnvironmentPath;
 import 
org.apache.shardingsphere.test.integration.framework.container.atomic.storage.StorageContainer;
@@ -44,23 +43,24 @@ public final class H2Container extends StorageContainer {
     @Override
     @SneakyThrows({IOException.class, SQLException.class})
     protected void execute() {
-        File file = new File(EnvironmentPath.getInitSQLFile(getDatabaseType(), 
getScenario()));
-        for (Entry<String, DataSource> each : 
getActualDataSourceMap().entrySet()) {
-            String databaseFileName = "init-" + each.getKey() + ".sql";
-            boolean sqlFileExist = 
EnvironmentPath.checkSQLFileExist(getDatabaseType(), getScenario(), 
databaseFileName);
-            try (Connection connection = each.getValue().getConnection(); 
FileReader reader = new FileReader(file)) {
+        File initSQLFile = new 
File(EnvironmentPath.getInitSQLFile(getDatabaseType(), getScenario()));
+        for (Entry<String, DataSource> entry : 
getActualDataSourceMap().entrySet()) {
+            String dbInitSQLFileName = "init-" + entry.getKey() + ".sql";
+            try (
+                    Connection connection = entry.getValue().getConnection();
+                    FileReader reader = new FileReader(initSQLFile)) {
                 RunScript.execute(connection, reader);
-                if (sqlFileExist) {
-                    executeDatabaseFile(getDatabaseType(), connection, 
databaseFileName);
+                if (EnvironmentPath.checkSQLFileExist(getDatabaseType(), 
getScenario(), dbInitSQLFileName)) {
+                    executeDataInitFile(connection, dbInitSQLFileName);
                 }
             }
         }
     }
     
-    private void executeDatabaseFile(final DatabaseType databaseType, final 
Connection connection, final String databaseFileName) throws IOException, 
SQLException {
-        File databaseFile = new 
File(EnvironmentPath.getInitSQLFile(databaseType, getScenario(), 
databaseFileName));
-        try (FileReader databaseFileReader = new FileReader(databaseFile)) {
-            RunScript.execute(connection, databaseFileReader);
+    private void executeDataInitFile(final Connection connection, final String 
dataInitFileName) throws IOException, SQLException {
+        File dataInitFile = new 
File(EnvironmentPath.getInitSQLFile(getDatabaseType(), getScenario(), 
dataInitFileName));
+        try (FileReader reader = new FileReader(dataInitFile)) {
+            RunScript.execute(connection, reader);
         }
     }
     

Reply via email to