From 01dabc0d1b76bafa136248ef7676060f65cc45ec Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 20 Dec 2020 18:47:56 +0000 Subject: [PATCH] Fix NullPointerException in Autosize introduced via #64981 Only happened when alignment is set Add a test reproducing the problem Also verify auto-size on SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1884653 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/usermodel/XSSFCellStyle.java | 2 +- .../TestSXSSFSheetAutosizeColumn.java | 39 +++++++++++++ .../BaseTestSheetAutosizeColumn.java | 55 +++++++++++++------ 3 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFSheetAutosizeColumn.java diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 871b1a933c..0bebc2d119 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -592,7 +592,7 @@ public class XSSFCellStyle implements CellStyle, Duplicatable { @Override public short getRotation() { CTCellAlignment align = _cellXf.getAlignment(); - return align == null ? 0 : align.getTextRotation().shortValue(); + return align == null || align.getTextRotation() == null ? 0 : align.getTextRotation().shortValue(); } @Override diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFSheetAutosizeColumn.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFSheetAutosizeColumn.java new file mode 100644 index 0000000000..38ff1fc352 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFSheetAutosizeColumn.java @@ -0,0 +1,39 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.xssf.usermodel; + +import org.apache.poi.ss.usermodel.BaseTestSheetAutosizeColumn; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.SXSSFITestDataProvider; +import org.apache.poi.xssf.streaming.SXSSFSheet; + +/** + * @author Yegor Kozlov + */ +public final class TestSXSSFSheetAutosizeColumn extends BaseTestSheetAutosizeColumn { + + public TestSXSSFSheetAutosizeColumn(){ + super(SXSSFITestDataProvider.instance); + } + + @Override + protected void trackColumnsForAutoSizingIfSXSSF(Sheet sheet) { + ((SXSSFSheet)sheet).trackAllColumnsForAutoSizing(); + } +} \ No newline at end of file diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java index 870b2179e3..abbb0d1815 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.util.Calendar; +import java.util.Date; import java.util.Locale; import org.apache.poi.ss.ITestDataProvider; @@ -40,22 +41,22 @@ public abstract class BaseTestSheetAutosizeColumn { private final ITestDataProvider _testDataProvider; private static Locale userLocale; - + @BeforeClass public static void initLocale() { userLocale = LocaleUtil.getUserLocale(); LocaleUtil.setUserLocale(Locale.ROOT); } - + @AfterClass public static void resetLocale() { LocaleUtil.setUserLocale(userLocale); } - + protected BaseTestSheetAutosizeColumn(ITestDataProvider testDataProvider) { _testDataProvider = testDataProvider; } - + protected void trackColumnsForAutoSizingIfSXSSF(Sheet sheet) { // do nothing for Sheet base class. This will be overridden for SXSSFSheets. } @@ -98,7 +99,7 @@ public abstract class BaseTestSheetAutosizeColumn { assertEquals(sheet.getColumnWidth(1), sheet.getColumnWidth(2)); // columns 1, 2 and 3 should have the same width assertEquals(sheet.getColumnWidth(2), sheet.getColumnWidth(3)); // columns 1, 2 and 3 should have the same width assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(5)); // 10.0000 and '10.0000' - + workbook.close(); } @@ -196,7 +197,7 @@ public abstract class BaseTestSheetAutosizeColumn { assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(3)); // 'mmm/dd/yyyy' is wider than 'mmm' assertEquals(sheet.getColumnWidth(6), sheet.getColumnWidth(5)); // date formatted as 'mmm/dd/yyyy' assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(7)); // date formula formatted as 'mmm' - + workbook.close(); } @@ -206,7 +207,7 @@ public abstract class BaseTestSheetAutosizeColumn { Sheet sheet = workbook.createSheet(); trackColumnsForAutoSizingIfSXSSF(sheet); Row row = sheet.createRow(0); - + Font defaultFont = workbook.getFontAt(0); CellStyle style1 = workbook.createCellStyle(); @@ -230,7 +231,7 @@ public abstract class BaseTestSheetAutosizeColumn { assertTrue(2*sheet.getColumnWidth(1) < sheet.getColumnWidth(2)); assertEquals(sheet.getColumnWidth(4), sheet.getColumnWidth(3)); assertTrue(sheet.getColumnWidth(5) > sheet.getColumnWidth(4)); //larger font results in a wider column width - + workbook.close(); } @@ -257,7 +258,7 @@ public abstract class BaseTestSheetAutosizeColumn { int w1 = sheet.getColumnWidth(1); assertTrue(w0*5 < w1); // rotated text occupies at least five times less horizontal space than normal text - + workbook.close(); } @@ -280,7 +281,7 @@ public abstract class BaseTestSheetAutosizeColumn { sheet.autoSizeColumn(0, true); assertTrue(sheet.getColumnWidth(0) > defaulWidth); - + workbook.close(); } @@ -293,40 +294,40 @@ public abstract class BaseTestSheetAutosizeColumn { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); trackColumnsForAutoSizingIfSXSSF(sheet); - + Row r0 = sheet.createRow(0); r0.createCell(0).setCellValue("I am ROW 0"); Row r200 = sheet.createRow(200); r200.createCell(0).setCellValue("I am ROW 200"); - + // This should work fine sheet.autoSizeColumn(0); - + // Get close to 32767 Row r32765 = sheet.createRow(32765); r32765.createCell(0).setCellValue("Nearly there..."); sheet.autoSizeColumn(0); - + // To it Row r32767 = sheet.createRow(32767); r32767.createCell(0).setCellValue("At the boundary"); sheet.autoSizeColumn(0); - + // And passed it Row r32768 = sheet.createRow(32768); r32768.createCell(0).setCellValue("Passed"); Row r32769 = sheet.createRow(32769); r32769.createCell(0).setCellValue("More Passed"); sheet.autoSizeColumn(0); - + // Long way passed Row r60708 = sheet.createRow(60708); r60708.createCell(0).setCellValue("Near the end"); sheet.autoSizeColumn(0); - + workbook.close(); } - + // TODO should we have this stuff in the FormulaEvaluator? private void evaluateWorkbook(Workbook workbook){ FormulaEvaluator eval = workbook.getCreationHelper().createFormulaEvaluator(); @@ -341,4 +342,22 @@ public abstract class BaseTestSheetAutosizeColumn { } } } + + + @Test + public void testExcelExporter() { + final Workbook wb = _testDataProvider.createWorkbook(); + final Sheet sheet = wb.createSheet("test"); + trackColumnsForAutoSizingIfSXSSF(sheet); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); + + CellStyle csDateTime = wb.createCellStyle(); + csDateTime.setAlignment(HorizontalAlignment.LEFT); + + cell.setCellValue(new Date(Long.parseLong("1439800763994"))); + cell.setCellStyle(csDateTime); + + sheet.autoSizeColumn(0); + } } \ No newline at end of file