]> source.dussan.org Git - poi.git/commitdiff
bug 61727: CellRangeUtil Merge cell ranges broken. Thanks for Sven Rieckhoff for...
authorJaven O'Neal <onealj@apache.org>
Mon, 6 Nov 2017 19:51:41 +0000 (19:51 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 6 Nov 2017 19:51:41 +0000 (19:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814432 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/CellRangeUtil.java
src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java [new file with mode: 0644]

index 892778d45729f026035cf5f21631fd9197300248..8c40a4d63a000e25ee4b96a85f86a522eefd7b1a 100644 (file)
@@ -118,6 +118,7 @@ public final class CellRangeUtil {
                     }
                     somethingGotMerged = true;
                     // overwrite range1 with first result
+                    range1 = mergeResult[0];
                     cellRangeList.set(i, mergeResult[0]);
                     // remove range2
                     cellRangeList.remove(j--);
diff --git a/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java
new file mode 100644 (file)
index 0000000..fd52a29
--- /dev/null
@@ -0,0 +1,69 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.util;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assume.assumeTrue;
+
+/**
+ * Tests CellRangeUtil.
+ *
+ * @see org.apache.poi.ss.util.CellRangeUtil
+ */
+public final class TestCellRangeUtil {
+    
+    private static final CellRangeAddress A1 = new CellRangeAddress(0, 0, 0, 0);
+    private static final CellRangeAddress B1 = new CellRangeAddress(0, 0, 1, 1);
+    private static final CellRangeAddress A2 = new CellRangeAddress(1, 1, 0, 0);
+    private static final CellRangeAddress B2 = new CellRangeAddress(1, 1, 1, 1);
+    
+    @Test
+    public void testMergeCellRanges() {
+        testMergeCellRange(asArray(A1, B1, A2, B2), 1);
+        // suboptimal result for permuted arguments (2 ranges instead of one)
+        testMergeCellRange(asArray(A1, B2, A2, A1), 2);
+        
+        testMergeCellRange(asArray(A1, B1, A2), 2);
+        testMergeCellRange(asArray(A2, A1, B1), 2);
+        testMergeCellRange(asArray(B1, A2, A1), 2);
+        
+        testMergeCellRange(asArray(A1, B2), 2);
+        testMergeCellRange(asArray(B2, A1), 2);
+    }
+    
+    private void testMergeCellRange(CellRangeAddress[] input, int expectedResultRangeLength) {
+        CellRangeAddress[] result = CellRangeUtil.mergeCellRanges( input );
+        assertEquals( expectedResultRangeLength, result.length );
+        assertResultExactlyContainsInput(result, result);
+    }
+    
+    private static void assertResultExactlyContainsInput(CellRangeAddress[] result, CellRangeAddress[] input) {
+        for(CellRangeAddress inputEntry: input) {
+            boolean isInResultRange = false;
+            for(CellRangeAddress resultEntry: result) {
+                isInResultRange |= resultEntry.intersects(inputEntry);
+            }
+            assumeTrue(isInResultRange);
+        }
+    }
+    
+    private static <T> T[] asArray(T...ts) {
+        return ts;
+    }
+}