]> source.dussan.org Git - poi.git/commitdiff
bug 59212: Do not check for overlapping regions when adding merged regions to a sheet
authorJaven O'Neal <onealj@apache.org>
Tue, 22 Mar 2016 09:22:03 +0000 (09:22 +0000)
committerJaven O'Neal <onealj@apache.org>
Tue, 22 Mar 2016 09:22:03 +0000 (09:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1736165 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java

index 9cca5dc252ae5365af51c5bbea2884f94a5474ee..8394bcf583b1f4ed2de60cf5f8231a5ae8aa331c 100644 (file)
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
 
 import java.io.IOException;
 import java.util.Iterator;
@@ -442,6 +443,54 @@ public abstract class BaseTestSheet {
         wb.close();
     }
 
+    /**
+     * bug 58885: checking for overlapping merged regions when
+     * adding a merged region is safe, but runs in O(n).
+     * the check for merged regions when adding a merged region
+     * can be skipped (unsafe) and run in O(1).
+     */
+    @Test
+    public void addMergedRegionUnsafe() throws IOException {
+        Workbook wb = _testDataProvider.createWorkbook();
+        Sheet sh = wb.createSheet();
+        CellRangeAddress region1 = CellRangeAddress.valueOf("A1:B2");
+        CellRangeAddress region2 = CellRangeAddress.valueOf("B2:C3");
+        CellRangeAddress region3 = CellRangeAddress.valueOf("C3:D4");
+        CellRangeAddress region4 = CellRangeAddress.valueOf("J10:K11");
+        assumeTrue(region1.intersects(region2));
+        assumeTrue(region2.intersects(region3));
+
+        sh.addMergedRegionUnsafe(region1);
+        assertTrue(sh.getMergedRegions().contains(region1));
+
+        // adding a duplicate or overlapping merged region should not
+        // raise an exception with the unsafe version of addMergedRegion.
+
+        sh.addMergedRegionUnsafe(region2);
+
+        // the safe version of addMergedRegion should throw when trying to add a merged region that overlaps an existing region
+        assertTrue(sh.getMergedRegions().contains(region2));
+        try {
+            sh.addMergedRegion(region3);
+            fail("Expected IllegalStateException. region3 overlaps already added merged region2.");
+        } catch (final IllegalStateException e) {
+            // expected
+            assertFalse(sh.getMergedRegions().contains(region3));
+        }
+        // addMergedRegion should not re-validate previously-added merged regions
+        sh.addMergedRegion(region4);
+
+        // validation methods should detect a problem with previously added merged regions (runs in O(n^2) time)
+        try {
+            sh.validateMergedRegions();
+            fail("Expected validation to fail. Sheet contains merged regions A1:B2 and B2:C3, which overlap at B2.");
+        } catch (final IllegalStateException e) {
+            // expected
+        }
+
+        wb.close();
+    }
+
     /**
      * Tests the display of gridlines, formulas, and rowcolheadings.
      */