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 4ef16afe4b7 Add more test cases on RuleMetaData (#33034)
4ef16afe4b7 is described below
commit 4ef16afe4b740dbd45287edefbf3b3b4e9fdeadc
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Sep 28 12:10:25 2024 +0800
Add more test cases on RuleMetaData (#33034)
* Add more test cases on RuleMetaData
* Add more test cases on RuleMetaData
* Add more test cases on RuleMetaData
---
.../infra/metadata/database/rule/RuleMetaData.java | 22 +++++++++++-----
.../metadata/database/rule/RuleMetaDataTest.java | 30 +++++++++++++++++++++-
2 files changed, 45 insertions(+), 7 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaData.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaData.java
index 388e8f53674..a7c2f4efe1d 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaData.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaData.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttri
import
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
@@ -107,13 +108,10 @@ public final class RuleMetaData {
public Map<String, Collection<Class<? extends ShardingSphereRule>>>
getInUsedStorageUnitNameAndRulesMap() {
Map<String, Collection<Class<? extends ShardingSphereRule>>> result =
new LinkedHashMap<>();
for (ShardingSphereRule each : rules) {
- Optional<DataSourceMapperRuleAttribute> ruleAttribute =
each.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class);
- if (ruleAttribute.isPresent()) {
- mergeInUsedStorageUnitNameAndRules(result,
getInUsedStorageUnitNameAndRulesMap(each,
getInUsedStorageUnitNames(ruleAttribute.get())));
- continue;
+ Collection<String> inUsedStorageUnitNames =
getInUsedStorageUnitNames(each);
+ if (!inUsedStorageUnitNames.isEmpty()) {
+ mergeInUsedStorageUnitNameAndRules(result,
getInUsedStorageUnitNameAndRulesMap(each, inUsedStorageUnitNames));
}
- each.getAttributes().findAttribute(DataNodeRuleAttribute.class)
- .ifPresent(optional ->
mergeInUsedStorageUnitNameAndRules(result,
getInUsedStorageUnitNameAndRulesMap(each,
getInUsedStorageUnitNames(optional))));
}
return result;
}
@@ -129,6 +127,18 @@ public final class RuleMetaData {
return result;
}
+ private Collection<String> getInUsedStorageUnitNames(final
ShardingSphereRule rule) {
+ Optional<DataSourceMapperRuleAttribute> dataSourceMapperRuleAttribute
= rule.getAttributes().findAttribute(DataSourceMapperRuleAttribute.class);
+ if (dataSourceMapperRuleAttribute.isPresent()) {
+ return
getInUsedStorageUnitNames(dataSourceMapperRuleAttribute.get());
+ }
+ Optional<DataNodeRuleAttribute> dataNodeRuleAttribute =
rule.getAttributes().findAttribute(DataNodeRuleAttribute.class);
+ if (dataNodeRuleAttribute.isPresent()) {
+ return getInUsedStorageUnitNames(dataNodeRuleAttribute.get());
+ }
+ return Collections.emptyList();
+ }
+
private Collection<String> getInUsedStorageUnitNames(final
DataSourceMapperRuleAttribute ruleAttribute) {
return
ruleAttribute.getDataSourceMapper().values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java
index 731b1e1366d..493a55c8c75 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java
@@ -17,18 +17,31 @@
package org.apache.shardingsphere.infra.metadata.database.rule;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.attribute.RuleAttribute;
+import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.junit.jupiter.api.Test;
+import java.util.Arrays;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
class RuleMetaDataTest {
- private final RuleMetaData ruleMetaData = new
RuleMetaData(Collections.singleton(new ShardingSphereRuleFixture()));
+ private final RuleMetaData ruleMetaData = new
RuleMetaData(Arrays.asList(new ShardingSphereRuleFixture(),
mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS)));
+
+ @Test
+ void assertGetConfigurations() {
+ assertThat(ruleMetaData.getConfigurations().size(), is(2));
+ }
@Test
void assertFindRules() {
@@ -40,13 +53,28 @@ class RuleMetaDataTest {
assertTrue(ruleMetaData.findSingleRule(ShardingSphereRuleFixture.class).isPresent());
}
+ @Test
+ void assertFindSingleRuleFailed() {
+
assertFalse(ruleMetaData.findSingleRule(mock(GlobalRule.class).getClass()).isPresent());
+ }
+
@Test
void assertGetSingleRule() {
assertThat(ruleMetaData.getSingleRule(ShardingSphereRuleFixture.class),
instanceOf(ShardingSphereRuleFixture.class));
}
+ @Test
+ void assertGetSingleRuleFailed() {
+ assertThrows(IllegalStateException.class, () ->
ruleMetaData.getSingleRule(mock(GlobalRule.class).getClass()));
+ }
+
@Test
void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesAreEmpty() {
assertTrue(new
RuleMetaData(Collections.emptyList()).getInUsedStorageUnitNameAndRulesMap().isEmpty());
}
+
+ @Test
+ void assertGetAttributes() {
+ assertTrue(ruleMetaData.getAttributes(RuleAttribute.class).isEmpty());
+ }
}