You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fesod.apache.org by be...@apache.org on 2026/10/10 12:08:00 UTC
(fesod) branch main updated: feat: add Character and Year converters (#1017) (#1087)
This is an automated email from the ASF dual-hosted git repository.
bengbengbalabalabeng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fesod.git
The following commit(s) were added to refs/heads/main by this push:
new bf9bf923 feat: add Character and Year converters (#1017) (#1087)
bf9bf923 is described below
commit bf9bf92332aedcb7d88bf90cd12c2c092b3d56f0
Author: BigDataDZ <76...@users.noreply.github.com>
AuthorDate: Sat Oct 10 20:07:56 2026 +0800
feat: add Character and Year converters (#1017) (#1087)
* feat: add Character and Year converters (#1017)
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* refactor: address review comments on Character and Year converters
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* refactor: align default year format field naming
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* test: cover YearStringConverter and DateUtils year scenarios
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* fix: register the Year converter under the wildcard write key
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* test: cover Character field round trip through FesodSheet
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* test: move converter field round trips to a dedicated integration test
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
* docs: cover the read-path converter lookup in the round trip test javadoc
The previous javadoc described only the write-path lookup keys. On the
read path, xlsx/xls resolve converters by the concrete cell type key
(type, CellDataType), not the wildcard (type, null) key, so describe
both directions.
---------
Signed-off-by: BigDataDZ <76...@users.noreply.github.com>
Co-authored-by: Bengbengbalabalabeng <70...@users.noreply.github.com>
---
.../sheet/converters/DefaultConverterLoader.java | 10 +++
.../charconverter/CharacterStringConverter.java | 62 ++++++++++++++
.../sheet/converters/year/YearStringConverter.java | 65 +++++++++++++++
.../org/apache/fesod/sheet/util/DateUtils.java | 36 ++++++++
.../CharacterAndYearFieldRoundTripTest.java | 92 +++++++++++++++++++++
.../CharacterStringConverterTest.java | 72 ++++++++++++++++
.../converters/year/YearStringConverterTest.java | 96 ++++++++++++++++++++++
.../org/apache/fesod/sheet/util/DateUtilsTest.java | 24 ++++++
8 files changed, 457 insertions(+)
diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java
index 749bcc7c..b0372279 100644
--- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java
@@ -44,6 +44,7 @@ import org.apache.fesod.sheet.converters.bytearray.ByteArrayImageConverter;
import org.apache.fesod.sheet.converters.byteconverter.ByteBooleanConverter;
import org.apache.fesod.sheet.converters.byteconverter.ByteNumberConverter;
import org.apache.fesod.sheet.converters.byteconverter.ByteStringConverter;
+import org.apache.fesod.sheet.converters.charconverter.CharacterStringConverter;
import org.apache.fesod.sheet.converters.date.DateDateConverter;
import org.apache.fesod.sheet.converters.date.DateNumberConverter;
import org.apache.fesod.sheet.converters.date.DateStringConverter;
@@ -84,6 +85,7 @@ import org.apache.fesod.sheet.converters.string.StringErrorConverter;
import org.apache.fesod.sheet.converters.string.StringNumberConverter;
import org.apache.fesod.sheet.converters.string.StringStringConverter;
import org.apache.fesod.sheet.converters.url.UrlImageConverter;
+import org.apache.fesod.sheet.converters.year.YearStringConverter;
/**
* Load default handler
@@ -117,6 +119,8 @@ public class DefaultConverterLoader {
putAllConverter(new ByteNumberConverter());
putAllConverter(new ByteStringConverter());
+ putAllConverter(new CharacterStringConverter());
+
putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter());
@@ -157,6 +161,8 @@ public class DefaultConverterLoader {
putAllConverter(new StringNumberConverter());
putAllConverter(new StringStringConverter());
putAllConverter(new StringErrorConverter());
+
+ putAllConverter(new YearStringConverter());
allConverter = Collections.unmodifiableMap(allConverter);
}
@@ -166,6 +172,7 @@ public class DefaultConverterLoader {
putWriteConverter(new BigIntegerNumberConverter());
putWriteConverter(new BooleanBooleanConverter());
putWriteConverter(new ByteNumberConverter());
+ putWriteConverter(new CharacterStringConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
@@ -178,6 +185,7 @@ public class DefaultConverterLoader {
putWriteConverter(new LongNumberConverter());
putWriteConverter(new ShortNumberConverter());
putWriteConverter(new StringStringConverter());
+ putWriteConverter(new YearStringConverter());
putWriteConverter(new FileImageConverter());
putWriteConverter(new InputStreamImageConverter());
putWriteConverter(new ByteArrayImageConverter());
@@ -189,6 +197,7 @@ public class DefaultConverterLoader {
putWriteStringConverter(new BigIntegerStringConverter());
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
+ putWriteStringConverter(new CharacterStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
@@ -201,6 +210,7 @@ public class DefaultConverterLoader {
putWriteStringConverter(new LongStringConverter());
putWriteStringConverter(new ShortStringConverter());
putWriteStringConverter(new StringStringConverter());
+ putWriteStringConverter(new YearStringConverter());
defaultWriteConverter = Collections.unmodifiableMap(defaultWriteConverter);
}
diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverter.java
new file mode 100644
index 00000000..d25ac1e3
--- /dev/null
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverter.java
@@ -0,0 +1,62 @@
+/*
+ * 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.charconverter;
+
+import org.apache.fesod.sheet.converters.Converter;
+import org.apache.fesod.sheet.enums.CellDataTypeEnum;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.ReadCellData;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
+
+/**
+ * Character and string converter
+ */
+public class CharacterStringConverter implements Converter<Character> {
+ @Override
+ public Class<?> supportJavaTypeKey() {
+ return Character.class;
+ }
+
+ @Override
+ public CellDataTypeEnum supportExcelTypeKey() {
+ return CellDataTypeEnum.STRING;
+ }
+
+ @Override
+ public Character convertToJavaData(
+ ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
+ String stringValue = cellData.getStringValue();
+ if (stringValue == null || stringValue.isEmpty()) {
+ return null;
+ }
+ if (stringValue.length() > 1) {
+ throw new IllegalArgumentException(
+ "Can not convert '" + stringValue + "' to a character, the length must be 1");
+ }
+ return stringValue.charAt(0);
+ }
+
+ @Override
+ public WriteCellData<?> convertToExcelData(
+ Character value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
+ return new WriteCellData<>(value.toString());
+ }
+}
diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/year/YearStringConverter.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/year/YearStringConverter.java
new file mode 100644
index 00000000..4dd2f07e
--- /dev/null
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/year/YearStringConverter.java
@@ -0,0 +1,65 @@
+/*
+ * 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.year;
+
+import java.time.Year;
+import org.apache.fesod.sheet.converters.Converter;
+import org.apache.fesod.sheet.enums.CellDataTypeEnum;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.ReadCellData;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
+import org.apache.fesod.sheet.util.DateUtils;
+
+/**
+ * Year and string converter
+ */
+public class YearStringConverter implements Converter<Year> {
+ @Override
+ public Class<?> supportJavaTypeKey() {
+ return Year.class;
+ }
+
+ @Override
+ public CellDataTypeEnum supportExcelTypeKey() {
+ return CellDataTypeEnum.STRING;
+ }
+
+ @Override
+ public Year convertToJavaData(
+ ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
+ return DateUtils.parseYear(
+ cellData.getStringValue(), getYearFormat(contentProperty), globalConfiguration.getLocale());
+ }
+
+ @Override
+ public WriteCellData<?> convertToExcelData(
+ Year value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
+ return new WriteCellData<>(
+ DateUtils.format(value, getYearFormat(contentProperty), globalConfiguration.getLocale()));
+ }
+
+ private static String getYearFormat(ExcelContentProperty contentProperty) {
+ if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
+ return null;
+ }
+ return contentProperty.getDateTimeFormatProperty().getFormat();
+ }
+}
diff --git a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java
index 0fc1c356..062d890a 100644
--- a/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java
+++ b/fesod-sheet/src/main/java/org/apache/fesod/sheet/util/DateUtils.java
@@ -33,6 +33,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
+import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
@@ -93,6 +94,8 @@ public class DateUtils {
public static String defaultLocalDateFormat = DATE_FORMAT_10;
+ public static String defaultYearFormat = "yyyy";
+
public static final String DEFAULT_LOCAL_TIME_FORMAT = TIME_FORMAT_8;
public static final int SECONDS_PER_MINUTE = 60;
@@ -349,6 +352,39 @@ public class DateUtils {
return time.format(getCacheDateTimeFormat(timeFormat, local));
}
+ /**
+ * convert string to year
+ *
+ * @param yearString
+ * @param yearFormat
+ * @param local
+ * @return
+ */
+ public static Year parseYear(String yearString, String yearFormat, Locale local) {
+ if (StringUtils.isEmpty(yearFormat)) {
+ yearFormat = defaultYearFormat;
+ }
+ return Year.parse(yearString, getCacheDateTimeFormat(yearFormat, local));
+ }
+
+ /**
+ * Format year
+ *
+ * @param year Year
+ * @param yearFormat year format
+ * @param local local
+ * @return format string
+ */
+ public static String format(Year year, String yearFormat, Locale local) {
+ if (year == null) {
+ return null;
+ }
+ if (StringUtils.isEmpty(yearFormat)) {
+ yearFormat = defaultYearFormat;
+ }
+ return year.format(getCacheDateTimeFormat(yearFormat, local));
+ }
+
/**
* Format date, preserving the offset in the output
*
diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/CharacterAndYearFieldRoundTripTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/CharacterAndYearFieldRoundTripTest.java
new file mode 100644
index 00000000..5e9be3ef
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/CharacterAndYearFieldRoundTripTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.time.Year;
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.fesod.sheet.annotation.ExcelProperty;
+import org.apache.fesod.sheet.testkit.Tags;
+import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
+import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
+import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper;
+import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.params.ParameterizedTest;
+
+/**
+ * Field-level round trips for the Character and Year converters across every supported format.
+ * Converter lookup differs by direction: on write, xlsx/xls resolve the wildcard key
+ * ((type, null)) while CSV forces a STRING target and resolves (type, STRING); on read, every
+ * format resolves by the concrete cell type key, here (type, STRING).
+ */
+@Tag(Tags.ROUND_TRIP)
+public class CharacterAndYearFieldRoundTripTest extends AbstractExcelTest {
+
+ @ParameterizedTest
+ @ExcelFormatSource
+ void yearFieldRoundTrip(ExcelFormat format) throws Exception {
+ File file = createTempFile(format);
+ List<YearFieldData> data = new ArrayList<>();
+ YearFieldData row = new YearFieldData();
+ row.setFlag(Year.of(2026));
+ data.add(row);
+
+ List<YearFieldData> result = RoundTripHelper.writeAndRead(file, YearFieldData.class, data);
+
+ Assertions.assertEquals(1, result.size());
+ Assertions.assertEquals(Year.of(2026), result.get(0).getFlag());
+ }
+
+ @ParameterizedTest
+ @ExcelFormatSource
+ void characterFieldRoundTrip(ExcelFormat format) throws Exception {
+ File file = createTempFile(format);
+ List<CharacterFieldData> data = new ArrayList<>();
+ CharacterFieldData row = new CharacterFieldData();
+ row.setFlag('A');
+ data.add(row);
+
+ List<CharacterFieldData> result = RoundTripHelper.writeAndRead(file, CharacterFieldData.class, data);
+
+ Assertions.assertEquals(1, result.size());
+ Assertions.assertEquals(Character.valueOf('A'), result.get(0).getFlag());
+ }
+
+ @Getter
+ @Setter
+ public static class YearFieldData {
+
+ @ExcelProperty("flag")
+ private Year flag;
+ }
+
+ @Getter
+ @Setter
+ public static class CharacterFieldData {
+
+ @ExcelProperty("flag")
+ private Character flag;
+ }
+}
diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverterTest.java
new file mode 100644
index 00000000..a5d406a1
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/charconverter/CharacterStringConverterTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.charconverter;
+
+import org.apache.fesod.sheet.enums.CellDataTypeEnum;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.ReadCellData;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.testkit.Tags;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag(Tags.UNIT)
+class CharacterStringConverterTest {
+
+ private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
+ private final CharacterStringConverter converter = new CharacterStringConverter();
+
+ @Test
+ void supportJavaTypeKey() {
+ Assertions.assertEquals(Character.class, converter.supportJavaTypeKey());
+ }
+
+ @Test
+ void supportExcelTypeKey() {
+ Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
+ }
+
+ @Test
+ void convertToJavaDataReturnsSingleCharacter() {
+ Assertions.assertEquals(
+ Character.valueOf('A'),
+ converter.convertToJavaData(new ReadCellData<>("A"), null, GLOBAL_CONFIGURATION));
+ }
+
+ @Test
+ void convertToJavaDataReturnsNullForEmptyString() {
+ Assertions.assertNull(converter.convertToJavaData(new ReadCellData<>(""), null, GLOBAL_CONFIGURATION));
+ }
+
+ @Test
+ void convertToJavaDataThrowsOnLongerStrings() {
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> converter.convertToJavaData(new ReadCellData<>("AB"), null, GLOBAL_CONFIGURATION));
+ }
+
+ @Test
+ void convertToExcelDataWritesStringCell() {
+ WriteCellData<?> cellData = converter.convertToExcelData('A', null, GLOBAL_CONFIGURATION);
+ Assertions.assertEquals(CellDataTypeEnum.STRING, cellData.getType());
+ Assertions.assertEquals("A", cellData.getStringValue());
+ }
+}
diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/year/YearStringConverterTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/year/YearStringConverterTest.java
new file mode 100644
index 00000000..64959c3d
--- /dev/null
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/year/YearStringConverterTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.year;
+
+import java.time.DateTimeException;
+import java.time.Year;
+import java.util.Locale;
+import org.apache.fesod.sheet.enums.CellDataTypeEnum;
+import org.apache.fesod.sheet.metadata.GlobalConfiguration;
+import org.apache.fesod.sheet.metadata.data.ReadCellData;
+import org.apache.fesod.sheet.metadata.data.WriteCellData;
+import org.apache.fesod.sheet.metadata.property.DateTimeFormatProperty;
+import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
+import org.apache.fesod.sheet.testkit.Tags;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag(Tags.UNIT)
+class YearStringConverterTest {
+
+ private static final GlobalConfiguration GLOBAL_CONFIGURATION = new GlobalConfiguration();
+ private final YearStringConverter converter = new YearStringConverter();
+
+ @Test
+ void supportJavaTypeKey() {
+ Assertions.assertEquals(Year.class, converter.supportJavaTypeKey());
+ }
+
+ @Test
+ void supportExcelTypeKey() {
+ Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
+ }
+
+ @Test
+ void convertToJavaDataUsesDefaultFormat() {
+ Assertions.assertEquals(
+ Year.of(2026), converter.convertToJavaData(new ReadCellData<>("2026"), null, GLOBAL_CONFIGURATION));
+ }
+
+ @Test
+ void convertToJavaDataThrowsOnInvalidValue() {
+ Assertions.assertThrows(
+ DateTimeException.class,
+ () -> converter.convertToJavaData(new ReadCellData<>("not-a-year"), null, GLOBAL_CONFIGURATION));
+ }
+
+ @Test
+ void convertToExcelDataUsesDefaultFormat() {
+ WriteCellData<?> cellData = converter.convertToExcelData(Year.of(2026), null, GLOBAL_CONFIGURATION);
+ Assertions.assertEquals(CellDataTypeEnum.STRING, cellData.getType());
+ Assertions.assertEquals("2026", cellData.getStringValue());
+ }
+
+ @Test
+ void convertToExcelDataUsesCustomFormat() {
+ ExcelContentProperty contentProperty = new ExcelContentProperty();
+ contentProperty.setDateTimeFormatProperty(new DateTimeFormatProperty("'Y'yyyy", null));
+ WriteCellData<?> cellData = converter.convertToExcelData(Year.of(2026), contentProperty, GLOBAL_CONFIGURATION);
+ Assertions.assertEquals("Y2026", cellData.getStringValue());
+ }
+
+ @Test
+ void convertToExcelDataRespectsLocaleForEraPatterns() {
+ GlobalConfiguration chinaConfiguration = new GlobalConfiguration();
+ chinaConfiguration.setLocale(Locale.CHINA);
+ GlobalConfiguration usConfiguration = new GlobalConfiguration();
+ usConfiguration.setLocale(Locale.US);
+ ExcelContentProperty contentProperty = new ExcelContentProperty();
+ contentProperty.setDateTimeFormatProperty(new DateTimeFormatProperty("G y", null));
+
+ WriteCellData<?> chinaCellData =
+ converter.convertToExcelData(Year.of(2026), contentProperty, chinaConfiguration);
+ Assertions.assertEquals("公元 2026", chinaCellData.getStringValue());
+
+ WriteCellData<?> usCellData = converter.convertToExcelData(Year.of(2026), contentProperty, usConfiguration);
+ Assertions.assertEquals("AD 2026", usCellData.getStringValue());
+ }
+}
diff --git a/fesod-sheet/src/test/java/org/apache/fesod/sheet/util/DateUtilsTest.java b/fesod-sheet/src/test/java/org/apache/fesod/sheet/util/DateUtilsTest.java
index 59c6be38..1d374baf 100644
--- a/fesod-sheet/src/test/java/org/apache/fesod/sheet/util/DateUtilsTest.java
+++ b/fesod-sheet/src/test/java/org/apache/fesod/sheet/util/DateUtilsTest.java
@@ -27,6 +27,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
+import java.time.Year;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
@@ -543,4 +544,27 @@ class DateUtilsTest {
Assertions.assertNull(((ThreadLocal<?>) f2.get(null)).get());
Assertions.assertNull(((ThreadLocal<?>) f3.get(null)).get());
}
+
+ @Test
+ void test_parseYearUsesDefaultFormat() {
+ Assertions.assertEquals(Year.of(2026), DateUtils.parseYear("2026", null, null));
+ Assertions.assertEquals(Year.of(2026), DateUtils.parseYear("2026", null, Locale.US));
+ }
+
+ @Test
+ void test_parseYearUsesCustomFormat() {
+ Assertions.assertEquals(Year.of(2026), DateUtils.parseYear("26", "uu", null));
+ }
+
+ @Test
+ void test_formatYearUsesDefaultFormat() {
+ Assertions.assertEquals("2026", DateUtils.format(Year.of(2026), null, null));
+ Assertions.assertEquals("2026", DateUtils.format(Year.of(2026), null, Locale.US));
+ }
+
+ @Test
+ void test_formatYearRespectsLocaleForEraPatterns() {
+ Assertions.assertEquals("公元 2026", DateUtils.format(Year.of(2026), "G y", Locale.CHINA));
+ Assertions.assertEquals("AD 2026", DateUtils.format(Year.of(2026), "G y", Locale.US));
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org