https://bz.apache.org/bugzilla/show_bug.cgi?id=66052

--- Comment #27 from XL <[email protected]> ---
I have further tested the CellUtil class,
and I have noticed the following behaviour.

If I set the fill foreground color,
and I do not set the fill background color,
then the fill background color gets not null unexpectedly.

If I set the fill foreground color,
and I set the fill background color to null as a workaround,
then the fill background color remains null as expected.

Please find below a new JUnit test,
which allows to reproduce this issue,
and which covers the issues of comment 13 and comment 15.

When testing the CellUtil class of r1903837 :
• CellUtilTest.testWithWorkaround  passes.
• CellUtilTest.testWithoutWorkaround  fails.







package test;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class CellUtilTest {

  @Test
  public void testWithWorkaround() throws IOException, DecoderException {

    try (Workbook workbook = new XSSFWorkbook()) {

      final Sheet sheet = workbook.createSheet("Sheet");
      final Row row = sheet.createRow(0);
      final Cell cell = row.createCell(0);
      final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA"));

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
        properties.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null); //
WORKAROUND
        properties.put(CellUtil.FILL_PATTERN,
FillPatternType.SOLID_FOREGROUND);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
        properties.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null); //
WORKAROUND
        properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());
    }
  }

  @Test
  public void testWithoutWorkaround() throws IOException, DecoderException {

    try (Workbook workbook = new XSSFWorkbook()) {

      final Sheet sheet = workbook.createSheet("Sheet");
      final Row row = sheet.createRow(0);
      final Cell cell = row.createCell(0);
      final XSSFColor color = new XSSFColor(Hex.decodeHex("FFAAAA"));

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
        properties.put(CellUtil.FILL_PATTERN,
FillPatternType.SOLID_FOREGROUND);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());

      {
        Map<String, Object> properties = new LinkedHashMap<String, Object>();

        properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
        properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);

        CellUtil.setCellStyleProperties(cell, properties);
      }

      assertNull(cell.getCellStyle().getFillForegroundColorColor());
      assertNull(cell.getCellStyle().getFillBackgroundColorColor());
    }
  }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to