From 8e92f370228d0f784f5c4766d70e96329e57ee71 Mon Sep 17 00:00:00 2001 From: John DeRegnaucourt Date: Sun, 4 Feb 2024 11:05:10 -0500 Subject: [PATCH] Added Year-Month tests. --- .../util/convert/StringConversions.java | 2 +- .../util/convert/ConverterEverythingTest.java | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index 25daef47..46ab1ebe 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -308,7 +308,7 @@ static YearMonth toYearMonth(Object from, Converter converter, ConverterOptions catch (DateTimeParseException e) { try { ZonedDateTime zdt = DateUtilities.parseDate(yearMonth, options.getZoneId(), true); - return YearMonth.of(zdt.getYear(), zdt.getDayOfMonth()); + return YearMonth.of(zdt.getYear(), zdt.getMonthValue()); } catch (Exception ex) { throw new IllegalArgumentException("Unable to extract Year-Month from string: " + yearMonth); diff --git a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java index 3f725573..7864801c 100644 --- a/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java +++ b/src/test/java/com/cedarsoftware/util/convert/ConverterEverythingTest.java @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.time.MonthDay; +import java.time.YearMonth; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -251,6 +252,29 @@ class ConverterEverythingTest { mapOf("month","6", "day", 30), MonthDay.of(6, 30) }, { mapOf("month",6L, "day", "30"), MonthDay.of(6, 30)}, }); + + TEST_FACTORY.put(pair(Void.class, YearMonth.class), new Object[][] { + { null, null }, + }); + TEST_FACTORY.put(pair(YearMonth.class, YearMonth.class), new Object[][] { + { YearMonth.of(2023, 12), YearMonth.of(2023, 12) }, + { YearMonth.of(1970, 1), YearMonth.of(1970, 1) }, + { YearMonth.of(1999, 6), YearMonth.of(1999, 6) }, + }); + TEST_FACTORY.put(pair(String.class, YearMonth.class), new Object[][] { + { "2024-01", YearMonth.of(2024, 1) }, + { "2024-1", new IllegalArgumentException("Unable to extract Year-Month from string: 2024-1") }, + { "2024-1-1", YearMonth.of(2024, 1) }, + { "2024-06-01", YearMonth.of(2024, 6) }, + { "2024-12-31", YearMonth.of(2024, 12) }, + { "05:45 2024-12-31", YearMonth.of(2024, 12) }, + }); + TEST_FACTORY.put(pair(Map.class, YearMonth.class), new Object[][] { + { mapOf("_v", "2024-01"), YearMonth.of(2024, 1) }, + { mapOf("year", "2024", "month", 12), YearMonth.of(2024, 12) }, + { mapOf("year", new BigInteger("2024"), "month", "12"), YearMonth.of(2024, 12) }, + { mapOf("year", mapOf("_v", 2024), "month", "12"), YearMonth.of(2024, 12) }, // Proving we recursively call .convert() + }); } @BeforeEach @@ -296,6 +320,7 @@ void testEverything() { System.err.println(); e.printStackTrace(); System.err.println(); + System.err.flush(); failed = true; } } @@ -304,6 +329,7 @@ void testEverything() { if (neededTests > 0) { System.err.println("Conversions needing tests: " + neededTests); + System.err.flush(); } if (failed) { throw new RuntimeException("One or more tests failed.");