/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.ss.formula.functions; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; 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.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.CellValue; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; @RunWith(Parameterized.class) public abstract class BaseTestFunctionsFromSpreadsheet { /** * This class defines constants for navigating around the test data spreadsheet used for these tests. */ interface SS { /** Name of the test spreadsheet (found in the standard test data folder) */ /** Name of the first sheet in the spreadsheet (contains comments) */ String README_SHEET_NAME = "Read Me"; /** Row (zero-based) in each sheet where the evaluation cases start. */ int START_TEST_CASES_ROW_INDEX = 4; // Row '5' /** Index of the column that contains the function names */ int COLUMN_INDEX_MARKER = 0; // Column 'A' int COLUMN_INDEX_EVALUATION = 1; // Column 'B' int COLUMN_INDEX_EXPECTED_RESULT = 2; // Column 'C' int COLUMN_ROW_COMMENT = 3; // Column 'D' /** Used to indicate when there are no more test cases on the current sheet */ String TEST_CASES_END_MARKER = ""; /** Used to indicate that the test on the current row should be ignored */ String SKIP_CURRENT_TEST_CASE_MARKER = ""; } @Parameter(value = 0) public String testName; @Parameter(value = 1) public String filename; @Parameter(value = 2) public HSSFSheet sheet; @Parameter(value = 3) public int formulasRowIdx; @Parameter(value = 4) public HSSFFormulaEvaluator evaluator; protected static Collection data(Class clazz, String filename) throws Exception { HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook(filename); confirmReadMeSheet(workbook, clazz); List data = new ArrayList(); int nSheets = workbook.getNumberOfSheets(); for(int sheetIdx=1; sheetIdx< nSheets; sheetIdx++) { HSSFSheet sheet = workbook.getSheetAt(sheetIdx); processFunctionGroup(data, sheet, SS.START_TEST_CASES_ROW_INDEX, null, filename); } workbook.close(); return data; } private static void processFunctionGroup(List data, HSSFSheet sheet, final int startRowIndex, String testFocusFunctionName, String filename) { HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet.getWorkbook()); String currentGroupComment = ""; final int maxRows = sheet.getLastRowNum()+1; for(int rowIndex=startRowIndex; rowIndex clazz) { String firstSheetName = workbook.getSheetName(0); assertTrue("First sheet's name was '" + firstSheetName + "' but expected '" + SS.README_SHEET_NAME + "'", firstSheetName.equalsIgnoreCase(SS.README_SHEET_NAME)); HSSFSheet sheet = workbook.getSheetAt(0); String specifiedClassName = sheet.getRow(2).getCell(0).getRichStringCellValue().getString(); assertEquals("Test class name in spreadsheet comment", clazz.getName(), specifiedClassName); } /** * @return null if cell is missing, empty or blank */ private static String getCellTextValue(HSSFRow r, int colIndex, String columnName) { if(r == null) { return null; } HSSFCell cell = r.getCell(colIndex); if(cell == null) { return null; } if(cell.getCellTypeEnum() == CellType.BLANK) { return null; } if(cell.getCellTypeEnum() == CellType.STRING) { return cell.getRichStringCellValue().getString(); } fail("Bad cell type for '" + columnName + "' column: (" + cell.getCellTypeEnum() + ") row (" + (r.getRowNum() +1) + ")"); return ""; } private static String formatValue(HSSFCell expecedCell) { switch (expecedCell.getCellTypeEnum()) { case BLANK: return ""; case BOOLEAN: return Boolean.toString(expecedCell.getBooleanCellValue()); case NUMERIC: return Double.toString(expecedCell.getNumericCellValue()); case STRING: return expecedCell.getRichStringCellValue().getString(); default: fail("Unexpected cell type of expected value (" + expecedCell.getCellTypeEnum() + ")"); } return ""; } }