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 2022/05/05 13:06:21 UTC
[shardingsphere] branch master updated: Refactor `CosIdIntervalShardingAlgorithmTest` to improve readability. (#17345)
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 f7feeb7c9d5 Refactor `CosIdIntervalShardingAlgorithmTest` to improve readability. (#17345)
f7feeb7c9d5 is described below
commit f7feeb7c9d5039f83d70c21426dba12afb77c9c8
Author: Ahoo Wang <ah...@qq.com>
AuthorDate: Thu May 5 21:06:16 2022 +0800
Refactor `CosIdIntervalShardingAlgorithmTest` to improve readability. (#17345)
---
...tractPreciseShardingValueParameterizedTest.java | 53 +++
...bstractRangeShardingValueParameterizedTest.java | 57 +++
.../CosIdIntervalShardingAlgorithmTest.java | 389 ---------------------
...osIdSnowflakeIntervalShardingAlgorithmTest.java | 27 +-
.../DateCosIdIntervalShardingAlgorithmTest.java | 73 ++++
...DateTimeCosIdIntervalShardingAlgorithmTest.java | 73 ++++
.../StringCosIdIntervalShardingAlgorithmTest.java | 72 ++++
...imestampCosIdIntervalShardingAlgorithmTest.java | 71 ++++
.../IntervalShardingAlgorithmDataFixture.java | 141 ++++++++
9 files changed, 554 insertions(+), 402 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractPreciseShardingValueParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractPreciseShardingValueParameterizedTest.java
new file mode 100644
index 00000000000..93632c27057
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractPreciseShardingValueParameterizedTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Before;
+
+@RequiredArgsConstructor
+public abstract class AbstractPreciseShardingValueParameterizedTest<T extends Comparable<?>> {
+ private final T input;
+
+ private final String expected;
+
+ private CosIdIntervalShardingAlgorithm algorithm;
+
+ @Before
+ public void setup() {
+ algorithm = IntervalShardingAlgorithmDataFixture.createShardingAlgorithm();
+ }
+
+ /**
+ * Do sharding test.
+ */
+ protected void doSharding() {
+ PreciseShardingValue<Comparable<?>> shardingValue = new PreciseShardingValue<>(IntervalShardingAlgorithmDataFixture.LOGIC_NAME, IntervalShardingAlgorithmDataFixture.COLUMN_NAME,
+ new DataNodeInfo(IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX, 6, '0'),
+ input);
+ String actual = algorithm.doSharding(IntervalShardingAlgorithmDataFixture.ALL_NODES, shardingValue);
+ assertThat(actual, is(expected));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractRangeShardingValueParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractRangeShardingValueParameterizedTest.java
new file mode 100644
index 00000000000..056e2a92af1
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/AbstractRangeShardingValueParameterizedTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import com.google.common.collect.Range;
+import java.util.Collection;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
+import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import org.junit.Before;
+
+@RequiredArgsConstructor
+public abstract class AbstractRangeShardingValueParameterizedTest<T extends Comparable<?>> {
+ private final Range<T> input;
+
+ private final Collection<String> expected;
+
+ private CosIdIntervalShardingAlgorithm algorithm;
+
+ @Before
+ public void init() {
+ algorithm = IntervalShardingAlgorithmDataFixture.createShardingAlgorithm();
+ }
+
+ /**
+ * Do sharding test.
+ */
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ protected void doSharding() {
+ RangeShardingValue shardingValue = new RangeShardingValue<>(
+ IntervalShardingAlgorithmDataFixture.LOGIC_NAME,
+ IntervalShardingAlgorithmDataFixture.COLUMN_NAME,
+ new DataNodeInfo(IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX, 6, '0'),
+ input);
+ Collection<String> actual = algorithm.doSharding(IntervalShardingAlgorithmDataFixture.ALL_NODES, shardingValue);
+ assertThat(actual, is(expected));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithmTest.java
deleted file mode 100644
index 15fdc45f5a9..00000000000
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdIntervalShardingAlgorithmTest.java
+++ /dev/null
@@ -1,389 +0,0 @@
-/*
- * 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.sharding.cosid.algorithm.sharding.interval;
-
-import com.google.common.collect.Range;
-import lombok.RequiredArgsConstructor;
-import me.ahoo.cosid.sharding.ExactCollection;
-import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import org.apache.shardingsphere.infra.datanode.DataNodeInfo;
-import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
-import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
-import org.apache.shardingsphere.sharding.cosid.algorithm.Arguments;
-import org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
-import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
-
-import java.time.LocalDateTime;
-import java.time.ZoneOffset;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Properties;
-import java.util.function.Function;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class CosIdIntervalShardingAlgorithmTest {
-
- static final ZoneOffset ZONE_OFFSET_SHANGHAI;
-
- static final LocalDateTime LOWER_DATE_TIME;
-
- static final LocalDateTime UPPER_DATE_TIME;
-
- static final String LOGIC_NAME;
-
- static final String LOGIC_NAME_PREFIX;
-
- static final String COLUMN_NAME;
-
- static final String SUFFIX_FORMATTER_STRING;
-
- static final ExactCollection<String> ALL_NODES;
-
- static {
- ZONE_OFFSET_SHANGHAI = ZoneOffset.of("+8");
- LOWER_DATE_TIME = LocalDateTime.of(2021, 1, 1, 0, 0);
- UPPER_DATE_TIME = LOWER_DATE_TIME.plusYears(1);
- LOGIC_NAME = "table";
- LOGIC_NAME_PREFIX = LOGIC_NAME + "_";
- COLUMN_NAME = "create_time";
- SUFFIX_FORMATTER_STRING = "yyyyMM";
- ALL_NODES = new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104",
- "table_202105", "table_202106", "table_202107", "table_202108", "table_202109",
- "table_202110", "table_202111", "table_202112", "table_202201");
- }
-
- static CosIdIntervalShardingAlgorithm createShardingAlgorithm() {
- return (CosIdIntervalShardingAlgorithm) ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID_INTERVAL", createProperties()));
- }
-
- private static Properties createProperties() {
- Properties result = new Properties();
- result.setProperty(CosIdIntervalShardingAlgorithm.ZONE_ID_KEY, "Asia/Shanghai");
- result.setProperty(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, LOGIC_NAME_PREFIX);
- result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- result.setProperty(CosIdIntervalShardingAlgorithm.SHARDING_SUFFIX_FORMAT_KEY, SUFFIX_FORMATTER_STRING);
- result.setProperty(CosIdIntervalShardingAlgorithm.INTERVAL_UNIT_KEY, "MONTHS");
- result.put(CosIdIntervalShardingAlgorithm.INTERVAL_AMOUNT_KEY, 1);
- return result;
- }
-
- static Iterable<Object[]> preciseArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) {
- return Arguments.ofArrayElement(
- Arguments.of(datetimeConvert.apply(LOWER_DATE_TIME), "table_202101"),
- Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 2, 14, 22, 0)), "table_202102"),
- Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 10, 1, 0, 0)), "table_202110"),
- Arguments.of(datetimeConvert.apply(UPPER_DATE_TIME), "table_202201"));
- }
-
- static Iterable<Object[]> preciseArgsProviderAsLocalDateTime() {
- return preciseArgsProvider(ldt -> ldt);
- }
-
- static Iterable<Object[]> preciseArgsProviderAsString() {
- return preciseArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- }
-
- static Iterable<Object[]> preciseArgsProviderAsDate() {
- return preciseArgsProvider(ldt -> new Date(ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli()));
- }
-
- static Iterable<Object[]> preciseArgsProviderAsTimestamp() {
- return preciseArgsProvider(ldt -> ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli());
- }
-
- static Iterable<Object[]> rangeArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) {
- return Arguments.ofArrayElement(
- Arguments.of(Range.all(), ALL_NODES),
- Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
- Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 1, 1, 0, 0)),
- datetimeConvert.apply(LocalDateTime.of(2021, 2, 1, 0, 0))),
- new ExactCollection<>("table_202101", "table_202102")),
- Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME.minusMonths(1)), datetimeConvert.apply(UPPER_DATE_TIME.plusMonths(1))), ALL_NODES),
- Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 12, 1, 0, 0)),
- datetimeConvert.apply(LocalDateTime.of(2022, 2, 1, 0, 0))),
- new ExactCollection<>("table_202112", "table_202201")),
- Arguments.of(Range.closedOpen(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)),
- new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105", "table_202106",
- "table_202107", "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")),
- Arguments.of(Range.openClosed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
- Arguments.of(Range.greaterThan(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES),
- Arguments.of(Range.atLeast(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES),
- Arguments.of(Range.greaterThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")),
- Arguments.of(Range.atLeast(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")),
- Arguments.of(Range.greaterThan(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))),
- new ExactCollection<>("table_202112", "table_202201")),
- Arguments.of(Range.atLeast(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))),
- new ExactCollection<>("table_202112", "table_202201")),
- Arguments.of(Range.lessThan(datetimeConvert.apply(LOWER_DATE_TIME)), ExactCollection.empty()),
- Arguments.of(Range.atMost(datetimeConvert.apply(LOWER_DATE_TIME)), new ExactCollection<>("table_202101")),
- Arguments.of(Range.lessThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202101",
- "table_202102", "table_202103", "table_202104", "table_202105", "table_202106", "table_202107",
- "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")),
- Arguments.of(Range.atMost(datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
- Arguments.of(Range.lessThan(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))),
- new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")),
- Arguments.of(Range.atMost(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))),
- new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")));
- }
-
- static Iterable<Object[]> rangeArgsProviderAsLocalDateTime() {
- return rangeArgsProvider(ldt -> ldt);
- }
-
- static Iterable<Object[]> rangeArgsProviderAsString() {
- return rangeArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- }
-
- static Iterable<Object[]> rangeArgsProviderAsDate() {
- return rangeArgsProvider(ldt -> new Date(ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli()));
- }
-
- static Iterable<Object[]> rangeArgsProviderAsTimestamp() {
- return rangeArgsProvider(ldt -> ldt.toInstant(ZONE_OFFSET_SHANGHAI).toEpochMilli());
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class LocalDateTimePreciseValueDoShardingTest {
-
- private final LocalDateTime dateTime;
-
- private final String expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return preciseArgsProviderAsLocalDateTime();
- }
-
- @Test
- public void assertDoSharding() {
- PreciseShardingValue<Comparable<?>> shardingValue = new PreciseShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), dateTime);
- String actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class LocalDateTimeRangeValueDoShardingTest {
-
- private final Range<LocalDateTime> rangeValue;
-
- private final Collection<String> expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return rangeArgsProviderAsLocalDateTime();
- }
-
- @Test
- public void assertDoSharding() {
- RangeShardingValue shardingValue = new RangeShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
- Collection<String> actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class StringPreciseValueDoShardingTest {
-
- private final String value;
-
- private final String expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return preciseArgsProviderAsString();
- }
-
- @Test
- public void assertDoSharding() {
- PreciseShardingValue shardingValue = new PreciseShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), value);
- String actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class StringRangeValueDoShardingTest {
-
- private final Range<String> rangeValue;
-
- private final Collection<String> expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return rangeArgsProviderAsString();
- }
-
- @Test
- public void assertDoSharding() {
- RangeShardingValue shardingValue = new RangeShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
- Collection<String> actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class DatePreciseValueDoShardingTest {
-
- private final Date value;
-
- private final String expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return preciseArgsProviderAsDate();
- }
-
- @Test
- public void assertDoSharding() {
- PreciseShardingValue shardingValue = new PreciseShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), value);
- String actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class DateRangeValueDoShardingTest {
-
- private final Range<Date> rangeValue;
-
- private final Collection<String> expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return rangeArgsProviderAsDate();
- }
-
- @Test
- public void assertDoSharding() {
- RangeShardingValue shardingValue = new RangeShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
- assertThat(algorithm.doSharding(ALL_NODES, shardingValue), is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class TimestampPreciseValueDoShardingTest {
-
- private final Long value;
-
- private final String expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return preciseArgsProviderAsTimestamp();
- }
-
- @Test
- public void assertDoSharding() {
- PreciseShardingValue shardingValue = new PreciseShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), value);
- assertThat(algorithm.doSharding(ALL_NODES, shardingValue), is(expected));
- }
- }
-
- @RunWith(Parameterized.class)
- @RequiredArgsConstructor
- public static class TimestampRangeValueDoShardingTest {
-
- private final Range<Long> rangeValue;
-
- private final Collection<String> expected;
-
- private CosIdIntervalShardingAlgorithm algorithm;
-
- @Before
- public void init() {
- algorithm = createShardingAlgorithm();
- }
-
- @Parameters
- public static Iterable<Object[]> argsProvider() {
- return rangeArgsProviderAsTimestamp();
- }
-
- @Test
- public void assertDoSharding() {
- RangeShardingValue shardingValue = new RangeShardingValue<>(LOGIC_NAME, COLUMN_NAME, new DataNodeInfo(LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
- Collection<String> actual = algorithm.doSharding(ALL_NODES, shardingValue);
- assertThat(actual, is(expected));
- }
- }
-}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
index b3203819809..2db1827e9ca 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/CosIdSnowflakeIntervalShardingAlgorithmTest.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstant
import org.apache.shardingsphere.sharding.cosid.algorithm.keygen.CosIdSnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,7 +43,7 @@ import static org.junit.Assert.assertThat;
public final class CosIdSnowflakeIntervalShardingAlgorithmTest {
private static long convertToSnowflakeId(final LocalDateTime dateTime) {
- long epochMilliseconds = dateTime.toInstant(CosIdIntervalShardingAlgorithmTest.ZONE_OFFSET_SHANGHAI).toEpochMilli();
+ long epochMilliseconds = dateTime.toInstant(IntervalShardingAlgorithmDataFixture.ZONE_OFFSET_SHANGHAI).toEpochMilli();
long timeDiff = epochMilliseconds - CosIdSnowflakeKeyGenerateAlgorithm.DEFAULT_EPOCH;
return timeDiff << (MillisecondSnowflakeId.DEFAULT_SEQUENCE_BIT + MillisecondSnowflakeId.DEFAULT_MACHINE_BIT);
}
@@ -50,10 +51,10 @@ public final class CosIdSnowflakeIntervalShardingAlgorithmTest {
private static Properties createProperties() {
Properties result = new Properties();
result.setProperty(CosIdIntervalShardingAlgorithm.ZONE_ID_KEY, "Asia/Shanghai");
- result.setProperty(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, CosIdIntervalShardingAlgorithmTest.LOGIC_NAME_PREFIX);
- result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, CosIdIntervalShardingAlgorithmTest.LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, CosIdIntervalShardingAlgorithmTest.UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
- result.setProperty(CosIdIntervalShardingAlgorithm.SHARDING_SUFFIX_FORMAT_KEY, CosIdIntervalShardingAlgorithmTest.SUFFIX_FORMATTER_STRING);
+ result.setProperty(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX);
+ result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, IntervalShardingAlgorithmDataFixture.LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, IntervalShardingAlgorithmDataFixture.UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ result.setProperty(CosIdIntervalShardingAlgorithm.SHARDING_SUFFIX_FORMAT_KEY, IntervalShardingAlgorithmDataFixture.SUFFIX_FORMATTER_STRING);
result.setProperty(CosIdIntervalShardingAlgorithm.INTERVAL_UNIT_KEY, "MONTHS");
result.put(CosIdIntervalShardingAlgorithm.INTERVAL_AMOUNT_KEY, 1);
return result;
@@ -69,16 +70,16 @@ public final class CosIdSnowflakeIntervalShardingAlgorithmTest {
@Parameters
public static Iterable<Object[]> argsProvider() {
- return CosIdIntervalShardingAlgorithmTest.preciseArgsProvider(CosIdSnowflakeIntervalShardingAlgorithmTest::convertToSnowflakeId);
+ return IntervalShardingAlgorithmDataFixture.preciseArgsProvider(CosIdSnowflakeIntervalShardingAlgorithmTest::convertToSnowflakeId);
}
@Test
public void assertDoSharding() {
CosIdSnowflakeIntervalShardingAlgorithm algorithm = (CosIdSnowflakeIntervalShardingAlgorithm) ShardingAlgorithmFactory.newInstance(
new ShardingSphereAlgorithmConfiguration("COSID_INTERVAL_SNOWFLAKE", createProperties()));
- PreciseShardingValue shardingValue = new PreciseShardingValue<>(CosIdIntervalShardingAlgorithmTest.LOGIC_NAME,
- CosIdIntervalShardingAlgorithmTest.COLUMN_NAME, new DataNodeInfo(CosIdIntervalShardingAlgorithmTest.LOGIC_NAME_PREFIX, 6, '0'), snowflakeId);
- String actual = algorithm.doSharding(CosIdIntervalShardingAlgorithmTest.ALL_NODES, shardingValue);
+ PreciseShardingValue shardingValue = new PreciseShardingValue<>(IntervalShardingAlgorithmDataFixture.LOGIC_NAME,
+ IntervalShardingAlgorithmDataFixture.COLUMN_NAME, new DataNodeInfo(IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX, 6, '0'), snowflakeId);
+ String actual = algorithm.doSharding(IntervalShardingAlgorithmDataFixture.ALL_NODES, shardingValue);
assertThat(actual, is(expected));
}
}
@@ -93,16 +94,16 @@ public final class CosIdSnowflakeIntervalShardingAlgorithmTest {
@Parameters
public static Iterable<Object[]> argsProvider() {
- return CosIdIntervalShardingAlgorithmTest.rangeArgsProvider(CosIdSnowflakeIntervalShardingAlgorithmTest::convertToSnowflakeId);
+ return IntervalShardingAlgorithmDataFixture.rangeArgsProvider(CosIdSnowflakeIntervalShardingAlgorithmTest::convertToSnowflakeId);
}
@Test
public void assertDoSharding() {
CosIdSnowflakeIntervalShardingAlgorithm algorithm = (CosIdSnowflakeIntervalShardingAlgorithm) ShardingAlgorithmFactory.newInstance(
new ShardingSphereAlgorithmConfiguration("COSID_INTERVAL_SNOWFLAKE", createProperties()));
- RangeShardingValue shardingValue = new RangeShardingValue<>(CosIdIntervalShardingAlgorithmTest.LOGIC_NAME,
- CosIdIntervalShardingAlgorithmTest.COLUMN_NAME, new DataNodeInfo(CosIdIntervalShardingAlgorithmTest.LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
- assertThat(algorithm.doSharding(CosIdIntervalShardingAlgorithmTest.ALL_NODES, shardingValue), is(expected));
+ RangeShardingValue shardingValue = new RangeShardingValue<>(IntervalShardingAlgorithmDataFixture.LOGIC_NAME,
+ IntervalShardingAlgorithmDataFixture.COLUMN_NAME, new DataNodeInfo(IntervalShardingAlgorithmDataFixture.LOGIC_NAME_PREFIX, 6, '0'), rangeValue);
+ assertThat(algorithm.doSharding(IntervalShardingAlgorithmDataFixture.ALL_NODES, shardingValue), is(expected));
}
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/DateCosIdIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/DateCosIdIntervalShardingAlgorithmTest.java
new file mode 100644
index 00000000000..48c6ff56453
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/DateCosIdIntervalShardingAlgorithmTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import com.google.common.collect.Range;
+import java.util.Collection;
+import java.util.Date;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+public final class DateCosIdIntervalShardingAlgorithmTest {
+
+ static Iterable<Object[]> preciseArgsProviderAsDate() {
+ return IntervalShardingAlgorithmDataFixture.preciseArgsProvider(ldt -> new Date(ldt.toInstant(IntervalShardingAlgorithmDataFixture.ZONE_OFFSET_SHANGHAI).toEpochMilli()));
+ }
+
+ static Iterable<Object[]> rangeArgsProviderAsDate() {
+ return IntervalShardingAlgorithmDataFixture.rangeArgsProvider(ldt -> new Date(ldt.toInstant(IntervalShardingAlgorithmDataFixture.ZONE_OFFSET_SHANGHAI).toEpochMilli()));
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class PreciseShardingValueDoShardingTest extends AbstractPreciseShardingValueParameterizedTest<Date> {
+
+ public PreciseShardingValueDoShardingTest(final Date input, final String expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return preciseArgsProviderAsDate();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class RangeShardingValueDoShardingTest extends AbstractRangeShardingValueParameterizedTest<Date> {
+
+ public RangeShardingValueDoShardingTest(final Range<Date> input, final Collection<String> expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return rangeArgsProviderAsDate();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/LocalDateTimeCosIdIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/LocalDateTimeCosIdIntervalShardingAlgorit [...]
new file mode 100644
index 00000000000..05e368c80b5
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/LocalDateTimeCosIdIntervalShardingAlgorithmTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import com.google.common.collect.Range;
+import java.time.LocalDateTime;
+import java.util.Collection;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+public final class LocalDateTimeCosIdIntervalShardingAlgorithmTest {
+
+ static Iterable<Object[]> preciseArgsProviderAsLocalDateTime() {
+ return IntervalShardingAlgorithmDataFixture.preciseArgsProvider(ldt -> ldt);
+ }
+
+ static Iterable<Object[]> rangeArgsProviderAsLocalDateTime() {
+ return IntervalShardingAlgorithmDataFixture.rangeArgsProvider(ldt -> ldt);
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class PreciseShardingValueDoShardingTest extends AbstractPreciseShardingValueParameterizedTest<LocalDateTime> {
+
+ public PreciseShardingValueDoShardingTest(final LocalDateTime input, final String expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return preciseArgsProviderAsLocalDateTime();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class RangeShardingValueDoShardingTest extends AbstractRangeShardingValueParameterizedTest<LocalDateTime> {
+
+ public RangeShardingValueDoShardingTest(final Range<LocalDateTime> input, final Collection<String> expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return rangeArgsProviderAsLocalDateTime();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/StringCosIdIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/StringCosIdIntervalShardingAlgorithmTest.java
new file mode 100644
index 00000000000..73d30ce0d8d
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/StringCosIdIntervalShardingAlgorithmTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import com.google.common.collect.Range;
+import java.util.Collection;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+public final class StringCosIdIntervalShardingAlgorithmTest {
+
+ static Iterable<Object[]> preciseArgsProviderAsString() {
+ return IntervalShardingAlgorithmDataFixture.preciseArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ }
+
+ static Iterable<Object[]> rangeArgsProviderAsString() {
+ return IntervalShardingAlgorithmDataFixture.rangeArgsProvider(ldt -> ldt.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class PreciseShardingValueDoShardingTest extends AbstractPreciseShardingValueParameterizedTest<String> {
+
+ public PreciseShardingValueDoShardingTest(final String input, final String expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return preciseArgsProviderAsString();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class RangeShardingValueDoShardingTest extends AbstractRangeShardingValueParameterizedTest<String> {
+
+ public RangeShardingValueDoShardingTest(final Range<String> input, final Collection<String> expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return rangeArgsProviderAsString();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/TimestampCosIdIntervalShardingAlgorithmTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/TimestampCosIdIntervalShardingAlgorithmTest.java
new file mode 100644
index 00000000000..352c23618a1
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/TimestampCosIdIntervalShardingAlgorithmTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval;
+
+import com.google.common.collect.Range;
+import java.util.Collection;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.fixture.IntervalShardingAlgorithmDataFixture;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+public final class TimestampCosIdIntervalShardingAlgorithmTest {
+ static Iterable<Object[]> preciseArgsProviderAsTimestamp() {
+ return IntervalShardingAlgorithmDataFixture.preciseArgsProvider(ldt -> ldt.toInstant(IntervalShardingAlgorithmDataFixture.ZONE_OFFSET_SHANGHAI).toEpochMilli());
+ }
+
+ static Iterable<Object[]> rangeArgsProviderAsTimestamp() {
+ return IntervalShardingAlgorithmDataFixture.rangeArgsProvider(ldt -> ldt.toInstant(IntervalShardingAlgorithmDataFixture.ZONE_OFFSET_SHANGHAI).toEpochMilli());
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class PreciseShardingValueDoShardingTest extends AbstractPreciseShardingValueParameterizedTest<Long> {
+
+ public PreciseShardingValueDoShardingTest(final Long input, final String expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return preciseArgsProviderAsTimestamp();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static final class RangeShardingValueDoShardingTest extends AbstractRangeShardingValueParameterizedTest<Long> {
+
+ public RangeShardingValueDoShardingTest(final Range<Long> input, final Collection<String> expected) {
+ super(input, expected);
+ }
+
+ @Parameterized.Parameters(name = "{index}: doSharding({0})={1}")
+ public static Iterable<Object[]> argsProvider() {
+ return rangeArgsProviderAsTimestamp();
+ }
+
+ @Test
+ public void assertDoSharding() {
+ doSharding();
+ }
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
new file mode 100644
index 00000000000..b971fe46027
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-plugin/shardingsphere-sharding-cosid/src/test/java/org/apache/shardingsphere/sharding/cosid/algorithm/sharding/interval/fixture/IntervalShardingAlgorithmDataFixture.java
@@ -0,0 +1,141 @@
+/*
+ * 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.sharding.cosid.algorithm.sharding.interval.fixture;
+
+import com.google.common.collect.Range;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.Properties;
+import java.util.function.Function;
+import lombok.AccessLevel;
+import lombok.RequiredArgsConstructor;
+import me.ahoo.cosid.sharding.ExactCollection;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.sharding.cosid.algorithm.Arguments;
+import org.apache.shardingsphere.sharding.cosid.algorithm.CosIdAlgorithmConstants;
+import org.apache.shardingsphere.sharding.cosid.algorithm.sharding.interval.CosIdIntervalShardingAlgorithm;
+import org.apache.shardingsphere.sharding.factory.ShardingAlgorithmFactory;
+
+@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
+public final class IntervalShardingAlgorithmDataFixture {
+
+ public static final ZoneOffset ZONE_OFFSET_SHANGHAI;
+
+ public static final LocalDateTime LOWER_DATE_TIME;
+
+ public static final LocalDateTime UPPER_DATE_TIME;
+
+ public static final String LOGIC_NAME;
+
+ public static final String LOGIC_NAME_PREFIX;
+
+ public static final String COLUMN_NAME;
+
+ public static final String SUFFIX_FORMATTER_STRING;
+
+ public static final ExactCollection<String> ALL_NODES;
+
+ static {
+ ZONE_OFFSET_SHANGHAI = ZoneOffset.of("+8");
+ LOWER_DATE_TIME = LocalDateTime.of(2021, 1, 1, 0, 0);
+ UPPER_DATE_TIME = LOWER_DATE_TIME.plusYears(1);
+ LOGIC_NAME = "table";
+ LOGIC_NAME_PREFIX = LOGIC_NAME + "_";
+ COLUMN_NAME = "create_time";
+ SUFFIX_FORMATTER_STRING = "yyyyMM";
+ ALL_NODES = new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104",
+ "table_202105", "table_202106", "table_202107", "table_202108", "table_202109",
+ "table_202110", "table_202111", "table_202112", "table_202201");
+ }
+
+ /**
+ * Precise parameter provider.
+ *
+ * @param datetimeConvert datetimeConvert
+ * @return unit test parameter collection
+ */
+ public static Iterable<Object[]> preciseArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) {
+ return Arguments.ofArrayElement(
+ Arguments.of(datetimeConvert.apply(LOWER_DATE_TIME), "table_202101"),
+ Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 2, 14, 22, 0)), "table_202102"),
+ Arguments.of(datetimeConvert.apply(LocalDateTime.of(2021, 10, 1, 0, 0)), "table_202110"),
+ Arguments.of(datetimeConvert.apply(UPPER_DATE_TIME), "table_202201"));
+ }
+
+ /**
+ * Range parameter provider.
+ *
+ * @param datetimeConvert datetimeConvert
+ * @return unit test parameter collection
+ */
+ public static Iterable<Object[]> rangeArgsProvider(final Function<LocalDateTime, ? extends Comparable<?>> datetimeConvert) {
+ return Arguments.ofArrayElement(
+ Arguments.of(Range.all(), ALL_NODES),
+ Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
+ Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 1, 1, 0, 0)),
+ datetimeConvert.apply(LocalDateTime.of(2021, 2, 1, 0, 0))),
+ new ExactCollection<>("table_202101", "table_202102")),
+ Arguments.of(Range.closed(datetimeConvert.apply(LOWER_DATE_TIME.minusMonths(1)), datetimeConvert.apply(UPPER_DATE_TIME.plusMonths(1))), ALL_NODES),
+ Arguments.of(Range.closed(datetimeConvert.apply(LocalDateTime.of(2021, 12, 1, 0, 0)),
+ datetimeConvert.apply(LocalDateTime.of(2022, 2, 1, 0, 0))),
+ new ExactCollection<>("table_202112", "table_202201")),
+ Arguments.of(Range.closedOpen(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)),
+ new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105", "table_202106",
+ "table_202107", "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")),
+ Arguments.of(Range.openClosed(datetimeConvert.apply(LOWER_DATE_TIME), datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
+ Arguments.of(Range.greaterThan(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES),
+ Arguments.of(Range.atLeast(datetimeConvert.apply(LOWER_DATE_TIME)), ALL_NODES),
+ Arguments.of(Range.greaterThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")),
+ Arguments.of(Range.atLeast(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202201")),
+ Arguments.of(Range.greaterThan(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))),
+ new ExactCollection<>("table_202112", "table_202201")),
+ Arguments.of(Range.atLeast(datetimeConvert.apply(LocalDateTime.of(2021, 12, 5, 0, 0))),
+ new ExactCollection<>("table_202112", "table_202201")),
+ Arguments.of(Range.lessThan(datetimeConvert.apply(LOWER_DATE_TIME)), ExactCollection.empty()),
+ Arguments.of(Range.atMost(datetimeConvert.apply(LOWER_DATE_TIME)), new ExactCollection<>("table_202101")),
+ Arguments.of(Range.lessThan(datetimeConvert.apply(UPPER_DATE_TIME)), new ExactCollection<>("table_202101",
+ "table_202102", "table_202103", "table_202104", "table_202105", "table_202106", "table_202107",
+ "table_202108", "table_202109", "table_202110", "table_202111", "table_202112")),
+ Arguments.of(Range.atMost(datetimeConvert.apply(UPPER_DATE_TIME)), ALL_NODES),
+ Arguments.of(Range.lessThan(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))),
+ new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")),
+ Arguments.of(Range.atMost(datetimeConvert.apply(LocalDateTime.of(2021, 5, 5, 0, 0))),
+ new ExactCollection<>("table_202101", "table_202102", "table_202103", "table_202104", "table_202105")));
+ }
+
+ /**
+ * Create CosIdIntervalShardingAlgorithm.
+ *
+ * @return CosIdIntervalShardingAlgorithm
+ */
+ public static CosIdIntervalShardingAlgorithm createShardingAlgorithm() {
+ return (CosIdIntervalShardingAlgorithm) ShardingAlgorithmFactory.newInstance(new ShardingSphereAlgorithmConfiguration("COSID_INTERVAL", createProperties()));
+ }
+
+ private static Properties createProperties() {
+ Properties result = new Properties();
+ result.setProperty(CosIdIntervalShardingAlgorithm.ZONE_ID_KEY, "Asia/Shanghai");
+ result.setProperty(CosIdAlgorithmConstants.LOGIC_NAME_PREFIX_KEY, LOGIC_NAME_PREFIX);
+ result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_LOWER_KEY, LOWER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ result.setProperty(CosIdIntervalShardingAlgorithm.DATE_TIME_UPPER_KEY, UPPER_DATE_TIME.format(CosIdIntervalShardingAlgorithm.DEFAULT_DATE_TIME_FORMATTER));
+ result.setProperty(CosIdIntervalShardingAlgorithm.SHARDING_SUFFIX_FORMAT_KEY, SUFFIX_FORMATTER_STRING);
+ result.setProperty(CosIdIntervalShardingAlgorithm.INTERVAL_UNIT_KEY, "MONTHS");
+ result.put(CosIdIntervalShardingAlgorithm.INTERVAL_AMOUNT_KEY, 1);
+ return result;
+ }
+}