You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/05/07 17:05:41 UTC
[shardingsphere] branch master updated: Use AlgorithmFactory to initialize test cases (#17433)
This is an automated email from the ASF dual-hosted git repository.
totalo 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 e24e5e4cce2 Use AlgorithmFactory to initialize test cases (#17433)
e24e5e4cce2 is described below
commit e24e5e4cce226fdad2035cb50f3f8ab40e1185d1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun May 8 01:05:33 2022 +0800
Use AlgorithmFactory to initialize test cases (#17433)
* Refactor ShadowRuleTest
* Refactor ShadowRuleTest
* Refactor ShadowRuleTest
* Refactor CosIdKeyGenerateAlgorithmTest
* Refactor AlgorithmProvidedShadowRuleConfigurationCheckerTest
* Refactor AlgorithmProvidedShadowRuleConfigurationCheckerTest
* Refactor CosIdSnowflakeKeyGenerateAlgorithmTest
* Refactor SnowflakeKeyGenerateAlgorithmTest
* Refactor AESEncryptAlgorithmTest
---
.../encrypt/algorithm/AESEncryptAlgorithmTest.java | 23 ++---
.../encrypt/algorithm/RC4EncryptAlgorithmTest.java | 4 +-
.../sm/algorithm/SM4EncryptAlgorithmTest.java | 4 +-
...ProvidedShadowRuleConfigurationCheckerTest.java | 49 ++-------
.../shardingsphere/shadow/rule/ShadowRuleTest.java | 42 +++-----
.../keygen/SnowflakeKeyGenerateAlgorithmTest.java | 114 +++++++++------------
.../keygen/CosIdKeyGenerateAlgorithmTest.java | 44 ++++----
.../CosIdSnowflakeKeyGenerateAlgorithmTest.java | 51 +++++----
8 files changed, 131 insertions(+), 200 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
index 7921a70925b..759c2285b12 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithmTest.java
@@ -46,21 +46,19 @@ public final class AESEncryptAlgorithmTest {
return result;
}
- @Test
- public void assertEncrypt() {
- Object actual = encryptAlgorithm.encrypt("test", mock(EncryptContext.class));
- assertThat(actual, is("dSpPiyENQGDUXMKFMJPGWA=="));
+ @Test(expected = IllegalArgumentException.class)
+ public void assertCreateNewInstanceWithoutAESKey() {
+ EncryptAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("AES", new Properties()));
}
- @Test(expected = IllegalArgumentException.class)
- public void assertEncryptWithoutKey() {
- encryptAlgorithm.init(new Properties());
+ @Test
+ public void assertEncrypt() {
Object actual = encryptAlgorithm.encrypt("test", mock(EncryptContext.class));
assertThat(actual, is("dSpPiyENQGDUXMKFMJPGWA=="));
}
@Test
- public void assertEncryptWithNullPlaintext() {
+ public void assertEncryptNullValue() {
assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
}
@@ -70,15 +68,8 @@ public final class AESEncryptAlgorithmTest {
assertThat(actual.toString(), is("test"));
}
- @Test(expected = IllegalArgumentException.class)
- public void assertDecryptWithoutKey() {
- encryptAlgorithm.init(new Properties());
- Object actual = encryptAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA==", mock(EncryptContext.class));
- assertThat(actual.toString(), is("test"));
- }
-
@Test
- public void assertDecryptWithNullCiphertext() {
+ public void assertDecryptNullValue() {
assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
}
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
index f18f1444671..1db7865b20f 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithmTest.java
@@ -53,7 +53,7 @@ public final class RC4EncryptAlgorithmTest {
}
@Test
- public void assertEncryptWithNullPlaintext() {
+ public void assertEncryptNullValue() {
assertNull(encryptAlgorithm.encrypt(null, mock(EncryptContext.class)));
}
@@ -78,7 +78,7 @@ public final class RC4EncryptAlgorithmTest {
}
@Test
- public void assertDecryptWithNullCiphertext() {
+ public void assertDecryptNullValue() {
assertNull(encryptAlgorithm.decrypt(null, mock(EncryptContext.class)));
}
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-plugin/shardingsphere-encrypt-sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-plugin/shardingsphere-encrypt-sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
index 078d40a225f..a1e00b55294 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-plugin/shardingsphere-encrypt-sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-plugin/shardingsphere-encrypt-sm/src/test/java/org/apache/shardingsphere/encrypt/sm/algorithm/SM4EncryptAlgorithmTest.java
@@ -46,7 +46,7 @@ public final class SM4EncryptAlgorithmTest {
}
@Test
- public void assertEncryptNullPlaintext() {
+ public void assertEncryptNullValue() {
EncryptAlgorithm<Object, String> algorithm = EncryptAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
assertNull(algorithm.encrypt(null, mock(EncryptContext.class)));
}
@@ -58,7 +58,7 @@ public final class SM4EncryptAlgorithmTest {
}
@Test
- public void assertDecryptNullCiphertext() {
+ public void assertDecryptNullValue() {
EncryptAlgorithm<Object, String> algorithm = EncryptAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SM4", createECBProperties()));
assertNull(algorithm.decrypt(null, mock(EncryptContext.class)));
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
index bab415917ed..133ec45de1d 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/checker/AlgorithmProvidedShadowRuleConfigurationCheckerTest.java
@@ -17,63 +17,30 @@
package org.apache.shardingsphere.shadow.checker;
-import org.apache.shardingsphere.infra.config.checker.RuleConfigurationChecker;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
-import org.apache.shardingsphere.shadow.algorithm.shadow.column.ColumnRegexMatchShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
-import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.apache.shardingsphere.shadow.factory.ShadowAlgorithmFactory;
import org.junit.Test;
-import java.util.Collection;
-import java.util.LinkedHashMap;
+import java.util.Collections;
import java.util.LinkedList;
-import java.util.Map;
import java.util.Properties;
public final class AlgorithmProvidedShadowRuleConfigurationCheckerTest {
- private final RuleConfigurationChecker<AlgorithmProvidedShadowRuleConfiguration> checker = new AlgorithmProvidedShadowRuleConfigurationChecker();
-
@Test
public void assertCheck() {
- checker.check("", createAlgorithmProvidedShadowRuleConfiguration());
+ new AlgorithmProvidedShadowRuleConfigurationChecker().check("", createAlgorithmProvidedShadowRuleConfiguration());
}
private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
AlgorithmProvidedShadowRuleConfiguration result = new AlgorithmProvidedShadowRuleConfiguration();
- result.setShadowAlgorithms(createShadowAlgorithms());
- result.setDataSources(createDataSources());
- result.setTables(createTables());
- return result;
- }
-
- private Map<String, ShadowTableConfiguration> createTables() {
- Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
- Collection<String> dataSourceNames = new LinkedList<>();
- Collection<String> shadowAlgorithmNames = new LinkedList<>();
- shadowAlgorithmNames.add("user-id-insert-match-algorithm");
- result.put("t_order", new ShadowTableConfiguration(dataSourceNames, shadowAlgorithmNames));
- return result;
- }
-
- private Map<String, ShadowDataSourceConfiguration> createDataSources() {
- Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>();
- result.put("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow"));
- return result;
- }
-
- private Map<String, ShadowAlgorithm> createShadowAlgorithms() {
- Map<String, ShadowAlgorithm> result = new LinkedHashMap<>();
- result.put("user-id-insert-match-algorithm", createColumnRegexMatchShadowAlgorithm());
- return result;
- }
-
- private ShadowAlgorithm createColumnRegexMatchShadowAlgorithm() {
- ColumnRegexMatchShadowAlgorithm result = new ColumnRegexMatchShadowAlgorithm();
- Properties props = createProperties();
- result.init(props);
- result.setProps(props);
+ result.setShadowAlgorithms(Collections.singletonMap(
+ "user-id-insert-match-algorithm", ShadowAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("REGEX_MATCH", createProperties()))));
+ result.setDataSources(Collections.singletonMap("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow")));
+ result.setTables(Collections.singletonMap("t_order", new ShadowTableConfiguration(new LinkedList<>(), new LinkedList<>(Collections.singleton("user-id-insert-match-algorithm")))));
return result;
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java
index c2a6983f0db..16676d6d530 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/rule/ShadowRuleTest.java
@@ -18,11 +18,11 @@
package org.apache.shardingsphere.shadow.rule;
import com.google.common.collect.Lists;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
-import org.apache.shardingsphere.shadow.algorithm.shadow.column.ColumnRegexMatchShadowAlgorithm;
-import org.apache.shardingsphere.shadow.algorithm.shadow.hint.SimpleHintShadowAlgorithm;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.factory.ShadowAlgorithmFactory;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.junit.Before;
import org.junit.Test;
@@ -40,11 +40,11 @@ import static org.junit.Assert.assertThat;
public final class ShadowRuleTest {
- private ShadowRule shadowRuleWithAlgorithm;
+ private ShadowRule shadowRule;
@Before
public void init() {
- shadowRuleWithAlgorithm = new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration());
+ shadowRule = new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration());
}
private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
@@ -57,18 +57,10 @@ public final class ShadowRuleTest {
private Map<String, ShadowAlgorithm> createShadowAlgorithms() {
Map<String, ShadowAlgorithm> result = new LinkedHashMap<>();
- result.put("simple-hint-algorithm", createHintShadowAlgorithm());
- result.put("user-id-insert-regex-algorithm", createColumnShadowAlgorithm("user_id", "insert"));
- result.put("user-id-update-regex-algorithm", createColumnShadowAlgorithm("user_id", "update"));
- result.put("order-id-insert-regex-algorithm", createColumnShadowAlgorithm("order_id", "insert"));
- return result;
- }
-
- private ShadowAlgorithm createHintShadowAlgorithm() {
- SimpleHintShadowAlgorithm result = new SimpleHintShadowAlgorithm();
- Properties props = createHintProperties();
- result.init(props);
- result.setProps(props);
+ result.put("simple-hint-algorithm", ShadowAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SIMPLE_HINT", createHintProperties())));
+ result.put("user-id-insert-regex-algorithm", ShadowAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("REGEX_MATCH", createColumnProperties("user_id", "insert"))));
+ result.put("user-id-update-regex-algorithm", ShadowAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("REGEX_MATCH", createColumnProperties("user_id", "update"))));
+ result.put("order-id-insert-regex-algorithm", ShadowAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("REGEX_MATCH", createColumnProperties("order_id", "insert"))));
return result;
}
@@ -78,14 +70,6 @@ public final class ShadowRuleTest {
return result;
}
- private ShadowAlgorithm createColumnShadowAlgorithm(final String column, final String operation) {
- ColumnRegexMatchShadowAlgorithm result = new ColumnRegexMatchShadowAlgorithm();
- Properties props = createColumnProperties(column, operation);
- result.init(props);
- result.setProps(props);
- return result;
- }
-
private Properties createColumnProperties(final String column, final String operation) {
Properties result = new Properties();
result.setProperty("column", column);
@@ -114,7 +98,7 @@ public final class ShadowRuleTest {
}
private Map<String, ShadowDataSourceConfiguration> createDataSources() {
- Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>();
+ Map<String, ShadowDataSourceConfiguration> result = new LinkedHashMap<>(2, 1);
result.put("shadow-data-source-0", new ShadowDataSourceConfiguration("ds", "ds_shadow"));
result.put("shadow-data-source-1", new ShadowDataSourceConfiguration("ds1", "ds1_shadow"));
return result;
@@ -122,8 +106,8 @@ public final class ShadowRuleTest {
@Test
public void assertNewShadowRulSuccessByAlgorithmProvidedShadowRuleConfiguration() {
- assertShadowDataSourceMappings(shadowRuleWithAlgorithm.getShadowDataSourceMappings());
- assertShadowTableRules(shadowRuleWithAlgorithm.getShadowTableRules());
+ assertShadowDataSourceMappings(shadowRule.getShadowDataSourceMappings());
+ assertShadowTableRules(shadowRule.getShadowTableRules());
}
private void assertShadowTableRules(final Map<String, ShadowTableRule> shadowTableRules) {
@@ -151,14 +135,14 @@ public final class ShadowRuleTest {
@Test
public void assertGetRelatedShadowTables() {
- Collection<String> relatedShadowTables = shadowRuleWithAlgorithm.getRelatedShadowTables(Lists.newArrayList("t_user", "t_auto"));
+ Collection<String> relatedShadowTables = shadowRule.getRelatedShadowTables(Lists.newArrayList("t_user", "t_auto"));
assertThat(relatedShadowTables.size(), is(1));
assertThat(relatedShadowTables.iterator().next(), is("t_user"));
}
@Test
public void assertGetAllShadowTableNames() {
- Collection<String> allShadowTableNames = shadowRuleWithAlgorithm.getAllShadowTableNames();
+ Collection<String> allShadowTableNames = shadowRule.getAllShadowTableNames();
assertThat(allShadowTableNames.size(), is(2));
Iterator<String> iterator = allShadowTableNames.iterator();
assertThat(iterator.next(), is("t_user"));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
index 69a4960e41c..10fac7d8aa0 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/algorithm/keygen/SnowflakeKeyGenerateAlgorithmTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.algorithm.keygen;
import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
@@ -25,6 +26,8 @@ import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.sharding.algorithm.keygen.fixture.FixedTimeService;
import org.apache.shardingsphere.sharding.algorithm.keygen.fixture.WorkerIdGeneratorFixture;
+import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.junit.Test;
import java.lang.reflect.Field;
@@ -50,18 +53,15 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
private static final int DEFAULT_KEY_AMOUNT = 10;
- private final SnowflakeKeyGenerateAlgorithm keyGenerateAlgorithm = new SnowflakeKeyGenerateAlgorithm();
-
@Test
public void assertGenerateKeyWithMultipleThreads() throws ExecutionException, InterruptedException {
int threadNumber = Runtime.getRuntime().availableProcessors() * 2;
ExecutorService executor = Executors.newFixedThreadPool(threadNumber);
int taskNumber = threadNumber * 4;
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.setProps(new Properties());
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
Set<Comparable<?>> actual = new HashSet<>(taskNumber, 1);
for (int i = 0; i < taskNumber; i++) {
- actual.add(executor.submit((Callable<Comparable<?>>) keyGenerateAlgorithm::generateKey).get());
+ actual.add(executor.submit((Callable<Comparable<?>>) algorithm::generateKey).get());
}
assertThat(actual.size(), is(taskNumber));
}
@@ -69,12 +69,11 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
@Test
public void assertGenerateKeyWithSingleThread() {
SnowflakeKeyGenerateAlgorithm.setTimeService(new FixedTimeService(1));
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.setProps(new Properties());
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
List<Comparable<?>> expected = Arrays.asList(0L, 4194305L, 4194306L, 8388608L, 8388609L, 12582913L, 12582914L, 16777216L, 16777217L, 20971521L);
List<Comparable<?>> actual = new ArrayList<>(DEFAULT_KEY_AMOUNT);
for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) {
- actual.add(keyGenerateAlgorithm.generateKey());
+ actual.add(algorithm.generateKey());
}
assertThat(actual, is(expected));
}
@@ -84,13 +83,12 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
SnowflakeKeyGenerateAlgorithm.setTimeService(new FixedTimeService(5));
Properties props = new Properties();
props.setProperty("max-vibration-offset", "3");
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- assertThat(keyGenerateAlgorithm.generateKey(), is(0L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(1L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(2L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(3L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(4L));
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props));
+ assertThat(algorithm.generateKey(), is(0L));
+ assertThat(algorithm.generateKey(), is(1L));
+ assertThat(algorithm.generateKey(), is(2L));
+ assertThat(algorithm.generateKey(), is(3L));
+ assertThat(algorithm.generateKey(), is(4L));
}
@Test
@@ -98,21 +96,20 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
Properties props = new Properties();
SnowflakeKeyGenerateAlgorithm.setTimeService(new TimeService());
props.setProperty("max-vibration-offset", String.valueOf(3));
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- String actualGenerateKey0 = Long.toBinaryString(Long.parseLong(keyGenerateAlgorithm.generateKey().toString()));
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props));
+ String actualGenerateKey0 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
assertThat(Integer.parseInt(actualGenerateKey0.substring(actualGenerateKey0.length() - 3), 2), is(0));
Thread.sleep(2L);
- String actualGenerateKey1 = Long.toBinaryString(Long.parseLong(keyGenerateAlgorithm.generateKey().toString()));
+ String actualGenerateKey1 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
assertThat(Integer.parseInt(actualGenerateKey1.substring(actualGenerateKey1.length() - 3), 2), is(1));
Thread.sleep(2L);
- String actualGenerateKey2 = Long.toBinaryString(Long.parseLong(keyGenerateAlgorithm.generateKey().toString()));
+ String actualGenerateKey2 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
assertThat(Integer.parseInt(actualGenerateKey2.substring(actualGenerateKey2.length() - 3), 2), is(2));
Thread.sleep(2L);
- String actualGenerateKey3 = Long.toBinaryString(Long.parseLong(keyGenerateAlgorithm.generateKey().toString()));
+ String actualGenerateKey3 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
assertThat(Integer.parseInt(actualGenerateKey3.substring(actualGenerateKey3.length() - 3), 2), is(3));
Thread.sleep(2L);
- String actualGenerateKey4 = Long.toBinaryString(Long.parseLong(keyGenerateAlgorithm.generateKey().toString()));
+ String actualGenerateKey4 = Long.toBinaryString(Long.parseLong(algorithm.generateKey().toString()));
assertThat(Integer.parseInt(actualGenerateKey4.substring(actualGenerateKey4.length() - 3), 2), is(0));
}
@@ -120,13 +117,12 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
public void assertGenerateKeyWithClockCallBack() {
TimeService timeService = new FixedTimeService(1);
SnowflakeKeyGenerateAlgorithm.setTimeService(timeService);
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.setProps(new Properties());
- setLastMilliseconds(keyGenerateAlgorithm, timeService.getCurrentMillis() + 2);
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
+ setLastMilliseconds(algorithm, timeService.getCurrentMillis() + 2);
List<Comparable<?>> expected = Arrays.asList(4194304L, 8388609L, 8388610L, 12582912L, 12582913L, 16777217L, 16777218L, 20971520L, 20971521L, 25165825L);
List<Comparable<?>> actual = new ArrayList<>(DEFAULT_KEY_AMOUNT);
for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) {
- actual.add(keyGenerateAlgorithm.generateKey());
+ actual.add(algorithm.generateKey());
}
assertThat(actual, is(expected));
}
@@ -137,12 +133,11 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
SnowflakeKeyGenerateAlgorithm.setTimeService(timeService);
Properties props = new Properties();
props.setProperty("max-tolerate-time-difference-milliseconds", String.valueOf(0));
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- setLastMilliseconds(keyGenerateAlgorithm, timeService.getCurrentMillis() + 2);
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props));
+ setLastMilliseconds(algorithm, timeService.getCurrentMillis() + 2);
List<Comparable<?>> actual = new ArrayList<>(DEFAULT_KEY_AMOUNT);
for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) {
- actual.add(keyGenerateAlgorithm.generateKey());
+ actual.add(algorithm.generateKey());
}
assertThat(actual.size(), not(10));
}
@@ -151,80 +146,69 @@ public final class SnowflakeKeyGenerateAlgorithmTest {
public void assertGenerateKeyBeyondMaxSequencePerMilliSecond() {
TimeService timeService = new FixedTimeService(2);
SnowflakeKeyGenerateAlgorithm.setTimeService(timeService);
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.setProps(new Properties());
- setLastMilliseconds(keyGenerateAlgorithm, timeService.getCurrentMillis());
- setSequence(keyGenerateAlgorithm, (1 << DEFAULT_SEQUENCE_BITS) - 1);
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
+ setLastMilliseconds(algorithm, timeService.getCurrentMillis());
+ setSequence(algorithm, (1 << DEFAULT_SEQUENCE_BITS) - 1);
List<Comparable<?>> expected = Arrays.asList(4194304L, 4194305L, 4194306L, 8388608L, 8388609L, 8388610L, 12582913L, 12582914L, 12582915L, 16777216L);
List<Comparable<?>> actual = new ArrayList<>(DEFAULT_KEY_AMOUNT);
for (int i = 0; i < DEFAULT_KEY_AMOUNT; i++) {
- actual.add(keyGenerateAlgorithm.generateKey());
+ actual.add(algorithm.generateKey());
}
assertThat(actual, is(expected));
}
@SneakyThrows(ReflectiveOperationException.class)
- private void setSequence(final SnowflakeKeyGenerateAlgorithm keyGenerateAlgorithm, final Number value) {
- Field sequence = SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("sequence");
- sequence.setAccessible(true);
- sequence.set(keyGenerateAlgorithm, value);
+ private void setLastMilliseconds(final KeyGenerateAlgorithm algorithm, final Number value) {
+ Field lastMilliseconds = SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("lastMilliseconds");
+ lastMilliseconds.setAccessible(true);
+ lastMilliseconds.set(algorithm, value);
}
@SneakyThrows(ReflectiveOperationException.class)
- private void setLastMilliseconds(final SnowflakeKeyGenerateAlgorithm keyGenerateAlgorithm, final Number value) {
- Field lastMilliseconds = SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("lastMilliseconds");
- lastMilliseconds.setAccessible(true);
- lastMilliseconds.set(keyGenerateAlgorithm, value);
+ private void setSequence(final KeyGenerateAlgorithm algorithm, final Number value) {
+ Field sequence = SnowflakeKeyGenerateAlgorithm.class.getDeclaredField("sequence");
+ sequence.setAccessible(true);
+ sequence.set(algorithm, value);
}
@Test(expected = IllegalArgumentException.class)
public void assertSetWorkerIdFailureWhenNegative() {
- keyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(-1L),
+ SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(-1L),
new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.generateKey();
- clearInstanceContext();
+ algorithm.init(new Properties());
+ algorithm.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetMaxVibrationOffsetFailureWhenNegative() {
Properties props = new Properties();
props.setProperty("max-vibration-offset", String.valueOf(-1));
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- keyGenerateAlgorithm.generateKey();
+ KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props)).generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetWorkerIdFailureWhenOutOfRange() {
- keyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(Long.MIN_VALUE),
+ SnowflakeKeyGenerateAlgorithm algorithm = (SnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties()));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(Long.MIN_VALUE),
new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.generateKey();
- clearInstanceContext();
+ algorithm.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertSetMaxVibrationOffsetFailureWhenOutOfRange() {
Properties props = new Properties();
props.setProperty("max-vibration-offset", String.valueOf(4096));
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- keyGenerateAlgorithm.generateKey();
+ KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props)).generateKey();
}
@Test
public void assertSetMaxTolerateTimeDifferenceMilliseconds() throws NoSuchFieldException, IllegalAccessException {
Properties props = new Properties();
props.setProperty("max-tolerate-time-difference-milliseconds", String.valueOf(1));
- keyGenerateAlgorithm.init(props);
- keyGenerateAlgorithm.setProps(props);
- Field field = keyGenerateAlgorithm.getClass().getDeclaredField("props");
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", props));
+ Field field = algorithm.getClass().getDeclaredField("props");
field.setAccessible(true);
- assertThat(((Properties) field.get(keyGenerateAlgorithm)).getProperty("max-tolerate-time-difference-milliseconds"), is("1"));
- }
-
- private void clearInstanceContext() {
- keyGenerateAlgorithm.setInstanceContext(null);
+ assertThat(((Properties) field.get(algorithm)).getProperty("max-tolerate-time-difference-milliseconds"), is("1"));
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
index 1e33bd87f56..bc7f65bbc7b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdKeyGenerateAlgorithmTest.java
@@ -22,7 +22,10 @@ import me.ahoo.cosid.provider.NotFoundIdGeneratorException;
import me.ahoo.cosid.segment.DefaultSegmentId;
import me.ahoo.cosid.segment.IdSegmentDistributor;
import me.ahoo.cosid.util.MockIdGenerator;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
+import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
+import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.junit.Test;
import java.util.Properties;
@@ -40,44 +43,47 @@ public final class CosIdKeyGenerateAlgorithmTest {
String idName = "test-cosid";
DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
DefaultIdGeneratorProvider.INSTANCE.set(idName, defaultSegmentId);
- Properties props = new Properties();
- props.setProperty(CosIdAlgorithmConstants.ID_NAME_KEY, idName);
- CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
- keyGenerateAlgorithm.init(props);
- assertThat(keyGenerateAlgorithm.generateKey(), is(1L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(2L));
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID", createAsLongProperties(idName)));
+ assertThat(algorithm.generateKey(), is(1L));
+ assertThat(algorithm.generateKey(), is(2L));
+ }
+
+ private Properties createAsLongProperties(final String idName) {
+ Properties result = new Properties();
+ result.setProperty(CosIdAlgorithmConstants.ID_NAME_KEY, idName);
+ return result;
}
@Test
public void assertGenerateKeyWhenNotSetIdName() {
DefaultSegmentId defaultSegmentId = new DefaultSegmentId(new IdSegmentDistributor.Mock());
DefaultIdGeneratorProvider.INSTANCE.setShare(defaultSegmentId);
- CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
- keyGenerateAlgorithm.init(new Properties());
- assertThat(keyGenerateAlgorithm.generateKey(), is(1L));
- assertThat(keyGenerateAlgorithm.generateKey(), is(2L));
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID", new Properties()));
+ assertThat(algorithm.generateKey(), is(1L));
+ assertThat(algorithm.generateKey(), is(2L));
}
@Test(expected = NotFoundIdGeneratorException.class)
public void assertGenerateKeyWhenIdProviderIsEmpty() {
DefaultIdGeneratorProvider.INSTANCE.clear();
- CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
- keyGenerateAlgorithm.init(new Properties());
- keyGenerateAlgorithm.generateKey();
+ KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID", new Properties())).generateKey();
}
@Test
public void assertGenerateKeyAsString() {
String idName = "test-cosid-as-string";
DefaultIdGeneratorProvider.INSTANCE.set(idName, MockIdGenerator.INSTANCE);
- Properties props = new Properties();
- props.setProperty(CosIdAlgorithmConstants.ID_NAME_KEY, idName);
- props.setProperty(CosIdKeyGenerateAlgorithm.AS_STRING_KEY, Boolean.TRUE.toString());
- CosIdKeyGenerateAlgorithm keyGenerateAlgorithm = new CosIdKeyGenerateAlgorithm();
- keyGenerateAlgorithm.init(props);
- Comparable<?> actual = keyGenerateAlgorithm.generateKey();
+ KeyGenerateAlgorithm algorithm = KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID", createAsStringProperties(idName)));
+ Comparable<?> actual = algorithm.generateKey();
assertThat(actual, instanceOf(String.class));
assertThat(actual.toString(), startsWith("test_"));
assertTrue(actual.toString().length() <= 16);
}
+
+ private Properties createAsStringProperties(final String idName) {
+ Properties result = new Properties();
+ result.setProperty(CosIdAlgorithmConstants.ID_NAME_KEY, idName);
+ result.setProperty(CosIdKeyGenerateAlgorithm.AS_STRING_KEY, Boolean.TRUE.toString());
+ return result;
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
index 0b9d32d92da..576cab73a8b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/keygen/CosIdSnowflakeKeyGenerateAlgorithmTest.java
@@ -22,20 +22,22 @@ import me.ahoo.cosid.snowflake.MillisecondSnowflakeId;
import me.ahoo.cosid.snowflake.MillisecondSnowflakeIdStateParser;
import me.ahoo.cosid.snowflake.SnowflakeIdState;
import me.ahoo.cosid.snowflake.SnowflakeIdStateParser;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.ComputeNodeInstance;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.instance.definition.InstanceDefinition;
import org.apache.shardingsphere.infra.lock.LockContext;
import org.apache.shardingsphere.sharding.cosid.algorithm.keygen.fixture.WorkerIdGeneratorFixture;
+import org.apache.shardingsphere.sharding.factory.KeyGenerateAlgorithmFactory;
import org.junit.Test;
-import org.mockito.Mockito;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@@ -49,12 +51,12 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@Test
public void assertGenerateKey() {
- CosIdSnowflakeKeyGenerateAlgorithm cosIdSnowflakeKeyGenerateAlgorithm = new CosIdSnowflakeKeyGenerateAlgorithm();
- cosIdSnowflakeKeyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(Mockito.mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID),
- new ModeConfiguration("Memory", null, false), Mockito.mock(LockContext.class)));
- cosIdSnowflakeKeyGenerateAlgorithm.init(new Properties());
- long firstActualKey = (Long) cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
- long secondActualKey = (Long) cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
+ CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID),
+ new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
+ long firstActualKey = (Long) algorithm.generateKey();
+ long secondActualKey = (Long) algorithm.generateKey();
SnowflakeIdState firstActualState = snowflakeIdStateParser.parse(firstActualKey);
SnowflakeIdState secondActualState = snowflakeIdStateParser.parse(secondActualKey);
assertThat(firstActualState.getMachineId(), is(FIXTURE_WORKER_ID));
@@ -66,14 +68,13 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@Test
public void assertGenerateKeyAsString() {
- CosIdSnowflakeKeyGenerateAlgorithm cosIdSnowflakeKeyGenerateAlgorithm = new CosIdSnowflakeKeyGenerateAlgorithm();
- cosIdSnowflakeKeyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(Mockito.mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID),
- new ModeConfiguration("Memory", null, false), Mockito.mock(LockContext.class)));
Properties props = new Properties();
props.setProperty(CosIdSnowflakeKeyGenerateAlgorithm.AS_STRING_KEY, Boolean.TRUE.toString());
- cosIdSnowflakeKeyGenerateAlgorithm.init(props);
- cosIdSnowflakeKeyGenerateAlgorithm.setProps(props);
- Comparable<?> actualKey = cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
+ CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("COSID_SNOWFLAKE", props));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)),
+ new WorkerIdGeneratorFixture(FIXTURE_WORKER_ID), new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
+ Comparable<?> actualKey = algorithm.generateKey();
assertThat(actualKey, instanceOf(String.class));
String actualStringKey = (String) actualKey;
assertThat(actualStringKey.length(), is(Radix62IdConverter.MAX_CHAR_SIZE));
@@ -85,26 +86,24 @@ public final class CosIdSnowflakeKeyGenerateAlgorithmTest {
@Test(expected = NullPointerException.class)
public void assertGenerateKeyWhenNoneInstanceContext() {
- CosIdSnowflakeKeyGenerateAlgorithm cosIdSnowflakeKeyGenerateAlgorithm = new CosIdSnowflakeKeyGenerateAlgorithm();
- cosIdSnowflakeKeyGenerateAlgorithm.init(new Properties());
- cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
+ KeyGenerateAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID_SNOWFLAKE", new Properties())).generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertGenerateKeyWhenNegative() {
- CosIdSnowflakeKeyGenerateAlgorithm cosIdSnowflakeKeyGenerateAlgorithm = new CosIdSnowflakeKeyGenerateAlgorithm();
- cosIdSnowflakeKeyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(Mockito.mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(-1),
- new ModeConfiguration("Memory", null, false), Mockito.mock(LockContext.class)));
- cosIdSnowflakeKeyGenerateAlgorithm.init(new Properties());
- cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
+ CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(-1),
+ new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
+ algorithm.generateKey();
}
@Test(expected = IllegalArgumentException.class)
public void assertGenerateKeyWhenGreaterThen1023() {
- CosIdSnowflakeKeyGenerateAlgorithm cosIdSnowflakeKeyGenerateAlgorithm = new CosIdSnowflakeKeyGenerateAlgorithm();
- cosIdSnowflakeKeyGenerateAlgorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(Mockito.mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(1024),
- new ModeConfiguration("Memory", null, false), Mockito.mock(LockContext.class)));
- cosIdSnowflakeKeyGenerateAlgorithm.init(new Properties());
- cosIdSnowflakeKeyGenerateAlgorithm.generateKey();
+ CosIdSnowflakeKeyGenerateAlgorithm algorithm = (CosIdSnowflakeKeyGenerateAlgorithm) KeyGenerateAlgorithmFactory.newInstance(
+ new ShardingSphereAlgorithmConfiguration("COSID_SNOWFLAKE", new Properties()));
+ algorithm.setInstanceContext(new InstanceContext(new ComputeNodeInstance(mock(InstanceDefinition.class)), new WorkerIdGeneratorFixture(1024),
+ new ModeConfiguration("Memory", null, false), mock(LockContext.class)));
+ algorithm.generateKey();
}
}