From cc8c15ca6c7a3c8d60c61d521da09462363e2c3e Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 4 Aug 2010 14:40:54 +0000 Subject: [PATCH] Fix bug #49702 - Correct XSSFWorkbook.getNumCellStyles to check the right styles list git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@982269 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xssf/model/StylesTable.java | 4 +- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 53 ++++++++++++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index effa0239f0..d5e39d3918 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49702 - Correct XSSFWorkbook.getNumCellStyles to check the right styles list 49690 - Add WorkbookUtil, which provies a way of generating valid sheet names 49694 - Use DataFormatter when autosizing columns, to better match the real display width of formatted cells 49441 - Allow overriding and guessing of HSMF non-unicode string encodings diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 98c3c5f18d..0230d660fd 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -280,7 +280,9 @@ public class StylesTable extends POIXMLDocumentPart { * get the size of cell styles */ public int getNumCellStyles(){ - return styleXfs.size(); + // Each cell style has a unique xfs entry + // Several might share the same styleXfs entry + return xfs.size(); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 5ec6257d7c..473b189239 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -271,7 +271,7 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { * Verify that the attached test data was not modified. If this test method * fails, the test data is not working properly. */ - public void test47668() throws Exception { + public void testBug47668() throws Exception { XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx"); List allPictures = workbook.getAllPictures(); assertEquals(2, allPictures.size()); @@ -354,4 +354,55 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { assertEquals("Numbers", wb.getSheetName(0)); assertEquals("Chart", wb.getSheetName(1)); } + + /** + * Problems with the count of the number of styles + * coming out wrong + */ + public void testBug49702() throws Exception { + // First try with a new file + XSSFWorkbook wb = new XSSFWorkbook(); + + // Should have one style + assertEquals(1, wb.getNumCellStyles()); + wb.getCellStyleAt((short)0); + try { + wb.getCellStyleAt((short)1); + fail("Shouldn't be able to get style at 1 that doesn't exist"); + } catch(IndexOutOfBoundsException e) {} + + // Add another one + CellStyle cs = wb.createCellStyle(); + cs.setDataFormat((short)11); + + // Re-check + assertEquals(2, wb.getNumCellStyles()); + wb.getCellStyleAt((short)0); + wb.getCellStyleAt((short)1); + try { + wb.getCellStyleAt((short)2); + fail("Shouldn't be able to get style at 2 that doesn't exist"); + } catch(IndexOutOfBoundsException e) {} + + // Save and reload + XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb); + assertEquals(2, nwb.getNumCellStyles()); + nwb.getCellStyleAt((short)0); + nwb.getCellStyleAt((short)1); + try { + nwb.getCellStyleAt((short)2); + fail("Shouldn't be able to get style at 2 that doesn't exist"); + } catch(IndexOutOfBoundsException e) {} + + // Now with an existing file + wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); + assertEquals(3, wb.getNumCellStyles()); + wb.getCellStyleAt((short)0); + wb.getCellStyleAt((short)1); + wb.getCellStyleAt((short)2); + try { + wb.getCellStyleAt((short)3); + fail("Shouldn't be able to get style at 3 that doesn't exist"); + } catch(IndexOutOfBoundsException e) {} + } } -- 2.39.5