Skip to content

Commit

Permalink
Add test cases on TemporalHandlerFactory (#33700)
Browse files Browse the repository at this point in the history
* Add ShardingSphereTemporal

* Add ShardingSphereTemporal

* Add ShardingSphereTemporal

* Add TemporalParser

* Add TemporalParser

* Add TemporalParser

* Add TemporalParser

* Add TemporalParser

* Add TemporalParser

* Add TemporalParser

* Add TemporalHandler

* Add TemporalHandler

* Add TemporalHandler

* Add TemporalHandler

* Add TemporalHandler

* Add TemporalHandler

* Add TemporalHandler

* Add test cases on TemporalHandlerFactory

* Add test cases on TemporalHandlerFactory
  • Loading branch information
terrymanu authored Nov 17, 2024
1 parent 53fc0f7 commit fa8e6d0
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.algorithm.sharding.datetime.temporal;

import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.LocalDateTemporalHandler;
import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.LocalDateTimeTemporalHandler;
import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.LocalTimeTemporalHandler;
import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.MonthTemporalHandler;
import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.YearMonthTemporalHandler;
import org.apache.shardingsphere.sharding.algorithm.sharding.datetime.temporal.type.YearTemporalHandler;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.Year;
import java.time.YearMonth;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;

class TemporalHandlerFactoryTest {

@Test
void assertNewInstanceWithLocalDate() {
assertThat(TemporalHandlerFactory.newInstance(LocalDate.now()), instanceOf(LocalDateTemporalHandler.class));
}

@Test
void assertNewInstanceWithLocalTime() {
assertThat(TemporalHandlerFactory.newInstance(LocalTime.now()), instanceOf(LocalTimeTemporalHandler.class));
}

@Test
void assertNewInstanceWithLocalDateTime() {
assertThat(TemporalHandlerFactory.newInstance(LocalDateTime.now()), instanceOf(LocalDateTimeTemporalHandler.class));
}

@Test
void assertNewInstanceWithYearMonth() {
assertThat(TemporalHandlerFactory.newInstance(YearMonth.now()), instanceOf(YearMonthTemporalHandler.class));
}

@Test
void assertNewInstanceWithYear() {
assertThat(TemporalHandlerFactory.newInstance(Year.now()), instanceOf(YearTemporalHandler.class));
}

@Test
void assertNewInstanceWithMonth() {
assertThat(TemporalHandlerFactory.newInstance(Month.JANUARY), instanceOf(MonthTemporalHandler.class));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class LocalDateTemporalHandlerTest {

private final LocalDateTemporalHandler temporalHandler = new LocalDateTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("2020-01-01", DateTimeFormatter.ISO_LOCAL_DATE), is(LocalDate.of(2020, 1, 1)));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(LocalDate.of(2020, 1, 1)), is(LocalDate.of(2020, 1, 1)));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(LocalDate.of(2020, 1, 1), LocalDate.of(2020, 1, 2), 1));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(LocalDate.of(2020, 1, 1), 1, ChronoUnit.DAYS), is(LocalDate.of(2020, 1, 2)));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class LocalDateTimeTemporalHandlerTest {

private final LocalDateTimeTemporalHandler temporalHandler = new LocalDateTimeTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("2020-01-01T00:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME), is(LocalDateTime.of(2020, 1, 1, 0, 0, 0)));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(LocalDateTime.of(2020, 1, 1, 0, 0, 0)), is(LocalDateTime.of(2020, 1, 1, 0, 0, 0)));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(LocalDateTime.of(2020, 1, 1, 0, 0, 0), LocalDateTime.of(2020, 1, 1, 0, 0, 0), 1));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(LocalDateTime.of(2020, 1, 1, 0, 0, 0), 1, ChronoUnit.DAYS), is(LocalDateTime.of(2020, 1, 2, 0, 0, 0)));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class LocalTimeTemporalHandlerTest {

private final LocalTimeTemporalHandler temporalHandler = new LocalTimeTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("00:00:00", DateTimeFormatter.ISO_LOCAL_TIME), is(LocalTime.of(0, 0, 0)));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(LocalTime.of(0, 0, 0)), is(LocalTime.of(0, 0, 0)));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(LocalTime.of(0, 0, 0), LocalTime.of(0, 0, 0), 1));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(LocalTime.of(0, 0, 0), 1, ChronoUnit.HOURS), is(LocalTime.of(1, 0, 0)));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class MonthTemporalHandlerTest {

private final MonthTemporalHandler temporalHandler = new MonthTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("1", DateTimeFormatter.ofPattern("M")), is(Month.JANUARY));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(Month.of(1)), is(Month.JANUARY));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(Month.JANUARY, Month.FEBRUARY, 1));
assertTrue(temporalHandler.isAfter(Month.FEBRUARY, Month.JANUARY, 1));
assertFalse(temporalHandler.isAfter(Month.OCTOBER, Month.DECEMBER, 1));
assertTrue(temporalHandler.isAfter(Month.OCTOBER, Month.NOVEMBER, 3));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(Month.JANUARY, 1, ChronoUnit.MONTHS), is(Month.FEBRUARY));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class YearMonthTemporalHandlerTest {

private final YearMonthTemporalHandler temporalHandler = new YearMonthTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("2000-01", DateTimeFormatter.ofPattern("yyyy-MM")), is(YearMonth.of(2000, 1)));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(YearMonth.of(2000, 1)), is(YearMonth.of(2000, 1)));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(YearMonth.of(2000, 1), YearMonth.of(2000, 1), 1));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(YearMonth.of(2000, 1), 1, ChronoUnit.MONTHS), is(YearMonth.of(2000, 2)));
}
}
Original file line number Diff line number Diff line change
@@ -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.algorithm.sharding.datetime.temporal.type;

import org.junit.jupiter.api.Test;

import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class YearTemporalHandlerTest {

private final YearTemporalHandler temporalHandler = new YearTemporalHandler();

@Test
void assertParse() {
assertThat(temporalHandler.parse("2000", DateTimeFormatter.ofPattern("yyyy")), is(Year.of(2000)));
}

@Test
void assertConvertTo() {
assertThat(temporalHandler.convertTo(Year.of(2000)), is(Year.of(2000)));
}

@Test
void assertIsAfter() {
assertFalse(temporalHandler.isAfter(Year.of(2000), Year.of(2001), 1));
}

@Test
void assertAdd() {
assertThat(temporalHandler.add(Year.of(2000), 1, ChronoUnit.YEARS), is(Year.of(2001)));
}
}

0 comments on commit fa8e6d0

Please sign in to comment.