diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java new file mode 100644 index 0000000..f9895a4 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/QuarterTests.java @@ -0,0 +1,202 @@ +package xyz.zhouxy.plusone.commons.base; + +import static org.junit.jupiter.api.Assertions.*; + +import java.time.Month; +import java.time.MonthDay; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public +class QuarterTests { + + @Test + void testQ1() { + Quarter quarter = Quarter.of(1); + assertSame(Quarter.Q1, quarter); + assertSame(quarter, Quarter.of("Q1")); + assertEquals(1, quarter.getValue()); + assertEquals("Q1", quarter.getDisplayName()); + + assertThrows(EnumConstantNotPresentException.class, () -> { + Quarter.of(0); + }); + + // ========== + + int startMonthValue = quarter.getStartMonthValue(); + log.info("startMonthValue: {}", startMonthValue); + assertEquals(1, startMonthValue); + + Month startMonth = quarter.getStartMonth(); + log.info("startMonth: {}", startMonth); + assertEquals(Month.JANUARY, startMonth); + + assertEquals(startMonthValue, startMonth.getValue()); + + // ========== + + int lastMonthValue = quarter.getLastMonthValue(); + log.info("lastMonthValue: {}", lastMonthValue); + assertEquals(3, lastMonthValue); + + Month lastMonth = quarter.getLastMonth(); + log.info("lastMonth: {}", lastMonth); + assertEquals(Month.MARCH, lastMonth); + + assertEquals(lastMonthValue, lastMonth.getValue()); + + // ========== + + MonthDay startMonthDay = quarter.getStartMonthDay(); + log.info("startMonthDay: {}", startMonthDay); + assertEquals(startMonthDay, MonthDay.of(1, 1)); + + MonthDay lastMonthDay = quarter.getLastMonthDay(); + log.info("lastMonthDay: {}", lastMonthDay); + assertEquals(lastMonthDay, MonthDay.of(3, 31)); + } + + @Test + void testQ2() { + Quarter quarter = Quarter.of(2); + assertSame(Quarter.Q2, quarter); + assertSame(quarter, Quarter.of("Q2")); + assertEquals(2, quarter.getValue()); + assertEquals("Q2", quarter.getDisplayName()); + + assertThrows(EnumConstantNotPresentException.class, () -> { + Quarter.of(5); + }); + + // ========== + + int startMonthValue = quarter.getStartMonthValue(); + log.info("startMonthValue: {}", startMonthValue); + assertEquals(4, startMonthValue); + + Month startMonth = quarter.getStartMonth(); + log.info("startMonth: {}", startMonth); + assertEquals(Month.APRIL, startMonth); + + assertEquals(startMonthValue, startMonth.getValue()); + + // ========== + + int lastMonthValue = quarter.getLastMonthValue(); + log.info("lastMonthValue: {}", lastMonthValue); + assertEquals(6, lastMonthValue); + + Month lastMonth = quarter.getLastMonth(); + log.info("lastMonth: {}", lastMonth); + assertEquals(Month.JUNE, lastMonth); + + assertEquals(lastMonthValue, lastMonth.getValue()); + + // ========== + + MonthDay startMonthDay = quarter.getStartMonthDay(); + log.info("startMonthDay: {}", startMonthDay); + assertEquals(startMonthDay, MonthDay.of(4, 1)); + + MonthDay lastMonthDay = quarter.getLastMonthDay(); + log.info("lastMonthDay: {}", lastMonthDay); + assertEquals(lastMonthDay, MonthDay.of(6, 30)); + } + + @Test + void testQ3() { + Quarter quarter = Quarter.of(3); + assertSame(Quarter.Q3, quarter); + assertSame(quarter, Quarter.of("Q3")); + assertEquals(3, quarter.getValue()); + assertEquals("Q3", quarter.getDisplayName()); + + assertThrows(EnumConstantNotPresentException.class, () -> { + Quarter.of("Abc"); + }); + + // ========== + + int startMonthValue = quarter.getStartMonthValue(); + log.info("startMonthValue: {}", startMonthValue); + assertEquals(7, startMonthValue); + + Month startMonth = quarter.getStartMonth(); + log.info("startMonth: {}", startMonth); + assertEquals(Month.JULY, startMonth); + + assertEquals(startMonthValue, startMonth.getValue()); + + // ========== + + int lastMonthValue = quarter.getLastMonthValue(); + log.info("lastMonthValue: {}", lastMonthValue); + assertEquals(9, lastMonthValue); + + Month lastMonth = quarter.getLastMonth(); + log.info("lastMonth: {}", lastMonth); + assertEquals(Month.SEPTEMBER, lastMonth); + + assertEquals(lastMonthValue, lastMonth.getValue()); + + // ========== + + MonthDay startMonthDay = quarter.getStartMonthDay(); + log.info("startMonthDay: {}", startMonthDay); + assertEquals(startMonthDay, MonthDay.of(7, 1)); + + MonthDay lastMonthDay = quarter.getLastMonthDay(); + log.info("lastMonthDay: {}", lastMonthDay); + assertEquals(lastMonthDay, MonthDay.of(9, 30)); + } + + @Test + void testQ4() { + Quarter quarter = Quarter.of(4); + assertSame(Quarter.Q4, quarter); + assertSame(quarter, Quarter.of("Q4")); + assertEquals(4, quarter.getValue()); + assertEquals("Q4", quarter.getDisplayName()); + + assertThrows(EnumConstantNotPresentException.class, () -> { + Quarter.of("Q5"); + }); + + // ========== + + int startMonthValue = quarter.getStartMonthValue(); + log.info("startMonthValue: {}", startMonthValue); + assertEquals(10, startMonthValue); + + Month startMonth = quarter.getStartMonth(); + log.info("startMonth: {}", startMonth); + assertEquals(Month.OCTOBER, startMonth); + + assertEquals(startMonthValue, startMonth.getValue()); + + // ========== + + int lastMonthValue = quarter.getLastMonthValue(); + log.info("lastMonthValue: {}", lastMonthValue); + assertEquals(12, lastMonthValue); + Month lastMonth = quarter.getLastMonth(); + log.info("lastMonth: {}", lastMonth); + assertEquals(Month.DECEMBER, lastMonth); + + assertEquals(lastMonthValue, lastMonth.getValue()); + + // ========== + + MonthDay startMonthDay = quarter.getStartMonthDay(); + log.info("startMonthDay: {}", startMonthDay); + assertEquals(startMonthDay, MonthDay.of(10, 1)); + + MonthDay lastMonthDay = quarter.getLastMonthDay(); + log.info("lastMonthDay: {}", lastMonthDay); + assertEquals(lastMonthDay, MonthDay.of(12, 31)); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java new file mode 100644 index 0000000..6f8b441 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/commons/base/YearQuarterTests.java @@ -0,0 +1,55 @@ +package xyz.zhouxy.plusone.commons.base; + +import org.junit.jupiter.api.Test; + +import lombok.extern.slf4j.Slf4j; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +@Slf4j +public class YearQuarterTests { + + @Test + void of_ValidYearAndQuarter_CreatesYearQuarter() { + int year = 2023; + Quarter quarter = Quarter.Q1; + + YearQuarter expected = YearQuarter.of(year, quarter); + YearQuarter actual = YearQuarter.of(LocalDate.of(year, 2, 28)); + + assertEquals(expected, actual); + + assertEquals("Q1 2023", actual.toString()); + } + + @Test + @SuppressWarnings("null") + void of_InvalidQuarter_ThrowsNullPointerException() { + int year = 2023; + Quarter quarter = null; + + assertThrows(NullPointerException.class, () -> YearQuarter.of(year, quarter)); + } + + @Test + void of_ValidYearQuarter_GetsCorrectStartAndEndDate() { + + for (int year = 1990; year <= 2024; year++) { + for (int qrtr = 1; qrtr <= 4; qrtr++) { + Quarter quarter = Quarter.of(qrtr); + YearQuarter yearQuarter = YearQuarter.of(year, quarter); + + LocalDate expectedStartDate = quarter.getStartMonthDay().atYear(year); + log.info("{} - expectedStartDate: {}", yearQuarter, expectedStartDate); + LocalDate expectedEndDate = quarter.getLastMonthDay().atYear(year); + log.info("{} - expectedEndDate: {}", yearQuarter, expectedEndDate); + + assertEquals(expectedStartDate, yearQuarter.getStartDate()); + assertEquals(expectedEndDate, yearQuarter.getLastDate()); + } + } + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java b/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java index 636d16d..885adfb 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/model/ValidatableStringRecordTests.java @@ -19,6 +19,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.function.Function; class ValidatableStringRecordTests { @@ -39,8 +40,8 @@ class ValidatableStringRecordTests { Username.of("Code108") ); log.info("{}", Collections.max(usernames)); - log.info("{}", Collections.max(usernames, - Comparator.comparing(o -> o.value().toLowerCase()))); + Function compare = o -> o.value().toLowerCase(); + log.info("{}", Collections.max(usernames, Comparator.comparing(compare))); } @Test diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java index 79c3908..4ebab7f 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java @@ -76,4 +76,13 @@ class DateTimeToolsTests { log.info(DateTimeTools.toJavaInstant(instant).toString()); log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York")).toString()); } + + @Test + void testToJodaInstant() { + java.time.Instant javaInstant = java.time.Instant.now(); + log.info("javaInstant: {}", javaInstant); + + org.joda.time.Instant jodaInstant = DateTimeTools.toJodaInstant(javaInstant); + log.info("jodaInstant: {}", jodaInstant); + } }