From ce1f30e57f21c03df2694d9139c8113afa5df5ca Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 10 Jun 2016 01:52:08 +0000 Subject: [PATCH] bug 57840: check for null to avoid NPE; add unit test for XSSFWorkbook.getTable() git-svn-id: https://svn.apache.org/repos/asf/poi/branches/xssf_structured_references@1747621 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/HSSFEvaluationWorkbook.java | 5 +++ .../usermodel/BaseXSSFEvaluationWorkbook.java | 5 ++- .../poi/xssf/usermodel/XSSFWorkbook.java | 2 +- .../poi/xssf/usermodel/TestXSSFSheet.java | 2 +- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 32 +++++++++++++++++++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java index 1d9459a71a..037d55d9f0 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java @@ -265,10 +265,15 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E return extIx; } + @Override public SpreadsheetVersion getSpreadsheetVersion(){ return SpreadsheetVersion.EXCEL97; } + /** + * @throws IllegalStateException: data tables are not supported in Excel 97-2003 format + */ + @Override public Table getTable(String name) { throw new IllegalStateException("XSSF-style tables are not supported for HSSF"); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java index 41dd05c6c3..8ef2c54392 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/BaseXSSFEvaluationWorkbook.java @@ -353,8 +353,11 @@ public abstract class BaseXSSFEvaluationWorkbook implements FormulaRenderingWork * @return The Data table in the workbook named name, or null if no table is named name. * @since 3.15 beta 2 */ + @Override public XSSFTable getTable(String name) { - return getTableCache().get(name.toLowerCase(Locale.ROOT)); + if (name == null) return null; + String lname = name.toLowerCase(Locale.ROOT); + return getTableCache().get(lname); } public UDFFinder getUDFFinder(){ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 1dcb3c42f2..74974fe7d2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -2271,7 +2271,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { * @since 3.15 beta 2 */ public XSSFTable getTable(String name) { - if (sheets != null) { + if (name != null && sheets != null) { for (XSSFSheet sheet : sheets) { for (XSSFTable tbl : sheet.getTables()) { if (name.equalsIgnoreCase(tbl.getName())) { 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 6b1eebe652..868557ae15 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -1137,7 +1137,7 @@ public final class TestXSSFSheet extends BaseTestXSheet { } /** - * See bug #50829 + * See bug #50829 test data tables */ @Test public void tables() throws IOException { 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 6e642ccca8..f4a4687522 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -1107,4 +1107,36 @@ public final class TestXSSFWorkbook extends BaseTestXWorkbook { assertTrue("Had: " + e.getCause(), e.getCause() instanceof IOException); } } + + /** + * See bug #57840 test data tables + */ + @Test + public void getTable() throws IOException { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTable.xlsx"); + XSSFTable table1 = wb.getTable("Tabella1"); + assertNotNull("Tabella1 was not found in workbook", table1); + assertEquals("Table name", "Tabella1", table1.getName()); + assertEquals("Sheet name", "Foglio1", table1.getSheetName()); + + // Table lookup should be case-insensitive + assertSame("Case insensitive table name lookup", table1, wb.getTable("TABELLA1")); + + // If workbook does not contain any data tables matching the provided name, getTable should return null + assertNull("Null table name should not throw NPE", wb.getTable(null)); + assertNull("Should not be able to find non-existent table", wb.getTable("Foglio1")); + + // If a table is added after getTable is called it should still be reachable by XSSFWorkbook.getTable + // This test makes sure that if any caching is done that getTable never uses a stale cache + XSSFTable table2 = wb.getSheet("Foglio2").createTable(); + table2.setName("Table2"); + assertSame("Did not find Table2", table2, wb.getTable("Table2")); + + // If table name is modified after getTable is called, the table can only be found by its new name + // This test makes sure that if any caching is done that getTable never uses a stale cache + table1.setName("Table1"); + assertSame("Did not find Tabella1 renamed to Table1", table1, wb.getTable("TABLE1")); + + wb.close(); + } } -- 2.39.5