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 bd7f5ca8eff Impl ShardingAlgorithm for ShardingAutoTableAlgorithm
(#19873)
bd7f5ca8eff is described below
commit bd7f5ca8effc4f4fcc376e1e795543ef62d38bd7
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 4 18:52:56 2022 +0800
Impl ShardingAlgorithm for ShardingAutoTableAlgorithm (#19873)
---
.../api/sharding/ShardingAutoTableAlgorithm.java | 4 +++-
...rdingAutoTableRuleConfigurationSwapperTest.java | 23 +++++++---------------
2 files changed, 10 insertions(+), 17 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
index 5201f1d3311..4b5e52f3db9 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/sharding/ShardingAutoTableAlgorithm.java
@@ -17,10 +17,12 @@
package org.apache.shardingsphere.sharding.api.sharding;
+import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+
/**
* Sharding auto table algorithm.
*/
-public interface ShardingAutoTableAlgorithm {
+public interface ShardingAutoTableAlgorithm extends ShardingAlgorithm {
/**
* Get auto tables amount.
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
index ccc2bf77459..b7ff9afb282 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/rule/YamlShardingAutoTableRuleConfigurationSwapperTest.java
@@ -22,7 +22,6 @@ import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleC
import
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
-import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import
org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration;
import
org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration;
import
org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
@@ -38,8 +37,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
@@ -47,28 +44,30 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.withSettings;
@RunWith(MockitoJUnitRunner.class)
public final class YamlShardingAutoTableRuleConfigurationSwapperTest {
+ private YamlShardingAutoTableRuleConfigurationSwapper tableSwapper;
+
@Mock
private YamlShardingStrategyConfigurationSwapper shardingStrategySwapper;
@Mock
private YamlKeyGenerateStrategyConfigurationSwapper
keyGenerateStrategySwapper;
- private final YamlShardingAutoTableRuleConfigurationSwapper tableSwapper =
new YamlShardingAutoTableRuleConfigurationSwapper(mockAlgorithms(),
Collections.emptyMap());
-
@Before
public void setUp() throws ReflectiveOperationException {
+ ShardingAutoTableAlgorithm shardingAlgorithm =
mock(ShardingAutoTableAlgorithm.class);
+ when(shardingAlgorithm.getAutoTablesAmount()).thenReturn(2);
+ tableSwapper = new
YamlShardingAutoTableRuleConfigurationSwapper(Collections.singletonMap("foo_algorithm",
shardingAlgorithm), Collections.emptyMap());
setSwapper("shardingStrategySwapper", shardingStrategySwapper);
when(shardingStrategySwapper.swapToYamlConfiguration(ArgumentMatchers.any())).thenReturn(mock(YamlShardingStrategyConfiguration.class));
setSwapper("keyGenerateStrategySwapper", keyGenerateStrategySwapper);
when(keyGenerateStrategySwapper.swapToYamlConfiguration(ArgumentMatchers.any())).thenReturn(mock(YamlKeyGenerateStrategyConfiguration.class));
}
- private void setSwapper(final String swapperFieldName, final
YamlConfigurationSwapper swapperFieldValue) throws ReflectiveOperationException
{
+ private void setSwapper(final String swapperFieldName, final
YamlConfigurationSwapper<?, ?> swapperFieldValue) throws
ReflectiveOperationException {
Field field =
YamlShardingAutoTableRuleConfigurationSwapper.class.getDeclaredField(swapperFieldName);
field.setAccessible(true);
field.set(tableSwapper, swapperFieldValue);
@@ -104,7 +103,7 @@ public final class
YamlShardingAutoTableRuleConfigurationSwapperTest {
ShardingAutoTableRuleConfiguration shardingTableRuleConfig = new
ShardingAutoTableRuleConfiguration("tbl", "ds0,ds1");
shardingTableRuleConfig.setActualTablePrefix("tmp_");
StandardShardingStrategyConfiguration strategyConfiguration =
mock(StandardShardingStrategyConfiguration.class);
-
when(strategyConfiguration.getShardingAlgorithmName()).thenReturn("mod_2");
+
when(strategyConfiguration.getShardingAlgorithmName()).thenReturn("foo_algorithm");
shardingTableRuleConfig.setShardingStrategy(strategyConfiguration);
shardingTableRuleConfig.setKeyGenerateStrategy(mock(KeyGenerateStrategyConfiguration.class));
YamlShardingAutoTableRuleConfiguration actual =
tableSwapper.swapToYamlConfiguration(shardingTableRuleConfig);
@@ -116,14 +115,6 @@ public final class
YamlShardingAutoTableRuleConfigurationSwapperTest {
assertNotNull(actual.getKeyGenerateStrategy());
}
- private Map<String, ShardingAlgorithm> mockAlgorithms() {
- Map<String, ShardingAlgorithm> result = new LinkedHashMap<>();
- ShardingAlgorithm algorithm = mock(ShardingAlgorithm.class,
withSettings().extraInterfaces(ShardingAutoTableAlgorithm.class));
- when(((ShardingAutoTableAlgorithm)
algorithm).getAutoTablesAmount()).thenReturn(2);
- result.put("mod_2", algorithm);
- return result;
- }
-
@Test(expected = NullPointerException.class)
public void assertSwapToObjectWithoutLogicTable() {
new
YamlShardingAutoTableRuleConfigurationSwapper(Collections.emptyMap(),
Collections.emptyMap()).swapToObject(new
YamlShardingAutoTableRuleConfiguration());