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 678ca7b4dc4 Add RuleBuilderTests (#37289)
678ca7b4dc4 is described below
commit 678ca7b4dc44f056e786660e7ab15e5e921a8495
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Dec 7 12:19:45 2025 +0800
Add RuleBuilderTests (#37289)
* Add RuleBuilderTests
* Add RuleBuilderTests
* Add RuleBuilderTests
* Add RuleBuilderTests
---
.../rule/builder/AuthorityRuleBuilderTest.java | 44 +++++++++++++++++
.../rule/builder/SQLFederationRuleBuilderTest.java | 55 ++++++++++++++++++++++
.../rule/builder/SQLParserRuleBuilderTest.java | 30 ++++--------
.../rule/builder/SQLTranslatorRuleBuilderTest.java | 43 +++++++++++++++++
.../builder/TimestampServiceRuleBuilderTest.java | 52 ++++++++++++++++++++
.../rule/builder/TransactionRuleBuilderTest.java | 33 ++++---------
6 files changed, 212 insertions(+), 45 deletions(-)
diff --git
a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilderTest.java
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilderTest.java
new file mode 100644
index 00000000000..6189cce0b69
--- /dev/null
+++
b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/rule/builder/AuthorityRuleBuilderTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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.authority.rule.builder;
+
+import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+
+class AuthorityRuleBuilderTest {
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertBuild() {
+ AuthorityRuleConfiguration ruleConfig = new
AuthorityRuleConfiguration(Collections.emptyList(), new
AlgorithmConfiguration("FIXTURE", new Properties()), Collections.emptyMap(),
"FIXTURE");
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.emptyList(), null), isA(AuthorityRule.class));
+ }
+}
diff --git
a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/builder/SQLFederationRuleBuilderTest.java
b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/builder/SQLFederationRuleBuilderTest.java
new file mode 100644
index 00000000000..54ca7382066
--- /dev/null
+++
b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/builder/SQLFederationRuleBuilderTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.sqlfederation.rule.builder;
+
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import
org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
+import
org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContextFactory;
+import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
+import
org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
+import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(CompilerContextFactory.class)
+class SQLFederationRuleBuilderTest {
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertBuild() {
+ SQLFederationRuleConfiguration ruleConfig = new
SQLFederationRuleConfiguration(true, false, new SQLFederationCacheOption(4,
64L));
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+
when(CompilerContextFactory.create(Collections.singleton(database))).thenReturn(mock(CompilerContext.class));
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.singleton(database), null), isA(SQLFederationRule.class));
+ }
+}
diff --git
a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/rule/builder/SQLParserRuleBuilderTest.java
b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/rule/builder/SQLParserRuleBuilderTest.java
index e6f6d80db8c..369ad93561d 100644
---
a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/rule/builder/SQLParserRuleBuilderTest.java
+++
b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/rule/builder/SQLParserRuleBuilderTest.java
@@ -17,39 +17,27 @@
package org.apache.shardingsphere.parser.rule.builder;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
-import org.apache.shardingsphere.parser.constant.SQLParserOrder;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.engine.api.CacheOption;
import org.junit.jupiter.api.Test;
import java.util.Collections;
-import java.util.Properties;
+import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.isA;
class SQLParserRuleBuilderTest {
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Test
void assertBuild() {
- SQLParserRuleConfiguration ruleConfig = new
SQLParserRuleConfiguration(new CacheOption(2, 5L), new CacheOption(4, 7L));
- SQLParserRule actualResult = new
SQLParserRuleBuilder().build(ruleConfig, Collections.emptyList(), new
ConfigurationProperties(new Properties()));
- assertThat(actualResult.getConfiguration(), is(ruleConfig));
- assertThat(actualResult.getSqlStatementCache().getInitialCapacity(),
is(4));
- assertThat(actualResult.getSqlStatementCache().getMaximumSize(),
is(7L));
- assertThat(actualResult.getParseTreeCache().getInitialCapacity(),
is(2));
- assertThat(actualResult.getParseTreeCache().getMaximumSize(), is(5L));
- }
-
- @Test
- void assertGetOrder() {
- assertThat(new SQLParserRuleBuilder().getOrder(),
is(SQLParserOrder.ORDER));
- }
-
- @Test
- void assertGetTypeClass() {
- assertThat(new SQLParserRuleBuilder().getTypeClass().toString(),
is(SQLParserRuleConfiguration.class.toString()));
+ SQLParserRuleConfiguration ruleConfig = new
SQLParserRuleConfiguration(new CacheOption(4, 64L), new CacheOption(8, 128L));
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.emptyList(), null), isA(SQLParserRule.class));
}
}
diff --git
a/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/builder/SQLTranslatorRuleBuilderTest.java
b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/builder/SQLTranslatorRuleBuilderTest.java
new file mode 100644
index 00000000000..91af539c58d
--- /dev/null
+++
b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/builder/SQLTranslatorRuleBuilderTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.sqltranslator.rule.builder;
+
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import
org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration;
+import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+
+class SQLTranslatorRuleBuilderTest {
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertBuild() {
+ SQLTranslatorRuleConfiguration ruleConfig = new
SQLTranslatorRuleConfiguration("FIXTURE", new Properties(), true);
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.emptyList(), null), isA(SQLTranslatorRule.class));
+ }
+}
diff --git
a/kernel/time-service/core/src/test/java/org/apache/shardingsphere/timeservice/core/rule/builder/TimestampServiceRuleBuilderTest.java
b/kernel/time-service/core/src/test/java/org/apache/shardingsphere/timeservice/core/rule/builder/TimestampServiceRuleBuilderTest.java
new file mode 100644
index 00000000000..3b90768ce78
--- /dev/null
+++
b/kernel/time-service/core/src/test/java/org/apache/shardingsphere/timeservice/core/rule/builder/TimestampServiceRuleBuilderTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.timeservice.core.rule.builder;
+
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.timeservice.config.TimestampServiceRuleConfiguration;
+import org.apache.shardingsphere.timeservice.core.rule.TimestampServiceRule;
+import org.apache.shardingsphere.timeservice.spi.TimestampService;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+
+class TimestampServiceRuleBuilderTest {
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertBuild() {
+ TimestampServiceRuleConfiguration ruleConfig = new
TimestampServiceRuleConfiguration("System", new Properties());
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ TimestampService timestampService = mock(TimestampService.class);
+ try (MockedStatic<TypedSPILoader> typedSpiLoader =
mockStatic(TypedSPILoader.class)) {
+ typedSpiLoader.when(() ->
TypedSPILoader.getService(TimestampService.class, "System", new
Properties())).thenReturn(timestampService);
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.emptyList(), null), isA(TimestampServiceRule.class));
+ }
+ }
+}
diff --git
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java
index 2c6422983c4..00a206773e0 100644
---
a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java
+++
b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/builder/TransactionRuleBuilderTest.java
@@ -17,42 +17,27 @@
package org.apache.shardingsphere.transaction.rule.builder;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
-import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
+import
org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.Test;
-import javax.sql.DataSource;
import java.util.Collections;
-import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.mockito.Mockito.mock;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
class TransactionRuleBuilderTest {
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Test
void assertBuild() {
- TransactionRuleConfiguration ruleConfig = new
TransactionRuleConfiguration("LOCAL", "provider", new Properties());
- ShardingSphereDatabase database = new
ShardingSphereDatabase("logic_db", null, new
ResourceMetaData(createDataSourceMap()),
- new
RuleMetaData(Collections.singletonList(mock(ShardingSphereRule.class))),
Collections.singleton(new ShardingSphereSchema("test")));
- try (TransactionRule rule = new
TransactionRuleBuilder().build(ruleConfig, Collections.singleton(database),
mock(ConfigurationProperties.class))) {
- assertNotNull(rule.getConfiguration());
- }
- }
-
- private Map<String, DataSource> createDataSourceMap() {
- Map<String, DataSource> result = new HashMap<>(2, 1F);
- result.put("not_change", new MockedDataSource());
- result.put("replace", new MockedDataSource());
- return result;
+ TransactionRuleConfiguration ruleConfig = new
TransactionRuleConfiguration("LOCAL", "FIXTURE", new Properties());
+ Map<GlobalRuleConfiguration, GlobalRuleBuilder> builders =
OrderedSPILoader.getServices(GlobalRuleBuilder.class,
Collections.singleton(ruleConfig));
+ assertThat(builders.get(ruleConfig).build(ruleConfig,
Collections.emptyList(), null), isA(TransactionRule.class));
}
}