This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 375f33a3908 Refactor unit tests for IntervalShardingAlgorithm (#29967)
375f33a3908 is described below

commit 375f33a390892c384a0fdbe9a65aa8e4a5094f9b
Author: Ling Hengqian <[email protected]>
AuthorDate: Fri Feb 2 23:16:44 2024 +0800

    Refactor unit tests for IntervalShardingAlgorithm (#29967)
---
 .../datetime/IntervalShardingAlgorithmTest.java    | 368 ++++++++-------------
 1 file changed, 139 insertions(+), 229 deletions(-)

diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
index 1c3a53adead..a4169e62b55 100644
--- 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/algorithm/sharding/datetime/IntervalShardingAlgorithmTest.java
@@ -29,7 +29,6 @@ import 
org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
-import java.sql.Date;
 import java.sql.Timestamp;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -42,13 +41,16 @@ import java.time.YearMonth;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.time.format.DateTimeFormatter;
+import java.time.temporal.UnsupportedTemporalTypeException;
 import java.util.Collection;
+import java.util.Date;
 import java.util.LinkedList;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 class IntervalShardingAlgorithmTest {
     
@@ -60,58 +62,22 @@ class IntervalShardingAlgorithmTest {
     
     private final Collection<String> availableTablesForDayDataSources = new 
LinkedList<>();
     
-    private final Collection<String> availableTablesForJDBCDateDataSources = 
new LinkedList<>();
-    
-    private final Collection<String> availableTablesForJDBCTimeDataSources = 
new LinkedList<>();
-    
-    private final Collection<String> availableTablesForYearDataSources = new 
LinkedList<>();
-    
-    private final Collection<String> availableTablesForYearMonthDataSources = 
new LinkedList<>();
-    
-    private final Collection<String> 
availableTablesForMonthInJSR310DataSources = new LinkedList<>();
-    
-    private final Collection<String> 
availableTablesForDayWithMillisecondDataSources = new LinkedList<>();
-    
     private IntervalShardingAlgorithm shardingAlgorithmByQuarter;
     
     private IntervalShardingAlgorithm shardingAlgorithmByMonth;
     
     private IntervalShardingAlgorithm shardingAlgorithmByDay;
     
-    private IntervalShardingAlgorithm shardingAlgorithmByJDBCDate;
-    
-    private IntervalShardingAlgorithm shardingAlgorithmByJDBCTime;
-    
-    private IntervalShardingAlgorithm shardingAlgorithmByDayWithMillisecond;
-    
-    private IntervalShardingAlgorithm shardingAlgorithmByYear;
-    
-    private IntervalShardingAlgorithm shardingAlgorithmByYearMonth;
-    
-    private IntervalShardingAlgorithm shardingAlgorithmByMonthInJSR310;
-    
     @BeforeEach
     void setup() {
         initShardStrategyByMonth();
         initShardStrategyByQuarter();
         initShardingStrategyByDay();
-        initShardStrategyByDayWithMillisecond();
-        initShardingStrategyByJDBCDate();
-        initShardingStrategyByJDBCTime();
-        initShardingStrategyByYear();
-        initShardingStrategyByYearMonth();
-        initShardingStrategyByMonthInJSR310();
     }
     
     private void initShardStrategyByQuarter() {
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM-dd HH:mm:ss"),
-                new Property("datetime-lower", "2016-01-01 00:00:00"),
-                new Property("datetime-upper", "2021-12-31 00:00:00"),
-                new Property("sharding-suffix-pattern", "yyyyQQ"),
-                new Property("datetime-interval-amount", "3"),
-                new Property("datetime-interval-unit", "Months"));
-        shardingAlgorithmByQuarter = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
+        shardingAlgorithmByQuarter = createAlgorithm("yyyy-MM-dd HH:mm:ss", 
"2016-01-01 00:00:00",
+                "2021-12-31 00:00:00", "yyyyQQ", 3, "Months");
         for (int i = 2016; i <= 2020; i++) {
             for (int j = 1; j <= 4; j++) {
                 
availableTablesForQuarterDataSources.add(String.format("t_order_%04d%02d", i, 
j));
@@ -120,14 +86,8 @@ class IntervalShardingAlgorithmTest {
     }
     
     private void initShardStrategyByMonth() {
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM-dd HH:mm:ss"),
-                new Property("datetime-lower", "2016-01-01 00:00:00"),
-                new Property("datetime-upper", "2021-12-31 00:00:00"),
-                new Property("sharding-suffix-pattern", "yyyyMM"),
-                new Property("datetime-interval-amount", "1"),
-                new Property("datetime-interval-unit", "Months"));
-        shardingAlgorithmByMonth = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
+        shardingAlgorithmByMonth = createAlgorithm("yyyy-MM-dd HH:mm:ss", 
"2016-01-01 00:00:00",
+                "2021-12-31 00:00:00", "yyyyMM", 1, "Months");
         for (int i = 2016; i <= 2020; i++) {
             for (int j = 1; j <= 12; j++) {
                 
availableTablesForMonthDataSources.add(String.format("t_order_%04d%02d", i, j));
@@ -137,13 +97,8 @@ class IntervalShardingAlgorithmTest {
     
     private void initShardingStrategyByDay() {
         int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM-dd HH:mm:ss"),
-                new Property("datetime-lower", "2021-06-01 00:00:00"),
-                new Property("datetime-upper", "2021-07-31 00:00:00"),
-                new Property("sharding-suffix-pattern", "yyyyMMdd"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)));
-        shardingAlgorithmByDay = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
+        shardingAlgorithmByDay = createAlgorithm("yyyy-MM-dd HH:mm:ss", 
"2021-06-01 00:00:00",
+                "2021-07-31 00:00:00", "yyyyMMdd", stepAmount, null);
         for (int j = 6; j <= 7; j++) {
             for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
                 
availableTablesForDayDataSources.add(String.format("t_order_%04d%02d%02d", 
2021, j, i));
@@ -151,101 +106,6 @@ class IntervalShardingAlgorithmTest {
         }
     }
     
-    private void initShardStrategyByDayWithMillisecond() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM-dd HH:mm:ss.SSS"),
-                new Property("datetime-lower", "2021-06-01 00:00:00.000"),
-                new Property("datetime-upper", "2021-07-31 00:00:00.000"),
-                new Property("sharding-suffix-pattern", "yyyyMMdd"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)),
-                new Property("datetime-interval-unit", "DAYS"));
-        shardingAlgorithmByDayWithMillisecond = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int j = 6; j <= 7; j++) {
-            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
-                
availableTablesForDayWithMillisecondDataSources.add(String.format("t_order_%04d%02d%02d",
 2021, j, i));
-            }
-        }
-    }
-    
-    private void initShardingStrategyByJDBCDate() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM-dd"),
-                new Property("datetime-lower", "2021-06-01"),
-                new Property("datetime-upper", "2021-07-31"),
-                new Property("sharding-suffix-pattern", "yyyyMMdd"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)));
-        shardingAlgorithmByJDBCDate = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int j = 6; j <= 7; j++) {
-            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
-                
availableTablesForJDBCDateDataSources.add(String.format("t_order_%04d%02d%02d", 
2021, j, i));
-            }
-        }
-    }
-    
-    private void initShardingStrategyByJDBCTime() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "HH:mm:ss.SSS"),
-                new Property("datetime-lower", "02:00:00.000"),
-                new Property("datetime-upper", "13:00:00.000"),
-                new Property("sharding-suffix-pattern", "HHmm"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)),
-                new Property("datetime-interval-unit", "Hours"));
-        shardingAlgorithmByJDBCTime = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int i = 2; i < 13; i++) {
-            
availableTablesForJDBCTimeDataSources.add(String.format("t_order_%02d%02d", i, 
0));
-        }
-    }
-    
-    private void initShardingStrategyByYear() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy"),
-                new Property("datetime-lower", "2000"),
-                new Property("datetime-upper", "2022"),
-                new Property("sharding-suffix-pattern", "yyyy"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)),
-                new Property("datetime-interval-unit", "Years"));
-        shardingAlgorithmByYear = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int i = 2000; i < 2023; i++) {
-            
availableTablesForYearDataSources.add(String.format("t_order_%04d", i));
-        }
-    }
-    
-    private void initShardingStrategyByYearMonth() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "yyyy-MM"),
-                new Property("datetime-lower", "2016-01"),
-                new Property("datetime-upper", "2021-12"),
-                new Property("sharding-suffix-pattern", "yyyyMM"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)),
-                new Property("datetime-interval-unit", "Years"));
-        shardingAlgorithmByYearMonth = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int i = 2016; i <= 2021; i++) {
-            for (int j = 1; j <= 12; j++) {
-                
availableTablesForYearMonthDataSources.add(String.format("t_order_%04d%02d", i, 
j));
-            }
-        }
-    }
-    
-    private void initShardingStrategyByMonthInJSR310() {
-        int stepAmount = 2;
-        Properties props = PropertiesBuilder.build(
-                new Property("datetime-pattern", "MM"),
-                new Property("datetime-lower", "02"),
-                new Property("datetime-upper", "12"),
-                new Property("sharding-suffix-pattern", "MM"),
-                new Property("datetime-interval-amount", 
Integer.toString(stepAmount)),
-                new Property("datetime-interval-unit", "Months"));
-        shardingAlgorithmByMonthInJSR310 = (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
-        for (int i = 2; i < 13; i++) {
-            
availableTablesForMonthInJSR310DataSources.add(String.format("t_order_%02d", 
i));
-        }
-    }
-    
     @Test
     void assertPreciseDoShardingByQuarter() {
         
assertThat(shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,
@@ -257,7 +117,7 @@ class IntervalShardingAlgorithmTest {
     @Test
     void assertRangeDoShardingByQuarter() {
         Collection<String> actual = 
shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08")));
+                createShardingValue("2019-10-15 10:59:08", "2020-04-08 
10:59:08"));
         assertThat(actual.size(), is(3));
     }
     
@@ -269,13 +129,6 @@ class IntervalShardingAlgorithmTest {
                 new PreciseShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, "2030-01-01 00:00:01")));
     }
     
-    @Test
-    void assertRangeDoShardingByMonth() {
-        Collection<String> actual = 
shardingAlgorithmByMonth.doSharding(availableTablesForMonthDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed("2019-10-15 10:59:08", "2020-04-08 10:59:08")));
-        assertThat(actual.size(), is(7));
-    }
-    
     @Test
     void assertLowerHalfRangeDoSharding() {
         Collection<String> actual = 
shardingAlgorithmByQuarter.doSharding(availableTablesForQuarterDataSources,
@@ -312,13 +165,6 @@ class IntervalShardingAlgorithmTest {
                 new PreciseShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, "2021-07-02 00:00:01")), is("t_order_20210701"));
     }
     
-    @Test
-    void assertRangeDoShardingByDay() {
-        Collection<String> actual = 
shardingAlgorithmByDay.doSharding(availableTablesForDayDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed("2021-06-15 00:00:00", "2021-07-31 01:00:00")));
-        assertThat(actual.size(), is(24));
-    }
-    
     @Test
     void assertFormat() {
         String inputFormat = "yyyy-MM-dd HH:mm:ss.SSS";
@@ -333,86 +179,150 @@ class IntervalShardingAlgorithmTest {
     }
     
     @Test
-    void assertTimestampInJDBCTypeWithZeroMillisecond() {
-        Collection<String> actualAsLocalDateTime = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(LocalDateTime.of(2021, 6, 15, 2, 25, 27), 
LocalDateTime.of(2021, 7, 31, 2, 25, 27))));
-        assertThat(actualAsLocalDateTime.size(), is(24));
-        Collection<String> actualAsInstant = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(
-                                LocalDateTime.of(2021, 6, 15, 2, 25, 
27).atZone(ZoneId.systemDefault()).toInstant(),
-                                LocalDateTime.of(2021, 7, 31, 2, 25, 
27).atZone(ZoneId.systemDefault()).toInstant())));
-        assertThat(actualAsInstant.size(), is(24));
-        Collection<String> actualAsTimestamp = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(
-                                Timestamp.valueOf(LocalDateTime.of(2021, 6, 
15, 2, 25, 27)),
-                                Timestamp.valueOf(LocalDateTime.of(2021, 7, 
31, 2, 25, 27)))));
-        assertThat(actualAsTimestamp.size(), is(24));
-        Collection<String> actualAsOffsetDateTime = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(
-                                OffsetDateTime.of(2021, 6, 15, 2, 25, 27, 0, 
OffsetDateTime.now().getOffset()),
-                                OffsetDateTime.of(2021, 7, 31, 2, 25, 27, 0, 
OffsetDateTime.now().getOffset()))));
-        assertThat(actualAsOffsetDateTime.size(), is(24));
-        Collection<String> actualAsZonedDateTime = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(
-                                ZonedDateTime.of(2021, 6, 15, 2, 25, 27, 0, 
ZoneId.systemDefault()),
-                                ZonedDateTime.of(2021, 7, 31, 2, 25, 27, 0, 
ZoneId.systemDefault()))));
-        assertThat(actualAsZonedDateTime.size(), is(24));
-        DateTimeFormatter dateTimeFormatter = 
DateTimeFormatterFactory.getLongMillsFormatter();
-        Collection<String> actualAsDate = 
shardingAlgorithmByDayWithMillisecond.doSharding(availableTablesForDayWithMillisecondDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        
Range.closed(Date.from(LocalDate.from(dateTimeFormatter.parse("2021-06-15 
02:25:27.000")).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()),
-                                
Date.from(LocalDate.from(dateTimeFormatter.parse("2021-07-31 
02:25:27.000")).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()))));
-        assertThat(actualAsDate.size(), is(24));
+    void assertRangeDoShardingByDays() {
+        final int expectSize = 24;
+        final int stepAmount = 2;
+        IntervalShardingAlgorithm algorithm = createAlgorithm("yyyy-MM-dd 
HH:mm:ss.SSS", "2021-06-01 00:00:00.000",
+                "2021-07-31 00:00:00.000", "yyyyMMdd", stepAmount, "DAYS");
+        Collection<String> availableTargetNames = new LinkedList<>();
+        for (int j = 6; j <= 7; j++) {
+            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
+                availableTargetNames.add(String.format("t_order_%04d%02d%02d", 
2021, j, i));
+            }
+        }
+        final LocalDateTime lower = LocalDateTime.of(2021, 6, 15, 2, 25, 27, 
0);
+        final LocalDateTime upper = LocalDateTime.of(2021, 7, 31, 2, 25, 27, 
0);
+        final RangeShardingValue<Comparable<?>> shardingValueAsLocalDateTime = 
createShardingValue(lower, upper);
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsLocalDateTime).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsInstant = 
createShardingValue(
+                lower.atZone(ZoneId.systemDefault()).toInstant(),
+                upper.atZone(ZoneId.systemDefault()).toInstant());
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsInstant).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsTimestamp = 
createShardingValue(Timestamp.valueOf(lower), Timestamp.valueOf(upper));
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsTimestamp).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsOffsetDateTime 
= createShardingValue(
+                OffsetDateTime.of(lower, OffsetDateTime.now().getOffset()),
+                OffsetDateTime.of(upper, OffsetDateTime.now().getOffset()));
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsOffsetDateTime).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsZonedDateTime = 
createShardingValue(
+                ZonedDateTime.of(lower, ZoneId.systemDefault()),
+                ZonedDateTime.of(upper, ZoneId.systemDefault()));
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsZonedDateTime).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsUtilDate = 
createShardingValue(
+                Date.from(lower.atZone(ZoneId.systemDefault()).toInstant()),
+                Date.from(upper.atZone(ZoneId.systemDefault()).toInstant()));
+        assertThat(algorithm.doSharding(availableTargetNames, 
shardingValueAsUtilDate).size(), is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsSqlDate = 
createShardingValue(
+                new 
java.sql.Date(lower.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()),
+                new 
java.sql.Date(upper.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()));
+        assertThrows(UnsupportedTemporalTypeException.class, () -> 
algorithm.doSharding(availableTargetNames, shardingValueAsSqlDate),
+                "SQL Date values do not have a time component.");
+        assertThat(createAlgorithm("yyyy-MM-dd", "2021-06-01",
+                "2021-07-31", "yyyyMMdd", 2, null)
+                        .doSharding(availableTargetNames, 
shardingValueAsSqlDate).size(),
+                is(expectSize));
+        final RangeShardingValue<Comparable<?>> shardingValueAsString = 
createShardingValue(
+                DateTimeFormatterFactory.getStandardFormatter().format(lower),
+                DateTimeFormatterFactory.getStandardFormatter().format(upper));
+        assertThrows(StringIndexOutOfBoundsException.class, () -> 
algorithm.doSharding(availableTargetNames, shardingValueAsString),
+                "The current requirement for strings is to be consistent with 
`datetime-pattern`.");
+        
assertThat(shardingAlgorithmByDay.doSharding(availableTablesForDayDataSources, 
shardingValueAsString).size(), is(expectSize));
     }
     
     @Test
-    void assertDateInJDBCType() {
-        Collection<String> actualAsLocalDate = 
shardingAlgorithmByJDBCDate.doSharding(availableTablesForJDBCDateDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(LocalDate.of(2021, 6, 15), 
LocalDate.of(2021, 7, 31))));
+    void assertRangeDoShardingByDaysInLocalDate() {
+        int stepAmount = 2;
+        Collection<String> availableTargetNames = new LinkedList<>();
+        for (int j = 6; j <= 7; j++) {
+            for (int i = 1; j == 6 ? i <= 30 : i <= 31; i = i + stepAmount) {
+                availableTargetNames.add(String.format("t_order_%04d%02d%02d", 
2021, j, i));
+            }
+        }
+        Collection<String> actualAsLocalDate = createAlgorithm("yyyy-MM-dd", 
"2021-06-01",
+                "2021-07-31", "yyyyMMdd", stepAmount, null)
+                        .doSharding(availableTargetNames,
+                                createShardingValue(LocalDate.of(2021, 6, 15), 
LocalDate.of(2021, 7, 31)));
         assertThat(actualAsLocalDate.size(), is(24));
     }
     
     @Test
-    void assertTimeInJDBCType() {
-        Collection<String> actualAsLocalTime = 
shardingAlgorithmByJDBCTime.doSharding(availableTablesForJDBCTimeDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(LocalTime.of(2, 25, 27), LocalTime.of(12, 
25, 27))));
+    void assertRangeDoShardingByHours() {
+        int stepAmount = 2;
+        IntervalShardingAlgorithm algorithm = createAlgorithm("HH:mm:ss.SSS", 
"02:00:00.000",
+                "13:00:00.000", "HHmm", stepAmount, "Hours");
+        Collection<String> availableTablesForJDBCTimeDataSources = new 
LinkedList<>();
+        for (int i = 2; i < 13; i++) {
+            
availableTablesForJDBCTimeDataSources.add(String.format("t_order_%02d%02d", i, 
0));
+        }
+        Collection<String> actualAsLocalTime = 
algorithm.doSharding(availableTablesForJDBCTimeDataSources,
+                createShardingValue(LocalTime.of(2, 25, 27), LocalTime.of(12, 
25, 27)));
         assertThat(actualAsLocalTime.size(), is(6));
-        Collection<String> actualAsOffsetTime = 
shardingAlgorithmByJDBCTime.doSharding(availableTablesForJDBCTimeDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(OffsetTime.of(2, 25, 27, 0, 
OffsetDateTime.now().getOffset()),
-                                OffsetTime.of(12, 25, 27, 0, 
OffsetDateTime.now().getOffset()))));
+        Collection<String> actualAsOffsetTime = 
algorithm.doSharding(availableTablesForJDBCTimeDataSources,
+                createShardingValue(OffsetTime.of(2, 25, 27, 0, 
OffsetDateTime.now().getOffset()),
+                        OffsetTime.of(12, 25, 27, 0, 
OffsetDateTime.now().getOffset())));
         assertThat(actualAsOffsetTime.size(), is(6));
     }
     
     @Test
-    void assertIntegerInJDBCType() {
-        Collection<String> actualAsYear = 
shardingAlgorithmByYear.doSharding(availableTablesForYearDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed(Year.of(2001), Year.of(2013))));
-        assertThat(actualAsYear.size(), is(7));
-        Collection<String> actualAsYearMonth = 
shardingAlgorithmByYearMonth.doSharding(availableTablesForYearMonthDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed(YearMonth.of(2016, 1), YearMonth.of(2020, 1))));
+    void assertRangeDoShardingByYears() {
+        Collection<String> availableTargetNames = new LinkedList<>();
+        for (int i = 2000; i < 2023; i++) {
+            availableTargetNames.add(String.format("t_order_%04d", i));
+        }
+        Collection<String> actual = createAlgorithm("yyyy", "2000",
+                "2022", "yyyy", 2, "Years")
+                        .doSharding(availableTargetNames, 
createShardingValue(Year.of(2001), Year.of(2013)));
+        assertThat(actual.size(), is(7));
+    }
+    
+    @Test
+    void assertRangeDoShardingByYearsInYearMonth() {
+        Collection<String> availableTargetNames = new LinkedList<>();
+        for (int i = 2016; i <= 2021; i++) {
+            for (int j = 1; j <= 12; j++) {
+                availableTargetNames.add(String.format("t_order_%04d%02d", i, 
j));
+            }
+        }
+        Collection<String> actualAsYearMonth = createAlgorithm("yyyy-MM", 
"2016-01",
+                "2021-12", "yyyyMM", 2, "Years")
+                        .doSharding(availableTargetNames,
+                                createShardingValue(YearMonth.of(2016, 1), 
YearMonth.of(2020, 1)));
         assertThat(actualAsYearMonth.size(), is(3));
-        Collection<String> actualAsMonth = 
shardingAlgorithmByMonthInJSR310.doSharding(availableTablesForMonthInJSR310DataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed(Month.of(4), Month.of(10))));
-        assertThat(actualAsMonth.size(), is(4));
-        Collection<String> actualAsMonthString = 
shardingAlgorithmByMonthInJSR310.doSharding(availableTablesForMonthInJSR310DataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed("04", "10")));
-        assertThat(actualAsMonthString.size(), is(4));
     }
     
     @Test
-    void assertDateInSqlDate() {
-        Collection<String> actualAsLocalDate = 
shardingAlgorithmByJDBCDate.doSharding(availableTablesForJDBCDateDataSources,
-                new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO,
-                        Range.closed(new Date(LocalDate.of(2021, 6, 
15).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()),
-                                new Date(LocalDate.of(2021, 7, 
31).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()))));
-        assertThat(actualAsLocalDate.size(), is(24));
+    void assertRangeDoShardingByMonths() {
+        IntervalShardingAlgorithm algorithm = createAlgorithm("MM", "02",
+                "12", "MM", 2, "Months");
+        Collection<String> availableTargetNames = new LinkedList<>();
+        for (int i = 2; i < 13; i++) {
+            availableTargetNames.add(String.format("t_order_%02d", i));
+        }
+        Collection<String> actual = algorithm.doSharding(availableTargetNames, 
createShardingValue(Month.of(4), Month.of(10)));
+        assertThat(actual.size(), is(4));
+        Collection<String> actualAsMonthString = 
algorithm.doSharding(availableTargetNames, createShardingValue("04", "10"));
+        assertThat(actualAsMonthString.size(), is(4));
+        Collection<String> actualAsString = 
shardingAlgorithmByMonth.doSharding(availableTablesForMonthDataSources,
+                createShardingValue("2019-10-15 10:59:08", "2020-04-08 
10:59:08"));
+        assertThat(actualAsString.size(), is(7));
+    }
+    
+    private IntervalShardingAlgorithm createAlgorithm(final String 
datetimePattern, final String datetimeLower,
+                                                      final String 
datetimeUpper, final String shardingSuffixPattern,
+                                                      final Integer 
datetimeIntervalAmount, final String datetimeIntervalUnit) {
+        Properties props = PropertiesBuilder.build(
+                new Property("datetime-pattern", datetimePattern),
+                new Property("datetime-lower", datetimeLower),
+                new Property("datetime-upper", datetimeUpper),
+                new Property("sharding-suffix-pattern", shardingSuffixPattern),
+                new Property("datetime-interval-amount", 
Integer.toString(datetimeIntervalAmount)));
+        if (null != datetimeIntervalUnit) {
+            props.put("datetime-interval-unit", datetimeIntervalUnit);
+        }
+        return (IntervalShardingAlgorithm) 
TypedSPILoader.getService(ShardingAlgorithm.class, "INTERVAL", props);
+    }
+    
+    private RangeShardingValue<Comparable<?>> createShardingValue(final 
Comparable<?> lower, final Comparable<?> upper) {
+        return new RangeShardingValue<>("t_order", "create_time", 
DATA_NODE_INFO, Range.closed(lower, upper));
     }
 }

Reply via email to