]> source.dussan.org Git - poi.git/commitdiff
Bug 54607: Add unit test and fix NPE for getTopRow() and getLeftCol()
authorDominik Stadler <centic@apache.org>
Mon, 17 Jun 2013 09:06:39 +0000 (09:06 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 17 Jun 2013 09:06:39 +0000 (09:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1493686 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
test-data/spreadsheet/54607.xlsx [new file with mode: 0644]

index 1b0fc8d3883b928d5f480281d26a84a11bbb7b8a..58b4aebd9e73934484b0615e83de47a86012dc97 100644 (file)
@@ -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();
     }
index e47632acf9eb0f1b889e4fbaa37e11341cabb560..8c96fb817ae6258f3b9bab0b43e53579f4e5cb19 100644 (file)
 
 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 (file)
index 0000000..bdfe143
Binary files /dev/null and b/test-data/spreadsheet/54607.xlsx differ