You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/05/22 10:24:44 UTC

[shardingsphere] branch master updated: Fix sonar issue of ContextManagerTest (#25846)

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

jianglongtao 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 d093a876f71 Fix sonar issue of ContextManagerTest (#25846)
d093a876f71 is described below

commit d093a876f7134343326ccd79fb549a9332cfbc38
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Mon May 22 18:24:37 2023 +0800

    Fix sonar issue of ContextManagerTest (#25846)
    
    * Fix sonar issue of ContextManagerTest
    
    * Fix checkstyle
---
 .../mode/fixture/FixtureDatabaseRule.java          | 39 ++++++++++++++++++
 .../fixture/FixtureDistributedRuleBuilder.java     | 48 ++++++++++++++++++++++
 .../FixtureDistributedRuleConfiguration.java       | 30 ++++++++++++++
 .../mode/manager/ContextManagerTest.java           |  8 ++--
 ...infra.rule.builder.database.DatabaseRuleBuilder | 18 ++++++++
 5 files changed, 138 insertions(+), 5 deletions(-)

diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDatabaseRule.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDatabaseRule.java
new file mode 100644
index 00000000000..99e158cf506
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDatabaseRule.java
@@ -0,0 +1,39 @@
+/*
+ * 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.mode.fixture;
+
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
+
+import static org.mockito.Mockito.mock;
+
+/**
+ * Fixture database rule.
+ */
+public final class FixtureDatabaseRule implements DatabaseRule {
+    
+    @Override
+    public RuleConfiguration getConfiguration() {
+        return mock(RuleConfiguration.class);
+    }
+    
+    @Override
+    public String getType() {
+        return FixtureDatabaseRule.class.getSimpleName();
+    }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleBuilder.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleBuilder.java
new file mode 100644
index 00000000000..3972dae6ebf
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleBuilder.java
@@ -0,0 +1,48 @@
+/*
+ * 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.mode.fixture;
+
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Fixture distributed rule builder.
+ */
+public final class FixtureDistributedRuleBuilder implements DatabaseRuleBuilder<FixtureDistributedRuleConfiguration> {
+    
+    @Override
+    public FixtureDatabaseRule build(final FixtureDistributedRuleConfiguration config, final String databaseName, final Map<String, DataSource> dataSources,
+                                     final Collection<ShardingSphereRule> builtRules, final InstanceContext instanceContext) {
+        return new FixtureDatabaseRule();
+    }
+    
+    @Override
+    public int getOrder() {
+        return 0;
+    }
+    
+    @Override
+    public Class<FixtureDistributedRuleConfiguration> getTypeClass() {
+        return FixtureDistributedRuleConfiguration.class;
+    }
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleConfiguration.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleConfiguration.java
new file mode 100644
index 00000000000..786b15a1ed7
--- /dev/null
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/fixture/FixtureDistributedRuleConfiguration.java
@@ -0,0 +1,30 @@
+/*
+ * 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.mode.fixture;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.rule.function.DistributedRuleConfiguration;
+
+/**
+ * Fixture distributed rule configuration.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class FixtureDistributedRuleConfiguration implements DistributedRuleConfiguration {
+}
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
index 72eda8e3a16..df32950d568 100644
--- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
@@ -38,6 +38,7 @@ import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
 import org.apache.shardingsphere.infra.state.cluster.ClusterState;
 import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
 import org.apache.shardingsphere.metadata.persist.service.database.DatabaseMetaDataPersistService;
+import org.apache.shardingsphere.mode.fixture.FixtureDistributedRuleConfiguration;
 import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
 import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
@@ -216,11 +217,8 @@ class ContextManagerTest {
         when(metaDataContexts.getMetaData().getDatabase("foo_db")).thenReturn(database);
         when(metaDataContexts.getMetaData().getGlobalRuleMetaData()).thenReturn(new ShardingSphereRuleMetaData(Collections.emptyList()));
         when(metaDataContexts.getPersistService()).thenReturn(mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS));
-        // TODO TransactionRule is global rule, do not use it in database rule test
-        RuleConfiguration ruleConfig = new TransactionRuleConfiguration("LOCAL", null, new Properties());
-        contextManager.alterRuleConfiguration("foo_db", Collections.singleton(ruleConfig));
-        // TODO create DistributedRuleFixture to assert alter rule
-        // assertTrue(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getConfigurations().contains(ruleConfig));
+        contextManager.alterRuleConfiguration("foo_db", Collections.singleton(new FixtureDistributedRuleConfiguration()));
+        assertThat(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getConfigurations().size(), is(1));
     }
     
     @Test
diff --git a/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder b/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
new file mode 100644
index 00000000000..9dc06203712
--- /dev/null
+++ b/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.database.DatabaseRuleBuilder
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.mode.fixture.FixtureDistributedRuleBuilder