cellC1.setCellFormula("1+D1");
cellD1.setCellFormula("1+E1");
cellE1.setCellFormula("1+A1");
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
CellValue cv;
// Happy day flow - evaluate A1 first
cv = fe.evaluate(cellA1);
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(42.0, cv.getNumberValue(), 0.0);
cv = fe.evaluate(cellB1); // no circ-ref-error because A1 result is cached
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(46.0, cv.getNumberValue(), 0.0);
// Show the bug - evaluate another cell from the loop first
fe.clearAllCachedResultValues();
cv = fe.evaluate(cellB1);
if (cv.getCellType() == ErrorEval.CIRCULAR_REF_ERROR.getErrorCode()) {
throw new AssertionFailedError("Identified bug 46898");
}
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(46.0, cv.getNumberValue(), 0.0);
// start evaluation on another cell
fe.clearAllCachedResultValues();
cv = fe.evaluate(cellE1);
assertEquals(Cell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(43.0, cv.getNumberValue(), 0.0);
}