Skip to content

Commit e8faf11

Browse files
committed
feat: Refactor date to LocalDateTime conversion tests to use parameterized tests
- Remove multiple individual @test methods for SQL date/time/timestamp conversions - Replace with 2 parameterized tests using @ParameterizedTest and @MethodSource - Introduce test data providers: - dateConversionProvider() for basic conversions (SQL Date/Time/Timestamp) - dateWithTimeZoneProvider() for timezone-aware conversions - Maintain full test coverage for: - Epoch date (1970-01-01) - Max time values (23:59:59) - Nano precision timestamps - Default/system time zone handling - Specific time zones (America/New_York, Asia/Shanghai, Pacific/Kiritimati) - Daylight Saving Time transitions - Improve test maintainability by reducing code duplication - Add null argument validation test for time zone parameter Signed-off-by: finger <finger.xie@foxmail.com>
1 parent 0d479ab commit e8faf11

1 file changed

Lines changed: 93 additions & 146 deletions

File tree

src/test/java/org/apache/commons/lang3/time/DateUtilsTest.java

Lines changed: 93 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.text.SimpleDateFormat;
3131
import java.time.Instant;
3232
import java.time.LocalDateTime;
33-
import java.time.ZoneId;
3433
import java.time.ZoneOffset;
3534
import java.util.Calendar;
3635
import java.util.Date;
@@ -39,12 +38,16 @@
3938
import java.util.Locale;
4039
import java.util.NoSuchElementException;
4140
import java.util.TimeZone;
41+
import java.util.stream.Stream;
4242

4343
import org.apache.commons.lang3.AbstractLangTest;
4444
import org.junit.jupiter.api.AfterEach;
4545
import org.junit.jupiter.api.BeforeAll;
4646
import org.junit.jupiter.api.BeforeEach;
4747
import org.junit.jupiter.api.Test;
48+
import org.junit.jupiter.params.ParameterizedTest;
49+
import org.junit.jupiter.params.provider.Arguments;
50+
import org.junit.jupiter.params.provider.MethodSource;
4851
import org.junitpioneer.jupiter.DefaultLocale;
4952
import org.junitpioneer.jupiter.ReadsDefaultLocale;
5053
import org.junitpioneer.jupiter.WritesDefaultLocale;
@@ -1289,169 +1292,113 @@ public void testToCalendarWithTimeZoneNull() {
12891292
assertThrows(NullPointerException.class, () -> DateUtils.toCalendar(date1, null));
12901293
}
12911294

1292-
@Test
1293-
void testToLocalDateTimeWithSqlDate() {
1294-
final java.sql.Date sqlDate = java.sql.Date.valueOf("2000-01-01");
1295-
1296-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlDate);
1297-
1298-
assertNotNull(result);
1299-
1300-
final LocalDateTime expected = LocalDateTime.of(2000, 1, 1, 0, 0, 0);
1301-
assertEquals(expected, result);
1302-
1303-
final Instant instant = Instant.ofEpochMilli(sqlDate.getTime());
1304-
final LocalDateTime expectedWithZone = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
1305-
assertEquals(expectedWithZone, result);
1306-
}
1307-
1308-
@Test
1309-
public void testToLocalDateTimeWithSqlTime() {
1310-
final java.sql.Time sqlTime = java.sql.Time.valueOf("12:30:45");
1311-
1312-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTime);
1313-
1314-
assertNotNull(result);
1315-
1316-
final LocalDateTime expected = LocalDateTime.of(1970, 1, 1, 12, 30, 45);
1317-
assertEquals(expected, result);
1318-
1319-
final Instant instant = Instant.ofEpochMilli(sqlTime.getTime());
1320-
final LocalDateTime expectedWithZone = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
1321-
assertEquals(expectedWithZone, result);
1322-
}
1323-
1324-
@Test
1325-
public void testToLocalDateTimeWithSqlTimestamp() {
1326-
final java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("2000-01-01 12:30:45.123456789");
1327-
1328-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTimestamp);
1329-
1295+
@ParameterizedTest
1296+
@MethodSource("dateConversionProvider")
1297+
void testToLocalDateTimeWithDate(Date sqlDate, LocalDateTime expected) {
1298+
LocalDateTime result = DateUtils.toLocalDateTime(sqlDate);
13301299
assertNotNull(result);
1331-
1332-
final LocalDateTime expected = LocalDateTime.of(
1333-
2000, 1, 1, 12, 30, 45, 123_456_789
1334-
);
13351300
assertEquals(expected, result);
1336-
1337-
final Instant instant = sqlTimestamp.toInstant();
1338-
final LocalDateTime expectedWithZone = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
1339-
assertEquals(expectedWithZone, result);
13401301
}
13411302

1342-
@Test
1343-
public void testToLocalDateTimeWithSqlTimestamp_NanoPrecision() {
1344-
final java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("2000-01-01 12:30:45.987654321");
1345-
1346-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTimestamp);
1347-
1348-
final LocalDateTime expected = LocalDateTime.of(
1349-
2000, 1, 1, 12, 30, 45, 987_654_321
1303+
private static Stream<Arguments> dateConversionProvider() {
1304+
return Stream.of(
1305+
Arguments.of(
1306+
java.sql.Date.valueOf("2000-01-01"),
1307+
LocalDateTime.of(2000, 1, 1, 0, 0, 0)
1308+
),
1309+
Arguments.of(
1310+
java.sql.Date.valueOf("1970-01-01"),
1311+
LocalDateTime.of(1970, 1, 1, 0, 0, 0)
1312+
),
1313+
Arguments.of(
1314+
java.sql.Time.valueOf("12:30:45"),
1315+
LocalDateTime.of(1970, 1, 1, 12, 30, 45)
1316+
),
1317+
Arguments.of(
1318+
java.sql.Time.valueOf("23:59:59"),
1319+
LocalDateTime.of(1970, 1, 1, 23, 59, 59)
1320+
),
1321+
Arguments.of(
1322+
java.sql.Timestamp.valueOf("2000-01-01 12:30:45.123456789"),
1323+
LocalDateTime.of(2000, 1, 1, 12, 30, 45, 123_456_789)
1324+
),
1325+
Arguments.of(
1326+
java.sql.Timestamp.valueOf("2000-01-01 12:30:45.987654321"),
1327+
LocalDateTime.of(2000, 1, 1, 12, 30, 45, 987_654_321)
1328+
)
13501329
);
1351-
assertEquals(expected, result);
1352-
}
1353-
1354-
@Test
1355-
public void testToLocalDateTimeWithSqlTimestamp_WithTimeZone() {
1356-
final java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("2000-01-01 12:30:45");
1357-
final TimeZone timeZone = TimeZone.getTimeZone("America/New_York");
1358-
1359-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTimestamp, timeZone);
1360-
1361-
final Instant instant = sqlTimestamp.toInstant();
1362-
final LocalDateTime expected = LocalDateTime.ofInstant(instant, timeZone.toZoneId());
1363-
assertEquals(expected, result);
13641330
}
13651331

1366-
@Test
1367-
public void testToLocalDateTimeWithSqlDate_Epoch() {
1368-
final java.sql.Date sqlDate = java.sql.Date.valueOf("1970-01-01");
1369-
1370-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlDate);
1371-
1372-
final LocalDateTime expected = LocalDateTime.of(1970, 1, 1, 0, 0, 0);
1373-
assertEquals(expected, result);
1374-
}
1375-
1376-
@Test
1377-
public void testToLocalDateTimeWithSqlTime_MaxValue() {
1378-
final java.sql.Time sqlTime = java.sql.Time.valueOf("23:59:59");
1379-
1380-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTime);
1381-
1382-
final LocalDateTime expected = LocalDateTime.of(1970, 1, 1, 23, 59, 59);
1383-
assertEquals(expected, result);
1384-
}
1385-
1386-
@Test
1387-
public void testToLocalDateTimeWithSqlTimestamp_DaylightSaving() {
1388-
final java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf("2023-03-12 02:30:00");
1389-
final TimeZone newYork = TimeZone.getTimeZone("America/New_York");
1390-
1391-
final LocalDateTime result = DateUtils.toLocalDateTime(sqlTimestamp, newYork);
1392-
1393-
final Instant instant = sqlTimestamp.toInstant();
1394-
final LocalDateTime expected = LocalDateTime.ofInstant(instant, newYork.toZoneId());
1332+
@ParameterizedTest
1333+
@MethodSource("dateWithTimeZoneProvider")
1334+
void testToLocalDateTimeWithDate(
1335+
Date date,
1336+
TimeZone timeZone,
1337+
LocalDateTime expected) {
1338+
LocalDateTime result ;
1339+
if (timeZone != null) {
1340+
result = DateUtils.toLocalDateTime(date, timeZone);
1341+
} else {
1342+
result = DateUtils.toLocalDateTime(date);
1343+
}
13951344
assertEquals(expected, result);
13961345
}
13971346

1398-
@Test
1399-
void shouldConvertDateToLocalDateTimeUsingDefaultTimeZone() {
1400-
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
1401-
1402-
final Instant instant = LocalDateTime.of(2023, 1, 1, 0, 0)
1403-
.atOffset(ZoneOffset.UTC)
1404-
.toInstant();
1405-
final Date date = Date.from(instant);
1406-
1407-
final LocalDateTime expected = LocalDateTime.of(2023, 1, 1, 8, 0);
1408-
1409-
assertEquals(expected, DateUtils.toLocalDateTime(date));
1347+
private static Stream<Arguments> dateWithTimeZoneProvider() {
1348+
return Stream.of(
1349+
Arguments.of(
1350+
java.sql.Timestamp.valueOf("2000-01-01 12:30:45"),
1351+
TimeZone.getTimeZone("America/New_York"),
1352+
LocalDateTime.ofInstant(
1353+
java.sql.Timestamp.valueOf("2000-01-01 12:30:45").toInstant(),
1354+
TimeZone.getTimeZone("America/New_York").toZoneId()
1355+
)
1356+
),
1357+
Arguments.of(
1358+
java.sql.Timestamp.valueOf("2023-03-12 02:30:00"),
1359+
TimeZone.getTimeZone("America/New_York"),
1360+
LocalDateTime.ofInstant(
1361+
java.sql.Timestamp.valueOf("2023-03-12 02:30:00").toInstant(),
1362+
TimeZone.getTimeZone("America/New_York").toZoneId()
1363+
)
1364+
), Arguments.of(
1365+
Date.from(LocalDateTime.of(2023, 1, 1, 0, 0)
1366+
.atOffset(ZoneOffset.UTC)
1367+
.toInstant()),
1368+
null,
1369+
LocalDateTime.of(2023, 1, 1, 8, 0)
1370+
),
1371+
Arguments.of(
1372+
Date.from(LocalDateTime.of(2023, 1, 1, 0, 0)
1373+
.atOffset(ZoneOffset.UTC)
1374+
.toInstant()),
1375+
TimeZone.getTimeZone("America/New_York"),
1376+
LocalDateTime.of(2022, 12, 31, 19, 0)
1377+
),
1378+
Arguments.of(
1379+
Date.from(LocalDateTime.of(2023, 3, 12, 7, 0)
1380+
.atOffset(ZoneOffset.UTC)
1381+
.toInstant()),
1382+
TimeZone.getTimeZone("America/New_York"),
1383+
LocalDateTime.of(2023, 3, 12, 3, 0)
1384+
),
1385+
Arguments.of(
1386+
Date.from(LocalDateTime.of(2023, 1, 1, 0, 0)
1387+
.atOffset(ZoneOffset.UTC)
1388+
.toInstant()),
1389+
TimeZone.getTimeZone("Pacific/Kiritimati"),
1390+
LocalDateTime.of(2023, 1, 1, 14, 0)
1391+
)
1392+
);
14101393
}
14111394

1412-
@Test
1413-
void shouldConvertDateToLocalDateTimeUsingSpecifiedTimeZone() {
1414-
final Instant instant = LocalDateTime.of(2023, 1, 1, 0, 0)
1415-
.atOffset(ZoneOffset.UTC)
1416-
.toInstant();
1417-
final Date date = Date.from(instant);
14181395

1419-
final TimeZone newYorkTimeZone = TimeZone.getTimeZone("America/New_York");
1420-
final LocalDateTime expected = LocalDateTime.of(2022, 12, 31, 19, 0);
1421-
1422-
assertEquals(expected, DateUtils.toLocalDateTime(date, newYorkTimeZone));
1423-
}
14241396

14251397
@Test
14261398
void shouldThrowNullPointerExceptionWhenDateIsNull() {
14271399
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(null));
14281400
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(null, TimeZone.getDefault()));
1429-
}
1430-
1431-
@Test
1432-
void shouldHandleDaylightSavingTimeCorrectly() {
1433-
final Instant instant = LocalDateTime.of(2023, 3, 12, 7, 0)
1434-
.atOffset(ZoneOffset.UTC)
1435-
.toInstant();
1436-
final Date date = Date.from(instant);
1437-
1438-
final TimeZone newYorkTimeZone = TimeZone.getTimeZone("America/New_York");
1439-
final LocalDateTime expected = LocalDateTime.of(2023, 3, 12, 3, 0);
1440-
1441-
assertEquals(expected, DateUtils.toLocalDateTime(date, newYorkTimeZone));
1442-
}
1443-
1444-
@Test
1445-
void shouldHandleExtremeTimeZoneCorrectly() {
1446-
final Instant instant = LocalDateTime.of(2023, 1, 1, 0, 0)
1447-
.atOffset(ZoneOffset.UTC)
1448-
.toInstant();
1449-
final Date date = Date.from(instant);
1450-
1451-
final TimeZone extremeTimeZone = TimeZone.getTimeZone("Pacific/Kiritimati");
1452-
final LocalDateTime expected = LocalDateTime.of(2023, 1, 1, 14, 0);
1453-
1454-
assertEquals(expected, DateUtils.toLocalDateTime(date, extremeTimeZone));
1401+
assertThrows(NullPointerException.class, () -> DateUtils.toLocalDateTime(new Date(), null));
14551402
}
14561403

14571404

0 commit comments

Comments
 (0)