From 44930ebfa0bc1a1bcde07022a6b8f745a3b05227 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 10 Jul 2022 18:46:54 +0000 Subject: [PATCH] [bug-66052] add test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902637 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/TestSSUtilVsXSSFColor.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 poi-ooxml/src/test/java/org/apache/poi/xssf/TestSSUtilVsXSSFColor.java diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/TestSSUtilVsXSSFColor.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestSSUtilVsXSSFColor.java new file mode 100644 index 0000000000..7ddc769486 --- /dev/null +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestSSUtilVsXSSFColor.java @@ -0,0 +1,66 @@ +package org.apache.poi.xssf; + +import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.PropertyTemplate; +import org.apache.poi.xssf.usermodel.*; +import org.apache.commons.codec.binary.Hex; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +class TestSSUtilVsXSSFColor { + + @Test + void testXSSFCellStyle() throws Exception { + + try ( + XSSFWorkbook workbook = new XSSFWorkbook(); + UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream() + ) { + XSSFCellStyle cellStyle = workbook.createCellStyle(); + final String rgbS = "ffff00"; + final byte[] rgbB = Hex.decodeHex(rgbS); + IndexedColorMap colorMap = workbook.getStylesSource().getIndexedColors(); + XSSFColor color = new XSSFColor(rgbB, colorMap); + cellStyle.setFillForegroundColor(color); + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + final int startDataRow = 6; // row 7 (index 0-based) + final int endDataRow = 11; // row 12 (index 0-based) + final int startDataColumn = 1; // column B (index 0-based) + final int endDataColumn = 10; // column K (index 0-based) + + Sheet sheet = workbook.createSheet(); + + for (int r = startDataRow; r <= endDataRow; r++) { + Row row = sheet.createRow(r); + for (int c = startDataColumn; c <= endDataColumn; c++) { + Cell cell = row.createCell(c); + cell.setCellValue(cell.getAddress().formatAsString()); + cell.setCellStyle(cellStyle); + } + } + + PropertyTemplate propertyTemplate = new PropertyTemplate(); + propertyTemplate.drawBorders(new CellRangeAddress(startDataRow, endDataRow, startDataColumn, endDataColumn), + BorderStyle.MEDIUM, BorderExtent.ALL); + + propertyTemplate.applyBorders(sheet); // after this all cell interiors are filled black, because IndexedColors 0 is set + // same is using all other org.apache.poi.ss.util classes which manipulate cell styles (CellUtil or RegionUtil) + + workbook.write(bos); + + try(XSSFWorkbook wb2 = new XSSFWorkbook(bos.toInputStream())) { + XSSFSheet sheetWb2 = wb2.getSheetAt(0); + XSSFCell testCell = sheetWb2.getRow(startDataRow).getCell(startDataColumn); + XSSFCellStyle testStyle = testCell.getCellStyle(); + XSSFColor testColor = testStyle.getFillForegroundXSSFColor(); + assertFalse(testColor.isIndexed()); + assertEquals(rgbS, Hex.encodeHexString(testColor.getRGB())); + } + } + } +} \ No newline at end of file -- 2.39.5