完成 Quarter 和 YearQuarter 的单元测试

unit-tests
ZhouXY108 2024-12-27 14:52:00 +08:00
parent 6bc32ce379
commit 491499d265
5 changed files with 1110 additions and 70 deletions

View File

@ -1,6 +1,6 @@
[ ] 未开始测试 - 15 (21.43%)
[-] 测试未完成 - 11 (15.71%)
[Y] 测试完成 - 23 (32.86%)
[-] 测试未完成 - 10 (14.29%)
[Y] 测试完成 - 24 (34.29%)
[x] 无需测试 - 21 (30.00%)
xyz.zhouxy.plusone.commons
@ -80,7 +80,7 @@ xyz.zhouxy.plusone.commons
├───time
│ Quarter.java [Y]
│ YearQuarter.java [-]
│ YearQuarter.java [Y]
└───util
ArrayTools.java [-]

View File

@ -16,6 +16,7 @@
package xyz.zhouxy.plusone.commons.time;
import java.time.DateTimeException;
import java.time.Month;
import java.time.MonthDay;
import java.time.temporal.ChronoField;
@ -171,7 +172,8 @@ public enum Quarter implements IWithIntCode {
// Getters end
public static int checkValidIntValue(int value) {
AssertTools.checkArgument(value >= 1 && value <= 4, () -> "Invalid value for Quarter: " + value);
AssertTools.checkCondition(value >= 1 && value <= 4,
() -> new DateTimeException("Invalid value for Quarter: " + value));
return value;
}

View File

@ -22,6 +22,7 @@ import java.io.Serializable;
import java.time.LocalDate;
import java.time.Month;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
@ -52,7 +53,6 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
private final LocalDate lastDate;
private YearQuarter(int year, @Nonnull Quarter quarter) {
AssertTools.checkNotNull(quarter, "Quarter can not be null.");
this.year = year;
this.quarter = quarter;
this.firstDate = quarter.firstMonthDay().atYear(year);
@ -70,7 +70,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(int year, int quarter) {
return of(year, Quarter.of(quarter));
return new YearQuarter(YEAR.checkValidIntValue(year), Quarter.of(quarter));
}
/**
@ -81,8 +81,8 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
* @return {@link YearQuarter}
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(int year, @Nonnull Quarter quarter) {
return new YearQuarter(year, quarter);
public static YearQuarter of(int year, Quarter quarter) {
return new YearQuarter(YEAR.checkValidIntValue(year), Objects.requireNonNull(quarter));
}
/**
@ -92,8 +92,9 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
* @return {@link YearQuarter}
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(@Nonnull LocalDate date) {
return of(date.getYear(), Quarter.fromMonth(date.getMonth()));
public static YearQuarter of(LocalDate date) {
AssertTools.checkNotNull(date);
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
}
/**
@ -103,12 +104,13 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
* @return {@link YearQuarter}
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(@Nonnull Date date) {
public static YearQuarter of(Date date) {
AssertTools.checkNotNull(date);
@SuppressWarnings("deprecation")
final int year = date.getYear() + 1900;
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
@SuppressWarnings("deprecation")
final int month = date.getMonth() + 1;
return of(year, Quarter.fromMonth(month));
final int monthValue = date.getMonth() + 1;
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
}
/**
@ -119,7 +121,10 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(Calendar date) {
return of(date.get(Calendar.YEAR), Quarter.fromMonth(date.get(Calendar.MONTH) + 1));
AssertTools.checkNotNull(date);
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
final int monthValue = date.get(Calendar.MONTH) + 1;
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
}
/**
@ -130,9 +135,15 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(YearMonth yearMonth) {
AssertTools.checkNotNull(yearMonth);
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
}
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter now() {
return of(LocalDate.now());
}
// #endregion
// #region - Getters
@ -185,7 +196,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
// #region - computes
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
public YearQuarter plusQuarters(long quartersToAdd) {
if (quartersToAdd == 0L) {
return this;
}
@ -193,27 +204,27 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
long calcQuarters = quarterCount + quartersToAdd; // safe overflow
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcQuarters, 4));
int newQuarter = (int) Math.floorMod(calcQuarters, 4) + 1;
return of(newYear, Quarter.of(newQuarter));
return new YearQuarter(newYear, Quarter.of(newQuarter));
}
public YearQuarter minusQuarters(long quartersToAdd) { // TODO 单元测试
public YearQuarter minusQuarters(long quartersToAdd) {
return plusQuarters(-quartersToAdd);
}
public YearQuarter nextQuarter() { // TODO 单元测试
public YearQuarter nextQuarter() {
return plusQuarters(1L);
}
public YearQuarter lastQuarter() { // TODO 单元测试
public YearQuarter lastQuarter() {
return minusQuarters(1L);
}
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
public YearQuarter plusYears(long yearsToAdd) {
if (yearsToAdd == 0L) {
return this;
}
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
return of(newYear, this.quarter);
return new YearQuarter(newYear, this.quarter);
}
public YearQuarter minusYears(long yearsToAdd) {
@ -270,11 +281,11 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
return this.compareTo(other) > 0;
}
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
return yearQuarter1.compareTo(yearQuarter2) <= 0 ? yearQuarter1 : yearQuarter2;
}
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) {
return yearQuarter1.compareTo(yearQuarter2) >= 0 ? yearQuarter1 : yearQuarter2;
}

View File

@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.time;
import static org.junit.jupiter.api.Assertions.*;
import java.time.DateTimeException;
import java.time.Month;
import java.time.MonthDay;
@ -37,7 +38,7 @@ class QuarterTests {
assertEquals(1, quarter.getValue());
assertEquals("Q1", quarter.name());
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(DateTimeException.class, () -> {
Quarter.of(0);
});
@ -84,7 +85,7 @@ class QuarterTests {
assertEquals(2, quarter.getValue());
assertEquals("Q2", quarter.name());
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(DateTimeException.class, () -> {
Quarter.of(5);
});