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/27 07:52:30 UTC

[shardingsphere] branch master updated: Add DefaultSchemaRuleConfigurationBuilderFactory (#17142)

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 ca100714781 Add DefaultSchemaRuleConfigurationBuilderFactory (#17142)
ca100714781 is described below

commit ca1007147818ecfb8a2305632406ab781439e084
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Apr 27 15:52:16 2022 +0800

    Add DefaultSchemaRuleConfigurationBuilderFactory (#17142)
---
 ...faultSchemaRuleConfigurationBuilderFactory.java | 35 ++++++++++++----------
 .../rule/builder/schema/SchemaRulesBuilder.java    | 11 ++-----
 ...gleTableSchemaRuleConfigurationBuilderTest.java | 17 ++++-------
 3 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/DefaultSchemaRuleConfigurationBuilderFactory.java
similarity index 54%
copy from shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/DefaultSchemaRuleConfigurationBuilderFactory.java
index d48689fdc21..ea3eabf2294 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/DefaultSchemaRuleConfigurationBuilderFactory.java
@@ -15,31 +15,34 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.singletable.rule.builder;
+package org.apache.shardingsphere.infra.rule.builder.schema;
 
-import java.util.Collection;
-import org.apache.shardingsphere.infra.rule.builder.schema.DefaultSchemaRuleConfigurationBuilder;
-import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
 
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
+import java.util.Collection;
+import java.util.Map;
 
-@RunWith(MockitoJUnitRunner.class)
-public final class DefaultSingleTableSchemaRuleConfigurationBuilderTest {
+/**
+ * Default schema rule configuration builder factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DefaultSchemaRuleConfigurationBuilderFactory {
     
     static {
         ShardingSphereServiceLoader.register(DefaultSchemaRuleConfigurationBuilder.class);
     }
     
-    @Test
-    public void assertBuild() {
-        Collection<DefaultSchemaRuleConfigurationBuilder> registeredServices = OrderedSPIRegistry.getRegisteredServices(DefaultSchemaRuleConfigurationBuilder.class);
-        DefaultSchemaRuleConfigurationBuilder builder = registeredServices.iterator().next();
-        assertThat(builder.build(), instanceOf(SingleTableRuleConfiguration.class));
+    /**
+     * Create new instance of schema rule builder.
+     *
+     * @param builders schema rule builders
+     * @return new instance of schema rule builder
+     */
+    @SuppressWarnings("rawtypes")
+    public static Map<SchemaRuleBuilder, DefaultSchemaRuleConfigurationBuilder> newInstance(final Collection<SchemaRuleBuilder> builders) {
+        return OrderedSPIRegistry.getRegisteredServices(DefaultSchemaRuleConfigurationBuilder.class, builders);
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/SchemaRulesBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/SchemaRulesBuilder.java
index a031167f6d1..679dda8465e 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/SchemaRulesBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/schema/SchemaRulesBuilder.java
@@ -20,13 +20,11 @@ package org.apache.shardingsphere.infra.rule.builder.schema;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.function.DistributedRuleConfiguration;
 import org.apache.shardingsphere.infra.config.function.EnhancedRuleConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -43,10 +41,6 @@ import java.util.stream.Collectors;
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public final class SchemaRulesBuilder {
     
-    static {
-        ShardingSphereServiceLoader.register(DefaultSchemaRuleConfigurationBuilder.class);
-    }
-    
     /**
      * Build rules.
      *
@@ -92,8 +86,7 @@ public final class SchemaRulesBuilder {
     @SuppressWarnings("rawtypes")
     private static Map<RuleConfiguration, SchemaRuleBuilder> getMissedDefaultRuleBuilderMap(final Collection<SchemaRuleBuilder> configuredBuilders) {
         Map<RuleConfiguration, SchemaRuleBuilder> result = new LinkedHashMap<>();
-        Map<SchemaRuleBuilder, DefaultSchemaRuleConfigurationBuilder> defaultBuilders =
-                OrderedSPIRegistry.getRegisteredServices(DefaultSchemaRuleConfigurationBuilder.class, getMissedDefaultRuleBuilders(configuredBuilders));
+        Map<SchemaRuleBuilder, DefaultSchemaRuleConfigurationBuilder> defaultBuilders = DefaultSchemaRuleConfigurationBuilderFactory.newInstance(getMissedDefaultRuleBuilders(configuredBuilders));
         // TODO consider about order for new put items
         for (Entry<SchemaRuleBuilder, DefaultSchemaRuleConfigurationBuilder> entry : defaultBuilders.entrySet()) {
             result.put(entry.getValue().build(), entry.getKey());
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java
index d48689fdc21..afbaf3c1a5d 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/DefaultSingleTableSchemaRuleConfigurationBuilderTest.java
@@ -17,29 +17,22 @@
 
 package org.apache.shardingsphere.singletable.rule.builder;
 
-import java.util.Collection;
 import org.apache.shardingsphere.infra.rule.builder.schema.DefaultSchemaRuleConfigurationBuilder;
+import org.apache.shardingsphere.infra.rule.builder.schema.DefaultSchemaRuleConfigurationBuilderFactory;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertThat;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class DefaultSingleTableSchemaRuleConfigurationBuilderTest {
     
-    static {
-        ShardingSphereServiceLoader.register(DefaultSchemaRuleConfigurationBuilder.class);
-    }
-    
+    @SuppressWarnings("rawtypes")
     @Test
     public void assertBuild() {
-        Collection<DefaultSchemaRuleConfigurationBuilder> registeredServices = OrderedSPIRegistry.getRegisteredServices(DefaultSchemaRuleConfigurationBuilder.class);
-        DefaultSchemaRuleConfigurationBuilder builder = registeredServices.iterator().next();
+        DefaultSchemaRuleConfigurationBuilder builder = DefaultSchemaRuleConfigurationBuilderFactory.newInstance(Collections.singletonList(new SingleTableRuleBuilder())).values().iterator().next();
         assertThat(builder.build(), instanceOf(SingleTableRuleConfiguration.class));
     }
 }