Add more unit tests to cover more of HSSFWorkbook and HSSFSheet, check one more case of IndexOutOfBoundsException

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1502661 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-07-12 19:21:12 +00:00
parent d6da9dd94b
commit 97d6444cb0
4 changed files with 336 additions and 35 deletions

View File

@ -417,7 +417,7 @@ final class LinkTable {
}
public int getSheetIndexFromExternSheetIndex(int extRefIndex) {
if (extRefIndex >= _externSheetRecord.getNumOfRefs()) {
if (extRefIndex >= _externSheetRecord.getNumOfRefs() || extRefIndex < 0) {
return -1;
}
return _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);

View File

@ -40,8 +40,11 @@ import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
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.ss.util.CellRangeAddressList;
import org.apache.poi.ss.util.Region;
import org.apache.poi.util.TempFile;
/**
@ -84,22 +87,27 @@ public final class TestHSSFSheet extends BaseTestSheet {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
GridsetRecord gridsetRec = s.getSheet().getGridsetRecord();
assertEquals(true, gridsetRec.getGridset());
assertTrue(gridsetRec.getGridset());
s.setGridsPrinted(true);
assertEquals(false, gridsetRec.getGridset());
assertFalse(gridsetRec.getGridset());
}
/**
* Test vertically centered output.
*/
@SuppressWarnings("deprecation")
public void testVerticallyCenter() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
VCenterRecord record = s.getSheet().getPageSettings().getVCenter();
assertEquals(false, record.getVCenter());
assertFalse(record.getVCenter());
assertFalse(s.getVerticallyCenter(true));
assertFalse(s.getVerticallyCenter(false));
s.setVerticallyCenter(true);
assertEquals(true, record.getVCenter());
assertTrue(record.getVCenter());
assertTrue(s.getVerticallyCenter(true));
assertTrue(s.getVerticallyCenter(false));
// wb.write(new FileOutputStream("c:\\test.xls"));
}
@ -112,9 +120,9 @@ public final class TestHSSFSheet extends BaseTestSheet {
HSSFSheet s = wb.createSheet();
HCenterRecord record = s.getSheet().getPageSettings().getHCenter();
assertEquals(false, record.getHCenter());
assertFalse(record.getHCenter());
s.setHorizontallyCenter(true);
assertEquals(true, record.getHCenter());
assertTrue(record.getHCenter());
}
@ -128,14 +136,14 @@ public final class TestHSSFSheet extends BaseTestSheet {
(WSBoolRecord) s.getSheet().findFirstRecordBySid(WSBoolRecord.sid);
// Check defaults
assertEquals(true, record.getAlternateExpression());
assertEquals(true, record.getAlternateFormula());
assertEquals(false, record.getAutobreaks());
assertEquals(false, record.getDialog());
assertEquals(false, record.getDisplayGuts());
assertEquals(true, record.getFitToPage());
assertEquals(false, record.getRowSumsBelow());
assertEquals(false, record.getRowSumsRight());
assertTrue(record.getAlternateExpression());
assertTrue(record.getAlternateFormula());
assertFalse(record.getAutobreaks());
assertFalse(record.getDialog());
assertFalse(record.getDisplayGuts());
assertTrue(record.getFitToPage());
assertFalse(record.getRowSumsBelow());
assertFalse(record.getRowSumsRight());
// Alter
s.setAlternativeExpression(false);
@ -148,22 +156,22 @@ public final class TestHSSFSheet extends BaseTestSheet {
s.setRowSumsRight(true);
// Check
assertEquals(true, record.getAlternateExpression()); //sheet.setRowSumsBelow alters this field too
assertEquals(false, record.getAlternateFormula());
assertEquals(true, record.getAutobreaks());
assertEquals(true, record.getDialog());
assertEquals(true, record.getDisplayGuts());
assertEquals(false, record.getFitToPage());
assertEquals(true, record.getRowSumsBelow());
assertEquals(true, record.getRowSumsRight());
assertEquals(true, s.getAlternateExpression());
assertEquals(false, s.getAlternateFormula());
assertEquals(true, s.getAutobreaks());
assertEquals(true, s.getDialog());
assertEquals(true, s.getDisplayGuts());
assertEquals(false, s.getFitToPage());
assertEquals(true, s.getRowSumsBelow());
assertEquals(true, s.getRowSumsRight());
assertTrue(record.getAlternateExpression()); //sheet.setRowSumsBelow alters this field too
assertFalse(record.getAlternateFormula());
assertTrue(record.getAutobreaks());
assertTrue(record.getDialog());
assertTrue(record.getDisplayGuts());
assertFalse(record.getFitToPage());
assertTrue(record.getRowSumsBelow());
assertTrue(record.getRowSumsRight());
assertTrue(s.getAlternateExpression());
assertFalse(s.getAlternateFormula());
assertTrue(s.getAutobreaks());
assertTrue(s.getDialog());
assertTrue(s.getDisplayGuts());
assertFalse(s.getFitToPage());
assertTrue(s.getRowSumsBelow());
assertTrue(s.getRowSumsRight());
}
/**
@ -611,8 +619,8 @@ public final class TestHSSFSheet extends BaseTestSheet {
r.getCell(0).setCellStyle(cs);
r.getCell(1).setCellStyle(cs);
assertEquals(true, DateUtil.isCellDateFormatted(r.getCell(0)));
assertEquals(true, DateUtil.isCellDateFormatted(r.getCell(1)));
assertTrue(DateUtil.isCellDateFormatted(r.getCell(0)));
assertTrue(DateUtil.isCellDateFormatted(r.getCell(1)));
// Should get much bigger now
s.autoSizeColumn((short)0);
@ -898,9 +906,9 @@ public final class TestHSSFSheet extends BaseTestSheet {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
assertEquals(false, s.isRightToLeft());
assertFalse(s.isRightToLeft());
s.setRightToLeft(true);
assertEquals(true, s.isRightToLeft());
assertTrue(s.isRightToLeft());
}
public void testAutoFilter(){
@ -951,4 +959,70 @@ public final class TestHSSFSheet extends BaseTestSheet {
assertTrue(subRecords.get(1) instanceof FtCblsSubRecord ); // must be present, see Bug 51481
assertTrue(subRecords.get(2) instanceof LbsDataSubRecord );
}
public void testGetSetColumnHiddenShort() {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet 1");
sheet.setColumnHidden((short)2, true);
assertTrue(sheet.isColumnHidden((short)2));
}
public void testColumnWidthShort() {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
//default column width measured in characters
sheet.setDefaultColumnWidth((short)10);
assertEquals(10, sheet.getDefaultColumnWidth());
//columns A-C have default width
assertEquals(256*10, sheet.getColumnWidth((short)0));
assertEquals(256*10, sheet.getColumnWidth((short)1));
assertEquals(256*10, sheet.getColumnWidth((short)2));
//set custom width for D-F
for (char i = 'D'; i <= 'F'; i++) {
//Sheet#setColumnWidth accepts the width in units of 1/256th of a character width
int w = 256*12;
sheet.setColumnWidth((short)i, w);
assertEquals(w, sheet.getColumnWidth((short)i));
}
//reset the default column width, columns A-C change, D-F still have custom width
sheet.setDefaultColumnWidth((short)20);
assertEquals(20, sheet.getDefaultColumnWidth());
assertEquals(256*20, sheet.getColumnWidth((short)0));
assertEquals(256*20, sheet.getColumnWidth((short)1));
assertEquals(256*20, sheet.getColumnWidth((short)2));
for (char i = 'D'; i <= 'F'; i++) {
int w = 256*12;
assertEquals(w, sheet.getColumnWidth((short)i));
}
// check for 16-bit signed/unsigned error:
sheet.setColumnWidth((short)10, 40000);
assertEquals(40000, sheet.getColumnWidth((short)10));
//The maximum column width for an individual cell is 255 characters
try {
sheet.setColumnWidth((short)9, 256*256);
fail("expected exception");
} catch(IllegalArgumentException e){
assertEquals("The maximum column width for an individual cell is 255 characters.", e.getMessage());
}
//serialize and read again
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sheet = wb.getSheetAt(0);
assertEquals(20, sheet.getDefaultColumnWidth());
//columns A-C have default width
assertEquals(256*20, sheet.getColumnWidth((short)0));
assertEquals(256*20, sheet.getColumnWidth((short)1));
assertEquals(256*20, sheet.getColumnWidth((short)2));
//columns D-F have custom width
for (char i = 'D'; i <= 'F'; i++) {
short w = (256*12);
assertEquals(w, sheet.getColumnWidth((short)i));
}
assertEquals(40000, sheet.getColumnWidth((short)10));
}
}

View File

@ -175,6 +175,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals(8, s.getLastRowNum());
}
@SuppressWarnings("deprecation")
public void testSelectedSheet_bug44523() {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
@ -189,6 +190,9 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
wb.setSelectedTab(1);
// see Javadoc, in this case selected means "active"
assertEquals(wb.getActiveSheetIndex(), wb.getSelectedTab());
// Demonstrate bug 44525:
// Well... not quite, since isActive + isSelected were also added in the same bug fix
if (sheet1.isSelected()) {
@ -708,4 +712,169 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
changeSheetNameWithSharedFormulas("shared_formulas.xls");
}
public void testEmptyDirectoryNode() throws IOException {
try {
assertNotNull(new HSSFWorkbook(new POIFSFileSystem()));
fail("Should catch exception about invalid POIFSFileSystem");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("does not contain a BIFF8"));
}
}
@SuppressWarnings("deprecation")
public void testSelectedSheetShort() {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
HSSFSheet sheet2 = wb.createSheet("Sheet2");
HSSFSheet sheet3 = wb.createSheet("Sheet3");
HSSFSheet sheet4 = wb.createSheet("Sheet4");
confirmActiveSelected(sheet1, true);
confirmActiveSelected(sheet2, false);
confirmActiveSelected(sheet3, false);
confirmActiveSelected(sheet4, false);
wb.setSelectedTab((short)1);
// see Javadoc, in this case selected means "active"
assertEquals(wb.getActiveSheetIndex(), wb.getSelectedTab());
// Demonstrate bug 44525:
// Well... not quite, since isActive + isSelected were also added in the same bug fix
if (sheet1.isSelected()) {
throw new AssertionFailedError("Identified bug 44523 a");
}
wb.setActiveSheet(1);
if (sheet1.isActive()) {
throw new AssertionFailedError("Identified bug 44523 b");
}
confirmActiveSelected(sheet1, false);
confirmActiveSelected(sheet2, true);
confirmActiveSelected(sheet3, false);
confirmActiveSelected(sheet4, false);
assertEquals(0, wb.getFirstVisibleTab());
wb.setDisplayedTab((short)2);
assertEquals(2, wb.getFirstVisibleTab());
assertEquals(2, wb.getDisplayedTab());
}
public void testAddSheetTwice() {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
assertNotNull(sheet1);
try {
wb.createSheet("Sheet1");
fail("Should fail if we add the same sheet twice");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("already contains a sheet of this name"));
}
}
public void testGetSheetIndex() {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
HSSFSheet sheet2 = wb.createSheet("Sheet2");
HSSFSheet sheet3 = wb.createSheet("Sheet3");
HSSFSheet sheet4 = wb.createSheet("Sheet4");
assertEquals(0, wb.getSheetIndex(sheet1));
assertEquals(1, wb.getSheetIndex(sheet2));
assertEquals(2, wb.getSheetIndex(sheet3));
assertEquals(3, wb.getSheetIndex(sheet4));
// remove sheets
wb.removeSheetAt(0);
wb.removeSheetAt(2);
// ensure that sheets are moved up and removed sheets are not found any more
assertEquals(-1, wb.getSheetIndex(sheet1));
assertEquals(0, wb.getSheetIndex(sheet2));
assertEquals(1, wb.getSheetIndex(sheet3));
assertEquals(-1, wb.getSheetIndex(sheet4));
}
@SuppressWarnings("deprecation")
public void testGetExternSheetIndex() {
HSSFWorkbook wb=new HSSFWorkbook();
wb.createSheet("Sheet1");
wb.createSheet("Sheet2");
assertEquals(0, wb.getExternalSheetIndex(0));
assertEquals(1, wb.getExternalSheetIndex(1));
assertEquals("Sheet1", wb.findSheetNameFromExternSheet(0));
assertEquals("Sheet2", wb.findSheetNameFromExternSheet(1));
//assertEquals(null, wb.findSheetNameFromExternSheet(2));
assertEquals(0, wb.getSheetIndexFromExternSheetIndex(0));
assertEquals(1, wb.getSheetIndexFromExternSheetIndex(1));
assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(2));
assertEquals(-1, wb.getSheetIndexFromExternSheetIndex(100));
}
@SuppressWarnings("deprecation")
public void testSSTString() {
HSSFWorkbook wb=new HSSFWorkbook();
int sst = wb.addSSTString("somestring");
assertEquals("somestring", wb.getSSTString(sst));
//assertNull(wb.getSSTString(sst+1));
}
public void testNames() {
HSSFWorkbook wb=new HSSFWorkbook();
try {
wb.getNameAt(0);
fail("Fails without any defined names");
} catch (IllegalStateException e) {
assertTrue(e.getMessage(), e.getMessage().contains("no defined names"));
}
HSSFName name = wb.createName();
assertNotNull(name);
assertNull(wb.getName("somename"));
name.setNameName("myname");
assertNotNull(wb.getName("myname"));
assertEquals(0, wb.getNameIndex(name));
assertEquals(0, wb.getNameIndex("myname"));
try {
wb.getNameAt(5);
fail("Fails without any defined names");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("outside the allowable range"));
}
try {
wb.getNameAt(-3);
fail("Fails without any defined names");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage(), e.getMessage().contains("outside the allowable range"));
}
}
public void testTestMethods() {
HSSFWorkbook wb=new HSSFWorkbook();
wb.insertChartRecord();
//wb.dumpDrawingGroupRecords(true);
//wb.dumpDrawingGroupRecords(false);
}
public void testWriteProtection() {
HSSFWorkbook wb=new HSSFWorkbook();
assertFalse(wb.isWriteProtected());
wb.writeProtectWorkbook("mypassword", "myuser");
assertTrue(wb.isWriteProtected());
wb.unwriteProtectWorkbook();
assertFalse(wb.isWriteProtected());
}
}

View File

@ -32,6 +32,7 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.Region;
import org.apache.poi.util.TempFile;
/**
@ -453,8 +454,13 @@ public final class TestWorkbook extends TestCase {
BackupRecord record = workbook.getBackupRecord();
assertEquals(0, record.getBackup());
assertFalse(wb.getBackupFlag());
wb.setBackupFlag(true);
assertEquals(1, record.getBackup());
assertTrue(wb.getBackupFlag());
wb.setBackupFlag(false);
assertEquals(0, record.getBackup());
assertFalse(wb.getBackupFlag());
}
private static final class RecordCounter implements RecordVisitor {
@ -545,4 +551,56 @@ public final class TestWorkbook extends TestCase {
assertTrue("file exists",file.exists());
}
@SuppressWarnings("deprecation")
public void testRepeatingColsRowsMinusOne() throws IOException
{
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Test Print Titles");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("hi"));
workbook.setRepeatingRowsAndColumns(0, -1, 1, -1, 0);
File file = TempFile.createTempFile("testPrintTitlesA",".xls");
FileOutputStream fileOut = new FileOutputStream(file);
workbook.write(fileOut);
fileOut.close();
assertTrue("file exists",file.exists());
}
@SuppressWarnings("deprecation")
public void testAddMergedRegionWithRegion() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
for (int rownum = 0; rownum < 100; rownum++) {
HSSFRow r = s.createRow(rownum);
for (int cellnum = 0; cellnum < 50; cellnum += 2) {
HSSFCell c = r.createCell(cellnum);
c.setCellValue(rownum * 10000 + cellnum
+ ((( double ) rownum / 1000)
+ (( double ) cellnum / 10000)));
c = r.createCell(cellnum + 1);
c.setCellValue(new HSSFRichTextString("TEST"));
}
}
s.addMergedRegion(new Region(0, (short)0, 10, (short)10));
s.addMergedRegion(new Region(30, (short)5, 40, (short)15));
sanityChecker.checkHSSFWorkbook(wb);
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
CellRangeAddress r1 = s.getMergedRegion(0);
CellRangeAddress r2 = s.getMergedRegion(1);
confirmRegion(new CellRangeAddress(0, 10, 0, 10), r1);
confirmRegion(new CellRangeAddress(30, 40,5, 15), r2);
}
}