From 0e8f4e3634da889cc9ed3e0c69f7368a4ff3c94a Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 13 May 2021 12:11:31 +0000 Subject: [PATCH] Bug 65227: Add some more tests, but the actual case is hard to reproduce with HSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889838 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/TestSheetUtil.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/poi/src/test/java/org/apache/poi/ss/util/TestSheetUtil.java b/poi/src/test/java/org/apache/poi/ss/util/TestSheetUtil.java index 1731d46371..0a69d41a0e 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/TestSheetUtil.java +++ b/poi/src/test/java/org/apache/poi/ss/util/TestSheetUtil.java @@ -27,6 +27,7 @@ import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -164,4 +165,54 @@ final class TestSheetUtil { assertEquals(-1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01, "Not having any widht for rows with all empty cells"); } } + + @Test + void testGetColumnWidthBlankCell() throws IOException { + try (Workbook wb = new HSSFWorkbook()) { + Sheet sheet = wb.createSheet("sheet"); + Row row = sheet.createRow(0); + sheet.createRow(1); + sheet.createRow(2); + Cell cell = row.createCell(0); + + cell.setCellValue((String)null); + + assertEquals(-1, SheetUtil.getColumnWidth(sheet, 0, true), "Having some width for rows with actual cells"); + assertEquals(-1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01, "Not having any widht for rows with all empty cells"); + } + } + + @Test + void testGetColumnWidthemptyString() throws IOException { + try (Workbook wb = new HSSFWorkbook()) { + Sheet sheet = wb.createSheet("sheet"); + Row row = sheet.createRow(0); + sheet.createRow(1); + sheet.createRow(2); + Cell cell = row.createCell(0); + + cell.setCellValue(""); + + assertTrue(SheetUtil.getColumnWidth(sheet, 0, true) > 0, "Having some width for rows with actual cells"); + assertEquals(-1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01, "Not having any widht for rows with all empty cells"); + } + } + + @Test + void testGetColumnWidthNullString() throws IOException { + try (Workbook wb = new HSSFWorkbook()) { + Sheet sheet = wb.createSheet("sheet"); + Row row = sheet.createRow(0); + sheet.createRow(1); + sheet.createRow(2); + Cell cell = row.createCell(0); + + cell.setCellValue((String)null); + //noinspection deprecation + cell.setCellType(CellType.STRING); + + assertTrue(SheetUtil.getColumnWidth(sheet, 0, true) > 0, "Having some width for rows with actual cells"); + assertEquals(-1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01, "Not having any widht for rows with all empty cells"); + } + } }