Rework test to not cause file-handle leaks and add an exclude for Log4j 2

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889203 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2021-04-26 15:12:22 +00:00
parent 6722f275d3
commit 9517445fa9
3 changed files with 135 additions and 154 deletions

View File

@ -67,6 +67,7 @@ java.text.BreakIterator.getBundle
sun.java2d.SunGraphicsEnvironment.<init> sun.java2d.SunGraphicsEnvironment.<init>
org.kohsuke.file_leak_detector.AgentMain.runHttpServer org.kohsuke.file_leak_detector.AgentMain.runHttpServer
org.apache.log4j.FileAppender.setFile org.apache.log4j.FileAppender.setFile
org.apache.logging.log4j.core.config.ConfigurationSource.getConfigurationSource
sun.launcher.LauncherHelper.validateMainClass sun.launcher.LauncherHelper.validateMainClass
sun.util.locale.provider.RuleBasedBreakIterator$1.run sun.util.locale.provider.RuleBasedBreakIterator$1.run
java.util.ServiceLoader$LazyIterator.hasNextService java.util.ServiceLoader$LazyIterator.hasNextService

View File

@ -35,20 +35,16 @@ import org.apache.poi.POIDataSamples;
import org.apache.poi.extractor.ExtractorFactory; import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.extractor.POIOLE2TextExtractor; import org.apache.poi.extractor.POIOLE2TextExtractor;
import org.apache.poi.extractor.POITextExtractor; import org.apache.poi.extractor.POITextExtractor;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.extractor.EventBasedExcelExtractor; import org.apache.poi.hssf.extractor.EventBasedExcelExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.ooxml.extractor.POIXMLExtractorFactory; import org.apache.poi.ooxml.extractor.POIXMLExtractorFactory;
import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException; import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor; import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.xmlbeans.XmlException;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
@ -118,7 +114,7 @@ class TestExtractorFactory {
Arguments.of("PowerPoint - pptx", pptx, "XSLFExtractor", 120), Arguments.of("PowerPoint - pptx", pptx, "XSLFExtractor", 120),
Arguments.of("Visio - vsdx", vsdx, "XDGFVisioExtractor", 20) Arguments.of("Visio - vsdx", vsdx, "XDGFVisioExtractor", 20)
); );
}; }
public static Stream<Arguments> testScratchData() { public static Stream<Arguments> testScratchData() {
return Stream.of( return Stream.of(
@ -132,12 +128,12 @@ class TestExtractorFactory {
Arguments.of("Publisher", pub, "PublisherTextExtractor", 50), Arguments.of("Publisher", pub, "PublisherTextExtractor", 50),
Arguments.of("Outlook msg", msg, "OutlookTextExtractor", 50) Arguments.of("Outlook msg", msg, "OutlookTextExtractor", 50)
); );
}; }
public static Stream<Arguments> testFileData() { public static Stream<Arguments> testFileData() {
return Stream.concat(testOOXMLData(), testScratchData()); return Stream.concat(testOOXMLData(), testScratchData());
// TODO Support OOXML-Strict / xlsxStrict, see bug #57699 // TODO Support OOXML-Strict / xlsxStrict, see bug #57699
}; }
@ParameterizedTest @ParameterizedTest

View File

@ -18,190 +18,174 @@
package org.apache.poi.ss.tests.usermodel; package org.apache.poi.ss.tests.usermodel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.Date;
import java.util.List;
import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
import org.apache.poi.ss.formula.EvaluationConditionalFormatRule; import org.apache.poi.ss.formula.EvaluationConditionalFormatRule;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class TestConditionalFormattingEval { import java.io.IOException;
import java.util.Date;
import java.util.List;
private XSSFWorkbook wb; import static org.junit.jupiter.api.Assertions.assertEquals;
private Sheet sheet; import static org.junit.jupiter.api.Assertions.assertFalse;
private XSSFFormulaEvaluator formulaEval; import static org.junit.jupiter.api.Assertions.assertNotEquals;
private ConditionalFormattingEvaluator cfe;
class TestConditionalFormattingEval {
private CellReference ref; private CellReference ref;
private List<EvaluationConditionalFormatRule> rules; private List<EvaluationConditionalFormatRule> rules;
@BeforeEach @Test
void openWB() { void testFormattingEvaluation() throws IOException {
wb = XSSFTestDataSamples.openSampleWorkbook("ConditionalFormattingSamples.xlsx"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ConditionalFormattingSamples.xlsx")) {
formulaEval = new XSSFFormulaEvaluator(wb); Sheet sheet = wb.getSheet("Products1");
cfe = new ConditionalFormattingEvaluator(wb, formulaEval); XSSFFormulaEvaluator formulaEval = new XSSFFormulaEvaluator(wb);
} ConditionalFormattingEvaluator cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
@AfterEach getRulesFor(12, 1, sheet, cfe);
void closeWB() { assertEquals(1, rules.size(), "wrong # of rules for " + ref);
formulaEval = null; assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
cfe = null; assertFalse(rules.get(0).getRule().getFontFormatting().isItalic(), "should not be italic " + ref);
ref = null;
rules = null; getRulesFor(16, 3, sheet, cfe);
IOUtils.closeQuietly(wb); assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals(0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001, "wrong bg color for " + ref);
getRulesFor(12, 3, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Products2");
getRulesFor(15, 1, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
getRulesFor(20, 3, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
// now change a cell value that's an input for the rules
Cell cell = sheet.getRow(1).getCell(6);
cell.setCellValue("Dairy");
formulaEval.notifyUpdateCell(cell);
cell = sheet.getRow(4).getCell(6);
cell.setCellValue(500);
formulaEval.notifyUpdateCell(cell);
// need to throw away all evaluations, since we don't know how value changes may have affected format formulas
cfe.clearAllCachedValues();
// test that the conditional validation evaluations changed
getRulesFor(15, 1, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(20, 3, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals(0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001, "wrong bg color for " + ref);
getRulesFor(20, 1, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
sheet = wb.getSheet("Book tour");
getRulesFor(8, 2, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Compare to totals");
getRulesFor(3, 2, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFF0000", getColor(rules.get(0).getRule().getFontFormatting().getFontColor()), "wrong fg color for " + ref);
getRulesFor(3, 3, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(15, 4, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(16, 1, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFF0000", getColor(rules.get(0).getRule().getFontFormatting().getFontColor()), "wrong fg color for " + ref);
sheet = wb.getSheet("Products3");
sheet.getRow(8).getCell(0).setCellValue(new Date());
getRulesFor(8, 0, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
getRulesFor(8, 3, sheet, cfe);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Customers2");
getRulesFor(3, 0, sheet, cfe);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
}
} }
@Test @Test
void testFormattingEvaluation() { void testFormattingOnUndefinedCell() throws IOException {
sheet = wb.getSheet("Products1"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_with_formula_on_second_sheet.xlsx")) {
XSSFFormulaEvaluator formulaEval = new XSSFFormulaEvaluator(wb);
ConditionalFormattingEvaluator cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
getRulesFor(12, 1); Sheet sheet = wb.getSheet("Sales Plan");
assertEquals(1, rules.size(), "wrong # of rules for " + ref); getRulesFor(9, 2, sheet, cfe);
assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref); assertNotEquals(0, rules.size(), "No rules for " + ref);
assertFalse(rules.get(0).getRule().getFontFormatting().isItalic(), "should not be italic " + ref); assertEquals("FFFFFF00", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
}
getRulesFor(16, 3);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals(0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001, "wrong bg color for " + ref);
getRulesFor(12, 3);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Products2");
getRulesFor(15,1);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
getRulesFor(20,3);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
// now change a cell value that's an input for the rules
Cell cell = sheet.getRow(1).getCell(6);
cell.setCellValue("Dairy");
formulaEval.notifyUpdateCell(cell);
cell = sheet.getRow(4).getCell(6);
cell.setCellValue(500);
formulaEval.notifyUpdateCell(cell);
// need to throw away all evaluations, since we don't know how value changes may have affected format formulas
cfe.clearAllCachedValues();
// test that the conditional validation evaluations changed
getRulesFor(15,1);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(20,3);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals(0.7999816888943144d, getTint(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), 0.000000000000001, "wrong bg color for " + ref);
getRulesFor(20,1);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFFEB9C", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref);
sheet = wb.getSheet("Book tour");
getRulesFor(8,2);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Compare to totals");
getRulesFor(3, 2);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFF0000", getColor(rules.get(0).getRule().getFontFormatting().getFontColor()), "wrong fg color for " + ref);
getRulesFor(3, 3);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(15, 4);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
getRulesFor(16, 1);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
assertEquals("FFFF0000", getColor(rules.get(0).getRule().getFontFormatting().getFontColor()), "wrong fg color for " + ref);
sheet = wb.getSheet("Products3");
sheet.getRow(8).getCell(0).setCellValue(new Date());
getRulesFor(8, 0);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
getRulesFor(8, 3);
assertEquals(1, rules.size(), "wrong # of rules for " + ref);
sheet = wb.getSheet("Customers2");
getRulesFor(3, 0);
assertEquals(0, rules.size(), "wrong # of rules for " + ref);
} }
@Test @Test
void testFormattingOnUndefinedCell() { void testRepeatedEval() throws IOException {
wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_with_formula_on_second_sheet.xlsx"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("test_conditional_formatting.xlsx")) {
formulaEval = new XSSFFormulaEvaluator(wb); XSSFFormulaEvaluator formulaEval = new XSSFFormulaEvaluator(wb);
cfe = new ConditionalFormattingEvaluator(wb, formulaEval); ConditionalFormattingEvaluator cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
sheet = wb.getSheet("Sales Plan"); Sheet sheet = wb.getSheetAt(0);
getRulesFor(9,2); assertEquals(0, getRulesFor(2, 1, sheet, cfe).size(), "no rules should apply");
assertNotEquals(0, rules.size(), "No rules for " + ref);
assertEquals("FFFFFF00", getColor(rules.get(0).getRule().getPatternFormatting().getFillBackgroundColorColor()), "wrong bg color for " + ref); assertEquals(0, getRulesFor(2, 1, sheet, cfe).size(), "no rules should apply");
}
} }
@Test @Test
void testRepeatedEval() { void testCellValueIsWrongType() throws IOException {
wb = XSSFTestDataSamples.openSampleWorkbook("test_conditional_formatting.xlsx"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_cell_is.xlsx")) {
formulaEval = new XSSFFormulaEvaluator(wb); XSSFFormulaEvaluator formulaEval = new XSSFFormulaEvaluator(wb);
cfe = new ConditionalFormattingEvaluator(wb, formulaEval); ConditionalFormattingEvaluator cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
sheet = wb.getSheetAt(0); Sheet sheet = wb.getSheetAt(1);
assertEquals(0, getRulesFor(2, 1).size(), "no rules should apply");
assertEquals(0, getRulesFor(2, 1).size(), "no rules should apply");
assertEquals(1, getRulesFor(3, 1, sheet, cfe).size(), "wrong # of matching rules");
}
} }
@Test @Test
void testCellValueIsWrongType() { void testRangeCondition() throws IOException {
wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_cell_is.xlsx"); try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_multiple_ranges.xlsx")) {
formulaEval = new XSSFFormulaEvaluator(wb); XSSFFormulaEvaluator formulaEval = new XSSFFormulaEvaluator(wb);
cfe = new ConditionalFormattingEvaluator(wb, formulaEval); ConditionalFormattingEvaluator cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
sheet = wb.getSheetAt(1); Sheet sheet = wb.getSheetAt(0);
assertEquals(1, getRulesFor(3, 1).size(), "wrong # of matching rules"); assertEquals(0, getRulesFor(0, 0, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 0, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(2, 0, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(3, 0, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(0, 1, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 1, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(2, 1, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(3, 1, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(0, 3, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 3, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(2, 3, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(0, 6, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(3, 6, sheet, cfe).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(2, 6, sheet, cfe).size(), "wrong # of matching rules");
}
} }
@Test private List<EvaluationConditionalFormatRule> getRulesFor(int row, int col, Sheet sheet, ConditionalFormattingEvaluator cfe) {
void testRangeCondition() {
wb = XSSFTestDataSamples.openSampleWorkbook("conditional_formatting_multiple_ranges.xlsx");
formulaEval = new XSSFFormulaEvaluator(wb);
cfe = new ConditionalFormattingEvaluator(wb, formulaEval);
sheet = wb.getSheetAt(0);
assertEquals(0, getRulesFor(0, 0).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 0).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(2, 0).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(3, 0).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(0, 1).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 1).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(2, 1).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(3, 1).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(0, 3).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(1, 3).size(), "wrong # of matching rules");
assertEquals(1, getRulesFor(2, 3).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(0, 6).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(3, 6).size(), "wrong # of matching rules");
assertEquals(0, getRulesFor(2, 6).size(), "wrong # of matching rules");
}
private List<EvaluationConditionalFormatRule> getRulesFor(int row, int col) {
ref = new CellReference(sheet.getSheetName(), row, col, false, false); ref = new CellReference(sheet.getSheetName(), row, col, false, false);
return rules = cfe.getConditionalFormattingForCell(ref); return rules = cfe.getConditionalFormattingForCell(ref);
} }