Skip to content

Commit e20e890

Browse files
committed
Add some sanity tests
1 parent 5ee500f commit e20e890

File tree

2 files changed

+145
-8
lines changed

2 files changed

+145
-8
lines changed

poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public class CellFormatPart {
120120
NAMED_COLORS = new TreeMap<>(
121121
String.CASE_INSENSITIVE_ORDER);
122122

123+
// Retain compatibility with original implementation
123124
for (HSSFColor.HSSFColorPredefined color : HSSFColor.HSSFColorPredefined.values()) {
124125
String name = color.name();
125126
short[] rgb = color.getTriplet();
@@ -189,8 +190,9 @@ public class CellFormatPart {
189190
// Escape special characters in the color name
190191
color += key.replaceAll("([^a-zA-Z0-9])", "\\\\$1") + "|";
191192
}
192-
// Match the indexed color table
193-
color += "color\\ [0-9]+)\\]";
193+
// Match the indexed color table (accept both e.g. COLOR2 and COLOR 2)
194+
// Both formats are accepted as input in other products
195+
color += "color\\s*[0-9]+)\\]";
194196

195197
// A number specification
196198
// Note: careful that in something like ##, that the trailing comma is not caught up in the integer part
@@ -242,6 +244,8 @@ public class CellFormatPart {
242244
// Once patterns have been compiled, add indexed colors to
243245
// NAMED_COLORS so they can be easily picked up by getColor().
244246
for (int i = 0; i < INDEXED_COLORS.size(); ++i) {
247+
NAMED_COLORS.put("color" + (i + 1), INDEXED_COLORS.get(i));
248+
// Also support space between "color" and number.
245249
NAMED_COLORS.put("color " + (i + 1), INDEXED_COLORS.get(i));
246250
}
247251
}
@@ -337,10 +341,23 @@ private static int findGroup(Pattern pat, String str, String marker) {
337341
private static Color getColor(Matcher m) {
338342
String cdesc = m.group(COLOR_GROUP);
339343
if (cdesc == null || cdesc.isEmpty())
344+
return getColor(m.group(COLOR_GROUP));
345+
}
346+
347+
/**
348+
* Get the Color object matching a color name, or {@code null} if the
349+
* color name is not recognized.
350+
*
351+
* @param cdesc Color name, such as "red" or "Color 15"
352+
*
353+
* @return a Color object or {@code null}.
354+
*/
355+
static Color getColor(String cname) {
356+
if (cname == null || cname.length() == 0)
340357
return null;
341-
Color c = NAMED_COLORS.get(cdesc);
358+
Color c = NAMED_COLORS.get(cname);
342359
if (c == null) {
343-
LOG.warn("Unknown color: {}", quote(cdesc));
360+
LOG.warn("Unknown color: {}", quote(cname));
344361
}
345362
return c;
346363
}

poi/src/test/java/org/apache/poi/ss/format/TestCellFormat.java

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

23+
import java.awt.Color;
2324
import java.io.IOException;
2425
import java.text.ParseException;
2526
import java.text.SimpleDateFormat;
@@ -33,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3334
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
3435
import org.apache.poi.hssf.util.HSSFColor;
3536
import org.apache.poi.ss.usermodel.Cell;
37+
import org.apache.poi.ss.usermodel.CellType;
3638
import org.apache.poi.ss.usermodel.DateUtil;
3739
import org.apache.poi.ss.usermodel.Row;
3840
import org.apache.poi.ss.usermodel.Sheet;
@@ -1040,12 +1042,130 @@ void testBug62865() {
10401042
}
10411043

10421044
@Test
1043-
void testNamedColors() {
1044-
assertTrue(CellFormatPart.NAMED_COLORS.size() >= HSSFColor.HSSFColorPredefined.values().length);
1045-
Stream.of("GREEN", "Green", "RED", "Red", "BLUE", "Blue", "YELLOW", "Yellow")
1046-
.map(CellFormatPart.NAMED_COLORS::get)
1045+
void testNamedColorsExist() {
1046+
// Make sure we have all standard named colors defined
1047+
// and are returned as non-null regardless of case
1048+
Stream.of("black", "white", "red", "green", "blue", "yellow", "magenta", "cyan",
1049+
"Black", "White", "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan",
1050+
"BLACK", "WHITE", "RED", "GREEN", "BLUE", "YELLOW", "MAGENTA", "CYAN")
1051+
.map(CellFormatPart::getColor)
10471052
.forEach(Assertions::assertNotNull);
10481053
}
1054+
1055+
@Test
1056+
void testIndexedColorsExist() {
1057+
// Make sure the standard indexed colors are returned correctly and regardless of case
1058+
for (int i = 0; i < 56; ++i) {
1059+
assertNotNull(CellFormatPart.getColor("Color " + (i + 1)));
1060+
assertNotNull(CellFormatPart.getColor("COLOR" + (i + 1)));
1061+
assertNotNull(CellFormatPart.getColor("color" + (i + 1)));
1062+
}
1063+
}
1064+
1065+
@Test
1066+
void verifyNamedColors() {
1067+
assertEquals(CellFormatPart.getColor("Black"), new Color(0x000000));
1068+
assertEquals(CellFormatPart.getColor("white"), new Color(0xFFFFFF));
1069+
assertEquals(CellFormatPart.getColor("RED"), new Color(0xFF0000));
1070+
assertEquals(CellFormatPart.getColor("Green"), new Color(0x00FF00));
1071+
assertEquals(CellFormatPart.getColor("blue"), new Color(0x0000FF));
1072+
assertEquals(CellFormatPart.getColor("YELLOW"), new Color(0xFFFF00));
1073+
assertEquals(CellFormatPart.getColor("Magenta"), new Color(0xFF00FF));
1074+
assertEquals(CellFormatPart.getColor("cyan"), new Color(0x00FFFF));
1075+
}
1076+
1077+
@Test
1078+
void verifyIndexedColors() {
1079+
assertEquals(CellFormatPart.getColor("Color1"), CellFormatPart.getColor("black"));
1080+
assertEquals(CellFormatPart.getColor("color2"), CellFormatPart.getColor("white"));
1081+
assertEquals(CellFormatPart.getColor("Color3"), CellFormatPart.getColor("red"));
1082+
assertEquals(CellFormatPart.getColor("color4"), CellFormatPart.getColor("green"));
1083+
assertEquals(CellFormatPart.getColor("Color5"), CellFormatPart.getColor("blue"));
1084+
assertEquals(CellFormatPart.getColor("color6"), CellFormatPart.getColor("yellow"));
1085+
assertEquals(CellFormatPart.getColor("Color7"), CellFormatPart.getColor("magenta"));
1086+
assertEquals(CellFormatPart.getColor("color8"), CellFormatPart.getColor("cyan"));
1087+
assertEquals(CellFormatPart.getColor("Color9"), new Color(0x800000));
1088+
assertEquals(CellFormatPart.getColor("color10"), new Color(0x008000));
1089+
assertEquals(CellFormatPart.getColor("Color11"), new Color(0x000080));
1090+
assertEquals(CellFormatPart.getColor("color12"), new Color(0x808000));
1091+
assertEquals(CellFormatPart.getColor("Color13"), new Color(0x800080));
1092+
assertEquals(CellFormatPart.getColor("color14"), new Color(0x008080));
1093+
assertEquals(CellFormatPart.getColor("Color15"), new Color(0xC0C0C0));
1094+
assertEquals(CellFormatPart.getColor("color16"), new Color(0x808080));
1095+
assertEquals(CellFormatPart.getColor("Color17"), new Color(0x9999FF));
1096+
assertEquals(CellFormatPart.getColor("COLOR18"), new Color(0x993366));
1097+
assertEquals(CellFormatPart.getColor("Color19"), new Color(0xFFFFCC));
1098+
assertEquals(CellFormatPart.getColor("color20"), new Color(0xCCFFFF));
1099+
assertEquals(CellFormatPart.getColor("Color21"), new Color(0x660066));
1100+
assertEquals(CellFormatPart.getColor("COLOR22"), new Color(0xFF8080));
1101+
assertEquals(CellFormatPart.getColor("Color23"), new Color(0x0066CC));
1102+
assertEquals(CellFormatPart.getColor("color24"), new Color(0xCCCCFF));
1103+
assertEquals(CellFormatPart.getColor("Color25"), new Color(0x000080));
1104+
assertEquals(CellFormatPart.getColor("color26"), new Color(0xFF00FF));
1105+
assertEquals(CellFormatPart.getColor("Color27"), new Color(0xFFFF00));
1106+
assertEquals(CellFormatPart.getColor("COLOR28"), new Color(0x00FFFF));
1107+
assertEquals(CellFormatPart.getColor("Color29"), new Color(0x800080));
1108+
assertEquals(CellFormatPart.getColor("color30"), new Color(0x800000));
1109+
assertEquals(CellFormatPart.getColor("Color31"), new Color(0x008080));
1110+
assertEquals(CellFormatPart.getColor("Color32"), new Color(0x0000FF));
1111+
assertEquals(CellFormatPart.getColor("Color33"), new Color(0x00CCFF));
1112+
assertEquals(CellFormatPart.getColor("Color34"), new Color(0xCCFFFF));
1113+
assertEquals(CellFormatPart.getColor("Color35"), new Color(0xCCFFCC));
1114+
assertEquals(CellFormatPart.getColor("Color36"), new Color(0xFFFF99));
1115+
assertEquals(CellFormatPart.getColor("Color37"), new Color(0x99CCFF));
1116+
assertEquals(CellFormatPart.getColor("Color38"), new Color(0xFF99CC));
1117+
assertEquals(CellFormatPart.getColor("Color39"), new Color(0xCC99FF));
1118+
assertEquals(CellFormatPart.getColor("Color40"), new Color(0xFFCC99));
1119+
assertEquals(CellFormatPart.getColor("Color41"), new Color(0x3366FF));
1120+
assertEquals(CellFormatPart.getColor("Color42"), new Color(0x33CCCC));
1121+
assertEquals(CellFormatPart.getColor("Color43"), new Color(0x99CC00));
1122+
assertEquals(CellFormatPart.getColor("Color44"), new Color(0xFFCC00));
1123+
assertEquals(CellFormatPart.getColor("Color45"), new Color(0xFF9900));
1124+
assertEquals(CellFormatPart.getColor("Color46"), new Color(0xFF6600));
1125+
assertEquals(CellFormatPart.getColor("Color47"), new Color(0x666699));
1126+
assertEquals(CellFormatPart.getColor("Color48"), new Color(0x969696));
1127+
assertEquals(CellFormatPart.getColor("Color49"), new Color(0x003366));
1128+
assertEquals(CellFormatPart.getColor("Color50"), new Color(0x339966));
1129+
assertEquals(CellFormatPart.getColor("Color51"), new Color(0x003300));
1130+
assertEquals(CellFormatPart.getColor("Color52"), new Color(0x333300));
1131+
assertEquals(CellFormatPart.getColor("Color53"), new Color(0x993300));
1132+
assertEquals(CellFormatPart.getColor("Color54"), new Color(0x993366));
1133+
assertEquals(CellFormatPart.getColor("Color55"), new Color(0x333399));
1134+
assertEquals(CellFormatPart.getColor("Color56"), new Color(0x333333));
1135+
}
1136+
1137+
@Test
1138+
void testColorsInWorkbook() throws IOException {
1139+
// Create a workbook, row and cell to test with
1140+
try (Workbook wb = new HSSFWorkbook()) {
1141+
Sheet sheet = wb.createSheet();
1142+
Row row = sheet.createRow(0);
1143+
Cell cell = row.createCell(0);
1144+
CellFormatResult result;
1145+
CellFormat cf = CellFormat.getInstance(
1146+
"[GREEN]#,##0.0;[RED]\\(#,##0.0\\);[COLOR22]\"===\";[COLOR 8]\\\"@\\\"");
1147+
1148+
cell.setCellValue(100.0);
1149+
result = cf.apply(cell);
1150+
assertEquals("100.0", result.text);
1151+
assertEquals(result.textColor, CellFormatPart.getColor("color 4"));
1152+
1153+
cell.setCellValue(-50.0);
1154+
result = cf.apply(cell);
1155+
assertEquals("(50.0)", result.text);
1156+
assertEquals(result.textColor, CellFormatPart.getColor("red"));
1157+
1158+
cell.setCellValue("foo");
1159+
result = cf.apply(cell);
1160+
assertEquals("\"foo\"", result.text);
1161+
assertEquals(result.textColor, CellFormatPart.getColor("cyan"));
1162+
1163+
cell.setCellValue(0.0);
1164+
result = cf.apply(cell);
1165+
assertEquals("===", result.text);
1166+
assertEquals(result.textColor, CellFormatPart.getColor("color 22"));
1167+
}
1168+
}
10491169

10501170
@Test
10511171
void testElapsedSecondsRound() {

0 commit comments

Comments
 (0)