* A simple class that encapsulates information about a cell evaluation
* from POI.
*
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ * @author Jon Svede (jon [at] loquatic [dot] com)
+ * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
*
*/
public class ExcelAntEvaluationResult {
- public ExcelAntEvaluationResult( boolean completedWithError,
+ public ExcelAntEvaluationResult(boolean completedWithError,
boolean passed,
double retValue,
String errMessage,
double delta,
- String cellId ) {
+ String cellId) {
evaluationCompletedWithError = completedWithError;
didPass = passed;
package org.apache.poi.ss.excelant.util;
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.FormulaError;
+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.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Typedef;
-import java.io.File;
-import java.io.FileInputStream;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-
/**
* A general utility class that abstracts the POI details of loading the
* workbook, accessing and updating cells.
*
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ * @author Jon Svede (jon [at] loquatic [dot] com)
+ * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
*
*/
public class ExcelAntWorkbookUtil extends Typedef {
private Workbook workbook;
- private final HashMap<String, FreeRefFunction> xlsMacroList = new HashMap<String, FreeRefFunction>();
+ private final Map<String, FreeRefFunction> xlsMacroList = new HashMap<String, FreeRefFunction>();
/**
* Constructs an instance using a String that contains the fully qualified
+ ". Make sure the path and file permissions are correct.", e);
}
- return workbook ;
+ return workbook;
}
/**
* @throws InstantiationException
* @throws IllegalAccessException
*/
- public void addFunction( String name, String clazzName ) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Class<?> clazzInst = Class.forName( clazzName ) ;
- Object newInst = clazzInst.newInstance() ;
- if( newInst instanceof FreeRefFunction ) {
- addFunction( name, (FreeRefFunction)newInst ) ;
+ public void addFunction(String name, String clazzName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+ Class<?> clazzInst = Class.forName(clazzName);
+ Object newInst = clazzInst.newInstance();
+ if(newInst instanceof FreeRefFunction) {
+ addFunction(name, (FreeRefFunction)newInst);
}
}
String[] names = new String[xlsMacroList.size()];
FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
- Iterator<String> keysIt = xlsMacroList.keySet().iterator();
int x = 0;
- while (keysIt.hasNext()) {
- String name = keysIt.next();
- FreeRefFunction function = xlsMacroList.get(name);
- names[x] = name;
- functions[x] = function;
+ for(Map.Entry<String, FreeRefFunction> entry : xlsMacroList.entrySet()) {
+ names[x] = entry.getKey();
+ functions[x] = entry.getValue();
}
UDFFinder udff1 = new DefaultUDFFinder(names, functions);
* @param fileName
* @return
*/
- protected FormulaEvaluator getEvaluator( String fileName ) {
- FormulaEvaluator evaluator ;
+ protected FormulaEvaluator getEvaluator(String fileName) {
+ FormulaEvaluator evaluator;
if (fileName.endsWith(".xlsx")) {
- if( xlsMacroList.size() > 0 ) {
- evaluator = XSSFFormulaEvaluator.create( (XSSFWorkbook) workbook,
+ if(xlsMacroList.size() > 0) {
+ evaluator = XSSFFormulaEvaluator.create((XSSFWorkbook) workbook,
null,
- getFunctions() ) ;
+ getFunctions());
}
evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
} else {
- if( xlsMacroList.size() > 0 ) {
- evaluator = HSSFFormulaEvaluator.create( (HSSFWorkbook)workbook,
+ if(xlsMacroList.size() > 0) {
+ evaluator = HSSFFormulaEvaluator.create((HSSFWorkbook)workbook,
null,
- getFunctions() ) ;
+ getFunctions());
}
evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
}
- return evaluator ;
+ return evaluator;
}
*
* @return
*/
- public ArrayList<String> getSheets() {
- ArrayList<String> sheets = new ArrayList<String>() ;
+ public List<String> getSheets() {
+ ArrayList<String> sheets = new ArrayList<String>();
- int sheetCount = workbook.getNumberOfSheets() ;
+ int sheetCount = workbook.getNumberOfSheets();
- for( int x=0; x<sheetCount; x++ ) {
- sheets.add( workbook.getSheetName( x ) ) ;
+ for(int x=0; x<sheetCount; x++) {
+ sheets.add(workbook.getSheetName(x));
}
- return sheets ;
+ return sheets;
}
/**
* @param cellName
* @param value
*/
- public void setStringValue( String cellName, String value ) {
+ public void setStringValue(String cellName, String value) {
Cell cell = getCell(cellName);
cell.setCellValue(value);
}
* @param cellName
* @param formula
*/
- public void setFormulaValue( String cellName, String formula ) {
+ public void setFormulaValue(String cellName, String formula) {
Cell cell = getCell(cellName);
- cell.setCellFormula( formula );
+ cell.setCellFormula(formula);
}
/**
* @param cellName
* @param date
*/
- public void setDateValue( String cellName, Date date ) {
+ public void setDateValue(String cellName, Date date) {
Cell cell = getCell(cellName);
- cell.setCellValue( date ) ;
+ cell.setCellValue(date);
}
/**
* Uses a String in standard Excel format (SheetName!CellId) to locate a
Cell cell = getCell(cellName);
- FormulaEvaluator evaluator = getEvaluator( excelFileName );
+ FormulaEvaluator evaluator = getEvaluator(excelFileName);
CellValue resultOfEval = evaluator.evaluate(cell);
evalResults = new ExcelAntEvaluationResult(false, false,
resultOfEval.getNumberValue(),
"Results was out of range based on precision " + " of "
- + precision + ". Delta was actually " + delta, delta, cellName );
+ + precision + ". Delta was actually " + delta, delta, cellName);
} else {
evalResults = new ExcelAntEvaluationResult(false, true,
resultOfEval.getNumberValue(),
- "Evaluation passed without error within in range.", delta, cellName );
+ "Evaluation passed without error within in range.", delta, cellName);
}
} else {
- String errorMeaning = null ;
+ String errorMeaning = null;
try {
- errorMeaning = ErrorConstants.getText( resultOfEval
- .getErrorValue() ) ;
- } catch( IllegalArgumentException iae ) {
+ errorMeaning = FormulaError.forInt(resultOfEval.getErrorValue()).getString();
+ } catch(IllegalArgumentException iae) {
errorMeaning = "unknown error code: " +
- Byte.toString( resultOfEval.getErrorValue() ) ;
+ Byte.toString(resultOfEval.getErrorValue());
}
evalResults = new ExcelAntEvaluationResult(true, false,
"Evaluation failed due to an evaluation error of "
+ resultOfEval.getErrorValue()
+ " which is "
- + errorMeaning, 0, cellName );
+ + errorMeaning, 0, cellName);
}
return evalResults;
* @param cellName
* @return
*/
- public String getCellAsString( String cellName ) {
- Cell cell = getCell( cellName ) ;
- return cell.getStringCellValue() ;
+ public String getCellAsString(String cellName) {
+ Cell cell = getCell(cellName);
+ return cell.getStringCellValue();
}
* @param cellName
* @return
*/
- public double getCellAsDouble( String cellName ) {
- Cell cell = getCell( cellName ) ;
- return cell.getNumericCellValue() ;
+ public double getCellAsDouble(String cellName) {
+ Cell cell = getCell(cellName);
+ return cell.getNumericCellValue();
}
/**
* Returns a cell reference based on a String in standard Excel format
int colIdx = cellRef.getCol();
Row row = sheet.getRow(rowIdx);
- if( row == null ) {
- row = sheet.createRow( rowIdx ) ;
+ if(row == null) {
+ row = sheet.createRow(rowIdx);
}
Cell cell = row.getCell(colIdx);
- if( cell == null ) {
- cell = row.createCell( colIdx ) ;
+ if(cell == null) {
+ cell = row.createCell(colIdx);
}
return cell;
package org.apache.poi.ss.excelant.util;\r
\r
import java.util.HashMap;\r
+import java.util.Map;\r
\r
\r
/**\r
* This is a factory class maps file names to WorkbookUtil instances. This\r
* helps ExcelAnt be more efficient when being run many times in an Ant build.\r
*\r
- * @author Jon Svede ( jon [at] loquatic [dot] com )\r
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
+ * @author Jon Svede (jon [at] loquatic [dot] com)\r
+ * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)\r
*\r
*/\r
-public class ExcelAntWorkbookUtilFactory {\r
+public final class ExcelAntWorkbookUtilFactory {\r
\r
- private static HashMap<String, ExcelAntWorkbookUtil> workbookUtilMap ;\r
-\r
- private static ExcelAntWorkbookUtilFactory factory ;\r
+ private static Map<String, ExcelAntWorkbookUtil> workbookUtilMap;\r
\r
private ExcelAntWorkbookUtilFactory() {\r
- workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>() ;\r
}\r
\r
/**\r
* @param fileName\r
* @return\r
*/\r
- public static ExcelAntWorkbookUtil getInstance( String fileName ) {\r
-\r
- if( factory == null ) {\r
- factory = new ExcelAntWorkbookUtilFactory() ;\r
+ public static ExcelAntWorkbookUtil getInstance(String fileName) {\r
+ if(workbookUtilMap == null) {\r
+ workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>();\r
}\r
- if( workbookUtilMap != null &&\r
- workbookUtilMap.containsKey( fileName ) ) {\r
- return workbookUtilMap.get( fileName ) ;\r
+ if(workbookUtilMap != null &&\r
+ workbookUtilMap.containsKey(fileName)) {\r
+ return workbookUtilMap.get(fileName);\r
}\r
\r
- ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil( fileName ) ;\r
- workbookUtilMap.put( fileName, wbu ) ;\r
- return wbu ;\r
+ ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName);\r
+ workbookUtilMap.put(fileName, wbu);\r
+ return wbu;\r
}\r
-\r
}\r
==================================================================== */
package org.apache.poi.ss.excelant.util;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
-public class TestExcelAntEvaluationResult extends TestCase {
-
- private ExcelAntEvaluationResult fixture ;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestExcelAntEvaluationResult {
+ private ExcelAntEvaluationResult fixture;
- private boolean completedWithError = false ;
- private boolean passed = false ;
- private double retValue = 1.1 ;
- private String errMessage = "error message" ;
- private double delta = 2.2 ;
- private String cellId = "testCell!$F$1" ;
+ private boolean completedWithError = false;
+ private boolean passed = false;
+ private double retValue = 1.1;
+ private String errMessage = "error message";
+ private double delta = 2.2;
+ private String cellId = "testCell!$F$1";
+ @Before
public void setUp() {
- fixture = new ExcelAntEvaluationResult( completedWithError,
+ fixture = new ExcelAntEvaluationResult(completedWithError,
passed,
retValue,
errMessage,
delta,
- cellId ) ;
+ cellId);
}
+ @After
public void tearDown() {
- fixture = null ;
+ fixture = null;
}
+ @Test
public void testCompletedWithErrorMessage() {
- String errMsg = fixture.getErrorMessage() ;
- assertNotNull( errMsg ) ;
- assertEquals( errMsg, errMessage ) ;
+ String errMsg = fixture.getErrorMessage();
+ assertNotNull(errMsg);
+ assertEquals(errMsg, errMessage);
}
+ @Test
public void testPassed() {
- boolean passedValue = fixture.didTestPass() ;
- assertEquals( passedValue, passed ) ;
+ boolean passedValue = fixture.didTestPass();
+ assertEquals(passedValue, passed);
}
+ @Test
public void testDelta() {
- double deltaValue = fixture.getDelta() ;
- assertEquals(deltaValue, delta, 0.0 ) ;
+ double deltaValue = fixture.getDelta();
+ assertEquals(deltaValue, delta, 0.0);
}
+ @Test
public void testCellId() {
- String cellIdValue = fixture.getCellName() ;
- assertNotNull( cellIdValue ) ;
- assertEquals( cellIdValue, cellId ) ;
+ String cellIdValue = fixture.getCellName();
+ assertNotNull(cellIdValue);
+ assertEquals(cellIdValue, cellId);
}
-
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.Date;
-
-import junit.framework.TestCase;
+import java.util.List;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.examples.formula.CalculateMortgageFunction;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.tools.ant.BuildException;
+import junit.framework.TestCase;
+
public class TestExcelAntWorkbookUtil extends TestCase {
private static final String mortgageCalculatorFileName =
precision);
//System.out.println(result);
- assertTrue(result.toString().contains("evaluationCompletedWithError=false"));
- assertTrue(result.toString().contains("returnValue=790.79"));
- assertTrue(result.toString().contains("cellName='MortgageCalculator'!B4"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=false"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=790.79"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("cellName='MortgageCalculator'!B4"));
+ assertFalse(result.toString().contains("#N/A"));
assertFalse(result.evaluationCompleteWithError());
assertTrue(result.didTestPass());
precision);
//System.out.println(result);
- assertTrue(result.toString().contains("evaluationCompletedWithError=false"));
- assertTrue(result.toString().contains("returnValue=790.79"));
- assertTrue(result.toString().contains("cellName='MortgageCalculator'!B4"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=false"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=790.79"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("cellName='MortgageCalculator'!B4"));
+ assertFalse("Should not see an error, but had:" + result.toString(), result.toString().contains("#"));
assertFalse(result.evaluationCompleteWithError());
assertFalse(result.didTestPass());
precision);
System.out.println(result);
- assertTrue(result.toString().contains("evaluationCompletedWithError=true"));
- assertTrue(result.toString().contains("returnValue=0.0"));
- assertTrue(result.toString().contains("cellName='ErrorCell'!A1"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=true"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=0.0"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("cellName='ErrorCell'!A1"));
+ assertTrue("Had:" + result.toString(), result.toString().contains("#N/A"));
assertTrue(result.evaluationCompleteWithError());
assertFalse(result.didTestPass());
fixture = new ExcelAntWorkbookUtilTestHelper(
mortgageCalculatorFileName);
- ArrayList<String> sheets = fixture.getSheets();
+ List<String> sheets = fixture.getSheets();
assertNotNull(sheets);
assertEquals(sheets.size(), 3);
==================================================================== */
package org.apache.poi.ss.excelant.util;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import org.apache.poi.ss.excelant.BuildFileTest;
+import org.junit.Test;
/**
* Tests for the ExcelAntWorbookUtilFactory.
*
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ * @author Jon Svede (jon [at] loquatic [dot] com)
+ * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
*
*/
-public class TestExcelAntWorkbookUtilFactory extends TestCase{
+public class TestExcelAntWorkbookUtilFactory {
private static final String mortgageCalculatorWorkbookFile =
BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ;
* Simple test to determine if the factory properly returns an non-null
* instance of the ExcelAntWorkbookUtil class.
*/
+ @Test
public void testGetNewWorkbookUtilInstance() {
-
ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance(
- mortgageCalculatorWorkbookFile ) ;
-
- assertNotNull( util ) ;
+ mortgageCalculatorWorkbookFile) ;
+ assertNotNull(util) ;
}
* to an ExcelAnt WorkbookUtil when two different Strings, that point to
* the same resource, are passed in.
*/
+ @Test
public void testVerifyEquivalence() {
String sameFileName = BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ;
ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance(
- mortgageCalculatorWorkbookFile ) ;
+ mortgageCalculatorWorkbookFile) ;
ExcelAntWorkbookUtil util2 = ExcelAntWorkbookUtilFactory.getInstance(
- sameFileName ) ;
+ sameFileName) ;
- assertNotNull( util ) ;
- assertNotNull( util2 ) ;
+ assertNotNull(util) ;
+ assertNotNull(util2) ;
- assertEquals( util, util2 ) ;
+ assertEquals(util, util2) ;
}
-
}
private static final Set<String> DIGIT_ENDING_FUNCTION_NAMES_SET = new HashSet<String>(Arrays.asList(DIGIT_ENDING_FUNCTION_NAMES));
public static FunctionMetadataRegistry createRegistry() {
- InputStream is = FunctionMetadataReader.class.getResourceAsStream(METADATA_FILE_NAME);
- if (is == null) {
- throw new RuntimeException("resource '" + METADATA_FILE_NAME + "' not found");
- }
-
- BufferedReader br;
- try {
- br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
- FunctionDataBuilder fdb = new FunctionDataBuilder(400);
-
- try {
- while (true) {
- String line = br.readLine();
- if (line == null) {
- break;
- }
- if (line.length() < 1 || line.charAt(0) == '#') {
- continue;
- }
- String trimLine = line.trim();
- if (trimLine.length() < 1) {
- continue;
- }
- processLine(fdb, line);
- }
- br.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
-
- return fdb.build();
+ try {
+ InputStream is = FunctionMetadataReader.class.getResourceAsStream(METADATA_FILE_NAME);
+ if (is == null) {
+ throw new RuntimeException("resource '" + METADATA_FILE_NAME + "' not found");
+ }
+
+ try {
+ BufferedReader br;
+ try {
+ br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
+ } catch(UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+
+ try {
+ FunctionDataBuilder fdb = new FunctionDataBuilder(400);
+
+ while (true) {
+ String line = br.readLine();
+ if (line == null) {
+ break;
+ }
+ if (line.length() < 1 || line.charAt(0) == '#') {
+ continue;
+ }
+ String trimLine = line.trim();
+ if (trimLine.length() < 1) {
+ continue;
+ }
+ processLine(fdb, line);
+ }
+
+ return fdb.build();
+ } finally {
+ br.close();
+ }
+ } finally {
+ is.close();
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
}
private static void processLine(FunctionDataBuilder fdb, String line) {
* @author Evgeniy Berlog\r
* @date 25.06.12\r
*/\r
+@SuppressWarnings("deprecation")\r
public class TestText {\r
\r
@Test\r