From e0ca0baebf7d9741b47dd67570ef755f8775bc0b Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 10 Jul 2022 13:54:09 +0000 Subject: [PATCH] try to fix tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902633 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/tests/util/TestSXSSFCellUtil.java | 11 ----- .../poi/ss/tests/util/TestXSSFCellUtil.java | 11 ----- .../java/org/apache/poi/ss/util/CellUtil.java | 43 ++++++++++++++++--- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestSXSSFCellUtil.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestSXSSFCellUtil.java index 77a21c09ec..0e2511f36d 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestSXSSFCellUtil.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestSXSSFCellUtil.java @@ -19,20 +19,9 @@ package org.apache.poi.ss.tests.util; import org.apache.poi.ss.util.BaseTestCellUtil; import org.apache.poi.xssf.SXSSFITestDataProvider; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.io.IOException; class TestSXSSFCellUtil extends BaseTestCellUtil { public TestSXSSFCellUtil() { super(SXSSFITestDataProvider.instance); } - - @Override - @Test - @Disabled("need to investigate why the super class version fails for (S)XSSF") - protected void setFillForegroundColorBeforeFillBackgroundColorEnum() throws IOException { - - } } \ No newline at end of file diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java index a67abfe840..40f16dbe61 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java @@ -19,20 +19,9 @@ package org.apache.poi.ss.tests.util; import org.apache.poi.ss.util.BaseTestCellUtil; import org.apache.poi.xssf.XSSFITestDataProvider; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import java.io.IOException; class TestXSSFCellUtil extends BaseTestCellUtil { public TestXSSFCellUtil() { super(XSSFITestDataProvider.instance); } - - @Override - @Test - @Disabled("need to investigate why the super class version fails for (S)XSSF") - protected void setFillForegroundColorBeforeFillBackgroundColorEnum() throws IOException { - - } } \ No newline at end of file diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index 8bab37d4da..bc47b10da0 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -401,7 +401,7 @@ public final class CellUtil { Map wbStyleMap = getFormatProperties(wbStyle); // the desired style already exists in the workbook. Use the existing style. - if (wbStyleMap.equals(values)) { + if (styleMapsMatch(wbStyleMap, values)) { newStyle = wbStyle; break; } @@ -416,6 +416,21 @@ public final class CellUtil { cell.setCellStyle(newStyle); } + private static boolean styleMapsMatch(final Map map1, final Map map2) { + final Map map1Copy = new HashMap<>(map1); + final Map map2Copy = new HashMap<>(map2); + final Object backColor1 = map1Copy.remove(FILL_BACKGROUND_COLOR_COLOR); + final Object backColor2 = map2Copy.remove(FILL_BACKGROUND_COLOR_COLOR); + final Object foreColor1 = map1Copy.remove(FILL_FOREGROUND_COLOR_COLOR); + final Object foreColor2 = map2Copy.remove(FILL_FOREGROUND_COLOR_COLOR); + if (map1Copy.equals(map2Copy)) { + final boolean backColorsMatch = backColor1 == null || backColor2 == null || backColor1.equals(backColor2); + final boolean foreColorsMatch = foreColor1 == null || foreColor2 == null || foreColor1.equals(foreColor2); + return backColorsMatch && foreColorsMatch; + } + return false; + } + /** *

This method attempts to find an existing CellStyle that matches the {@code cell}'s * current style plus a single style property {@code propertyName} with value @@ -529,11 +544,27 @@ public final class CellUtil { style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR)); style.setDataFormat(getShort(properties, DATA_FORMAT)); style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); - - style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); - style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); - style.setFillForegroundColor(getColor(properties, FILL_FOREGROUND_COLOR_COLOR), true); - style.setFillBackgroundColor(getColor(properties, FILL_BACKGROUND_COLOR_COLOR), true); + + Color foregroundFillColor = getColor(properties, FILL_FOREGROUND_COLOR_COLOR); + Color backgroundFillColor = getColor(properties, FILL_BACKGROUND_COLOR_COLOR); + if (foregroundFillColor != null) { + try { + style.setFillForegroundColor(foregroundFillColor); + } catch (IllegalArgumentException iae) { + style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); + } + } else { + style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); + } + if (backgroundFillColor != null) { + try { + style.setFillBackgroundColor(backgroundFillColor); + } catch (IllegalArgumentException iae) { + style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); + } + } else { + style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); + } style.setFont(workbook.getFontAt(getInt(properties, FONT))); style.setHidden(getBoolean(properties, HIDDEN));