|
| 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.write.handler; |
| 21 | + |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.File; |
| 24 | +import java.io.FileReader; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.List; |
| 29 | +import org.apache.fesod.sheet.FesodSheet; |
| 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.params.ExcelFormatSource; |
| 34 | +import org.apache.poi.ss.usermodel.Workbook; |
| 35 | +import org.apache.poi.ss.usermodel.WorkbookFactory; |
| 36 | +import org.apache.poi.xssf.streaming.SXSSFCell; |
| 37 | +import org.junit.jupiter.api.Assertions; |
| 38 | +import org.junit.jupiter.api.Tag; |
| 39 | +import org.junit.jupiter.params.ParameterizedTest; |
| 40 | + |
| 41 | +@Tag(Tags.ROUND_TRIP) |
| 42 | +class EscapeHexCellWriteHandlerRoundTripTest extends AbstractExcelTest { |
| 43 | + |
| 44 | + private File writeEscapedWorkbook(ExcelFormat format) throws IOException { |
| 45 | + File file = createTempFile("escape-hex", format); |
| 46 | + List<List<String>> rows = new ArrayList<>(); |
| 47 | + rows.add(Collections.singletonList("_xB9f0_ and _x1234_")); |
| 48 | + |
| 49 | + FesodSheet.write(file) |
| 50 | + .excelType(format.toExcelTypeEnum()) |
| 51 | + .head(Collections.singletonList(Collections.singletonList("value"))) |
| 52 | + .registerWriteHandler(new EscapeHexCellWriteHandler()) |
| 53 | + .sheet("escape") |
| 54 | + .doWrite(rows); |
| 55 | + return file; |
| 56 | + } |
| 57 | + |
| 58 | + private String readBackFirstDataValue(File file, ExcelFormat format) throws IOException { |
| 59 | + if (format == ExcelFormat.CSV) { |
| 60 | + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { |
| 61 | + reader.readLine(); // header |
| 62 | + return reader.readLine(); |
| 63 | + } |
| 64 | + } |
| 65 | + try (Workbook workbook = WorkbookFactory.create(file)) { |
| 66 | + return workbook.getSheetAt(0).getRow(1).getCell(0).getStringCellValue(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Writes a file with the handler registered and reads it back: the caller must see the literal they typed. |
| 72 | + * |
| 73 | + * <p>All three formats expect the same value, for different reasons. On XLSX the handler escapes the sequence |
| 74 | + * and POI's reader decodes that escape away again. On XLS and CSV the handler never fires, since it only |
| 75 | + * touches {@link SXSSFCell}, so there was nothing to undo. |
| 76 | + */ |
| 77 | + @ParameterizedTest(name = "[{index}] {0} round-trips the literal hex sequence") |
| 78 | + @ExcelFormatSource |
| 79 | + void registeredOnAWrite_keepsLiteralHexSequencesIntactAcrossFormats(ExcelFormat format) throws IOException { |
| 80 | + File file = writeEscapedWorkbook(format); |
| 81 | + Assertions.assertEquals("_xB9f0_ and _x1234_", readBackFirstDataValue(file, format)); |
| 82 | + } |
| 83 | +} |
0 commit comments