From 047dcc60ca2a280b84330a5001b34f8501ad3e83 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 6 Nov 2017 19:51:41 +0000 Subject: [PATCH] bug 61727: CellRangeUtil Merge cell ranges broken. Thanks for Sven Rieckhoff for the patch git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814432 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/CellRangeUtil.java | 1 + .../apache/poi/ss/util/TestCellRangeUtil.java | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java diff --git a/src/java/org/apache/poi/ss/util/CellRangeUtil.java b/src/java/org/apache/poi/ss/util/CellRangeUtil.java index 892778d457..8c40a4d63a 100644 --- a/src/java/org/apache/poi/ss/util/CellRangeUtil.java +++ b/src/java/org/apache/poi/ss/util/CellRangeUtil.java @@ -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 index 0000000000..fd52a29a4b --- /dev/null +++ b/src/testcases/org/apache/poi/ss/util/TestCellRangeUtil.java @@ -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[] asArray(T...ts) { + return ts; + } +} -- 2.39.5