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
This commit is contained in:
Dominik Stadler 2018-12-25 10:00:07 +00:00
parent 19884d0ca3
commit 49f4ad6aba
3 changed files with 64 additions and 1 deletions

View File

@ -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);

View File

@ -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
}
}
}

View File

@ -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();
}