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/08/10 10:38:56 UTC
[shardingsphere] branch master updated: Add test case for DataNodes
contains DataSourceRoutedRule branch (#6756)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 73d8666 Add test case for DataNodes contains DataSourceRoutedRule branch (#6756)
73d8666 is described below
commit 73d866673b6de770826df5aa891043c1ec14ccf5
Author: Andrew <zh...@aliyun.com>
AuthorDate: Mon Aug 10 18:38:41 2020 +0800
Add test case for DataNodes contains DataSourceRoutedRule branch (#6756)
* Add test case for DataNodeTest
* Add test case for DataNodes
---
.../shardingsphere/infra/rule/DataNodeTest.java | 7 +++++
.../shardingsphere/infra/rule/DataNodesTest.java | 34 ++++++++++++---------
.../rule/fixture/TestDataSourceRoutedRule.java | 35 ++++++++++++++++++++++
3 files changed, 62 insertions(+), 14 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
index cadfd08..195522f 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
@@ -23,6 +23,7 @@ import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@@ -56,6 +57,7 @@ public final class DataNodeTest {
assertThat(dataNode, is(new DataNode("ds_0.tbl_0")));
assertThat(dataNode, is(dataNode));
assertThat(dataNode, not(new DataNode("ds_0.tbl_1")));
+ assertFalse(dataNode.equals(null));
assertNotNull(dataNode);
}
@@ -68,4 +70,9 @@ public final class DataNodeTest {
public void assertToString() {
assertThat(new DataNode("ds_0.tbl_0").toString(), is("DataNode(dataSourceName=ds_0, tableName=tbl_0)"));
}
+
+ @Test
+ public void assertRequiredArgsConstructor() {
+ assertNotNull(new DataNode("ds_0", "tbl_0"));
+ }
}
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 b1e11f4..19d37c4 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,6 +19,7 @@ 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.TestDataSourceRoutedRule;
import org.apache.shardingsphere.infra.rule.fixture.TestShardingRule;
import org.apache.shardingsphere.infra.rule.fixture.TestShardingSphereRule;
import org.apache.shardingsphere.infra.rule.fixture.TestTableRule;
@@ -27,13 +28,14 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedHashMap;
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;
public final class DataNodesTest {
@@ -45,6 +47,10 @@ public final class DataNodesTest {
private final Collection<String> dataSourceNames2 = Arrays.asList("master_db_3", "slave_db_3");
+ private final String logicDataSourceName = "master_db_1";
+
+ private final Collection<String> replicaDataSourceNames = Arrays.asList("route_db_1", "route_db_2");
+
@Test(expected = NullPointerException.class)
public void assertWrongTable() {
DataNodes dataNodes = getRoutedRuleDataNodes();
@@ -59,8 +65,8 @@ public final class DataNodesTest {
@Test
public void assertGetDataNodes() {
DataNodes dataNodes = getRoutedRuleDataNodes();
- assertThat(dataNodes.getDataNodes(logicTableName1), is(getExpectedDataNodes(dataSourceNames1, logicTableName1)));
- assertThat(dataNodes.getDataNodes(logicTableName2), is(getExpectedDataNodes(dataSourceNames2, logicTableName2)));
+ assertTrue(dataNodes.getDataNodes(logicTableName1).containsAll(getExpectedDataNodes(dataSourceNames1, logicTableName1)));
+ assertTrue(dataNodes.getDataNodes(logicTableName2).containsAll(getExpectedDataNodes(dataSourceNames2, logicTableName2)));
}
@Test
@@ -74,8 +80,10 @@ public final class DataNodesTest {
TestTableRule tableRule1 = new TestTableRule(dataSourceNames1, logicTableName1);
TestTableRule tableRule2 = new TestTableRule(dataSourceNames2, logicTableName2);
List<TestTableRule> tableRules = Arrays.asList(tableRule1, tableRule2);
- ShardingSphereRule rule = new TestShardingRule(tableRules);
- return new DataNodes(Collections.singleton(rule));
+ ShardingSphereRule rule1 = new TestShardingRule(tableRules);
+ Map<String, Collection<String>> dataSourceMapper = Collections.singletonMap(logicDataSourceName, replicaDataSourceNames);
+ TestDataSourceRoutedRule rule2 = new TestDataSourceRoutedRule(dataSourceMapper);
+ return new DataNodes(Arrays.asList(rule1, rule2));
}
private DataNodes getNonRoutedRuleDataNodes() {
@@ -85,18 +93,16 @@ public final class DataNodesTest {
private Collection<DataNode> getExpectedDataNodes(final Collection<String> dataSourceNames, final String logicTableName) {
Collection<DataNode> result = new LinkedList<>();
for (String each : dataSourceNames) {
- result.add(new DataNode(each, logicTableName));
+ if (logicDataSourceName.equals(each)) {
+ replicaDataSourceNames.stream().forEach(a -> result.add(new DataNode(a, logicTableName)));
+ } else {
+ result.add(new DataNode(each, logicTableName));
+ }
}
return result;
}
- private Map<String, Collection<DataNode>> getExpectedDataNodeGroups(final Collection<String> dataSourceNames, final String logicTableName) {
- Map<String, Collection<DataNode>> result = new LinkedHashMap<>(dataSourceNames.size(), 1);
- for (String each : dataSourceNames) {
- Collection<DataNode> self = new LinkedList<>();
- self.add(new DataNode(each, logicTableName));
- result.put(each, self);
- }
- return result;
+ private Map<String, List<DataNode>> getExpectedDataNodeGroups(final Collection<String> dataSourceNames, final String logicTableName) {
+ return getExpectedDataNodes(dataSourceNames, logicTableName).stream().collect(Collectors.groupingBy(DataNode::getDataSourceName));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestDataSourceRoutedRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestDataSourceRoutedRule.java
new file mode 100644
index 0000000..df056e8
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestDataSourceRoutedRule.java
@@ -0,0 +1,35 @@
+/*
+ * 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.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.DataSourceRoutedRule;
+
+import java.util.Collection;
+import java.util.Map;
+
+@RequiredArgsConstructor
+public final class TestDataSourceRoutedRule implements DataSourceRoutedRule {
+
+ private final Map<String, Collection<String>> dataSourceMapper;
+
+ @Override
+ public Map<String, Collection<String>> getDataSourceMapper() {
+ return dataSourceMapper;
+ }
+}