GenericFormulaTestCasepublic class GenericFormulaTestCase extends TestCase
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 void | assertEquals(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 void | genericTest()
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 short | getBeginCol()
return beginCell.getCol();
| protected int | getBeginRow()
return beginCell.getRow();
| protected final org.apache.poi.hssf.usermodel.HSSFCell | getExpectedValueCell(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 void | setUp()
|
|