|
| 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 | +} |
0 commit comments