|
| 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 org.apache.fesod.sheet.enums.CellDataTypeEnum; |
| 23 | +import org.apache.fesod.sheet.metadata.data.WriteCellData; |
| 24 | +import org.apache.fesod.sheet.testkit.Tags; |
| 25 | +import org.apache.poi.hssf.usermodel.HSSFCell; |
| 26 | +import org.apache.poi.xssf.streaming.SXSSFCell; |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.Tag; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.CsvSource; |
| 32 | +import org.junit.jupiter.params.provider.ValueSource; |
| 33 | +import org.mockito.Mockito; |
| 34 | + |
| 35 | +@Tag(Tags.UNIT) |
| 36 | +class EscapeHexCellWriteHandlerTest { |
| 37 | + |
| 38 | + private final EscapeHexCellWriteHandler handler = new EscapeHexCellWriteHandler(); |
| 39 | + |
| 40 | + /** |
| 41 | + * The handler only checks that the cell is an {@link SXSSFCell} and never reads from it, so a mock is all it |
| 42 | + * needs. |
| 43 | + */ |
| 44 | + private final SXSSFCell cell = Mockito.mock(SXSSFCell.class); |
| 45 | + |
| 46 | + /** |
| 47 | + * Runs the handler over a string cell and returns the value it left behind. |
| 48 | + */ |
| 49 | + private String escape(String input) { |
| 50 | + WriteCellData<?> cellData = new WriteCellData<>(input); |
| 51 | + handler.afterCellDataConverted(null, null, cellData, cell, null, 0, Boolean.FALSE); |
| 52 | + return cellData.getStringValue(); |
| 53 | + } |
| 54 | + |
| 55 | + @ParameterizedTest(name = "[{index}] {0} -> {1}") |
| 56 | + @CsvSource( |
| 57 | + delimiter = '|', |
| 58 | + value = { |
| 59 | + "_xB9f0_|_x005F_xB9f0_", |
| 60 | + "abc_x0041_|abc_x005F_x0041_", |
| 61 | + "_x0041__x0042_|_x005F_x0041__x005F_x0042_", |
| 62 | + "_xB9f0_ and _x1234_ and _xABCD_|_x005F_xB9f0_ and _x005F_x1234_ and _x005F_xABCD_", |
| 63 | + // 3 below check for partially valid cases - 1st format is valid, 2nd is invalid. |
| 64 | + "_x1234_ _xGHIJ_|_x005F_x1234_ _xGHIJ_", |
| 65 | + "_x0041__x12|_x005F_x0041__x12", |
| 66 | + "_x0041__x12345|_x005F_x0041__x12345", |
| 67 | + }) |
| 68 | + void afterCellDataConverted_escapesEveryValidHexPattern(String input, String expected) { |
| 69 | + Assertions.assertEquals(expected, escape(input)); |
| 70 | + } |
| 71 | + |
| 72 | + @ParameterizedTest(name = "[{index}] {0} is left alone") |
| 73 | + @ValueSource( |
| 74 | + strings = { |
| 75 | + "normalString", |
| 76 | + "_x12345_", // seventh character is not underscore |
| 77 | + "_x0041", // one character short of a complete pattern |
| 78 | + "_x00G1_", // a non-hex character |
| 79 | + "_x_x0041", // an unterminated pattern |
| 80 | + "", // empty input must not trip the scan |
| 81 | + "_x00é1_", // a non-ASCII character |
| 82 | + "_X1234_", // uppercase X |
| 83 | + }) |
| 84 | + void afterCellDataConverted_leavesInvalidPatternsUntouched(String input) { |
| 85 | + Assertions.assertEquals(input, escape(input)); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Escaping is not idempotent: an already-escaped literal is escaped again |
| 90 | + */ |
| 91 | + @Test |
| 92 | + void afterCellDataConverted_escapesAnAlreadyEscapedSequenceAgain() { |
| 93 | + Assertions.assertEquals("_x005F_x005F_x0041_", escape("_x005F_x0041_")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void afterCellDataConverted_ignoresNonStringCellData() { |
| 98 | + WriteCellData<?> cellData = new WriteCellData<>(CellDataTypeEnum.ERROR, "_x0041_"); |
| 99 | + |
| 100 | + handler.afterCellDataConverted(null, null, cellData, cell, null, 0, Boolean.FALSE); |
| 101 | + |
| 102 | + Assertions.assertEquals("_x0041_", cellData.getStringValue()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void afterCellDataConverted_ignoresNonSxssfCells() { |
| 107 | + WriteCellData<?> cellData = new WriteCellData<>("_x0041_"); |
| 108 | + |
| 109 | + handler.afterCellDataConverted(null, null, cellData, Mockito.mock(HSSFCell.class), null, 0, Boolean.FALSE); |
| 110 | + |
| 111 | + Assertions.assertEquals("_x0041_", cellData.getStringValue()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void afterCellDataConverted_toleratesNullCellDataAndNullStringValue() { |
| 116 | + WriteCellData<?> emptyStringData = new WriteCellData<>(CellDataTypeEnum.STRING); |
| 117 | + |
| 118 | + Assertions.assertDoesNotThrow( |
| 119 | + () -> handler.afterCellDataConverted(null, null, null, cell, null, 0, Boolean.FALSE)); |
| 120 | + Assertions.assertDoesNotThrow( |
| 121 | + () -> handler.afterCellDataConverted(null, null, emptyStringData, cell, null, 0, Boolean.FALSE)); |
| 122 | + Assertions.assertNull(emptyStringData.getStringValue()); |
| 123 | + } |
| 124 | +} |
0 commit comments