Throw an IllegalArgumentException if asked to addMergeRegion with invalid data, rather than writing out a corrupt file (bug #43807)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@594320 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2007-11-12 22:24:15 +00:00
parent c1a4f1c8e8
commit eb65900774
4 changed files with 51 additions and 35 deletions

View File

@ -36,6 +36,7 @@
<!-- Don't forget to update status.xml too! -->
<release version="3.0.2-FINAL" date="2007-??-??">
<action dev="POI-DEVELOPERS" type="fix">43807 - Throw an IllegalArgumentException if asked to create a merged region with invalid columns or rows, rather than writing out a corrupt file</action>
<action dev="POI-DEVELOPERS" type="fix">43837 - [PATCH] Support for unicode NameRecords</action>
<action dev="POI-DEVELOPERS" type="add">43721 - [PATCH] Support for Chart Title Format records</action>
<action dev="POI-DEVELOPERS" type="fix">42794 - [PATCH] Fix for BOF records from things like Access</action>

View File

@ -33,6 +33,7 @@
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.0.2-FINAL" date="2007-??-??">
<action dev="POI-DEVELOPERS" type="fix">43807 - Throw an IllegalArgumentException if asked to create a merged region with invalid columns or rows, rather than writing out a corrupt file</action>
<action dev="POI-DEVELOPERS" type="fix">43837 - [PATCH] Support for unicode NameRecords</action>
<action dev="POI-DEVELOPERS" type="add">43721 - [PATCH] Support for Chart Title Format records</action>
<action dev="POI-DEVELOPERS" type="fix">42794 - [PATCH] Fix for BOF records from things like Access</action>

View File

@ -497,6 +497,14 @@ public class Sheet implements Model
public int addMergedRegion(int rowFrom, short colFrom, int rowTo,
short colTo)
{
// Validate input
if(rowTo < rowFrom) {
throw new IllegalArgumentException("The row to ("+rowTo+") must be >= the row from ("+rowFrom+")");
}
if(colTo < colFrom) {
throw new IllegalArgumentException("The col to ("+colTo+") must be >= the col from ("+colFrom+")");
}
if (merged == null || merged.getNumAreas() == 1027)
{
merged = ( MergeCellsRecord ) createMergedCells();

View File

@ -33,13 +33,13 @@ import org.apache.poi.hssf.record.StringRecord;
/**
* @author Tony Poppleton
*/
public class SheetTest extends TestCase
public class TestSheetAdditional extends TestCase
{
/**
* Constructor for SheetTest.
* @param arg0
*/
public SheetTest(String arg0)
public TestSheetAdditional(String arg0)
{
super(arg0);
}
@ -66,6 +66,16 @@ public class SheetTest extends TestCase
if ((regionsToAdd % 1027) != 0)
recordsExpected++;
assertTrue("The " + regionsToAdd + " merged regions should have been spread out over " + recordsExpected + " records, not " + recordsAdded, recordsAdded == recordsExpected);
// Check we can't add one with invalud date
try {
sheet.addMergedRegion(10, (short)10, 9, (short)12);
fail();
} catch(IllegalArgumentException e) {}
try {
sheet.addMergedRegion(10, (short)10, 12, (short)9);
fail();
} catch(IllegalArgumentException e) {}
}
public void testRemoveMergedRegion()
@ -126,17 +136,18 @@ public class SheetTest extends TestCase
//TODO
}
public void testGetCellWidth()
public void DISBALEDtestGetCellWidth() throws Exception
{
try{
Sheet sheet = Sheet.createSheet();
ColumnInfoRecord nci = ( ColumnInfoRecord ) sheet.createColInfo();
//prepare test model
// Prepare test model
nci.setFirstColumn((short)5);
nci.setLastColumn((short)10);
nci.setColumnWidth((short)100);
Field f = Sheet.class.getDeclaredField("columnSizes");
Field f = null;
f = Sheet.class.getDeclaredField("columnSizes");
f.setAccessible(true);
List columnSizes = new ArrayList();
f.set(sheet,columnSizes);
@ -159,11 +170,6 @@ public class SheetTest extends TestCase
assertEquals((short)100,sheet.getColumnWidth((short)8));
assertEquals((short)100,sheet.getColumnWidth((short)9));
assertEquals((short)100,sheet.getColumnWidth((short)10));
}
catch(Exception e){e.printStackTrace();fail(e.getMessage());}
}
/**