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 2021/06/12 05:07:57 UTC

[shardingsphere] branch master updated: Add test case for RuleConfigurationChecker (#10778)

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 c1f1152  Add test case for RuleConfigurationChecker (#10778)
c1f1152 is described below

commit c1f115258ef8d127cbfbca43a011fc08ef9296a5
Author: Zhu jun <zh...@163.com>
AuthorDate: Sat Jun 12 13:07:20 2021 +0800

    Add test case for RuleConfigurationChecker (#10778)
    
    * add test case for RuleConfigurationChecker
    
    * fix code style
---
 ...abaseDiscoveryRuleConfigurationCheckerTest.java | 66 +++++++++++++++++++++
 .../ShadowRuleConfigurationCheckerTest.java        | 67 ++++++++++++++++++++++
 2 files changed, 133 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/checker/DatabaseDiscoveryRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/checker/DatabaseDiscoveryRuleConfigurationCheckerTest.java
new file mode 100644
index 0000000..0e87f5b
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/checker/DatabaseDiscoveryRuleConfigurationCheckerTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dbdiscovery.rule.checker;
+
+import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
+import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.checker.RuleConfigurationChecker;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Database discovery rule configuration checker test.
+ */
+public final class DatabaseDiscoveryRuleConfigurationCheckerTest {
+    static {
+        ShardingSphereServiceLoader.register(RuleConfigurationChecker.class);
+    }
+
+    @Test
+    public void assertCheckPass() {
+        DatabaseDiscoveryRuleConfiguration ruleConfig = mock(DatabaseDiscoveryRuleConfiguration.class);
+        DatabaseDiscoveryDataSourceRuleConfiguration ds0 = mock(DatabaseDiscoveryDataSourceRuleConfiguration.class);
+        when(ds0.getDiscoveryTypeName()).thenReturn("jdbc");
+        when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(ds0));
+        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), RuleConfigurationChecker.class).get(ruleConfig);
+        assertNotNull(checker);
+        assertThat(checker, instanceOf(DatabaseDiscoveryRuleConfigurationChecker.class));
+        checker.check("test", ruleConfig);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void assertCheckNoPass() {
+        DatabaseDiscoveryRuleConfiguration ruleConfig = mock(DatabaseDiscoveryRuleConfiguration.class);
+        DatabaseDiscoveryDataSourceRuleConfiguration ds0 = mock(DatabaseDiscoveryDataSourceRuleConfiguration.class);
+        when(ds0.getDiscoveryTypeName()).thenReturn("");
+        when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(ds0));
+        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), RuleConfigurationChecker.class).get(ruleConfig);
+        assertNotNull(checker);
+        assertThat(checker, instanceOf(DatabaseDiscoveryRuleConfigurationChecker.class));
+        checker.check("test", ruleConfig);
+    }
+}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/checker/ShadowRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/checker/ShadowRuleConfigurationCheckerTest.java
new file mode 100644
index 0000000..9e43b5a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/checker/ShadowRuleConfigurationCheckerTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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.shadow.rule.checker;
+
+import org.apache.shardingsphere.infra.rule.checker.RuleConfigurationChecker;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Shadow rule configuration checker test.
+ */
+public final class ShadowRuleConfigurationCheckerTest {
+
+    static {
+        ShardingSphereServiceLoader.register(RuleConfigurationChecker.class);
+    }
+
+    @Test
+    public void assertCheckPass() {
+        ShadowRuleConfiguration ruleConfig = mock(ShadowRuleConfiguration.class);
+        when(ruleConfig.getColumn()).thenReturn("id");
+        when(ruleConfig.getSourceDataSourceNames()).thenReturn(Collections.singletonList("ds0"));
+        when(ruleConfig.getShadowDataSourceNames()).thenReturn(Collections.singletonList("shadow0"));
+        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), RuleConfigurationChecker.class).get(ruleConfig);
+        assertNotNull(checker);
+        assertThat(checker, instanceOf(ShadowRuleConfigurationChecker.class));
+        checker.check("test", ruleConfig);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void assertCheckNoPass() {
+        ShadowRuleConfiguration ruleConfig = mock(ShadowRuleConfiguration.class);
+        when(ruleConfig.getColumn()).thenReturn("");
+        when(ruleConfig.getSourceDataSourceNames()).thenReturn(Collections.emptyList());
+        when(ruleConfig.getShadowDataSourceNames()).thenReturn(Collections.emptyList());
+        RuleConfigurationChecker checker = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), RuleConfigurationChecker.class).get(ruleConfig);
+        assertNotNull(checker);
+        assertThat(checker, instanceOf(ShadowRuleConfigurationChecker.class));
+        checker.check("test", ruleConfig);
+    }
+
+}