Skip to content

Commit d0fc9b3

Browse files
committed
fix: give the escape-hex round-trip its own class and tag
1 parent d6290ec commit d0fc9b3

2 files changed

Lines changed: 83 additions & 55 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

fesod-sheet/src/test/java/org/apache/fesod/sheet/write/handler/EscapeHexCellWriteHandlerTest.java

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,22 @@
1919

2020
package org.apache.fesod.sheet.write.handler;
2121

22-
import java.io.BufferedReader;
23-
import java.io.File;
24-
import java.io.FileReader;
2522
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;
3023
import org.apache.fesod.sheet.enums.CellDataTypeEnum;
3124
import org.apache.fesod.sheet.metadata.data.WriteCellData;
3225
import org.apache.fesod.sheet.testkit.Tags;
33-
import org.apache.fesod.sheet.testkit.enums.ExcelFormat;
34-
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
35-
import org.apache.poi.ss.usermodel.Workbook;
36-
import org.apache.poi.ss.usermodel.WorkbookFactory;
3726
import org.apache.poi.xssf.streaming.SXSSFCell;
3827
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
3928
import org.junit.jupiter.api.Assertions;
4029
import org.junit.jupiter.api.Tag;
4130
import org.junit.jupiter.api.Test;
42-
import org.junit.jupiter.api.io.TempDir;
4331
import org.junit.jupiter.params.ParameterizedTest;
4432
import org.junit.jupiter.params.provider.CsvSource;
4533
import org.junit.jupiter.params.provider.ValueSource;
4634

4735
@Tag(Tags.UNIT)
4836
class EscapeHexCellWriteHandlerTest {
4937

50-
@TempDir
51-
File tempDir;
52-
5338
private final EscapeHexCellWriteHandler handler = new EscapeHexCellWriteHandler();
5439

5540
/**
@@ -130,44 +115,4 @@ void afterCellDataConverted_toleratesNullCellDataAndNullStringValue() throws IOE
130115
Assertions.assertNull(emptyStringData.getStringValue());
131116
}
132117
}
133-
134-
private File writeEscapedWorkbook(ExcelFormat format) throws IOException {
135-
File file = format.createTempFile("escape-hex", tempDir);
136-
List<List<String>> rows = new ArrayList<>();
137-
rows.add(Collections.singletonList("_xB9f0_ and _x1234_"));
138-
139-
FesodSheet.write(file)
140-
.excelType(format.toExcelTypeEnum())
141-
.head(Collections.singletonList(Collections.singletonList("value")))
142-
.registerWriteHandler(new EscapeHexCellWriteHandler())
143-
.sheet("escape")
144-
.doWrite(rows);
145-
return file;
146-
}
147-
148-
private String readBackFirstDataValue(File file, ExcelFormat format) throws IOException {
149-
if (format == ExcelFormat.CSV) {
150-
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
151-
reader.readLine(); // header
152-
return reader.readLine();
153-
}
154-
}
155-
try (Workbook workbook = WorkbookFactory.create(file)) {
156-
return workbook.getSheetAt(0).getRow(1).getCell(0).getStringCellValue();
157-
}
158-
}
159-
160-
/**
161-
* Writes a file with the handler registered and reads it back: the caller must see the literal they typed.
162-
*
163-
* <p>All three formats expect the same value, for different reasons. On XLSX the handler escapes the sequence
164-
* and POI's reader decodes that escape away again. On XLS and CSV the handler never fires, since it only
165-
* touches {@link SXSSFCell}, so there was nothing to undo.
166-
*/
167-
@ParameterizedTest(name = "[{index}] {0} round-trips the literal hex sequence")
168-
@ExcelFormatSource
169-
void registeredOnAWrite_keepsLiteralHexSequencesIntactAcrossFormats(ExcelFormat format) throws IOException {
170-
File file = writeEscapedWorkbook(format);
171-
Assertions.assertEquals("_xB9f0_ and _x1234_", readBackFirstDataValue(file, format));
172-
}
173118
}

0 commit comments

Comments
 (0)