Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.chrono.IsoEra;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -389,7 +393,11 @@ private static DateTimeFormatter getCacheDateTimeFormat(String dateFormat, Local
}
DateTimeFormatter formatter = formatCache.get(dateFormat);
if (formatter == null) {
formatter = DateTimeFormatter.ofPattern(dateFormat, actualLocale);
formatter = new DateTimeFormatterBuilder()
.appendPattern(dateFormat)
.parseDefaulting(ChronoField.ERA, IsoEra.CE.getValue())
.toFormatter(actualLocale)
.withResolverStyle(ResolverStyle.STRICT);
formatCache.put(dateFormat, formatter);
}
return formatter;
Expand All @@ -407,6 +415,7 @@ private static DateFormat getCacheDateFormat(String dateFormat) {
}
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
simpleDateFormat.setLenient(false);
dateFormatMap.put(dateFormat, simpleDateFormat);
return simpleDateFormat;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.date;

import java.text.ParseException;
import java.util.Date;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.apache.fesod.sheet.util.DateUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests {@link DateStringConverter}.
*/
@Tag(Tags.UNIT)
class DateStringConverterTest {

private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
private final DateStringConverter converter = new DateStringConverter();

@ParameterizedTest
@ValueSource(strings = {"2024-02-31", "2023-02-29", "2024-13-01"})
void convertToJavaDataRejectsInvalidCalendarDates(String value) {
Assertions.assertThrows(
ParseException.class,
() -> converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION));
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-29", "2023-02-28"})
void convertToJavaDataAcceptsValidCalendarDates(String value) throws ParseException {
Date parsed = converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION);

Assertions.assertEquals(value, DateUtils.format(parsed, DateUtils.DATE_FORMAT_10));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fesod.sheet.converters.localdate;

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
import org.apache.fesod.sheet.metadata.data.ReadCellData;
import org.apache.fesod.sheet.testkit.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests {@link LocalDateStringConverter}.
*/
@Tag(Tags.UNIT)
class LocalDateStringConverterTest {

private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
private final LocalDateStringConverter converter = new LocalDateStringConverter();

@ParameterizedTest
@ValueSource(strings = {"2024-02-31", "2023-02-29", "2024-13-01"})
void convertToJavaDataRejectsInvalidCalendarDates(String value) {
Assertions.assertThrows(
DateTimeParseException.class,
() -> converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION));
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-29", "2023-02-28"})
void convertToJavaDataAcceptsValidCalendarDates(String value) throws ParseException {
LocalDate parsed = converter.convertToJavaData(new ReadCellData<>(value), null, GLOBAL_CONFIGURATION);

Assertions.assertEquals(LocalDate.parse(value), parsed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -100,6 +101,20 @@ void test_parseDate() throws ParseException {
Assertions.assertEquals(30, cal2.get(Calendar.MINUTE));
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-31", "2023-02-29", "2024-13-01"})
void test_parseDateRejectsInvalidCalendarDates(String value) {
Assertions.assertThrows(ParseException.class, () -> DateUtils.parseDate(value, DateUtils.DATE_FORMAT_10));
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-29", "2023-02-28"})
void test_parseDateAcceptsValidCalendarDates(String value) throws ParseException {
Date parsed = DateUtils.parseDate(value, DateUtils.DATE_FORMAT_10);

Assertions.assertEquals(value, DateUtils.format(parsed, DateUtils.DATE_FORMAT_10));
}

@Test
void test_parseLocalDateTime() {
String dateStr = "2026-10-01 12:30:45";
Expand Down Expand Up @@ -155,6 +170,21 @@ void test_parseLocalDate() {
Assertions.assertEquals(1, autoDetectFormatResult.getDayOfMonth());
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-31", "2023-02-29", "2024-13-01"})
void test_parseLocalDateRejectsInvalidCalendarDates(String value) {
Assertions.assertThrows(
DateTimeParseException.class, () -> DateUtils.parseLocalDate(value, DateUtils.DATE_FORMAT_10, null));
}

@ParameterizedTest
@ValueSource(strings = {"2024-02-29", "2023-02-28"})
void test_parseLocalDateAcceptsValidCalendarDates(String value) {
LocalDate parsed = DateUtils.parseLocalDate(value, DateUtils.DATE_FORMAT_10, null);

Assertions.assertEquals(value, DateUtils.format(parsed, DateUtils.DATE_FORMAT_10));
}

@Test
void test_parseLocalTime() {
LocalTime usResult = DateUtils.parseLocalTime("12:30:45", DateUtils.TIME_FORMAT_8, Locale.US);
Expand Down
Loading