Skip to content

Commit e957161

Browse files
committed
Merge main and retain Instant and UUID converter tests
2 parents c651b43 + 2e606a5 commit e957161

9 files changed

Lines changed: 425 additions & 2 deletions

File tree

‎fesod-sheet/src/main/java/org/apache/fesod/sheet/converters/DefaultConverterLoader.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import org.apache.fesod.sheet.converters.string.StringNumberConverter;
8787
import org.apache.fesod.sheet.converters.string.StringStringConverter;
8888
import org.apache.fesod.sheet.converters.url.UrlImageConverter;
89+
import org.apache.fesod.sheet.converters.uuid.UUIDStringConverter;
8990
import org.apache.fesod.sheet.converters.year.YearStringConverter;
9091

9192
/**
@@ -165,6 +166,8 @@ private static void initAllConverter() {
165166
putAllConverter(new StringStringConverter());
166167
putAllConverter(new StringErrorConverter());
167168

169+
putAllConverter(new UUIDStringConverter());
170+
168171
putAllConverter(new YearStringConverter());
169172
allConverter = Collections.unmodifiableMap(allConverter);
170173
}
@@ -195,6 +198,7 @@ private static void initDefaultWriteConverter() {
195198
putWriteConverter(new ByteArrayImageConverter());
196199
putWriteConverter(new BoxingByteArrayImageConverter());
197200
putWriteConverter(new UrlImageConverter());
201+
putWriteConverter(new UUIDStringConverter());
198202

199203
// In some cases, it must be converted to string
200204
putWriteStringConverter(new BigDecimalStringConverter());
@@ -215,6 +219,7 @@ private static void initDefaultWriteConverter() {
215219
putWriteStringConverter(new LongStringConverter());
216220
putWriteStringConverter(new ShortStringConverter());
217221
putWriteStringConverter(new StringStringConverter());
222+
putWriteStringConverter(new UUIDStringConverter());
218223
putWriteStringConverter(new YearStringConverter());
219224
defaultWriteConverter = Collections.unmodifiableMap(defaultWriteConverter);
220225
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.sheet.converters.uuid;
21+
22+
import java.util.UUID;
23+
import org.apache.fesod.common.util.StringUtils;
24+
import org.apache.fesod.sheet.converters.Converter;
25+
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
26+
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
27+
import org.apache.fesod.sheet.metadata.data.ReadCellData;
28+
import org.apache.fesod.sheet.metadata.data.WriteCellData;
29+
import org.apache.fesod.sheet.metadata.property.ExcelContentProperty;
30+
31+
/**
32+
* Converts UUID values to canonical lowercase strings and reads strings using {@link UUID#fromString(String)}.
33+
* Blank strings are treated as missing values, like blank cells. Leading and trailing whitespace is trimmed before parsing.
34+
*/
35+
public class UUIDStringConverter implements Converter<UUID> {
36+
37+
@Override
38+
public Class<UUID> supportJavaTypeKey() {
39+
return UUID.class;
40+
}
41+
42+
@Override
43+
public CellDataTypeEnum supportExcelTypeKey() {
44+
return CellDataTypeEnum.STRING;
45+
}
46+
47+
@Override
48+
public UUID convertToJavaData(
49+
ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
50+
String value = cellData.getStringValue();
51+
return StringUtils.isBlank(value) ? null : UUID.fromString(value.trim());
52+
}
53+
54+
@Override
55+
public WriteCellData<?> convertToExcelData(
56+
UUID value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
57+
return new WriteCellData<>(value.toString());
58+
}
59+
}

‎fesod-sheet/src/main/java/org/apache/fesod/sheet/metadata/format/DataFormatter.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private Format getFormat(Double data, Short dataFormat, String dataFormatString)
253253
}
254254

255255
private Format createFormat(Short dataFormat, String dataFormatString) {
256-
String formatStr = dataFormatString;
256+
String formatStr = quoteEscapedPercent(dataFormatString);
257257

258258
Format format = checkSpecialConverter(formatStr);
259259
if (format != null) {
@@ -475,6 +475,10 @@ private Format createDateFormat(String pFormatStr) {
475475
}
476476
}
477477

478+
private static String quoteEscapedPercent(String formatStr) {
479+
return formatStr.replace("\\%", "'%'");
480+
}
481+
478482
private String cleanFormatForNumber(String formatStr) {
479483
StringBuilder sb = new StringBuilder(formatStr);
480484
// If they requested spacers, with "_",
@@ -677,7 +681,11 @@ private String getFormattedNumberString(BigDecimal data, Short dataFormat, Strin
677681
*/
678682
public String format(BigDecimal data, Short dataFormat, String dataFormatString) {
679683
if (DateUtils.isADateFormat(dataFormat, dataFormatString)) {
680-
return getFormattedDateString(data.doubleValue(), dataFormat, dataFormatString);
684+
if (DateUtils.isValidExcelDate(data.doubleValue())) {
685+
return getFormattedDateString(data.doubleValue(), dataFormat, dataFormatString);
686+
}
687+
688+
return getDefaultFormat().format(data);
681689
}
682690
return getFormattedNumberString(data, dataFormat, dataFormatString);
683691
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.sheet.converter;
21+
22+
import java.io.File;
23+
import java.util.Arrays;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.Locale;
27+
import java.util.UUID;
28+
import lombok.Data;
29+
import org.apache.fesod.sheet.exception.ExcelDataConvertException;
30+
import org.apache.fesod.sheet.testkit.Tags;
31+
import org.apache.fesod.sheet.testkit.base.AbstractExcelTest;
32+
import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
33+
import org.apache.fesod.sheet.testkit.helpers.RoundTripHelper;
34+
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
35+
import org.junit.jupiter.api.Assertions;
36+
import org.junit.jupiter.api.Tag;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
39+
@Tag(Tags.ROUND_TRIP)
40+
class UUIDConverterRoundTripTest extends AbstractExcelTest {
41+
42+
private static final String TEXT = "123e4567-e89b-12d3-a456-426614174000";
43+
44+
@ParameterizedTest
45+
@ExcelFormatSource
46+
void roundTripsUuidAndNullFieldsWithoutCustomRegistration(ExcelFormat format) throws Exception {
47+
File file = createTempFile(format);
48+
UUIDData first = new UUIDData();
49+
first.setId(UUID.fromString(TEXT));
50+
first.setLabel("populated");
51+
UUIDData second = new UUIDData();
52+
second.setLabel("null UUID");
53+
List<UUIDData> expected = Arrays.asList(first, second);
54+
Assertions.assertEquals(expected, RoundTripHelper.writeAndRead(file, UUIDData.class, expected));
55+
List<StringData> strings = RoundTripHelper.read(file, StringData.class);
56+
Assertions.assertEquals(TEXT, strings.get(0).getId());
57+
}
58+
59+
@ParameterizedTest
60+
@ExcelFormatSource
61+
void readsUppercaseAndBlankCells(ExcelFormat format) throws Exception {
62+
File file = createTempFile(format);
63+
StringData first = new StringData();
64+
first.setId(TEXT.toUpperCase(Locale.ROOT));
65+
first.setLabel("uppercase");
66+
StringData second = new StringData();
67+
second.setId("");
68+
second.setLabel("blank UUID");
69+
RoundTripHelper.write(file, StringData.class, Arrays.asList(first, second));
70+
List<UUIDData> rows = RoundTripHelper.read(file, UUIDData.class);
71+
Assertions.assertEquals(2, rows.size());
72+
Assertions.assertEquals(UUID.fromString(TEXT), rows.get(0).getId());
73+
Assertions.assertNull(rows.get(1).getId());
74+
}
75+
76+
@ParameterizedTest
77+
@ExcelFormatSource
78+
void reportsInvalidCellAsConversionFailure(ExcelFormat format) throws Exception {
79+
File file = createTempFile(format);
80+
StringData row = new StringData();
81+
row.setId("not-a-uuid");
82+
row.setLabel("invalid");
83+
RoundTripHelper.write(file, StringData.class, Collections.singletonList(row));
84+
ExcelDataConvertException error = Assertions.assertThrows(
85+
ExcelDataConvertException.class, () -> RoundTripHelper.read(file, UUIDData.class));
86+
Assertions.assertInstanceOf(IllegalArgumentException.class, error.getCause());
87+
}
88+
89+
@Data
90+
public static class UUIDData {
91+
private UUID id;
92+
private String label;
93+
}
94+
95+
@Data
96+
public static class StringData {
97+
private String id;
98+
private String label;
99+
}
100+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.sheet.converter;
21+
22+
import java.util.Locale;
23+
import java.util.UUID;
24+
import org.apache.fesod.sheet.converters.uuid.UUIDStringConverter;
25+
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
26+
import org.apache.fesod.sheet.metadata.GlobalConfiguration;
27+
import org.apache.fesod.sheet.metadata.data.ReadCellData;
28+
import org.apache.fesod.sheet.metadata.data.WriteCellData;
29+
import org.apache.fesod.sheet.testkit.Tags;
30+
import org.junit.jupiter.api.Assertions;
31+
import org.junit.jupiter.api.Tag;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.NullSource;
35+
import org.junit.jupiter.params.provider.ValueSource;
36+
37+
@Tag(Tags.UNIT)
38+
class UUIDConverterTest {
39+
40+
private static final String TEXT = "123e4567-e89b-12d3-a456-426614174000";
41+
private final UUIDStringConverter converter = new UUIDStringConverter();
42+
private final GlobalConfiguration configuration = new GlobalConfiguration();
43+
44+
@Test
45+
void supportsUuidStrings() {
46+
Assertions.assertEquals(UUID.class, converter.supportJavaTypeKey());
47+
Assertions.assertEquals(CellDataTypeEnum.STRING, converter.supportExcelTypeKey());
48+
}
49+
50+
@ParameterizedTest
51+
@ValueSource(
52+
strings = {
53+
TEXT,
54+
"123E4567-E89B-12D3-A456-426614174000",
55+
"00000000-0000-0000-0000-000000000000",
56+
"ffffffff-ffff-ffff-ffff-ffffffffffff"
57+
})
58+
void readsAndWritesCanonicalStrings(String input) {
59+
UUID value = converter.convertToJavaData(new ReadCellData<>(input), null, configuration);
60+
WriteCellData<?> cell = converter.convertToExcelData(value, null, configuration);
61+
Assertions.assertEquals(CellDataTypeEnum.STRING, cell.getType());
62+
Assertions.assertEquals(input.toLowerCase(Locale.ROOT), cell.getStringValue());
63+
}
64+
65+
@ParameterizedTest
66+
@ValueSource(
67+
strings = {
68+
"not-a-uuid",
69+
"123e4567-e89b-12d3-a456-42661417400g",
70+
"123e4567e89b12d3a456426614174000",
71+
" not-a-uuid\t",
72+
"123e4567-e89b-12d3-a456-42661417 4000"
73+
})
74+
void rejectsInvalidInput(String input) {
75+
Assertions.assertThrows(
76+
IllegalArgumentException.class,
77+
() -> converter.convertToJavaData(new ReadCellData<>(input), null, configuration));
78+
}
79+
80+
@ParameterizedTest
81+
@NullSource
82+
@ValueSource(strings = {"", " ", "\t\r\n", "\u2003"})
83+
void readsBlankStringAsMissingValue(String input) {
84+
ReadCellData<String> cell = new ReadCellData<>();
85+
cell.setStringValue(input);
86+
Assertions.assertNull(converter.convertToJavaData(cell, null, configuration));
87+
}
88+
89+
@ParameterizedTest
90+
@ValueSource(strings = {" " + TEXT, TEXT + " ", "\t" + TEXT + "\r\n"})
91+
void trimsWhitespaceBeforeParsing(String input) {
92+
Assertions.assertEquals(
93+
UUID.fromString(TEXT), converter.convertToJavaData(new ReadCellData<>(input), null, configuration));
94+
}
95+
}

‎fesod-sheet/src/test/java/org/apache/fesod/sheet/converters/DefaultConverterLoaderTest.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.Instant;
2424
import java.time.LocalTime;
2525
import java.util.Map;
26+
import java.util.UUID;
2627
import org.apache.fesod.sheet.converters.ConverterKeyBuild.ConverterKey;
2728
import org.apache.fesod.sheet.converters.instant.InstantStringConverter;
2829
import org.apache.fesod.sheet.converters.localtime.LocalTimeDateConverter;
@@ -31,6 +32,7 @@
3132
import org.apache.fesod.sheet.converters.sqltime.SqlTimeDateConverter;
3233
import org.apache.fesod.sheet.converters.sqltime.SqlTimeNumberConverter;
3334
import org.apache.fesod.sheet.converters.sqltime.SqlTimeStringConverter;
35+
import org.apache.fesod.sheet.converters.uuid.UUIDStringConverter;
3436
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
3537
import org.junit.jupiter.api.Assertions;
3638
import org.junit.jupiter.api.Test;
@@ -88,6 +90,20 @@ void loadConvertersRegistersInstantStringConverter() {
8890
writeConverter.get(ConverterKeyBuild.buildKey(Instant.class, CellDataTypeEnum.STRING)));
8991
}
9092

93+
@Test
94+
void loadConvertersRegistersUUIDFamily() {
95+
Assertions.assertInstanceOf(
96+
UUIDStringConverter.class,
97+
DefaultConverterLoader.loadDefaultReadConverter()
98+
.get(ConverterKeyBuild.buildKey(UUID.class, CellDataTypeEnum.STRING)));
99+
Map<ConverterKey, Converter<?>> writeConverter = DefaultConverterLoader.loadDefaultWriteConverter();
100+
Assertions.assertInstanceOf(
101+
UUIDStringConverter.class, writeConverter.get(ConverterKeyBuild.buildKey(UUID.class)));
102+
Assertions.assertInstanceOf(
103+
UUIDStringConverter.class,
104+
writeConverter.get(ConverterKeyBuild.buildKey(UUID.class, CellDataTypeEnum.STRING)));
105+
}
106+
91107
@Test
92108
void loadConvertersRegistersSqlTimeFamily() {
93109
Map<ConverterKey, Converter<?>> allConverter = DefaultConverterLoader.loadAllConverter();

0 commit comments

Comments
 (0)