FileDocCategorySizeDatePackage
GenericFormulaTestCase.javaAPI DocApache Poi 3.0.16168Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hssf.record.formula.eval

GenericFormulaTestCase

public class GenericFormulaTestCase extends TestCase
author
Amol S. Deshmukh < amolweb at ya hoo dot com >

Fields Summary
protected static final String
FILENAME
protected static HSSFWorkbook
workbook
protected CellReference
beginCell
Constructors Summary
public GenericFormulaTestCase(String beginCell)

        super("genericTest");
        if (workbook == null) {
          FileInputStream fin = new FileInputStream( FILENAME );
          workbook = new HSSFWorkbook( fin );
          fin.close();        
        }
        this.beginCell = new CellReference(beginCell);
    
Methods Summary
protected voidassertEquals(java.lang.String msg, org.apache.poi.hssf.usermodel.HSSFCell expected, HSSFFormulaEvaluator.CellValue actual)

        if (expected != null && actual!=null) {
            if (expected!=null && expected.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                String value = expected.getRichStringCellValue().getString();
                if (value.startsWith("#")) {
                    expected.setCellType(HSSFCell.CELL_TYPE_ERROR);
                }
            }
            if (!(expected == null || actual == null)) {
                switch (expected.getCellType()) {
                case HSSFCell.CELL_TYPE_BLANK:
                    assertEquals(msg, HSSFCell.CELL_TYPE_BLANK, actual.getCellType());
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    assertEquals(msg, HSSFCell.CELL_TYPE_BOOLEAN, actual.getCellType());
                    assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    assertEquals(msg, HSSFCell.CELL_TYPE_ERROR, actual.getCellType()); // TODO: check if exact error matches
                    break;
                case HSSFCell.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 HSSFCell.CELL_TYPE_NUMERIC:
                    assertEquals(msg, HSSFCell.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 HSSFCell.CELL_TYPE_STRING:
                    assertEquals(msg, HSSFCell.CELL_TYPE_STRING, actual.getCellType());
                    assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getRichTextStringValue().getString());
                    break;
                }
            }
            else {
                throw new AssertionFailedError("expected: " + expected + " got:" + actual);
            }
        }
    
public voidgenericTest()

        HSSFSheet s = workbook.getSheetAt( 0 );
        HSSFRow r = s.getRow(getBeginRow());
        short endcolnum = r.getLastCellNum();
        HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(s, workbook);
        evaluator.setCurrentRow(r);

        HSSFCell c = null;
        for (short colnum=getBeginCol(); colnum < endcolnum; colnum++) {
            try {
            c = r.getCell(colnum);
            if (c==null || c.getCellType() != HSSFCell.CELL_TYPE_FORMULA)
                continue;
            
            HSSFFormulaEvaluator.CellValue actualValue = evaluator.evaluate(c);
            
            HSSFCell expectedValueCell = getExpectedValueCell(s, r, c);
            assertEquals("Formula: " + c.getCellFormula() 
                    + " @ " + getBeginRow() + ":" + colnum, 
                    expectedValueCell, actualValue);
            } catch (RuntimeException re) {
                throw new RuntimeException("CELL["+getBeginRow()+","+colnum+"]: "+re.getMessage(), re);
            }
        }
    
protected shortgetBeginCol()

        return beginCell.getCol();
    
protected intgetBeginRow()

       
        return beginCell.getRow();
    
protected final org.apache.poi.hssf.usermodel.HSSFCellgetExpectedValueCell(org.apache.poi.hssf.usermodel.HSSFSheet sheet, org.apache.poi.hssf.usermodel.HSSFRow row, org.apache.poi.hssf.usermodel.HSSFCell cell)

        HSSFCell retval = null;
        if (sheet != null) {
            row = sheet.getRow(row.getRowNum()+1);
            if (row != null) {
                retval = row.getCell(cell.getCellNum());
            }
        }
        
        return retval;
    
public voidsetUp()