From 637ea11f98725be9a60d95116d8eb448275af112 Mon Sep 17 00:00:00 2001 From: Danny Mui Date: Fri, 27 Jun 2003 22:05:20 +0000 Subject: [PATCH] Committing RemoveMerged fix to the release branch. PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353165 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/Sheet.java | 3 +- .../poi/hssf/usermodel/TestHSSFSheet.java | 38 ++++++++++++++++--- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 9a7abadc3f..1dbafd7bc1 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 8e58186b55..66d475edc9 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -54,17 +54,19 @@ package org.apache.poi.hssf.usermodel; +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.SCLRecord; import org.apache.poi.hssf.record.WindowTwoRecord; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +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); + } + } -- 2.39.5