From d1a21260ca0ee0ca110a0f320fb1bd542c0a24fb Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Mon, 17 Jun 2013 09:06:39 +0000 Subject: [PATCH] Bug 54607: Add unit test and fix NPE for getTopRow() and getLeftCol() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1493686 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFSheet.java | 6 ++ .../poi/xssf/usermodel/TestXSSFSheet.java | 81 +++++++++++++++++- test-data/spreadsheet/54607.xlsx | Bin 0 -> 8532 bytes 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 test-data/spreadsheet/54607.xlsx diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 1b0fc8d388..58b4aebd9e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -866,6 +866,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { public short getLeftCol() { String cellRef = worksheet.getSheetViews().getSheetViewArray(0).getTopLeftCell(); + if(cellRef == null) { + return 0; + } CellReference cellReference = new CellReference(cellRef); return cellReference.getCol(); } @@ -1178,6 +1181,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ public short getTopRow() { String cellRef = getSheetTypeSheetView().getTopLeftCell(); + if(cellRef == null) { + return 0; + } CellReference cellReference = new CellReference(cellRef); return (short) cellReference.getRow(); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index e47632acf9..8c96fb817a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -17,18 +17,29 @@ package org.apache.poi.xssf.usermodel; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.List; -import org.apache.poi.ss.usermodel.*; +import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.record.PasswordRecord; +import org.apache.poi.ss.usermodel.BaseTestSheet; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CreationHelper; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.util.HexDump; import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.model.CalculationChain; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.StylesTable; -import org.apache.poi.xssf.model.CalculationChain; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.helpers.ColumnHelper; -import org.apache.poi.util.HexDump; -import org.apache.poi.hssf.record.PasswordRecord; import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; @@ -1091,4 +1102,66 @@ public final class TestXSSFSheet extends BaseTestSheet { sheet = workbook.getSheet("Sheet 1"); assertEquals(false, sheet.getForceFormulaRecalculation()); } + + public void test54607() { + // run with the file provided in the Bug-Report + runGetTopRow("54607.xlsx", true, 1, 0, 0); + runGetLeftCol("54607.xlsx", true, 0, 0, 0); + + // run with some other flie to see + runGetTopRow("54436.xlsx", true, 0); + runGetLeftCol("54436.xlsx", true, 0); + runGetTopRow("TwoSheetsNoneHidden.xlsx", true, 0, 0); + runGetLeftCol("TwoSheetsNoneHidden.xlsx", true, 0, 0); + runGetTopRow("TwoSheetsNoneHidden.xls", false, 0, 0); + runGetLeftCol("TwoSheetsNoneHidden.xls", false, 0, 0); + } + + private void runGetTopRow(String file, boolean isXSSF, int... topRows) { + final Workbook wb; + if(isXSSF) { + wb = XSSFTestDataSamples.openSampleWorkbook(file); + } else { + wb = HSSFTestDataSamples.openSampleWorkbook(file); + } + for (int si = 0; si < wb.getNumberOfSheets(); si++) { + Sheet sh = wb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); + } + + // for XSSF also test with SXSSF + if(isXSSF) { + Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); + for (int si = 0; si < swb.getNumberOfSheets(); si++) { + Sheet sh = swb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getTopRow()); + } + } + } + + private void runGetLeftCol(String file, boolean isXSSF, int... topRows) { + final Workbook wb; + if(isXSSF) { + wb = XSSFTestDataSamples.openSampleWorkbook(file); + } else { + wb = HSSFTestDataSamples.openSampleWorkbook(file); + } + for (int si = 0; si < wb.getNumberOfSheets(); si++) { + Sheet sh = wb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); + } + + // for XSSF also test with SXSSF + if(isXSSF) { + Workbook swb = new SXSSFWorkbook((XSSFWorkbook) wb); + for (int si = 0; si < swb.getNumberOfSheets(); si++) { + Sheet sh = swb.getSheetAt(si); + assertNotNull(sh.getSheetName()); + assertEquals("Did not match for sheet " + si, topRows[si], sh.getLeftCol()); + } + } + } } diff --git a/test-data/spreadsheet/54607.xlsx b/test-data/spreadsheet/54607.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..bdfe143bb3be84b388ea778715c52d95af9bf809 GIT binary patch literal 8532 zcmeHM^;?wd)*e6_q!AckKvHrjK?&(j=~6mmfI&J1B!*HNgaM=kK_nEUJESC~ySqET z(S7!DZ?@<9{(y6yAKvGh>z#Gayzjd2wbrwisshR_VgMQd0{{Tf0*;2Vw+)a005Vhn zfCzwrtS@12=VWf@^i;zgYVK&r;bse=$+(5g{1$+W`2PRafAJgWitSWx0^>^^%U--( zWS5^H#iSklj7_b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