aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2022-09-02 22:40:21 +0000
committerPJ Fanning <fanningpj@apache.org>2022-09-02 22:40:21 +0000
commitff2e90e9a5ccc69a65733aee47c30dae32a11bee (patch)
treeb53111b1b1e5eeab743cfd93e2e55c227f1ba01b
parentaeb8c3f2c63738d2571702f00ea6ed0d18abc877 (diff)
downloadpoi-ff2e90e9a5ccc69a65733aee47c30dae32a11bee.tar.gz
poi-ff2e90e9a5ccc69a65733aee47c30dae32a11bee.zip
rework CellUtil test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903836 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java4
-rw-r--r--poi/src/main/java/org/apache/poi/ss/util/CellUtil.java36
2 files changed, 29 insertions, 11 deletions
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 d62bb690eb..8a6f85e85a 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
@@ -79,8 +79,8 @@ class TestXSSFCellUtil extends BaseTestCellUtil {
cell, CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
assertNotEquals(color, cell.getCellStyle().getFillForegroundColorColor());
- assertEquals(IndexedColors.AUTOMATIC.getIndex(),
- ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getIndex());
+ assertNull(cell.getCellStyle().getFillForegroundColorColor());
+ assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor());
}
}
} \ 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 7fd48aeae3..44ee0b53c1 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
@@ -463,12 +463,12 @@ public final class CellUtil {
disableNullColorCheck = true;
propMap = new HashMap<>();
propMap.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
- propMap.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.AUTOMATIC.getIndex());
+ propMap.put(CellUtil.FILL_FOREGROUND_COLOR, null);
} else if (CellUtil.FILL_BACKGROUND_COLOR_COLOR.equals(propertyName) && propertyValue == null) {
disableNullColorCheck = true;
propMap = new HashMap<>();
propMap.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null);
- propMap.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.AUTOMATIC.getIndex());
+ propMap.put(CellUtil.FILL_BACKGROUND_COLOR, null);
} else {
propMap = Collections.singletonMap(propertyName, propertyValue);
}
@@ -527,7 +527,7 @@ public final class CellUtil {
private static void putAll(final Map<String, Object> src, Map<String, Object> dest) {
for (final String key : src.keySet()) {
if (shortValues.contains(key)) {
- dest.put(key, getShort(src, key));
+ dest.put(key, nullableShort(src, key));
} else if (colorValues.contains(key)) {
dest.put(key, getColor(src, key));
} else if (intValues.contains(key)) {
@@ -567,11 +567,18 @@ public final class CellUtil {
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));
+ Short fillForeColorShort = nullableShort(properties, FILL_FOREGROUND_COLOR);
+ if (fillForeColorShort != null) {
+ style.setFillForegroundColor(fillForeColorShort);
+ }
+ Short fillBackColorShort = nullableShort(properties, FILL_BACKGROUND_COLOR);
+ if (fillBackColorShort != null) {
+ style.setFillBackgroundColor(fillBackColorShort);
+ }
Color foregroundFillColor = getColor(properties, FILL_FOREGROUND_COLOR_COLOR);
Color backgroundFillColor = getColor(properties, FILL_BACKGROUND_COLOR_COLOR);
+
if (foregroundFillColor != null) {
try {
style.setFillForegroundColor(foregroundFillColor);
@@ -615,6 +622,17 @@ public final class CellUtil {
}
return 0;
}
+
+ private static Short nullableShort(Map<String, Object> properties, String name) {
+ Object value = properties.get(name);
+ if (value instanceof Short) {
+ return (Short) value;
+ }
+ if (value instanceof Number) {
+ return ((Number) value).shortValue();
+ }
+ return null;
+ }
/**
* Utility method that returns the named Color value from the given map.
@@ -672,7 +690,7 @@ public final class CellUtil {
border = BorderStyle.NONE;
}
else {
- throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated).");
+ throw new IllegalStateException("Unexpected border style class. Must be BorderStyle or Short (deprecated).");
}
return border;
}
@@ -701,7 +719,7 @@ public final class CellUtil {
pattern = FillPatternType.NO_FILL;
}
else {
- throw new RuntimeException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated).");
+ throw new IllegalStateException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated).");
}
return pattern;
}
@@ -730,7 +748,7 @@ public final class CellUtil {
align = HorizontalAlignment.GENERAL;
}
else {
- throw new RuntimeException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated).");
+ throw new IllegalStateException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated).");
}
return align;
}
@@ -759,7 +777,7 @@ public final class CellUtil {
align = VerticalAlignment.BOTTOM;
}
else {
- throw new RuntimeException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated).");
+ throw new IllegalStateException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated).");
}
return align;
}