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 ca4a3a8e250 Add DML E2E support init and destory sql (#32504)
ca4a3a8e250 is described below

commit ca4a3a8e250496a556cfcd6bac6a3e2dc04544e8
Author: ZhangCheng <[email protected]>
AuthorDate: Wed Aug 14 17:37:55 2024 +0800

    Add DML E2E support init and destory sql (#32504)
---
 .../e2e/engine/type/dml/AdditionalDMLE2EIT.java    |  6 +++
 .../test/e2e/engine/type/dml/BaseDMLE2EIT.java     | 56 ++++++++++++++++++++--
 .../test/e2e/engine/type/dml/BatchDMLE2EIT.java    | 24 ++++++++++
 .../test/e2e/engine/type/dml/GeneralDMLE2EIT.java  | 12 +++++
 4 files changed, 93 insertions(+), 5 deletions(-)

diff --git 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/AdditionalDMLE2EIT.java
 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/AdditionalDMLE2EIT.java
index 6d4ed1b313a..b137d0b7b2f 100644
--- 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/AdditionalDMLE2EIT.java
+++ 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/AdditionalDMLE2EIT.java
@@ -26,6 +26,7 @@ import 
org.apache.shardingsphere.test.e2e.framework.param.array.E2ETestParameter
 import 
org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
 import org.apache.shardingsphere.test.e2e.framework.type.SQLCommandType;
 import org.apache.shardingsphere.test.e2e.framework.type.SQLExecuteType;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.condition.EnabledIf;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -324,4 +325,9 @@ class AdditionalDMLE2EIT extends BaseDMLE2EIT {
     private static boolean isEnabled() {
         return E2ETestParameterFactory.containsTestParameter() && 
E2ETestEnvironment.getInstance().isRunAdditionalTestCases();
     }
+    
+    @AfterEach
+    void tearDown() {
+        super.tearDown();
+    }
 }
diff --git 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BaseDMLE2EIT.java
 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BaseDMLE2EIT.java
index b8d272db66f..565e25cfefe 100644
--- 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BaseDMLE2EIT.java
+++ 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BaseDMLE2EIT.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.test.e2e.engine.type.dml;
 
+import com.google.common.base.Splitter;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.datanode.DataNode;
@@ -28,10 +29,10 @@ import 
org.apache.shardingsphere.test.e2e.cases.dataset.DataSetLoader;
 import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetColumn;
 import 
org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetMetaData;
 import org.apache.shardingsphere.test.e2e.cases.dataset.row.DataSetRow;
-import org.apache.shardingsphere.test.e2e.env.E2EEnvironmentAware;
-import org.apache.shardingsphere.test.e2e.env.E2EEnvironmentEngine;
 import org.apache.shardingsphere.test.e2e.engine.context.E2ETestContext;
 import org.apache.shardingsphere.test.e2e.env.DataSetEnvironmentManager;
+import org.apache.shardingsphere.test.e2e.env.E2EEnvironmentAware;
+import org.apache.shardingsphere.test.e2e.env.E2EEnvironmentEngine;
 import 
org.apache.shardingsphere.test.e2e.env.runtime.scenario.database.DatabaseEnvironmentManager;
 import 
org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath;
 import 
org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath.Type;
@@ -40,7 +41,7 @@ import 
org.apache.shardingsphere.test.e2e.framework.database.DatabaseAssertionMe
 import 
org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
 import 
org.apache.shardingsphere.test.e2e.framework.param.model.CaseTestParameter;
 import 
org.apache.shardingsphere.test.e2e.framework.param.model.E2ETestParameter;
-import org.junit.jupiter.api.AfterEach;
+import org.testcontainers.shaded.org.awaitility.Awaitility;
 
 import javax.sql.DataSource;
 import javax.xml.bind.JAXBException;
@@ -62,6 +63,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -88,13 +90,12 @@ public abstract class BaseDMLE2EIT implements 
E2EEnvironmentAware {
      * @throws IOException IO exception
      * @throws JAXBException JAXB exception
      */
-    public final void init(final E2ETestParameter testParam) throws 
SQLException, IOException, JAXBException {
+    protected void init(final E2ETestParameter testParam) throws SQLException, 
IOException, JAXBException {
         dataSetEnvironmentManager =
                 new DataSetEnvironmentManager(new 
ScenarioDataPath(testParam.getScenario()).getDataSetFile(Type.ACTUAL), 
getEnvironmentEngine().getActualDataSourceMap(), testParam.getDatabaseType());
         dataSetEnvironmentManager.fillData();
     }
     
-    @AfterEach
     void tearDown() {
         // TODO make sure test case can not be null
         if (null != dataSetEnvironmentManager) {
@@ -280,4 +281,49 @@ public abstract class BaseDMLE2EIT implements 
E2EEnvironmentAware {
         assertMetaData(generatedKeys.getMetaData(), 
generatedKeyDataSet.getMetaDataList().get(0).getColumns());
         assertRows(generatedKeys, generatedKeyDataSet.getRows(), databaseType);
     }
+    
+    protected void executeInitSQLs(final E2ETestCaseAssertion assertion) 
throws SQLException {
+        if (null == assertion.getInitialSQL()) {
+            return;
+        }
+        try (Connection connection = 
getEnvironmentEngine().getTargetDataSource().getConnection()) {
+            executeInitSQLs(assertion, connection);
+        }
+    }
+    
+    private void executeInitSQLs(final E2ETestCaseAssertion assertion, final 
Connection connection) throws SQLException {
+        if (null == assertion.getInitialSQL().getSql()) {
+            return;
+        }
+        for (String each : 
Splitter.on(";").trimResults().omitEmptyStrings().splitToList(assertion.getInitialSQL().getSql()))
 {
+            try (PreparedStatement preparedStatement = 
connection.prepareStatement(each)) {
+                preparedStatement.executeUpdate();
+            }
+            waitCompleted();
+        }
+    }
+    
+    private void waitCompleted() {
+        Awaitility.await().pollDelay(1500L, TimeUnit.MILLISECONDS).until(() -> 
true);
+    }
+    
+    protected void executeDestroySQLs(final E2ETestCaseAssertion assertion) 
throws SQLException {
+        if (null != assertion.getDestroySQL()) {
+            try (Connection connection = 
getEnvironmentEngine().getTargetDataSource().getConnection()) {
+                executeDestroySQLs(assertion, connection);
+            }
+        }
+    }
+    
+    private void executeDestroySQLs(final E2ETestCaseAssertion assertion, 
final Connection connection) throws SQLException {
+        if (null == assertion.getDestroySQL().getSql()) {
+            return;
+        }
+        for (String each : 
Splitter.on(";").trimResults().omitEmptyStrings().splitToList(assertion.getDestroySQL().getSql()))
 {
+            try (PreparedStatement preparedStatement = 
connection.prepareStatement(each)) {
+                preparedStatement.executeUpdate();
+            }
+            waitCompleted();
+        }
+    }
 }
diff --git 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BatchDMLE2EIT.java
 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BatchDMLE2EIT.java
index 9a5347420a7..cce603e75d0 100644
--- 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BatchDMLE2EIT.java
+++ 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/BatchDMLE2EIT.java
@@ -54,6 +54,29 @@ class BatchDMLE2EIT extends BaseDMLE2EIT {
             actualUpdateCounts = executeBatchForPreparedStatement(testParam, 
connection);
         }
         assertDataSet(actualUpdateCounts, testParam);
+        tearDown(testParam);
+    }
+    
+    void init(final CaseTestParameter testParam) throws SQLException, 
IOException, JAXBException {
+        super.init(testParam);
+        executeInitSQLs(testParam);
+    }
+    
+    void tearDown(final CaseTestParameter testParam) throws SQLException {
+        super.tearDown();
+        executeDestroySQLs(testParam);
+    }
+    
+    private void executeInitSQLs(final CaseTestParameter testParam) throws 
SQLException {
+        for (E2ETestCaseAssertion each : 
testParam.getTestCaseContext().getTestCase().getAssertions()) {
+            executeInitSQLs(each);
+        }
+    }
+    
+    private void executeDestroySQLs(final CaseTestParameter testParam) throws 
SQLException {
+        for (E2ETestCaseAssertion each : 
testParam.getTestCaseContext().getTestCase().getAssertions()) {
+            executeDestroySQLs(each);
+        }
     }
     
     private int[] executeBatchForPreparedStatement(final CaseTestParameter 
testParam, final Connection connection) throws SQLException {
@@ -90,6 +113,7 @@ class BatchDMLE2EIT extends BaseDMLE2EIT {
             preparedStatement.clearBatch();
             assertThat(preparedStatement.executeBatch().length, is(0));
         }
+        tearDown(testParam);
     }
     
     private static boolean isEnabled() {
diff --git 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/GeneralDMLE2EIT.java
 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/GeneralDMLE2EIT.java
index 1866f8ac938..46d3012d487 100644
--- 
a/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/GeneralDMLE2EIT.java
+++ 
b/test/e2e/sql/src/test/java/org/apache/shardingsphere/test/e2e/engine/type/dml/GeneralDMLE2EIT.java
@@ -58,6 +58,17 @@ class GeneralDMLE2EIT extends BaseDMLE2EIT {
                     : executeUpdateForPreparedStatement(context, connection);
         }
         assertDataSet(context, actualUpdateCount, testParam);
+        tearDown(context);
+    }
+    
+    void init(final AssertionTestParameter testParam) throws SQLException, 
IOException, JAXBException {
+        super.init(testParam);
+        executeInitSQLs(testParam.getAssertion());
+    }
+    
+    void tearDown(final E2ETestContext context) throws SQLException {
+        super.tearDown();
+        executeDestroySQLs(context.getAssertion());
     }
     
     private int executeUpdateForStatement(final E2ETestContext context, final 
Connection connection) throws SQLException {
@@ -92,6 +103,7 @@ class GeneralDMLE2EIT extends BaseDMLE2EIT {
                     : executeForPreparedStatement(context, connection);
         }
         assertDataSet(context, actualUpdateCount, testParam);
+        tearDown(context);
     }
     
     private int executeForStatement(final E2ETestContext context, final 
Connection connection) throws SQLException {

Reply via email to