You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2016/02/18 02:33:15 UTC
svn commit: r1730991 - in /poi/trunk/src:
java/org/apache/poi/hssf/usermodel/HSSFSheet.java
ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
Author: onealj
Date: Thu Feb 18 01:33:14 2016
New Revision: 1730991
URL: http://svn.apache.org/viewvc?rev=1730991&view=rev
Log:
bug 56345: reject single-cell merged regions
Modified:
poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java?rev=1730991&r1=1730990&r2=1730991&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java Thu Feb 18 01:33:14 2016
@@ -662,11 +662,15 @@ public final class HSSFSheet implements
*
* @param region (rowfrom/colfrom-rowto/colto) to merge
* @return index of this region
+ * @throws IllegalArgumentException if region contains fewer than 2 cells
* @throws IllegalStateException if region intersects with an existing merged region
* or multi-cell array formula on this sheet
*/
@Override
public int addMergedRegion(CellRangeAddress region) {
+ if (region.getNumberOfCells() < 2) {
+ throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells");
+ }
region.validate(SpreadsheetVersion.EXCEL97);
// throw IllegalStateException if the argument CellRangeAddress intersects with
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1730991&r1=1730990&r2=1730991&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Thu Feb 18 01:33:14 2016
@@ -301,6 +301,7 @@ public class XSSFSheet extends POIXMLDoc
*
* @param region to merge
* @return index of this region
+ * @throws IllegalArgumentException if region contains fewer than 2 cells
* @throws IllegalStateException if region intersects with a multi-cell array formula
* @throws IllegalStateException if region intersects with an existing region on this sheet
*/
@@ -320,6 +321,7 @@ public class XSSFSheet extends POIXMLDoc
*
* @param region to merge
* @return index of this region
+ * @throws IllegalArgumentException if region contains fewer than 2 cells
*/
public int addMergedRegionUnsafe(CellRangeAddress region) {
return addMergedRegion(region, false);
@@ -333,6 +335,7 @@ public class XSSFSheet extends POIXMLDoc
* @param region to merge
* @param validate whether to validate merged region
* @return index of this region
+ * @throws IllegalArgumentException if region contains fewer than 2 cells (this check is inexpensive and is performed regardless of <tt>validate</tt>)
* @throws IllegalStateException if region intersects with a multi-cell array formula
* @throws IllegalStateException if region intersects with an existing region on this sheet
*/
Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java?rev=1730991&r1=1730990&r2=1730991&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java Thu Feb 18 01:33:14 2016
@@ -274,37 +274,59 @@ public abstract class BaseTestSheet {
final Workbook wb = _testDataProvider.createWorkbook();
final Sheet sheet = wb.createSheet();
- final CellRangeAddress baseRegion = new CellRangeAddress(0, 1, 0, 1);
+ final CellRangeAddress baseRegion = new CellRangeAddress(0, 1, 0, 1); //A1:B2
sheet.addMergedRegion(baseRegion);
try {
- final CellRangeAddress duplicateRegion = new CellRangeAddress(0, 1, 0, 1);
+ final CellRangeAddress duplicateRegion = new CellRangeAddress(0, 1, 0, 1); //A1:B2
sheet.addMergedRegion(duplicateRegion);
- fail("Should not be able to add a merged region if sheet already contains the same merged region");
+ fail("Should not be able to add a merged region (" + duplicateRegion.formatAsString() + ") " +
+ "if sheet already contains the same merged region (" + baseRegion.formatAsString() + ")");
} catch (final IllegalStateException e) { } //expected
try {
- final CellRangeAddress partiallyOverlappingRegion = new CellRangeAddress(1, 2, 1, 2);
+ final CellRangeAddress partiallyOverlappingRegion = new CellRangeAddress(1, 2, 1, 2); //B2:C3
sheet.addMergedRegion(partiallyOverlappingRegion);
- fail("Should not be able to add a merged region if it partially overlaps with an existing merged region");
+ fail("Should not be able to add a merged region (" + partiallyOverlappingRegion.formatAsString() + ") " +
+ "if it partially overlaps with an existing merged region (" + baseRegion.formatAsString() + ")");
} catch (final IllegalStateException e) { } //expected
try {
- final CellRangeAddress subsetRegion = new CellRangeAddress(0, 1, 0, 0);
+ final CellRangeAddress subsetRegion = new CellRangeAddress(0, 1, 0, 0); //A1:A2
sheet.addMergedRegion(subsetRegion);
- fail("Should not be able to add a merged region if it is a formal subset of an existing merged region");
+ fail("Should not be able to add a merged region (" + subsetRegion.formatAsString() + ") " +
+ "if it is a formal subset of an existing merged region (" + baseRegion.formatAsString() + ")");
} catch (final IllegalStateException e) { } //expected
try {
- final CellRangeAddress supersetRegion = new CellRangeAddress(0, 2, 0, 2);
+ final CellRangeAddress supersetRegion = new CellRangeAddress(0, 2, 0, 2); //A1:C3
sheet.addMergedRegion(supersetRegion);
- fail("Should not be able to add a merged region if it is a formal superset of an existing merged region");
+ fail("Should not be able to add a merged region (" + supersetRegion.formatAsString() + ") " +
+ "if it is a formal superset of an existing merged region (" + baseRegion.formatAsString() + ")");
} catch (final IllegalStateException e) { } //expected
final CellRangeAddress disjointRegion = new CellRangeAddress(10, 11, 10, 11);
sheet.addMergedRegion(disjointRegion); //allowed
}
+ /*
+ * Bug 56345: Reject single-cell merged regions
+ */
+ @Test
+ public void addMergedRegionWithSingleCellShouldFail() throws IOException {
+ final Workbook wb = _testDataProvider.createWorkbook();
+
+ final Sheet sheet = wb.createSheet();
+ final CellRangeAddress region = CellRangeAddress.valueOf("A1:A1");
+ try {
+ sheet.addMergedRegion(region);
+ fail("Should not be able to add a single-cell merged region (" + region.formatAsString() + ")");
+ } catch (final IllegalArgumentException e) {
+ // expected
+ }
+ wb.close();
+ }
+
/**
* Test adding merged regions. If the region's bounds are outside of the allowed range
* then an IllegalArgumentException should be thrown
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org