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 2024/06/29 04:02:14 UTC
(shardingsphere) branch master updated: Refactor ContextManagerTest (#31919)
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 e99d425edca Refactor ContextManagerTest (#31919)
e99d425edca is described below
commit e99d425edca1a8fd6f8278545e185e6794310d8b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Jun 29 12:02:08 2024 +0800
Refactor ContextManagerTest (#31919)
* Refactor ContextManagerTest
* Refactor SingleRuleConfigurationDecorator
---
.../SingleRuleConfigurationDecorator.java | 81 ++++++++++------------
.../mode/manager/ContextManagerTest.java | 12 +++-
2 files changed, 47 insertions(+), 46 deletions(-)
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java
index 458441cae88..4eb6a573b2a 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/decorator/SingleRuleConfigurationDecorator.java
@@ -40,6 +40,7 @@ import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.stream.Collectors;
/**
* Single rule configuration decorator.
@@ -78,6 +79,35 @@ public final class SingleRuleConfigurationDecorator implements RuleConfiguration
return splitTables.stream().anyMatch(each -> each.contains(SingleTableConstants.ASTERISK));
}
+ private void checkRuleConfiguration(final String databaseName, final Map<String, DataSource> dataSources, final Collection<String> excludedTables, final Collection<DataNode> dataNodes) {
+ for (DataNode each : dataNodes) {
+ if (!SingleTableConstants.ASTERISK.equals(each.getDataSourceName())) {
+ ShardingSpherePreconditions.checkContainsKey(dataSources, each.getDataSourceName(),
+ () -> new InvalidSingleRuleConfigurationException(String.format("Data source `%s` does not exist in database `%s`", each.getDataSourceName(), databaseName)));
+ }
+ ShardingSpherePreconditions.checkNotContains(excludedTables, each.getTableName(),
+ () -> new InvalidSingleRuleConfigurationException(String.format("Table `%s` existed and is not a single table in database `%s`", each.getTableName(), databaseName)));
+ }
+ }
+
+ private Collection<String> loadAllTables(final boolean isSchemaSupportedDatabaseType, final Map<String, Collection<DataNode>> actualDataNodes) {
+ return actualDataNodes.values().stream().map(each -> getTableNodeString(isSchemaSupportedDatabaseType, each.iterator().next())).collect(Collectors.toList());
+ }
+
+ private String getTableNodeString(final boolean isSchemaSupportedDatabaseType, final DataNode dataNode) {
+ return isSchemaSupportedDatabaseType
+ ? formatTableName(dataNode.getDataSourceName(), dataNode.getSchemaName(), dataNode.getTableName())
+ : formatTableName(dataNode.getDataSourceName(), dataNode.getTableName());
+ }
+
+ private String formatTableName(final String dataSourceName, final String schemaName, final String tableName) {
+ return String.format("%s.%s.%s", dataSourceName, schemaName, tableName);
+ }
+
+ private String formatTableName(final String dataSourceName, final String tableName) {
+ return String.format("%s.%s", dataSourceName, tableName);
+ }
+
private Collection<String> loadSpecifiedTables(final boolean isSchemaSupportedDatabaseType, final Map<String, Collection<DataNode>> actualDataNodes,
final Collection<ShardingSphereRule> builtRules, final Collection<DataNode> configuredDataNodes) {
Collection<String> expandRequiredDataSources = new LinkedHashSet<>(configuredDataNodes.size(), 1F);
@@ -89,11 +119,10 @@ public final class SingleRuleConfigurationDecorator implements RuleConfiguration
expectedDataNodes.put(each.getTableName(), each);
}
}
- if (expandRequiredDataSources.isEmpty()) {
- return loadSpecifiedTablesWithoutExpand(isSchemaSupportedDatabaseType, actualDataNodes, configuredDataNodes);
- }
- Collection<String> featureRequiredSingleTables = SingleTableLoadUtils.getFeatureRequiredSingleTables(builtRules);
- return loadSpecifiedTablesWithExpand(isSchemaSupportedDatabaseType, actualDataNodes, featureRequiredSingleTables, expandRequiredDataSources, expectedDataNodes);
+ return expandRequiredDataSources.isEmpty()
+ ? loadSpecifiedTablesWithoutExpand(isSchemaSupportedDatabaseType, actualDataNodes, configuredDataNodes)
+ : loadSpecifiedTablesWithExpand(
+ isSchemaSupportedDatabaseType, actualDataNodes, SingleTableLoadUtils.getFeatureRequiredSingleTables(builtRules), expandRequiredDataSources, expectedDataNodes);
}
private Collection<String> loadSpecifiedTablesWithExpand(final boolean isSchemaSupportedDatabaseType, final Map<String, Collection<DataNode>> actualDataNodes,
@@ -121,54 +150,20 @@ public final class SingleRuleConfigurationDecorator implements RuleConfiguration
return result;
}
- private Collection<String> loadSpecifiedTablesWithoutExpand(final boolean isSchemaSupportedDatabaseType, final Map<String, Collection<DataNode>> actualDataNodes,
- final Collection<DataNode> configuredDataNodes) {
+ private Collection<String> loadSpecifiedTablesWithoutExpand(final boolean isSchemaSupportedDatabaseType,
+ final Map<String, Collection<DataNode>> actualDataNodes, final Collection<DataNode> configuredDataNodes) {
Collection<String> result = new LinkedHashSet<>(configuredDataNodes.size(), 1F);
for (DataNode each : configuredDataNodes) {
ShardingSpherePreconditions.checkContainsKey(actualDataNodes, each.getTableName(), () -> new SingleTableNotFoundException(getTableNodeString(isSchemaSupportedDatabaseType, each)));
DataNode actualDataNode = actualDataNodes.get(each.getTableName()).iterator().next();
String tableNodeStr = getTableNodeString(isSchemaSupportedDatabaseType, actualDataNode);
- ShardingSpherePreconditions.checkState(actualDataNode.equals(each),
- () -> new InvalidSingleRuleConfigurationException(String.format("Single table '%s' is found that does not match %s", tableNodeStr,
- getTableNodeString(isSchemaSupportedDatabaseType, each))));
+ ShardingSpherePreconditions.checkState(actualDataNode.equals(each), () -> new InvalidSingleRuleConfigurationException(
+ String.format("Single table '%s' is found that does not match %s", tableNodeStr, getTableNodeString(isSchemaSupportedDatabaseType, each))));
result.add(tableNodeStr);
}
return result;
}
- private Collection<String> loadAllTables(final boolean isSchemaSupportedDatabaseType, final Map<String, Collection<DataNode>> actualDataNodes) {
- Collection<String> result = new LinkedList<>();
- for (Entry<String, Collection<DataNode>> entry : actualDataNodes.entrySet()) {
- result.add(getTableNodeString(isSchemaSupportedDatabaseType, entry.getValue().iterator().next()));
- }
- return result;
- }
-
- private String getTableNodeString(final boolean isSchemaSupportedDatabaseType, final DataNode dataNode) {
- return isSchemaSupportedDatabaseType
- ? formatTableName(dataNode.getDataSourceName(), dataNode.getSchemaName(), dataNode.getTableName())
- : formatTableName(dataNode.getDataSourceName(), dataNode.getTableName());
- }
-
- private void checkRuleConfiguration(final String databaseName, final Map<String, DataSource> dataSources, final Collection<String> excludedTables, final Collection<DataNode> dataNodes) {
- for (DataNode each : dataNodes) {
- if (!SingleTableConstants.ASTERISK.equals(each.getDataSourceName())) {
- ShardingSpherePreconditions.checkContainsKey(dataSources, each.getDataSourceName(),
- () -> new InvalidSingleRuleConfigurationException(String.format("Data source `%s` does not exist in database `%s`", each.getDataSourceName(), databaseName)));
- }
- ShardingSpherePreconditions.checkNotContains(excludedTables, each.getTableName(),
- () -> new InvalidSingleRuleConfigurationException(String.format("Table `%s` existed and is not a single table in database `%s`", each.getTableName(), databaseName)));
- }
- }
-
- private String formatTableName(final String dataSourceName, final String tableName) {
- return String.format("%s.%s", dataSourceName, tableName);
- }
-
- private String formatTableName(final String dataSourceName, final String schemaName, final String tableName) {
- return String.format("%s.%s.%s", dataSourceName, schemaName, tableName);
- }
-
@Override
public Class<SingleRuleConfiguration> getType() {
return SingleRuleConfiguration.class;
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 cb4e3ba07ea..9bc2010ca2b 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
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.mode.manager;
import org.apache.groovy.util.Maps;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
@@ -49,6 +48,12 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
import java.sql.Types;
import java.util.Collections;
@@ -72,16 +77,17 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
class ContextManagerTest {
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private MetaDataContexts metaDataContexts;
private ContextManager contextManager;
@BeforeEach
void setUp() {
- metaDataContexts = mock(MetaDataContexts.class, RETURNS_DEEP_STUBS);
- when(metaDataContexts.getMetaData().getProps().getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE)).thenReturn(1);
when(metaDataContexts.getMetaData().getProps()).thenReturn(new ConfigurationProperties(new Properties()));
ShardingSphereDatabase database = mockDatabase();
when(metaDataContexts.getMetaData().containsDatabase("foo_db")).thenReturn(true);