You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConditionalFormattingEvalTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import java.io.IOException;
  17. import java.util.List;
  18. import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
  19. import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
  20. import org.apache.poi.ss.util.CellReference;
  21. import org.apache.poi.xssf.XSSFTestDataSamples;
  22. import org.apache.poi.xssf.usermodel.XSSFColor;
  23. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import org.junit.After;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static org.junit.Assert.*;
  29. public class ConditionalFormattingEvalTest {
  30. private XSSFWorkbook wb;
  31. private Sheet sheet;
  32. private XSSFFormulaEvaluator formulaEval;
  33. private ConditionalFormattingEvaluator cfe;
  34. private CellReference ref;
  35. private List<EvaluationConditionalFormatRule> rules;
  36. @Before
  37. public void openWB() {
  38. wb = XSSFTestDataSamples.openSampleWorkbook("ConditionalFormattingSamples.xlsx");
  39. formulaEval = new XSSFFormulaEvaluator(wb);
  40. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  41. }
  42. @After
  43. public void closeWB() {
  44. formulaEval = null;
  45. cfe = null;
  46. ref = null;
  47. rules = null;
  48. try {
  49. if (wb != null) wb.close();
  50. } catch (IOException e) {
  51. // keep going, this shouldn't cancel things
  52. e.printStackTrace();
  53. }
  54. }
  55. @Test
  56. public void testFormattingEvaluation() {
  57. sheet = wb.getSheet("Products1");
  58. getRulesFor(12, 1);
  59. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  60. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  61. assertFalse("should not be italic " + ref, rules.get(0).getRule().getFontFormatting().isItalic());
  62. getRulesFor(16, 3);
  63. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  64. assertEquals("wrong bg color for " + ref, 0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001);
  65. getRulesFor(12, 3);
  66. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  67. sheet = wb.getSheet("Products2");
  68. getRulesFor(15,1);
  69. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  70. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  71. getRulesFor(20,3);
  72. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  73. // now change a cell value that's an input for the rules
  74. Cell cell = sheet.getRow(1).getCell(6);
  75. cell.setCellValue("Dairy");
  76. formulaEval.notifyUpdateCell(cell);
  77. cell = sheet.getRow(4).getCell(6);
  78. cell.setCellValue(500);
  79. formulaEval.notifyUpdateCell(cell);
  80. // need to throw away all evaluations, since we don't know how value changes may have affected format formulas
  81. cfe.clearAllCachedValues();
  82. // test that the conditional validation evaluations changed
  83. getRulesFor(15,1);
  84. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  85. getRulesFor(20,3);
  86. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  87. assertEquals("wrong bg color for " + ref, 0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001);
  88. getRulesFor(20,1);
  89. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  90. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  91. sheet = wb.getSheet("Book tour");
  92. getRulesFor(8,2);
  93. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  94. }
  95. @Test
  96. public void testFormattingOnUndefinedCell() throws Exception {
  97. wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_with_formula_on_second_sheet.xlsx");
  98. formulaEval = new XSSFFormulaEvaluator(wb);
  99. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  100. sheet = wb.getSheet("Sales Plan");
  101. getRulesFor(9,2);
  102. assertNotEquals("No rules for " + ref, 0, rules.size());
  103. assertEquals("wrong bg color for " + ref, "FFFFFF00", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  104. }
  105. private List<EvaluationConditionalFormatRule> getRulesFor(int row, int col) {
  106. ref = new CellReference(sheet.getSheetName(), row, col, false, false);
  107. return rules = cfe.getConditionalFormattingForCell(ref);
  108. }
  109. private String getColor(Color color) {
  110. final XSSFColor c = XSSFColor.toXSSFColor(color);
  111. return c.getARGBHex();
  112. }
  113. private double getTint(Color color) {
  114. final XSSFColor c = XSSFColor.toXSSFColor(color);
  115. return c.getTint();
  116. }
  117. }