完善单元测试。

dev
ZhouXY108 2024-08-20 00:42:38 +08:00
parent 4ac281d65b
commit 91c30412de
4 changed files with 269 additions and 2 deletions

View File

@ -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));
}
}

View File

@ -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());
}
}
}
}

View File

@ -19,6 +19,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.function.Function;
class ValidatableStringRecordTests { class ValidatableStringRecordTests {
@ -39,8 +40,8 @@ class ValidatableStringRecordTests {
Username.of("Code108") Username.of("Code108")
); );
log.info("{}", Collections.max(usernames)); log.info("{}", Collections.max(usernames));
log.info("{}", Collections.max(usernames, Function<Username, String> compare = o -> o.value().toLowerCase();
Comparator.comparing(o -> o.value().toLowerCase()))); log.info("{}", Collections.max(usernames, Comparator.comparing(compare)));
} }
@Test @Test

View File

@ -76,4 +76,13 @@ class DateTimeToolsTests {
log.info(DateTimeTools.toJavaInstant(instant).toString()); log.info(DateTimeTools.toJavaInstant(instant).toString());
log.info(DateTimeTools.toZonedDateTime(instant, org.joda.time.DateTimeZone.forID("America/New_York")).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);
}
} }