[bug-66052] add test

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902637 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-07-10 18:46:54 +00:00
parent 5386fe171d
commit 44930ebfa0
1 changed files with 66 additions and 0 deletions

View File

@ -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()));
}
}
}
}