From 1c2ae742fd277a6f2e1dfcb515d6754edc281c19 Mon Sep 17 00:00:00 2001 From: Danny Muid Date: Fri, 27 Jun 2003 21:59:46 +0000 Subject: [PATCH] When removing one MergedRegion, the record was removed before it was ready, causing a IndexOutOfBoundsException. Shifted the remove after the get to fix this. TestCase should verify old functionality hasn't been broken. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353164 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/Sheet.java | 3 +- .../poi/hssf/usermodel/TestHSSFSheet.java | 44 +++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 9a7abadc3..1dbafd7bc 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -469,11 +469,12 @@ public class Sheet implements Model numMergedRegions--; if (rec.getNumAreas() == 0) { - mergedRecords.remove(pos); + if (merged == rec) merged = (MergeCellsRecord) mergedRecords.get(mergedRecords.size() - 1); int removePos = ((Integer) mergedLocs.get(pos)).intValue(); records.remove(removePos); + mergedRecords.remove(pos); mergedLocs.remove(pos); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 8e58186b5..66d475edc 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -54,18 +54,20 @@ package org.apache.poi.hssf.usermodel; -import junit.framework.TestCase; -import org.apache.poi.hssf.model.Sheet; -import org.apache.poi.hssf.record.HCenterRecord; -import org.apache.poi.hssf.record.VCenterRecord; -import org.apache.poi.hssf.record.WSBoolRecord; -import org.apache.poi.hssf.record.SCLRecord; -import org.apache.poi.hssf.record.WindowTwoRecord; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import junit.framework.TestCase; + +import org.apache.poi.hssf.model.Sheet; +import org.apache.poi.hssf.record.HCenterRecord; +import org.apache.poi.hssf.record.SCLRecord; +import org.apache.poi.hssf.record.VCenterRecord; +import org.apache.poi.hssf.record.WSBoolRecord; +import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.util.Region; + /** * Tests HSSFSheet. This test case is very incomplete at the moment. * @@ -254,4 +256,30 @@ public class TestHSSFSheet assertTrue(sclLoc == window2Loc + 1); } + + + /** + * When removing one merged region, it would break + * + */ + public void testRemoveMerged() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + Region region = new Region(0, (short)0, 1, (short)1); + sheet.addMergedRegion(region); + region = new Region(1, (short)0, 2, (short)1); + sheet.addMergedRegion(region); + + sheet.removeMergedRegion(0); + + region = sheet.getMergedRegionAt(0); + assertEquals("Left over region should be starting at row 1", 1, region.getRowFrom()); + + sheet.removeMergedRegion(0); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(TestHSSFSheet.class); + } + }