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

[shardingsphere] branch master updated: Remove useless DMLIntegrateTestCaseAssertion.dmlReturning (#8849)

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

zhangyonglun 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 c44153f  Remove useless DMLIntegrateTestCaseAssertion.dmlReturning (#8849)
c44153f is described below

commit c44153f24984b2d377fbaeb8d8819fba90ece7fb
Author: Liang Zhang <te...@163.com>
AuthorDate: Thu Dec 31 20:08:08 2020 +0800

    Remove useless DMLIntegrateTestCaseAssertion.dmlReturning (#8849)
    
    * Refactor BaseDMLIT
    
    * Rename DataSetEnvironmentManager.clearData
    
    * Refactor DataSetEnvironmentManager
    
    * Refactor BaseDMLIT
    
    * Add todo for using multiple threads
    
    * Add todo for using multiple threads
    
    * Remove useless DMLIntegrateTestCaseAssertion.dmlReturning
---
 .../dml/DMLIntegrateTestCaseAssertion.java         |  4 -
 .../test/integration/engine/it/BatchIT.java        |  2 +-
 .../test/integration/engine/it/dml/BaseDMLIT.java  | 95 ++++++++++++----------
 .../integration/engine/it/dml/GeneralDMLIT.java    | 24 +-----
 .../env/dataset/DataSetEnvironmentManager.java     | 18 ++--
 .../env/schema/SchemaEnvironmentManager.java       |  4 +
 6 files changed, 65 insertions(+), 82 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/cases/assertion/dml/DMLIntegrateTestCaseAssertion.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/cases/assertion/dml/DMLIntegrateTestCaseAssertion.java
index 14f882e..ff750d3 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/cases/assertion/dml/DMLIntegrateTestCaseAssertion.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/cases/assertion/dml/DMLIntegrateTestCaseAssertion.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.test.integration.cases.assertion.dml;
 
-import javax.xml.bind.annotation.XmlAttribute;
 import lombok.Getter;
 import org.apache.shardingsphere.test.integration.cases.assertion.root.IntegrateTestCaseAssertion;
 
@@ -26,7 +25,4 @@ import org.apache.shardingsphere.test.integration.cases.assertion.root.Integrate
  */
 @Getter
 public final class DMLIntegrateTestCaseAssertion extends IntegrateTestCaseAssertion {
-    
-    @XmlAttribute(name = "dml-returning")
-    private boolean dmlReturning;
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BatchIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BatchIT.java
index d2a9cc7..4c9e873 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BatchIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BatchIT.java
@@ -99,7 +99,7 @@ public abstract class BatchIT extends BaseIT {
     
     @After
     public void clearData() {
-        dataSetEnvironmentManager.clear();
+        dataSetEnvironmentManager.clearData();
     }
     
     protected final void assertDataSet(final int[] actualUpdateCounts) throws SQLException {
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BaseDMLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BaseDMLIT.java
index ae68d2d..685ecfe 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BaseDMLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BaseDMLIT.java
@@ -65,23 +65,22 @@ public abstract class BaseDMLIT extends SingleIT {
     @BeforeClass
     public static void initDatabasesAndTables() throws IOException, JAXBException {
         SchemaEnvironmentManager.createDatabases();
-        SchemaEnvironmentManager.dropTables();
         SchemaEnvironmentManager.createTables();
     }
     
     @AfterClass
-    public static void destroyDatabasesAndTables() throws IOException, JAXBException {
+    public static void destroyDatabases() throws IOException, JAXBException {
         SchemaEnvironmentManager.dropDatabases();
     }
     
     @Before
-    public void fillData() throws SQLException, ParseException {
+    public final void fillData() throws SQLException, ParseException {
         dataSetEnvironmentManager.fillData();
     }
     
     @After
-    public void clearData() {
-        dataSetEnvironmentManager.clear();
+    public final void clearData() {
+        dataSetEnvironmentManager.clearData();
     }
     
     protected final void assertDataSet(final int actualUpdateCount) throws SQLException {
@@ -90,65 +89,71 @@ public abstract class BaseDMLIT extends SingleIT {
         DataSetMetadata expectedDataSetMetadata = getDataSet().getMetadataList().get(0);
         for (String each : new InlineExpressionParser(expectedDataSetMetadata.getDataNodes()).splitAndEvaluate()) {
             DataNode dataNode = new DataNode(each);
-            String sql;
-            if (getDatabaseType() instanceof PostgreSQLDatabaseType) {
-                try (Connection connection = getActualDataSources().get(dataNode.getDataSourceName()).getConnection()) {
-                    String primaryKeyColumnName = getPostgreSQLTablePrimaryKeyColumnName(connection, dataNode.getTableName());
-                    sql = String.format("SELECT * FROM %s ORDER BY %s ASC", dataNode.getTableName(), primaryKeyColumnName);
-                }
-            } else {
-                sql = String.format("SELECT * FROM %s", dataNode.getTableName());
-            }
-            try (Connection connection = getActualDataSources().get(dataNode.getDataSourceName()).getConnection();
-                 PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
-                assertDataSet(preparedStatement, getDataSet().findRows(dataNode), expectedDataSetMetadata);
+            try (
+                    Connection connection = getActualDataSources().get(dataNode.getDataSourceName()).getConnection();
+                    PreparedStatement preparedStatement = connection.prepareStatement(generateFetchActualDataSQL(dataNode))) {
+                assertDataSet(preparedStatement, expectedDataSetMetadata, getDataSet().findRows(dataNode));
             }
         }
     }
     
-    private void assertDataSet(final PreparedStatement actualPreparedStatement, final List<DataSetRow> expectedDataSetRows, final DataSetMetadata expectedDataSetMetadata) throws SQLException {
+    private void assertDataSet(final PreparedStatement actualPreparedStatement, final DataSetMetadata expectedDataSetMetadata, final List<DataSetRow> expectedDataSetRows) throws SQLException {
         try (ResultSet actualResultSet = actualPreparedStatement.executeQuery()) {
             assertMetaData(actualResultSet.getMetaData(), expectedDataSetMetadata.getColumns());
             assertRows(actualResultSet, expectedDataSetRows);
         }
     }
     
-    private String getPostgreSQLTablePrimaryKeyColumnName(final Connection connection, final String tableName) throws SQLException {
-        String sql = "SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type FROM pg_index i JOIN pg_attribute a "
-            + "ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) WHERE i.indrelid = '"
-            + tableName + "'::regclass AND i.indisprimary";
-        try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
-            if (!resultSet.next()) {
-                throw new SQLException("can not get primary key of " + tableName);
+    private String generateFetchActualDataSQL(final DataNode dataNode) throws SQLException {
+        if (getDatabaseType() instanceof PostgreSQLDatabaseType) {
+            String primaryKeyColumnName = getPrimaryKeyColumnNameForPostgreSQL(dataNode);
+            return String.format("SELECT * FROM %s ORDER BY %s ASC", dataNode.getTableName(), primaryKeyColumnName);
+        }
+        return String.format("SELECT * FROM %s", dataNode.getTableName());
+    }
+    
+    private String getPrimaryKeyColumnNameForPostgreSQL(final DataNode dataNode) throws SQLException {
+        String sql = String.format("SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type "
+                + "FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) WHERE i.indrelid = '%s'::regclass AND i.indisprimary", dataNode.getTableName());
+        try (
+                Connection connection = getActualDataSources().get(dataNode.getDataSourceName()).getConnection();
+                Statement statement = connection.createStatement();
+                ResultSet resultSet = statement.executeQuery(sql)) {
+            if (resultSet.next()) {
+                return resultSet.getString("attname");
             }
-            return resultSet.getString("attname");
+            throw new SQLException(String.format("Can not get primary key of `%s`", dataNode.getTableName()));
         }
     }
     
-    private void assertMetaData(final ResultSetMetaData actualMetaData, final Collection<DataSetColumn> columnMetadataList) throws SQLException {
-        assertThat(actualMetaData.getColumnCount(), is(columnMetadataList.size()));
+    private void assertMetaData(final ResultSetMetaData actual, final Collection<DataSetColumn> expected) throws SQLException {
+        assertThat(actual.getColumnCount(), is(expected.size()));
         int index = 1;
-        for (DataSetColumn each : columnMetadataList) {
-            assertThat(actualMetaData.getColumnLabel(index++), is(each.getName()));
+        for (DataSetColumn each : expected) {
+            assertThat(actual.getColumnLabel(index++), is(each.getName()));
+        }
+    }
+    
+    private void assertRows(final ResultSet actual, final List<DataSetRow> expected) throws SQLException {
+        int rowCount = 0;
+        while (actual.next()) {
+            int columnIndex = 1;
+            for (String each : expected.get(rowCount).getValues()) {
+                assertValue(actual, columnIndex, each);
+                columnIndex++;
+            }
+            rowCount++;
         }
+        assertThat("Size of actual result set is different with size of expected dat set rows.", rowCount, is(expected.size()));
     }
     
-    private void assertRows(final ResultSet actualResultSet, final List<DataSetRow> expectedDatSetRows) throws SQLException {
-        int count = 0;
-        while (actualResultSet.next()) {
-            int index = 1;
-            for (String each : expectedDatSetRows.get(count).getValues()) {
-                if (Types.DATE == actualResultSet.getMetaData().getColumnType(index)) {
-                    if (!NOT_VERIFY_FLAG.equals(each)) {
-                        assertThat(new SimpleDateFormat("yyyy-MM-dd").format(actualResultSet.getDate(index)), is(each));
-                    }
-                } else {
-                    assertThat(String.valueOf(actualResultSet.getObject(index)), is(each));
-                }
-                index++;
+    private void assertValue(final ResultSet actual, final int columnIndex, final String expected) throws SQLException {
+        if (Types.DATE == actual.getMetaData().getColumnType(columnIndex)) {
+            if (!NOT_VERIFY_FLAG.equals(expected)) {
+                assertThat(new SimpleDateFormat("yyyy-MM-dd").format(actual.getDate(columnIndex)), is(expected));
             }
-            count++;
+        } else {
+            assertThat(String.valueOf(actual.getObject(columnIndex)), is(expected));
         }
-        assertThat("Size of actual result set is different with size of expected dat set rows.", count, is(expectedDatSetRows.size()));
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/GeneralDMLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/GeneralDMLIT.java
index 08e88df..c6b6bc1 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/GeneralDMLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/GeneralDMLIT.java
@@ -18,11 +18,11 @@
 package org.apache.shardingsphere.test.integration.engine.it.dml;
 
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import org.apache.shardingsphere.test.integration.engine.param.IntegrateTestParameters;
 import org.apache.shardingsphere.test.integration.cases.IntegrateTestCaseType;
 import org.apache.shardingsphere.test.integration.cases.assertion.dml.DMLIntegrateTestCaseAssertion;
 import org.apache.shardingsphere.test.integration.cases.assertion.root.SQLCaseType;
 import org.apache.shardingsphere.test.integration.cases.assertion.root.SQLValue;
+import org.apache.shardingsphere.test.integration.engine.param.IntegrateTestParameters;
 import org.junit.Test;
 import org.junit.runners.Parameterized.Parameters;
 
@@ -30,7 +30,6 @@ import javax.xml.bind.JAXBException;
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.text.ParseException;
@@ -63,9 +62,6 @@ public final class GeneralDMLIT extends BaseDMLIT {
         if ("shadow".equals(getRuleType())) {
             return;
         }
-        if (assertion.isDmlReturning()) {
-            return;
-        }
         int actualUpdateCount;
         try (Connection connection = getTargetDataSource().getConnection()) {
             actualUpdateCount = SQLCaseType.Literal == getCaseType() ? executeUpdateForStatement(connection) : executeUpdateForPreparedStatement(connection);
@@ -107,15 +103,6 @@ public final class GeneralDMLIT extends BaseDMLIT {
     
     private int executeForStatement(final Connection connection) throws SQLException {
         try (Statement statement = connection.createStatement()) {
-            if (assertion.isDmlReturning()) {
-                if (statement.execute(getSql())) {
-                    ResultSet resultSet = statement.getResultSet();
-                    boolean moreResults = statement.getMoreResults();
-                    // TODO resultSet is null
-                    return moreResults || null != resultSet ? statement.getUpdateCount() : 1;
-                }
-                return statement.getUpdateCount();
-            }
             assertFalse("Not a DML statement.", statement.execute(getSql()));
             return statement.getUpdateCount();
         }
@@ -126,15 +113,6 @@ public final class GeneralDMLIT extends BaseDMLIT {
             for (SQLValue each : assertion.getSQLValues()) {
                 preparedStatement.setObject(each.getIndex(), each.getValue());
             }
-            if (assertion.isDmlReturning()) {
-                if (preparedStatement.execute()) {
-                    ResultSet resultSet = preparedStatement.getResultSet();
-                    boolean moreResults = preparedStatement.getMoreResults();
-                    // TODO resultSet is null
-                    return moreResults || null != resultSet ? preparedStatement.getUpdateCount() : 1;
-                }
-                return preparedStatement.getUpdateCount();
-            }
             assertFalse("Not a DML statement.", preparedStatement.execute());
             return preparedStatement.getUpdateCount();
         }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/dataset/DataSetEnvironmentManager.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/dataset/DataSetEnvironmentManager.java
index 24ffce2..4e5e79e 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/dataset/DataSetEnvironmentManager.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/dataset/DataSetEnvironmentManager.java
@@ -60,13 +60,13 @@ public final class DataSetEnvironmentManager {
     
     private final DataSet dataSet;
     
-    private final Map<String, DataSource> dataSourceMap;
+    private final Map<String, DataSource> actualDataSources;
     
-    public DataSetEnvironmentManager(final String dataSetFile, final Map<String, DataSource> dataSourceMap) throws IOException, JAXBException {
+    public DataSetEnvironmentManager(final String dataSetFile, final Map<String, DataSource> actualDataSources) throws IOException, JAXBException {
         try (FileReader reader = new FileReader(dataSetFile)) {
             dataSet = (DataSet) JAXBContext.newInstance(DataSet.class).createUnmarshaller().unmarshal(reader);
         }
-        this.dataSourceMap = dataSourceMap;
+        this.actualDataSources = actualDataSources;
     }
     
     private static String generateTableName(final String tableName, final DatabaseType databaseType) {
@@ -85,7 +85,7 @@ public final class DataSetEnvironmentManager {
      */
     public void fillData() throws SQLException, ParseException {
         Map<DataNode, List<DataSetRow>> dataNodeListMap = getDataSetRowMap();
-        List<Callable<Void>> insertTasks = new LinkedList<>();
+        List<Callable<Void>> fillDataTasks = new LinkedList<>();
         for (Entry<DataNode, List<DataSetRow>> entry : dataNodeListMap.entrySet()) {
             DataNode dataNode = entry.getKey();
             List<DataSetRow> dataSetRows = entry.getValue();
@@ -95,13 +95,13 @@ public final class DataSetEnvironmentManager {
                 sqlValueGroups.add(new SQLValueGroup(dataSetMetadata, row.getValues()));
             }
             String insertSQL;
-            try (Connection connection = dataSourceMap.get(dataNode.getDataSourceName()).getConnection()) {
+            try (Connection connection = actualDataSources.get(dataNode.getDataSourceName()).getConnection()) {
                 insertSQL = generateInsertSQL(generateTableName(dataNode.getTableName(), DatabaseTypeRegistry.getDatabaseTypeByURL(connection.getMetaData().getURL())), dataSetMetadata.getColumns());
             }
-            insertTasks.add(new InsertTask(dataSourceMap.get(dataNode.getDataSourceName()), insertSQL, sqlValueGroups));
+            fillDataTasks.add(new InsertTask(actualDataSources.get(dataNode.getDataSourceName()), insertSQL, sqlValueGroups));
         }
         try {
-            EXECUTOR_SERVICE_MANAGER.getExecutorService().invokeAll(insertTasks);
+            EXECUTOR_SERVICE_MANAGER.getExecutorService().invokeAll(fillDataTasks);
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
@@ -134,10 +134,10 @@ public final class DataSetEnvironmentManager {
      * Clear data.
      * 
      */
-    public void clear() {
+    public void clearData() {
         List<Callable<Void>> deleteTasks = new LinkedList<>();
         for (Entry<String, Collection<String>> entry : getDataNodeMap().entrySet()) {
-            deleteTasks.add(new DeleteTask(dataSourceMap.get(entry.getKey()), entry.getValue()));
+            deleteTasks.add(new DeleteTask(actualDataSources.get(entry.getKey()), entry.getValue()));
         }
         try {
             EXECUTOR_SERVICE_MANAGER.getExecutorService().invokeAll(deleteTasks);
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 2ac5b4a..eb9aadb 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
@@ -78,6 +78,7 @@ public final class SchemaEnvironmentManager {
     private static void createDatabases(final String ruleType) throws IOException, JAXBException {
         SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
+            // TODO use multiple threads to improve performance
             DataSource dataSource = ActualDataSourceBuilder.build(null, each);
             executeSQLScript(dataSource, generateCreateDatabaseSQLs(each, schemaEnvironment.getDatabases()));
         }
@@ -109,6 +110,7 @@ public final class SchemaEnvironmentManager {
     private static void dropDatabases(final String ruleType) throws IOException, JAXBException {
         SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
+            // TODO use multiple threads to improve performance
             DataSource dataSource = ActualDataSourceBuilder.build(null, each);
             executeSQLScript(dataSource, generatePrepareDropDatabaseSQLs(each, schemaEnvironment.getDatabases()));
             executeSQLScript(dataSource, generateDropDatabaseSQLs(each, schemaEnvironment.getDatabases()));
@@ -155,6 +157,7 @@ public final class SchemaEnvironmentManager {
     
     private static void createTables(final SchemaEnvironment schemaEnvironment, final DatabaseType databaseType) {
         for (String each : schemaEnvironment.getDatabases()) {
+            // TODO use multiple threads to improve performance
             DataSource dataSource = ActualDataSourceBuilder.build(each, databaseType);
             executeSQLScript(dataSource, schemaEnvironment.getTableCreateSQLs());
         }
@@ -181,6 +184,7 @@ public final class SchemaEnvironmentManager {
     
     private static void dropTables(final SchemaEnvironment schemaEnvironment, final DatabaseType databaseType) {
         for (String each : schemaEnvironment.getDatabases()) {
+            // TODO use multiple threads to improve performance
             DataSource dataSource = ActualDataSourceBuilder.build(each, databaseType);
             executeSQLScript(dataSource, schemaEnvironment.getTableDropSQLs());
         }