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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.formula.eval.NotImplementedException;
  21. import org.apache.poi.ss.util.CellReference;
  22. import org.apache.poi.xssf.XSSFTestDataSamples;
  23. import org.apache.poi.xssf.usermodel.XSSFColor;
  24. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  26. import org.junit.After;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class ConditionalFormattingEvalTest {
  31. private XSSFWorkbook wb;
  32. private Sheet sheet;
  33. private XSSFFormulaEvaluator formulaEval;
  34. private ConditionalFormattingEvaluator cfe;
  35. private CellReference ref;
  36. private List<EvaluationConditionalFormatRule> rules;
  37. @Before
  38. public void openWB() {
  39. wb = XSSFTestDataSamples.openSampleWorkbook("ConditionalFormattingSamples.xlsx");
  40. formulaEval = new XSSFFormulaEvaluator(wb);
  41. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  42. }
  43. @After
  44. public void closeWB() {
  45. formulaEval = null;
  46. cfe = null;
  47. ref = null;
  48. rules = null;
  49. try {
  50. if (wb != null) wb.close();
  51. } catch (IOException e) {
  52. // keep going, this shouldn't cancel things
  53. e.printStackTrace();
  54. }
  55. }
  56. @Test
  57. public void testFormattingEvaluation() {
  58. sheet = wb.getSheet("Products1");
  59. getRulesFor(12, 1);
  60. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  61. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  62. assertFalse("should not be italic " + ref, rules.get(0).getRule().getFontFormatting().isItalic());
  63. getRulesFor(16, 3);
  64. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  65. assertEquals("wrong bg color for " + ref, 0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001);
  66. getRulesFor(12, 3);
  67. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  68. sheet = wb.getSheet("Products2");
  69. getRulesFor(15,1);
  70. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  71. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  72. getRulesFor(20,3);
  73. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  74. // now change a cell value that's an input for the rules
  75. Cell cell = sheet.getRow(1).getCell(6);
  76. cell.setCellValue("Dairy");
  77. formulaEval.notifyUpdateCell(cell);
  78. cell = sheet.getRow(4).getCell(6);
  79. cell.setCellValue(500);
  80. formulaEval.notifyUpdateCell(cell);
  81. // need to throw away all evaluations, since we don't know how value changes may have affected format formulas
  82. cfe.clearAllCachedValues();
  83. // test that the conditional validation evaluations changed
  84. getRulesFor(15,1);
  85. assertEquals("wrong # of rules for " + ref, 0, rules.size());
  86. getRulesFor(20,3);
  87. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  88. assertEquals("wrong bg color for " + ref, 0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001);
  89. getRulesFor(20,1);
  90. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  91. assertEquals("wrong bg color for " + ref, "FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  92. sheet = wb.getSheet("Book tour");
  93. getRulesFor(8,2);
  94. assertEquals("wrong # of rules for " + ref, 1, rules.size());
  95. }
  96. @Test
  97. public void testFormattingOnUndefinedCell() throws Exception {
  98. wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_with_formula_on_second_sheet.xlsx");
  99. formulaEval = new XSSFFormulaEvaluator(wb);
  100. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  101. sheet = wb.getSheet("Sales Plan");
  102. getRulesFor(9,2);
  103. assertNotEquals("No rules for " + ref, 0, rules.size());
  104. assertEquals("wrong bg color for " + ref, "FFFFFF00", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()));
  105. }
  106. @Test
  107. public void testRepeatedEval() throws Exception {
  108. wb = XSSFTestDataSamples.openSampleWorkbook("test_conditional_formatting.xlsx");
  109. formulaEval = new XSSFFormulaEvaluator(wb);
  110. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  111. sheet = wb.getSheetAt(0);
  112. try {
  113. getRulesFor(2, 1);
  114. fail("Got rules when an unsupported function error was expected.");
  115. } catch (NotImplementedException e) {
  116. // expected
  117. }
  118. try {
  119. getRulesFor(2, 1);
  120. fail("Got rules the second time when an unsupported function error was expected.");
  121. } catch (NotImplementedException e) {
  122. // expected
  123. }
  124. }
  125. @Test
  126. public void testCellValueIsWrongType() throws Exception {
  127. wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_cell_is.xlsx");
  128. formulaEval = new XSSFFormulaEvaluator(wb);
  129. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  130. sheet = wb.getSheetAt(1);
  131. assertEquals("wrong # of matching rules", 1, getRulesFor(3, 1).size());
  132. }
  133. @Test
  134. public void testRangeCondition() throws Exception {
  135. wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_multiple_ranges.xlsx");
  136. formulaEval = new XSSFFormulaEvaluator(wb);
  137. cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
  138. sheet = wb.getSheetAt(0);
  139. assertEquals("wrong # of matching rules", 0, getRulesFor(0, 0).size());
  140. assertEquals("wrong # of matching rules", 0, getRulesFor(1, 0).size());
  141. assertEquals("wrong # of matching rules", 0, getRulesFor(2, 0).size());
  142. assertEquals("wrong # of matching rules", 1, getRulesFor(3, 0).size());
  143. assertEquals("wrong # of matching rules", 0, getRulesFor(0, 1).size());
  144. assertEquals("wrong # of matching rules", 0, getRulesFor(1, 1).size());
  145. assertEquals("wrong # of matching rules", 1, getRulesFor(2, 1).size());
  146. assertEquals("wrong # of matching rules", 1, getRulesFor(3, 1).size());
  147. assertEquals("wrong # of matching rules", 1, getRulesFor(0, 3).size());
  148. assertEquals("wrong # of matching rules", 0, getRulesFor(1, 3).size());
  149. assertEquals("wrong # of matching rules", 1, getRulesFor(2, 3).size());
  150. assertEquals("wrong # of matching rules", 0, getRulesFor(0, 6).size());
  151. assertEquals("wrong # of matching rules", 0, getRulesFor(3, 6).size());
  152. assertEquals("wrong # of matching rules", 0, getRulesFor(2, 6).size());
  153. }
  154. private List<EvaluationConditionalFormatRule> getRulesFor(int row, int col) {
  155. ref = new CellReference(sheet.getSheetName(), row, col, false, false);
  156. return rules = cfe.getConditionalFormattingForCell(ref);
  157. }
  158. private String getColor(Color color) {
  159. final XSSFColor c = XSSFColor.toXSSFColor(color);
  160. return c.getARGBHex();
  161. }
  162. private double getTint(Color color) {
  163. final XSSFColor c = XSSFColor.toXSSFColor(color);
  164. return c.getTint();
  165. }
  166. }