From 11f188778460fa0d051134d667bed26da2eba8c1 Mon Sep 17 00:00:00 2001 From: David North Date: Wed, 23 Sep 2015 12:53:13 +0000 Subject: [PATCH] Make XSSF and HSSF consistent in getMergedRegions - return empty list if there are none, rather than an exception. Tests for this. https://bz.apache.org/bugzilla/show_bug.cgi?id=58350 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1704839 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/xssf/usermodel/XSSFSheet.java | 8 +++----- .../org/apache/poi/xssf/usermodel/TestXSSFSheet.java | 7 +++++++ .../org/apache/poi/hssf/usermodel/TestHSSFSheet.java | 7 +++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 4156433bd4..728fedf86a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -1090,7 +1090,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * this each time. * * @return the merged region at the specified index - * @throws IllegalStateException if this worksheet does not contain merged regions */ @Override public CellRangeAddress getMergedRegion(int index) { @@ -1107,15 +1106,14 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * faster than calling {@link #getMergedRegion(int)} each time. * * @return the list of merged regions - * @throws IllegalStateException if this worksheet does not contain merged regions */ @SuppressWarnings("deprecation") @Override public List getMergedRegions() { - CTMergeCells ctMergeCells = worksheet.getMergeCells(); - if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions"); - List addresses = new ArrayList(); + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + if(ctMergeCells == null) return addresses; + for(CTMergeCell ctMergeCell : ctMergeCells.getMergeCellArray()) { String ref = ctMergeCell.getRef(); addresses.add(CellRangeAddress.valueOf(ref)); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 8e77b2c499..aed4465620 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -1382,4 +1382,11 @@ public final class TestXSSFSheet extends BaseTestSheet { XSSFSheet sheet = wb.createSheet(); assertNotNull(sheet.createComment()); } + + @Test + public void testNoMergedRegionsIsEmptyList() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sheet = wb.createSheet(); + assertTrue(sheet.getMergedRegions().isEmpty()); + } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 4b7d8491be..723bf0d3e9 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -657,6 +657,13 @@ public final class TestHSSFSheet extends BaseTestSheet { assertTrue(sheet3.getColumnWidth(0) >= minWithRow1And2); assertTrue(sheet3.getColumnWidth(0) <= maxWithRow1And2); } + + @Test + public void testNoMergedRegionsIsEmptyList() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet("Sheet1"); + assertTrue(s.getMergedRegions().isEmpty()); + } @Test public void autoSizeDate() throws Exception {