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 2022/04/20 00:26:39 UTC

[shardingsphere] branch master updated: Refactor ShardingRuleConfigurationCheckerTest (#16935)

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 e4412b84bae Refactor ShardingRuleConfigurationCheckerTest (#16935)
e4412b84bae is described below

commit e4412b84bae893cc5d5330943e6ae626f2325d0f
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Apr 20 08:26:22 2022 +0800

    Refactor ShardingRuleConfigurationCheckerTest (#16935)
    
    * Refactor DataConsistencyChecker
    
    * Refactor ShardingRuleConfigurationCheckerTest
    
    * Refactor RenameTableStatementSchemaRefresherTest
    
    * Refactor RenameTableStatementSchemaRefresherTest
    
    * Refactor RenameTableStatementSchemaRefresherTest
    
    * Refactor RenameTableStatementSchemaRefresherTest
    
    * Refactor RenameTableStatementSchemaRefresherTest
---
 .../ShardingRuleConfigurationCheckerTest.java      |  32 ++---
 .../RenameTableStatementSchemaRefresherTest.java   | 150 +++++++--------------
 .../check/consistency/DataConsistencyChecker.java  |   4 +-
 3 files changed, 63 insertions(+), 123 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
index 5e6920e5c4a..439ee8c9ab4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/checker/ShardingRuleConfigurationCheckerTest.java
@@ -18,44 +18,39 @@
 package org.apache.shardingsphere.sharding.checker;
 
 import org.apache.shardingsphere.infra.config.checker.RuleConfigurationChecker;
+import org.apache.shardingsphere.infra.config.checker.RuleConfigurationCheckerFactory;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
 import org.junit.Test;
 
 import java.util.Collections;
+import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 public final class ShardingRuleConfigurationCheckerTest {
     
-    static {
-        ShardingSphereServiceLoader.register(RuleConfigurationChecker.class);
-    }
-    
     @SuppressWarnings({"rawtypes", "unchecked"})
     @Test
     public void assertValidCheck() {
         ShardingRuleConfiguration config = getValidConfiguration();
-        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(RuleConfigurationChecker.class, Collections.singleton(config)).get(config);
-        assertThat(checker, instanceOf(ShardingRuleConfigurationChecker.class));
-        checker.check("test", config);
+        Optional<RuleConfigurationChecker> checker = RuleConfigurationCheckerFactory.newInstance(config);
+        assertTrue(checker.isPresent());
+        assertThat(checker.get(), instanceOf(ShardingRuleConfigurationChecker.class));
+        checker.get().check("test", config);
     }
     
     private ShardingRuleConfiguration getValidConfiguration() {
-        ShardingStrategyConfiguration strategyConfiguration = mock(ShardingStrategyConfiguration.class);
-        ShardingTableRuleConfiguration ruleConfiguration = mock(ShardingTableRuleConfiguration.class);
-        ShardingAutoTableRuleConfiguration autoTableRuleConfiguration = mock(ShardingAutoTableRuleConfiguration.class);
         ShardingRuleConfiguration result = mock(ShardingRuleConfiguration.class);
-        when(result.getTables()).thenReturn(Collections.singleton(ruleConfiguration));
-        when(result.getAutoTables()).thenReturn(Collections.singleton(autoTableRuleConfiguration));
-        when(result.getDefaultTableShardingStrategy()).thenReturn(strategyConfiguration);
+        when(result.getTables()).thenReturn(Collections.singleton(mock(ShardingTableRuleConfiguration.class)));
+        when(result.getAutoTables()).thenReturn(Collections.singleton(mock(ShardingAutoTableRuleConfiguration.class)));
+        when(result.getDefaultTableShardingStrategy()).thenReturn(mock(ShardingStrategyConfiguration.class));
         return result;
     }
     
@@ -63,9 +58,10 @@ public final class ShardingRuleConfigurationCheckerTest {
     @Test(expected = IllegalStateException.class)
     public void assertInvalidCheck() {
         ShardingRuleConfiguration config = getInvalidConfiguration();
-        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(RuleConfigurationChecker.class, Collections.singleton(config)).get(config);
-        assertThat(checker, instanceOf(ShardingRuleConfigurationChecker.class));
-        checker.check("test", config);
+        Optional<RuleConfigurationChecker> checker = RuleConfigurationCheckerFactory.newInstance(config);
+        assertTrue(checker.isPresent());
+        assertThat(checker.get(), instanceOf(ShardingRuleConfigurationChecker.class));
+        checker.get().check("test", config);
     }
     
     private ShardingRuleConfiguration getInvalidConfiguration() {
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/refresher/type/RenameTableStatementSchemaRefresherTest.java b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/refresher/type/RenameTableStatementSchemaRefresherTest.java
index cd3b630367e..a9673550f13 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/refresher/type/RenameTableStatementSchemaRefresherTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/refresher/type/RenameTableStatementSchemaRefresherTest.java
@@ -18,12 +18,11 @@
 package org.apache.shardingsphere.infra.context.refresher.type;
 
 import com.google.common.eventbus.Subscribe;
+import lombok.Getter;
 import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.type.dialect.SQL92DatabaseType;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
-import org.apache.shardingsphere.infra.federation.optimizer.context.planner.OptimizerPlannerContext;
 import org.apache.shardingsphere.infra.federation.optimizer.metadata.FederationDatabaseMetaData;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
@@ -35,125 +34,70 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Sim
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.RenameTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-import javax.sql.DataSource;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedList;
+
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class RenameTableStatementSchemaRefresherTest {
-
-    private final String oriTableNamePrefix = "TABLE";
-
-    private final String newTableNamePrefix = "NEW_TABLE";
-
-    private final String schemaMetaDataName = "SCHEMA_META_DATA_NAME";
-
-    private final String databaseName = "DATABASE_NAME";
-
-    private final String logicDataSourceName = "LOGIC_DATA_SOURCE_NAME";
-
-    @Mock
-    private ShardingSphereMetaData schemaMetaData;
-
-    @Mock
-    private FederationDatabaseMetaData database;
-
-    @Mock
-    private Map<String, OptimizerPlannerContext> optimizerPlanners;
-
-    private List<String> logicDataSourceNames;
-
-    @Mock
-    private RenameTableStatement sqlStatement;
-
-    @InjectMocks
-    private ConfigurationProperties props;
-
-    @Mock
-    private ShardingSphereResource shardingSphereResource;
-
-    @Mock
-    private ShardingSphereSchema shardingSphereSchema;
-
-    private RenameTableStatementSchemaRefresher renameTableStatementSchemaRefresher;
-
-    @Before
-    public void setup() {
-        renameTableStatementSchemaRefresher = new RenameTableStatementSchemaRefresher();
-    }
-
+    
     @Test
-    public void assertRefreshNonRenameTableStatement() {
-        refreshRenameTableStatement(0);
+    public void assertRefresh() throws SQLException {
+        RenameTableLister listener = new RenameTableLister(2);
+        ShardingSphereEventBus.getInstance().register(listener);
+        new RenameTableStatementSchemaRefresher().refresh(createShardingSphereMetaData(), new FederationDatabaseMetaData("foo_database", Collections.emptyMap()), 
+                new HashMap<>(), Collections.singleton("foo_ds"), createRenameTableStatement(), mock(ConfigurationProperties.class));
+        assertThat(listener.getActualCount(), is(listener.getRenameCount()));
+        ShardingSphereEventBus.getInstance().unregister(listener);
     }
-
-    @Test
-    public void assertRefreshOneRenameTableStatement() {
-        refreshRenameTableStatement(1);
+    
+    private RenameTableStatement createRenameTableStatement() {
+        RenameTableStatement result = mock(RenameTableStatement.class);
+        when(result.getRenameTables()).thenReturn(
+                Arrays.asList(createRenameTableDefinitionSegment("tbl_0", "new_tbl_0"), createRenameTableDefinitionSegment("tbl_1", "new_tbl_1")));
+        return result;
     }
-
-    @Test
-    public void assertRefreshMultipleRenameTableStatement() {
-        refreshRenameTableStatement(2);
+    
+    
+    private RenameTableDefinitionSegment createRenameTableDefinitionSegment(final String originTableName, final String newTableName) {
+        RenameTableDefinitionSegment result = new RenameTableDefinitionSegment(0, 1);
+        result.setTable(new SimpleTableSegment(new TableNameSegment(0, 1, new IdentifierValue(originTableName))));
+        result.setRenameTable(new SimpleTableSegment(new TableNameSegment(0, 1, new IdentifierValue(newTableName))));
+        return result;
     }
-
-    @SneakyThrows
-    private void refreshRenameTableStatement(final int renameStatementCount) {
-        Collection<RenameTableDefinitionSegment> renameTables = new LinkedList<>();
-        for (int i = 0; i < renameStatementCount; i++) {
-            SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new TableNameSegment(0, 1, new IdentifierValue(oriTableNamePrefix + i)));
-            SimpleTableSegment newSimpleTableSegment = new SimpleTableSegment(new TableNameSegment(0, 1, new IdentifierValue(newTableNamePrefix + i)));
-            RenameTableDefinitionSegment renameTableDefinitionSegment = new RenameTableDefinitionSegment(0, 1);
-            renameTableDefinitionSegment.setTable(simpleTableSegment);
-            renameTableDefinitionSegment.setRenameTable(newSimpleTableSegment);
-            renameTables.add(renameTableDefinitionSegment);
-        }
-        when(sqlStatement.getRenameTables()).thenReturn(renameTables);
-        when(schemaMetaData.getName()).thenReturn(schemaMetaDataName);
-        ShardingSphereRuleMetaData shardingSphereRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(), new LinkedList<>());
-        when(schemaMetaData.getRuleMetaData()).thenReturn(shardingSphereRuleMetaData);
-        when(schemaMetaData.getResource()).thenReturn(shardingSphereResource);
-        when(schemaMetaData.getDefaultSchema()).thenReturn(shardingSphereSchema);
-        doNothing().when(shardingSphereSchema).remove(anyString());
-        Map<String, DataSource> dataSources = new HashMap<>();
-        when(shardingSphereResource.getDataSources()).thenReturn(dataSources);
-        when(shardingSphereResource.getDatabaseType()).thenReturn(new SQL92DatabaseType());
-        when(database.getName()).thenReturn(databaseName);
-        logicDataSourceNames = new LinkedList<>();
-        logicDataSourceNames.add(logicDataSourceName);
-        RenameTableLister listener = new RenameTableLister(renameStatementCount);
-        ShardingSphereEventBus.getInstance().register(listener);
-        renameTableStatementSchemaRefresher.refresh(schemaMetaData, database, optimizerPlanners, logicDataSourceNames, sqlStatement, props);
-        assertThat(listener.actualCount, is(listener.renameCount));
-        ShardingSphereEventBus.getInstance().unregister(listener);
+    
+    private ShardingSphereMetaData createShardingSphereMetaData() {
+        return new ShardingSphereMetaData("foo_database",
+                mockShardingSphereResource(), new ShardingSphereRuleMetaData(new LinkedList<>(), new LinkedList<>()), Collections.singletonMap("foo_database", mock(ShardingSphereSchema.class)));
     }
-
+    
+    private ShardingSphereResource mockShardingSphereResource() {
+        ShardingSphereResource result = mock(ShardingSphereResource.class);
+        when(result.getDataSources()).thenReturn(Collections.emptyMap());
+        when(result.getDatabaseType()).thenReturn(new SQL92DatabaseType());
+        return result;
+    }
+    
     @RequiredArgsConstructor
-    private final class RenameTableLister {
-
+    @Getter
+    private static final class RenameTableLister {
+        
         private final int renameCount;
-
+        
         private int actualCount = -1;
-
+        
         @Subscribe
         public void process(final Object message) {
             if (message instanceof SchemaAlteredEvent) {
-                SchemaAlteredEvent newMessage = (SchemaAlteredEvent) message;
-                actualCount = newMessage.getAlteredTables().size();
+                actualCount = ((SchemaAlteredEvent) message).getAlteredTables().size();
             }
         }
     }
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/DataConsistencyChecker.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/DataConsistencyChecker.java
index 69968a43808..60209337146 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/DataConsistencyChecker.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/core/check/consistency/DataConsistencyChecker.java
@@ -86,7 +86,7 @@ public final class DataConsistencyChecker {
     public Map<String, DataConsistencyCheckResult> check(final DataConsistencyCalculateAlgorithm calculator) {
         Map<String, DataConsistencyCountCheckResult> countCheckResult = checkCount();
         Map<String, DataConsistencyContentCheckResult> contentCheckResult = countCheckResult.values().stream().allMatch(DataConsistencyCountCheckResult::isMatched)
-                ? checkContent(calculator) : Collections.emptyMap();
+                ? checkData(calculator) : Collections.emptyMap();
         Map<String, DataConsistencyCheckResult> result = new LinkedHashMap<>(countCheckResult.size());
         for (Entry<String, DataConsistencyCountCheckResult> entry : countCheckResult.entrySet()) {
             result.put(entry.getKey(), new DataConsistencyCheckResult(entry.getValue(), contentCheckResult.getOrDefault(entry.getKey(), new DataConsistencyContentCheckResult(false))));
@@ -140,7 +140,7 @@ public final class DataConsistencyChecker {
         }
     }
     
-    private Map<String, DataConsistencyContentCheckResult> checkContent(final DataConsistencyCalculateAlgorithm calculator) {
+    private Map<String, DataConsistencyContentCheckResult> checkData(final DataConsistencyCalculateAlgorithm calculator) {
         PipelineDataSourceConfiguration sourceDataSourceConfig = getPipelineDataSourceConfiguration(calculator, jobConfig.getPipelineConfig().getSource());
         PipelineDataSourceConfiguration targetDataSourceConfig = getPipelineDataSourceConfiguration(calculator, jobConfig.getPipelineConfig().getTarget());
         ThreadFactory threadFactory = ExecutorThreadFactoryBuilder.build("job-" + jobConfig.getHandleConfig().getJobIdDigest() + "-data-check-%d");