You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/11/19 06:54:59 UTC

[shardingsphere] branch master updated: Use mock to instead of TestShardingRule and TestTableRule in DataNodesTest class (#8141)

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

kimmking 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 739e0b5  Use mock to instead of TestShardingRule and TestTableRule in DataNodesTest class (#8141)
739e0b5 is described below

commit 739e0b5362c5aaaeaa8f40a921e60aca6626ae9a
Author: wenweibin <41...@users.noreply.github.com>
AuthorDate: Thu Nov 19 14:51:22 2020 +0800

    Use mock to instead of TestShardingRule and TestTableRule in DataNodesTest class (#8141)
    
    * Improve mysql Call Statement definition and Visitor #7791
    
    * Improve mysql Call Statement definition and Visitor #7791
    
    * Improve mysql Call Statement definition and Visitor #7791
    
    * Improve mysql Call Statement definition and Visitor #7791
    
    * Use mock to instead of TestShardingRule and TestTableRule in DataNodesTest class
    
    * format-add four blank at empty line
    
    * delete unused TestShardingRule.java and TestTableRule.java file
---
 .../shardingsphere/infra/rule/DataNodesTest.java   | 27 ++++----
 .../infra/rule/fixture/TestShardingRule.java       | 65 ------------------
 .../infra/rule/fixture/TestTableRule.java          | 78 ----------------------
 3 files changed, 13 insertions(+), 157 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
index a95ba5c..8e3d126 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
@@ -19,26 +19,24 @@ package org.apache.shardingsphere.infra.rule;
 
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.datanode.DataNodes;
-import org.apache.shardingsphere.infra.rule.fixture.TestShardingRule;
-import org.apache.shardingsphere.infra.rule.fixture.TestShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.fixture.TestTableRule;
+import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
 import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import org.junit.Test;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
-import static org.hamcrest.CoreMatchers.is;
-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 DataNodesTest {
     
     private final String logicTableName1 = "user";
@@ -79,10 +77,11 @@ public final class DataNodesTest {
     }
     
     private DataNodes getRoutedRuleDataNodes() {
-        TestTableRule tableRule1 = new TestTableRule(dataSourceNames1, logicTableName1);
-        TestTableRule tableRule2 = new TestTableRule(dataSourceNames2, logicTableName2);
-        List<TestTableRule> tableRules = Arrays.asList(tableRule1, tableRule2);
-        ShardingSphereRule rule1 = new TestShardingRule(tableRules);
+        Map<String, Collection<DataNode>> nodeMap = new HashMap<>();
+        nodeMap.put(logicTableName1, getExpectedDataNodes(dataSourceNames1, logicTableName1));
+        nodeMap.put(logicTableName2, getExpectedDataNodes(dataSourceNames2, logicTableName2));
+        DataNodeContainedRule rule1 = mock(DataNodeContainedRule.class);
+        when(rule1.getAllDataNodes()).thenReturn(nodeMap);
         Map<String, Collection<String>> dataSourceMapper = Collections.singletonMap(logicDataSourceName, replicaDataSourceNames);
         DataSourceContainedRule rule2 = mock(DataSourceContainedRule.class);
         when(rule2.getDataSourceMapper()).thenReturn(dataSourceMapper);
@@ -90,7 +89,7 @@ public final class DataNodesTest {
     }
     
     private DataNodes getNonRoutedRuleDataNodes() {
-        return new DataNodes(Collections.singleton(new TestShardingSphereRule()));
+        return new DataNodes(Collections.singleton(mock(ShardingSphereRule.class)));
     }
     
     private Collection<DataNode> getExpectedDataNodes(final Collection<String> dataSourceNames, final String logicTableName) {
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingRule.java
deleted file mode 100644
index 70ad60a..0000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingRule.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.rule.fixture;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
-
-import java.util.Collection;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
-@RequiredArgsConstructor
-@Getter
-public final class TestShardingRule implements DataNodeContainedRule {
-    
-    private final Collection<TestTableRule> tableRules;
-    
-    @Override
-    public Map<String, Collection<DataNode>> getAllDataNodes() {
-        return tableRules.stream().collect(Collectors.toMap(TestTableRule::getLogicTable, TestTableRule::getActualDataNodes, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
-    }
-    
-    @Override
-    public Collection<String> getAllActualTables() {
-        return tableRules.stream().flatMap(each -> each.getActualDataNodes().stream().map(DataNode::getTableName)).collect(Collectors.toSet());
-    }
-    
-    @Override
-    public Optional<String> findFirstActualTable(final String logicTable) {
-        return findTableRule(logicTable).map(tableRule -> tableRule.getActualDataNodes().get(0).getTableName());
-    }
-
-    private Optional<TestTableRule> findTableRule(final String logicTableName) {
-        return tableRules.stream().filter(each -> each.getLogicTable().equalsIgnoreCase(logicTableName)).findFirst();
-    }
-    
-    @Override
-    public boolean isNeedAccumulate(final Collection<String> tables) {
-        return false;
-    }
-    
-    @Override
-    public Optional<String> findLogicTableByActualTable(final String actualTable) {
-        return Optional.empty();
-    }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestTableRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestTableRule.java
deleted file mode 100644
index 7c2caa1..0000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestTableRule.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.rule.fixture;
-
-import lombok.AccessLevel;
-import lombok.Getter;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-@Getter
-public final class TestTableRule {
-    
-    private final String logicTable;
-    
-    private final List<DataNode> actualDataNodes;
-    
-    @Getter(AccessLevel.NONE)
-    private final Set<String> actualTables;
-    
-    @Getter(AccessLevel.NONE)
-    private final Map<DataNode, Integer> dataNodeIndexMap;
-    
-    private final Map<String, Collection<String>> datasourceToTablesMap = new HashMap<>();
-    
-    private final Collection<String> actualDatasourceNames = new LinkedHashSet<>();
-    
-    public TestTableRule(final Collection<String> dataSourceNames, final String logicTableName) {
-        logicTable = logicTableName.toLowerCase();
-        dataNodeIndexMap = new HashMap<>(dataSourceNames.size(), 1);
-        actualDataNodes = generateDataNodes(logicTableName, dataSourceNames);
-        actualTables = getActualTables();
-    }
-    
-    private List<DataNode> generateDataNodes(final String logicTable, final Collection<String> dataSourceNames) {
-        List<DataNode> result = new LinkedList<>();
-        int index = 0;
-        for (String each : dataSourceNames) {
-            DataNode dataNode = new DataNode(each, logicTable);
-            result.add(dataNode);
-            dataNodeIndexMap.put(dataNode, index);
-            actualDatasourceNames.add(each);
-            addActualTable(dataNode.getDataSourceName(), dataNode.getTableName());
-            index++;
-        }
-        return result;
-    }
-    
-    private Set<String> getActualTables() {
-        return actualDataNodes.stream().map(DataNode::getTableName).collect(Collectors.toSet());
-    }
-    
-    private void addActualTable(final String datasourceName, final String tableName) {
-        datasourceToTablesMap.computeIfAbsent(datasourceName, key -> new LinkedHashSet<>()).add(tableName);
-    }
-}