diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2015-09-13 12:36:56 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2015-09-13 12:36:56 +0000 |
commit | 118e3ec20a2ecdfb2024088943675b26e5824644 (patch) | |
tree | 57437c6c636102b47f42cc07ae745995638addc1 /src/ooxml/testcases/org | |
parent | fbcf869f48e696a28ea364b8ff394476a96cdd21 (diff) | |
download | poi-118e3ec20a2ecdfb2024088943675b26e5824644.tar.gz poi-118e3ec20a2ecdfb2024088943675b26e5824644.zip |
fix eclipse warning - mostly generics cosmetics
close resources in tests
junit4 conversions
convert spreadsheet based formular test to junit parameterized tests
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1702773 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org')
3 files changed, 239 insertions, 268 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java index dd966bddc3..55b0725a31 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestFormulaEvaluatorOnXSSF.java @@ -20,6 +20,9 @@ package org.apache.poi.xssf.usermodel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import static org.junit.Assume.assumeFalse; +import static org.junit.Assume.assumeNotNull; +import static org.junit.Assume.assumeTrue; import java.util.ArrayList; import java.util.Collection; @@ -37,6 +40,8 @@ import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.LocaleUtil; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.junit.AfterClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -56,6 +61,7 @@ import org.junit.runners.Parameterized.Parameters; */ @RunWith(Parameterized.class) public final class TestFormulaEvaluatorOnXSSF { + private static final POILogger logger = POILogFactory.getLogger(TestFormulaEvaluatorOnXSSF.class); private static XSSFWorkbook workbook; private static Sheet sheet; @@ -144,12 +150,15 @@ public final class TestFormulaEvaluatorOnXSSF { private static void processFunctionGroup(List<Object[]> data, int startRowIndex, String testFocusFunctionName) { for (int rowIndex = startRowIndex; true; rowIndex += SS.NUMBER_OF_ROWS_PER_FUNCTION) { Row r = sheet.getRow(rowIndex); + + // only evaluate non empty row + if(r == null) continue; + String targetFunctionName = getTargetFunctionName(r); - if(targetFunctionName == null) { - fail("Test spreadsheet cell empty on row (" - + (rowIndex+1) + "). Expected function name or '" - + SS.FUNCTION_NAMES_END_SENTINEL + "'"); - } + assertNotNull("Test spreadsheet cell empty on row (" + + (rowIndex+1) + "). Expected function name or '" + + SS.FUNCTION_NAMES_END_SENTINEL + "'", targetFunctionName); + if(targetFunctionName.equals(SS.FUNCTION_NAMES_END_SENTINEL)) { // found end of functions list break; @@ -158,11 +167,10 @@ public final class TestFormulaEvaluatorOnXSSF { // expected results are on the row below Row expectedValuesRow = sheet.getRow(rowIndex + 1); - if(expectedValuesRow == null) { - int missingRowNum = rowIndex + 2; //+1 for 1-based, +1 for next row - fail("Missing expected values row for function '" - + targetFunctionName + " (row " + missingRowNum + ")"); - } + // +1 for 1-based, +1 for next row + assertNotNull("Missing expected values row for function '" + + targetFunctionName + " (row " + rowIndex + 2 + ")" + , expectedValuesRow); data.add(new Object[]{targetFunctionName, rowIndex, rowIndex + 1}); } @@ -180,12 +188,9 @@ public final class TestFormulaEvaluatorOnXSSF { // iterate across the row for all the evaluation cases for (short colnum=SS.COLUMN_INDEX_FIRST_TEST_VALUE; colnum < endcolnum; colnum++) { Cell c = formulasRow.getCell(colnum); - if (c == null || c.getCellType() != Cell.CELL_TYPE_FORMULA) { - continue; - } - if(isIgnoredFormulaTestCase(c.getCellFormula())) { - continue; - } + assumeNotNull(c); + assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); + ignoredFormulaTestCase(c.getCellFormula()); CellValue actValue = evaluator.evaluate(c); Cell expValue = (expectedValuesRow == null) ? null : expectedValuesRow.getCell(colnum); @@ -230,19 +235,16 @@ public final class TestFormulaEvaluatorOnXSSF { /* * TODO - these are all formulas which currently (Apr-2008) break on ooxml */ - private static boolean isIgnoredFormulaTestCase(String cellFormula) { - if ("COLUMN(1:2)".equals(cellFormula) || "ROW(2:3)".equals(cellFormula)) { - // full row ranges are not parsed properly yet. - // These cases currently work in svn trunk because of another bug which causes the - // formula to get rendered as COLUMN($A$1:$IV$2) or ROW($A$2:$IV$3) - return true; - } - if ("ISREF(currentcell())".equals(cellFormula)) { - // currently throws NPE because unknown function "currentcell" causes name lookup - // Name lookup requires some equivalent object of the Workbook within xSSFWorkbook. - return true; - } - return false; + private static void ignoredFormulaTestCase(String cellFormula) { + // full row ranges are not parsed properly yet. + // These cases currently work in svn trunk because of another bug which causes the + // formula to get rendered as COLUMN($A$1:$IV$2) or ROW($A$2:$IV$3) + assumeFalse("COLUMN(1:2)".equals(cellFormula)); + assumeFalse("ROW(2:3)".equals(cellFormula)); + + // currently throws NPE because unknown function "currentcell" causes name lookup + // Name lookup requires some equivalent object of the Workbook within xSSFWorkbook. + assumeFalse("ISREF(currentcell())".equals(cellFormula)); } /** @@ -250,12 +252,12 @@ public final class TestFormulaEvaluatorOnXSSF { */ private static String getTargetFunctionName(Row r) { if(r == null) { - System.err.println("Warning - given null row, can't figure out function name"); + logger.log(POILogger.WARN, "Warning - given null row, can't figure out function name"); return null; } Cell cell = r.getCell(SS.COLUMN_INDEX_FUNCTION_NAME); if(cell == null) { - System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name"); + logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name"); return null; } if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java index d75961f3cb..95fe30803e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestMultiSheetFormulaEvaluatorOnXSSF.java @@ -17,15 +17,20 @@ package org.apache.poi.xssf.usermodel; -import java.io.InputStream; -import java.io.PrintStream; -import java.util.Collection; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeNotNull; +import static org.junit.Assume.assumeTrue; -import junit.framework.Assert; -import junit.framework.AssertionFailedError; -import junit.framework.TestCase; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Locale; import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.ss.formula.eval.TestFormulasFromSpreadsheet; import org.apache.poi.ss.formula.functions.TestMathX; import org.apache.poi.ss.usermodel.Cell; @@ -33,268 +38,193 @@ import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.FormulaEvaluator; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; /** * Tests formulas for multi sheet reference (i.e. SUM(Sheet1:Sheet5!A1)) */ -public final class TestMultiSheetFormulaEvaluatorOnXSSF extends TestCase { +@RunWith(Parameterized.class) +public final class TestMultiSheetFormulaEvaluatorOnXSSF { private static final POILogger logger = POILogFactory.getLogger(TestFormulasFromSpreadsheet.class); - private static final class Result { - public static final int SOME_EVALUATIONS_FAILED = -1; - public static final int ALL_EVALUATIONS_SUCCEEDED = +1; - public static final int NO_EVALUATIONS_FOUND = 0; - } + private static XSSFWorkbook workbook; + private static Sheet sheet; + private static FormulaEvaluator evaluator; /** * This class defines constants for navigating around the test data spreadsheet used for these tests. */ - private static final class SS { + interface SS { /** * Name of the test spreadsheet (found in the standard test data folder) */ - public final static String FILENAME = "FormulaSheetRange.xlsx"; + String FILENAME = "FormulaSheetRange.xlsx"; /** * Row (zero-based) in the test spreadsheet where the function examples start. */ - public static final int START_FUNCTIONS_ROW_INDEX = 10; // Row '11' + int START_FUNCTIONS_ROW_INDEX = 10; // Row '11' /** * Index of the column that contains the function names */ - public static final int COLUMN_INDEX_FUNCTION_NAME = 0; // Column 'A' + int COLUMN_INDEX_FUNCTION_NAME = 0; // Column 'A' /** * Index of the column that contains the test names */ - public static final int COLUMN_INDEX_TEST_NAME = 1; // Column 'B' + int COLUMN_INDEX_TEST_NAME = 1; // Column 'B' /** * Used to indicate when there are no more functions left */ - public static final String FUNCTION_NAMES_END_SENTINEL = "<END>"; + String FUNCTION_NAMES_END_SENTINEL = "<END>"; /** * Index of the column where the test expected value is present */ - public static final short COLUMN_INDEX_EXPECTED_VALUE = 2; // Column 'C' + short COLUMN_INDEX_EXPECTED_VALUE = 2; // Column 'C' /** * Index of the column where the test actual value is present */ - public static final short COLUMN_INDEX_ACTUAL_VALUE = 3; // Column 'D' + short COLUMN_INDEX_ACTUAL_VALUE = 3; // Column 'D' /** * Test sheet name (sheet with all test formulae) */ - public static final String TEST_SHEET_NAME = "test"; + String TEST_SHEET_NAME = "test"; } - private XSSFWorkbook workbook; - private Sheet sheet; - // Note - multiple failures are aggregated before ending. - // If one or more functions fail, a single AssertionFailedError is thrown at the end - private int _functionFailureCount; - private int _functionSuccessCount; - private int _evaluationFailureCount; - private int _evaluationSuccessCount; - - private static void confirmExpectedResult(String msg, Cell expected, CellValue actual) { - if (expected == null) { - throw new AssertionFailedError(msg + " - Bad setup data expected value is null"); - } - if(actual == null) { - throw new AssertionFailedError(msg + " - actual value was null"); - } - - switch (expected.getCellType()) { - case Cell.CELL_TYPE_BLANK: - assertEquals(msg, Cell.CELL_TYPE_BLANK, actual.getCellType()); - break; - case Cell.CELL_TYPE_BOOLEAN: - assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actual.getCellType()); - assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue()); - break; - case Cell.CELL_TYPE_ERROR: - assertEquals(msg, Cell.CELL_TYPE_ERROR, actual.getCellType()); - if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values - assertEquals(msg, expected.getErrorCellValue(), actual.getErrorValue()); - } - break; - case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation - throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg); - case Cell.CELL_TYPE_NUMERIC: - assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actual.getCellType()); - TestMathX.assertEquals(msg, expected.getNumericCellValue(), actual.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); -// double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue()); -// double pctExpected = Math.abs(0.00001*expected.getNumericCellValue()); -// assertTrue(msg, delta <= pctExpected); - break; - case Cell.CELL_TYPE_STRING: - assertEquals(msg, Cell.CELL_TYPE_STRING, actual.getCellType()); - assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue()); - break; - } - } + @Parameter(value = 0) + public String targetTestName; + @Parameter(value = 1) + public String targetFunctionName; + @Parameter(value = 2) + public int formulasRowIdx; + @AfterClass + public static void closeResource() throws Exception { + workbook.close(); + } - protected void setUp() throws Exception { - if (workbook == null) { - InputStream is = HSSFTestDataSamples.openSampleFileStream(SS.FILENAME); - OPCPackage pkg = OPCPackage.open(is); - workbook = new XSSFWorkbook( pkg ); - sheet = workbook.getSheet( SS.TEST_SHEET_NAME ); - } - _functionFailureCount = 0; - _functionSuccessCount = 0; - _evaluationFailureCount = 0; - _evaluationSuccessCount = 0; - } + @Parameters(name="{0}") + public static Collection<Object[]> data() throws Exception { + workbook = new XSSFWorkbook( OPCPackage.open(HSSFTestDataSamples.getSampleFile(SS.FILENAME), PackageAccess.READ) ); + sheet = workbook.getSheet( SS.TEST_SHEET_NAME ); + evaluator = new XSSFFormulaEvaluator(workbook); - public void testFunctionsFromTestSpreadsheet() { + List<Object[]> data = new ArrayList<Object[]>(); - processFunctionGroup(SS.START_FUNCTIONS_ROW_INDEX, null); + processFunctionGroup(data, SS.START_FUNCTIONS_ROW_INDEX, null); - // confirm results - String successMsg = "There were " - + _evaluationSuccessCount + " successful evaluation(s) and " - + _functionSuccessCount + " function(s) without error"; - if(_functionFailureCount > 0) { - String msg = _functionFailureCount + " function(s) failed in " - + _evaluationFailureCount + " evaluation(s). " + successMsg; - throw new AssertionFailedError(msg); - } - logger.log(POILogger.INFO, getClass().getName() + ": " + successMsg); - } + return data; + } - /** - * @param startRowIndex row index in the spreadsheet where the first function/operator is found - * @param testFocusFunctionName name of a single function/operator to test alone. - * Typically pass <code>null</code> to test all functions - */ - private void processFunctionGroup(int startRowIndex, String testFocusFunctionName) { - FormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook); - - int rowIndex = startRowIndex; - while (true) { - Row r = sheet.getRow(rowIndex); - - // only evaluate non empty row - if( r != null ) - { - String targetFunctionName = getTargetFunctionName(r); - String targetTestName = getTargetTestName(r); - if(targetFunctionName == null) { - throw new AssertionFailedError("Test spreadsheet cell empty on row (" - + (rowIndex+1) + "). Expected function name or '" - + SS.FUNCTION_NAMES_END_SENTINEL + "'"); - } - if(targetFunctionName.equals(SS.FUNCTION_NAMES_END_SENTINEL)) { - // found end of functions list - break; - } - if(testFocusFunctionName == null || targetFunctionName.equalsIgnoreCase(testFocusFunctionName)) { - - // expected results are on the row below - Cell expectedValueCell = r.getCell(SS.COLUMN_INDEX_EXPECTED_VALUE); - if(expectedValueCell == null) { - int missingRowNum = rowIndex + 1; - throw new AssertionFailedError("Missing expected values cell for function '" - + targetFunctionName + ", test" + targetTestName + " (row " + - missingRowNum + ")"); - } - - switch(processFunctionRow(evaluator, targetFunctionName, targetTestName, r, expectedValueCell)) { - case Result.ALL_EVALUATIONS_SUCCEEDED: _functionSuccessCount++; break; - case Result.SOME_EVALUATIONS_FAILED: _functionFailureCount++; break; - default: - throw new RuntimeException("unexpected result"); - case Result.NO_EVALUATIONS_FOUND: // do nothing - break; - } - } - } - rowIndex ++; - } - } + /** + * @param startRowIndex row index in the spreadsheet where the first function/operator is found + * @param testFocusFunctionName name of a single function/operator to test alone. + * Typically pass <code>null</code> to test all functions + */ + private static void processFunctionGroup(List<Object[]> data, int startRowIndex, String testFocusFunctionName) { + for (int rowIndex = startRowIndex; true; rowIndex++) { + Row r = sheet.getRow(rowIndex); - /** - * - * @return a constant from the local Result class denoting whether there were any evaluation - * cases, and whether they all succeeded. - */ - private int processFunctionRow(FormulaEvaluator evaluator, String targetFunctionName, - String targetTestName, Row formulasRow, Cell expectedValueCell) { + // only evaluate non empty row + if(r == null) continue; - int result = Result.NO_EVALUATIONS_FOUND; // so far + String targetFunctionName = getTargetFunctionName(r); + assertNotNull("Test spreadsheet cell empty on row (" + + (rowIndex+1) + "). Expected function name or '" + + SS.FUNCTION_NAMES_END_SENTINEL + "'", targetFunctionName); - Cell c = formulasRow.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE); - if (c == null || c.getCellType() != Cell.CELL_TYPE_FORMULA) { - return result; - } + if(targetFunctionName.equals(SS.FUNCTION_NAMES_END_SENTINEL)) { + // found end of functions list + break; + } - CellValue actualValue = evaluator.evaluate(c); - - try { - confirmExpectedResult("Function '" + targetFunctionName + "': Test: '" + targetTestName + "' Formula: " + c.getCellFormula() - + " @ " + formulasRow.getRowNum() + ":" + SS.COLUMN_INDEX_ACTUAL_VALUE, - expectedValueCell, actualValue); - _evaluationSuccessCount ++; - if(result != Result.SOME_EVALUATIONS_FAILED) { - result = Result.ALL_EVALUATIONS_SUCCEEDED; - } - } catch (AssertionFailedError e) { - _evaluationFailureCount ++; - printShortStackTrace(System.err, e); - result = Result.SOME_EVALUATIONS_FAILED; - } - - return result; - } + String targetTestName = getTargetTestName(r); + if(testFocusFunctionName == null || targetFunctionName.equalsIgnoreCase(testFocusFunctionName)) { - /** - * Useful to keep output concise when expecting many failures to be reported by this test case - */ - private static void printShortStackTrace(PrintStream ps, AssertionFailedError e) { - StackTraceElement[] stes = e.getStackTrace(); - - int startIx = 0; - // skip any top frames inside junit.framework.Assert - while(startIx<stes.length) { - if(!stes[startIx].getClassName().equals(Assert.class.getName())) { - break; - } - startIx++; - } - // skip bottom frames (part of junit framework) - int endIx = startIx+1; - while(endIx < stes.length) { - if(stes[endIx].getClassName().equals(TestCase.class.getName())) { - break; - } - endIx++; - } - if(startIx >= endIx) { - // something went wrong. just print the whole stack trace - e.printStackTrace(ps); - } - endIx -= 4; // skip 4 frames of reflection invocation - ps.println(e.toString()); - for(int i=startIx; i<endIx; i++) { - ps.println("\tat " + stes[i].toString()); - } - } + // expected results are on the row below + Cell expectedValueCell = r.getCell(SS.COLUMN_INDEX_EXPECTED_VALUE); + assertNotNull("Missing expected values cell for function '" + + targetFunctionName + ", test" + targetTestName + " (row " + + rowIndex + 1 + ")", expectedValueCell); - /** + data.add(new Object[]{targetTestName, targetFunctionName, rowIndex}); + } + } + } + + /** + * + * @return a constant from the local Result class denoting whether there were any evaluation + * cases, and whether they all succeeded. + */ + @Test + public void processFunctionRow() { + Row r = sheet.getRow(formulasRowIdx); + + Cell expValue = r.getCell(SS.COLUMN_INDEX_EXPECTED_VALUE); + assertNotNull("Missing expected values cell for function '" + + targetFunctionName + ", test" + targetTestName + " (row " + + formulasRowIdx + 1 + ")", expValue); + + Cell c = r.getCell(SS.COLUMN_INDEX_ACTUAL_VALUE); + assumeNotNull(c); + assumeTrue(c.getCellType() == Cell.CELL_TYPE_FORMULA); + + CellValue actValue = evaluator.evaluate(c); + + String msg = String.format(Locale.ROOT, "Function '%s': Test: '%s': Formula: %s @ %d:%d", + targetFunctionName, targetTestName, c.getCellFormula(), formulasRowIdx, SS.COLUMN_INDEX_ACTUAL_VALUE); + + assertNotNull(msg + " - actual value was null", actValue); + + switch (expValue.getCellType()) { + case Cell.CELL_TYPE_BLANK: + assertEquals(msg, Cell.CELL_TYPE_BLANK, actValue.getCellType()); + break; + case Cell.CELL_TYPE_BOOLEAN: + assertEquals(msg, Cell.CELL_TYPE_BOOLEAN, actValue.getCellType()); + assertEquals(msg, expValue.getBooleanCellValue(), actValue.getBooleanValue()); + break; + case Cell.CELL_TYPE_ERROR: + assertEquals(msg, Cell.CELL_TYPE_ERROR, actValue.getCellType()); +// if(false) { // TODO: fix ~45 functions which are currently returning incorrect error values +// assertEquals(msg, expected.getErrorCellValue(), actual.getErrorValue()); +// } + break; + case Cell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation + fail("Cannot expect formula as result of formula evaluation: " + msg); + case Cell.CELL_TYPE_NUMERIC: + assertEquals(msg, Cell.CELL_TYPE_NUMERIC, actValue.getCellType()); + TestMathX.assertEquals(msg, expValue.getNumericCellValue(), actValue.getNumberValue(), TestMathX.POS_ZERO, TestMathX.DIFF_TOLERANCE_FACTOR); +// double delta = Math.abs(expected.getNumericCellValue()-actual.getNumberValue()); +// double pctExpected = Math.abs(0.00001*expected.getNumericCellValue()); +// assertTrue(msg, delta <= pctExpected); + break; + case Cell.CELL_TYPE_STRING: + assertEquals(msg, Cell.CELL_TYPE_STRING, actValue.getCellType()); + assertEquals(msg, expValue.getRichStringCellValue().getString(), actValue.getStringValue()); + break; + } + } + + /** * @return <code>null</code> if cell is missing, empty or blank */ private static String getTargetFunctionName(Row r) { if(r == null) { - System.err.println("Warning - given null row, can't figure out function name"); + logger.log(POILogger.WARN, "Warning - given null row, can't figure out function name"); return null; } Cell cell = r.getCell(SS.COLUMN_INDEX_FUNCTION_NAME); if(cell == null) { - System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name"); + logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_FUNCTION_NAME + ", can't figure out function name"); return null; } if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { @@ -304,20 +234,21 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF extends TestCase { return cell.getRichStringCellValue().getString(); } - throw new AssertionFailedError("Bad cell type for 'function name' column: (" - + cell.getCellType() + ") row (" + (r.getRowNum() +1) + ")"); + fail("Bad cell type for 'function name' column: (" + + cell.getCellType() + ") row (" + (r.getRowNum() +1) + ")"); + return ""; } /** * @return <code>null</code> if cell is missing, empty or blank */ private static String getTargetTestName(Row r) { if(r == null) { - System.err.println("Warning - given null row, can't figure out test name"); + logger.log(POILogger.WARN, "Warning - given null row, can't figure out test name"); return null; } Cell cell = r.getCell(SS.COLUMN_INDEX_TEST_NAME); if(cell == null) { - System.err.println("Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_TEST_NAME + ", can't figure out test name"); + logger.log(POILogger.WARN, "Warning - Row " + r.getRowNum() + " has no cell " + SS.COLUMN_INDEX_TEST_NAME + ", can't figure out test name"); return null; } if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { @@ -327,8 +258,9 @@ public final class TestMultiSheetFormulaEvaluatorOnXSSF extends TestCase { return cell.getRichStringCellValue().getString(); } - throw new AssertionFailedError("Bad cell type for 'test name' column: (" - + cell.getCellType() + ") row (" + (r.getRowNum() +1) + ")"); + fail("Bad cell type for 'test name' column: (" + + cell.getCellType() + ") row (" + (r.getRowNum() +1) + ")"); + return ""; } - + } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index 034e7118b3..43d8cce0f9 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -17,7 +17,8 @@ package org.apache.poi.xwpf.usermodel; -import java.io.FileOutputStream; +import static org.junit.Assert.*; + import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; @@ -34,12 +35,12 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.xmlbeans.XmlCursor; +import org.junit.Test; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; -import junit.framework.TestCase; - -public final class TestXWPFDocument extends TestCase { +public final class TestXWPFDocument { + @Test public void testContainsMainContentType() throws Exception { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); OPCPackage pack = doc.getPackage(); @@ -49,31 +50,36 @@ public final class TestXWPFDocument extends TestCase { if (part.getContentType().equals(XWPFRelation.DOCUMENT.getContentType())) { found = true; } - if (false) { - // successful tests should be silent - System.out.println(part); - } +// if (false) { +// // successful tests should be silent +// System.out.println(part); +// } } assertTrue(found); + + pack.close(); + doc.close(); } + @Test public void testOpen() throws Exception { - XWPFDocument xml; - // Simple file - xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); + XWPFDocument xml1 = XWPFTestDataSamples.openSampleDocument("sample.docx"); // Check it has key parts - assertNotNull(xml.getDocument()); - assertNotNull(xml.getDocument().getBody()); - assertNotNull(xml.getStyle()); + assertNotNull(xml1.getDocument()); + assertNotNull(xml1.getDocument().getBody()); + assertNotNull(xml1.getStyle()); + xml1.close(); // Complex file - xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); - assertNotNull(xml.getDocument()); - assertNotNull(xml.getDocument().getBody()); - assertNotNull(xml.getStyle()); + XWPFDocument xml2 = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); + assertNotNull(xml2.getDocument()); + assertNotNull(xml2.getDocument().getBody()); + assertNotNull(xml2.getStyle()); + xml2.close(); } + @Test public void testMetadataBasics() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertNotNull(xml.getProperties().getCoreProperties()); @@ -85,8 +91,10 @@ public final class TestXWPFDocument extends TestCase { assertEquals(null, xml.getProperties().getCoreProperties().getTitle()); assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); + xml.close(); } + @Test public void testMetadataComplex() throws IOException { XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx"); assertNotNull(xml.getProperties().getCoreProperties()); @@ -98,15 +106,19 @@ public final class TestXWPFDocument extends TestCase { assertEquals(" ", xml.getProperties().getCoreProperties().getTitle()); assertEquals(" ", xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); + xml.close(); } - public void testWorkbookProperties() { + @Test + public void testWorkbookProperties() throws Exception { XWPFDocument doc = new XWPFDocument(); POIXMLProperties props = doc.getProperties(); assertNotNull(props); assertEquals("Apache POI", props.getExtendedProperties().getUnderlyingProperties().getApplication()); + doc.close(); } + @Test public void testAddParagraph() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertEquals(3, doc.getParagraphs().size()); @@ -125,8 +137,10 @@ public final class TestXWPFDocument extends TestCase { XWPFParagraph cP = doc.insertNewParagraph(cursor); assertSame(cP, doc.getParagraphs().get(0)); assertEquals(5, doc.getParagraphs().size()); + doc.close(); } + @Test public void testAddPicture() throws IOException, InvalidFormatException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); byte[] jpeg = XWPFTestDataSamples.getImage("nature1.jpg"); @@ -137,8 +151,10 @@ public final class TestXWPFDocument extends TestCase { for (int i = 0; i < jpeg.length; i++) { assertEquals(newJpeg[i], jpeg[i]); } + doc.close(); } + @Test public void testAllPictureFormats() throws IOException, InvalidFormatException { XWPFDocument doc = new XWPFDocument(); @@ -156,11 +172,13 @@ public final class TestXWPFDocument extends TestCase { assertEquals(11, doc.getAllPictures().size()); - doc = XWPFTestDataSamples.writeOutAndReadBack(doc); - assertEquals(11, doc.getAllPictures().size()); - + XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc); + assertEquals(11, doc2.getAllPictures().size()); + doc2.close(); + doc.close(); } + @Test public void testRemoveBodyElement() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx"); assertEquals(3, doc.getParagraphs().size()); @@ -220,8 +238,10 @@ public final class TestXWPFDocument extends TestCase { assertEquals(p3, doc.getBodyElements().get(0)); assertEquals(p3, doc.getParagraphs().get(0)); + doc.close(); } + @Test public void testRegisterPackagePictureData() throws IOException, InvalidFormatException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); @@ -250,8 +270,11 @@ public final class TestXWPFDocument extends TestCase { assertTrue(doc.getAllPackagePictures().contains(newPicData)); doc.getPackage().revert(); + opcPckg.close(); + doc.close(); } + @Test public void testFindPackagePictureData() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); byte[] nature1 = XWPFTestDataSamples.getImage("nature1.gif"); @@ -260,8 +283,10 @@ public final class TestXWPFDocument extends TestCase { assertTrue(doc.getAllPictures().contains(part)); assertTrue(doc.getAllPackagePictures().contains(part)); doc.getPackage().revert(); + doc.close(); } + @Test public void testGetAllPictures() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); List<XWPFPictureData> allPictures = doc.getAllPictures(); @@ -281,8 +306,10 @@ public final class TestXWPFDocument extends TestCase { } doc.getPackage().revert(); + doc.close(); } + @Test public void testGetAllPackagePictures() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); List<XWPFPictureData> allPackagePictures = doc.getAllPackagePictures(); @@ -298,8 +325,10 @@ public final class TestXWPFDocument extends TestCase { } doc.getPackage().revert(); + doc.close(); } + @Test public void testPictureHandlingSimpleFile() throws IOException, InvalidFormatException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_1.docx"); assertEquals(1, doc.getAllPackagePictures().size()); @@ -311,16 +340,20 @@ public final class TestXWPFDocument extends TestCase { String id2 = doc.addPictureData(newPicCopy, Document.PICTURE_TYPE_JPEG); assertEquals(id1, id2); doc.getPackage().revert(); + doc.close(); } + @Test public void testPictureHandlingHeaderDocumentImages() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_2.docx"); assertEquals(1, doc.getAllPictures().size()); assertEquals(1, doc.getAllPackagePictures().size()); assertEquals(1, doc.getHeaderArray(0).getAllPictures().size()); doc.getPackage().revert(); + doc.close(); } + @Test public void testPictureHandlingComplex() throws IOException, InvalidFormatException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("issue_51265_3.docx"); XWPFHeader xwpfHeader = doc.getHeaderArray(0); @@ -336,8 +369,10 @@ public final class TestXWPFDocument extends TestCase { assertSame(part1, part2); doc.getPackage().revert(); + doc.close(); } + @Test public void testZeroLengthLibreOfficeDocumentWithWaterMarkHeader() throws IOException { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("zero-length.docx"); POIXMLProperties properties = doc.getProperties(); @@ -352,8 +387,10 @@ public final class TestXWPFDocument extends TestCase { POIXMLProperties.ExtendedProperties extendedProperties = properties.getExtendedProperties(); assertNotNull(extendedProperties); assertEquals(0, extendedProperties.getUnderlyingProperties().getCharacters()); + doc.close(); } + @Test public void testSettings() throws IOException { XWPFSettings settings = new XWPFSettings(); assertEquals(100, settings.getZoomPercent()); |