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 @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -78,6 +79,7 @@
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
Expand Down Expand Up @@ -111,6 +113,8 @@ private static void initAllConverter() {
putAllConverter(new ByteNumberConverter());
putAllConverter(new ByteStringConverter());

putAllConverter(new CharacterStringConverter());

putAllConverter(new DateNumberConverter());
putAllConverter(new DateStringConverter());

Expand Down Expand Up @@ -147,6 +151,8 @@ private static void initAllConverter() {
putAllConverter(new StringNumberConverter());
putAllConverter(new StringStringConverter());
putAllConverter(new StringErrorConverter());

putAllConverter(new YearStringConverter());
allConverter = Collections.unmodifiableMap(allConverter);
}

Expand All @@ -156,6 +162,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new BigIntegerNumberConverter());
putWriteConverter(new BooleanBooleanConverter());
putWriteConverter(new ByteNumberConverter());
putWriteConverter(new CharacterStringConverter());
putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
Expand All @@ -166,6 +173,7 @@ private static void initDefaultWriteConverter() {
putWriteConverter(new LongNumberConverter());
putWriteConverter(new ShortNumberConverter());
putWriteConverter(new StringStringConverter());
putWriteConverter(new YearStringConverter());
putWriteConverter(new FileImageConverter());
putWriteConverter(new InputStreamImageConverter());
putWriteConverter(new ByteArrayImageConverter());
Expand All @@ -177,6 +185,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new BigIntegerStringConverter());
putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new CharacterStringConverter());
putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter());
Expand All @@ -187,6 +196,7 @@ private static void initDefaultWriteConverter() {
putWriteStringConverter(new LongStringConverter());
putWriteStringConverter(new ShortStringConverter());
putWriteStringConverter(new StringStringConverter());
putWriteStringConverter(new YearStringConverter());
defaultWriteConverter = Collections.unmodifiableMap(defaultWriteConverter);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
Expand Down Expand Up @@ -91,6 +92,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;
Expand Down Expand Up @@ -328,6 +331,39 @@ public static String format(LocalTime time, String timeFormat, Locale local) {
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
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 java.io.File;
import java.util.Collections;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.apache.fesod.sheet.FesodSheet;
import org.apache.fesod.sheet.annotation.ExcelProperty;
import org.apache.fesod.sheet.context.AnalysisContext;
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
import org.apache.fesod.sheet.event.AnalysisEventListener;
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());
}

@Test
void characterFieldRoundTripsThroughFesodSheet() throws Exception {
File file = File.createTempFile("fesod-character", ".xlsx");
file.deleteOnExit();
CharacterWriteData row = new CharacterWriteData();
row.setFlag('A');

FesodSheet.write(file, CharacterWriteData.class).sheet().doWrite(Collections.singletonList(row));

List<CharacterReadData> rows = FesodSheet.read(file, CharacterReadData.class, new CharacterReadListener())
.sheet()
.doReadSync();
Comment on lines +83 to +93

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest extending AbstractExcelTest and using its createTempFile(...) helper to cover multiple formats (e.g., XLSX, XLS, CSV). And using RoundTripHelper#writeAndRead(...) to simplify the boilerplate read/write code.

Additionally, it would be better to move this test into a dedicated integration test class rather than mixing it with pure unit tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same advice: YearStringConverterTest#yearFieldRoundTripsThroughFesodSheet()

Assertions.assertEquals(1, rows.size());
Assertions.assertEquals(Character.valueOf('A'), rows.get(0).getFlag());
}

@Getter
@Setter
public static class CharacterWriteData {

@ExcelProperty("flag")
private Character flag;
}

@Getter
@Setter
public static class CharacterReadData {

@ExcelProperty("flag")
private Character flag;
}

public static class CharacterReadListener extends AnalysisEventListener<CharacterReadData> {

@Override
public void invoke(CharacterReadData data, AnalysisContext context) {}

@Override
public void doAfterAllAnalysed(AnalysisContext context) {}
}
}
Loading