From 49f4ad6aba1afdcdf95845165501d66a87d429ec Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 25 Dec 2018 10:00:07 +0000 Subject: [PATCH] Bug 62884: sheetnum is not checked in InternalWorkbook.setSheetHidden() git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1849717 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/model/InternalWorkbook.java | 2 + .../poi/hssf/usermodel/TestSheetHiding.java | 42 +++++++++++++++++++ .../poi/ss/usermodel/BaseTestSheetHiding.java | 21 +++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index da9ecab337..5fa2d315e2 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -728,6 +728,8 @@ public final class InternalWorkbook { * @since 3.16 beta 2 */ public void setSheetHidden(int sheetnum, SheetVisibility visibility) { + checkSheets(sheetnum); + BoundSheetRecord bsr = getBoundSheetRec(sheetnum); bsr.setHidden(visibility == SheetVisibility.HIDDEN); bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java index ef70b1325b..f9d172310b 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestSheetHiding.java @@ -18,11 +18,53 @@ package org.apache.poi.hssf.usermodel; import org.apache.poi.hssf.HSSFITestDataProvider; +import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.ss.usermodel.BaseTestSheetHiding; +import org.apache.poi.ss.usermodel.SheetVisibility; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public final class TestSheetHiding extends BaseTestSheetHiding { public TestSheetHiding() { super(HSSFITestDataProvider.instance, "TwoSheetsOneHidden.xls", "TwoSheetsNoneHidden.xls"); } + + @Test + public void testInternalWorkbookHidden() { + HSSFWorkbook wb = new HSSFWorkbook(); + wb.createSheet("MySheet"); + InternalWorkbook intWb = wb.getWorkbook(); + + assertFalse(intWb.isSheetHidden(0)); + assertFalse(intWb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.VISIBLE, intWb.getSheetVisibility(0)); + + intWb.setSheetHidden(0, SheetVisibility.HIDDEN); + assertTrue(intWb.isSheetHidden(0)); + assertFalse(intWb.isSheetVeryHidden(0)); + assertEquals(SheetVisibility.HIDDEN, intWb.getSheetVisibility(0)); + + // InternalWorkbook currently behaves slightly different + // than HSSFWorkbook, but it should not have effect in normal usage + // as checked limits are more strict in HSSFWorkbook + + // check sheet-index with one more will work and add the sheet + intWb.setSheetHidden(1, SheetVisibility.HIDDEN); + assertTrue(intWb.isSheetHidden(1)); + assertFalse(intWb.isSheetVeryHidden(1)); + assertEquals(SheetVisibility.HIDDEN, intWb.getSheetVisibility(1)); + + // check sheet-index with index out of bounds => throws exception + try { + wb.setSheetVisibility(10, SheetVisibility.HIDDEN); + fail("Should catch exception here"); + } catch (RuntimeException e) { + // expected here + } + } } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java index 2bce1c05d9..1611f95a8e 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetHiding.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.IOException; @@ -81,7 +82,25 @@ public abstract class BaseTestSheetHiding { assertFalse(wb.isSheetHidden(0)); assertFalse(wb.isSheetVeryHidden(0)); assertEquals(SheetVisibility.VISIBLE, wb.getSheetVisibility(0)); - + + // verify limits-check + + // check sheet-index with one more => throws exception + try { + wb.setSheetVisibility(1, SheetVisibility.HIDDEN); + fail("Should catch exception here"); + } catch (IllegalArgumentException e) { + // expected here + } + + // check sheet-index with index out of bounds => throws exception + try { + wb.setSheetVisibility(10, SheetVisibility.HIDDEN); + fail("Should catch exception here"); + } catch (IllegalArgumentException e) { + // expected here + } + wb.close(); } -- 2.39.5