Eclipse warnings, code formatting, simplify ExcelAntWorkbookUtilFactory, always close resources, ...

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1703028 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-09-14 18:40:23 +00:00
parent a5918f26fe
commit 8f604e8563
8 changed files with 190 additions and 168 deletions

View File

@ -21,8 +21,8 @@ package org.apache.poi.ss.excelant.util;
* A simple class that encapsulates information about a cell evaluation * A simple class that encapsulates information about a cell evaluation
* from POI. * from POI.
* *
* @author Jon Svede ( jon [at] loquatic [dot] com ) * @author Jon Svede (jon [at] loquatic [dot] com)
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
* *
*/ */
public class ExcelAntEvaluationResult { public class ExcelAntEvaluationResult {
@ -62,12 +62,12 @@ public class ExcelAntEvaluationResult {
public ExcelAntEvaluationResult( boolean completedWithError, public ExcelAntEvaluationResult(boolean completedWithError,
boolean passed, boolean passed,
double retValue, double retValue,
String errMessage, String errMessage,
double delta, double delta,
String cellId ) { String cellId) {
evaluationCompletedWithError = completedWithError; evaluationCompletedWithError = completedWithError;
didPass = passed; didPass = passed;

View File

@ -17,13 +17,28 @@
package org.apache.poi.ss.excelant.util; 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.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.udf.AggregatingUDFFinder; import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
import org.apache.poi.ss.formula.udf.DefaultUDFFinder; import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
import org.apache.poi.ss.formula.udf.UDFFinder; 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.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -31,19 +46,12 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Typedef; 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 * A general utility class that abstracts the POI details of loading the
* workbook, accessing and updating cells. * workbook, accessing and updating cells.
* *
* @author Jon Svede ( jon [at] loquatic [dot] com ) * @author Jon Svede (jon [at] loquatic [dot] com)
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
* *
*/ */
public class ExcelAntWorkbookUtil extends Typedef { public class ExcelAntWorkbookUtil extends Typedef {
@ -52,7 +60,7 @@ public class ExcelAntWorkbookUtil extends Typedef {
private Workbook workbook; 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 * Constructs an instance using a String that contains the fully qualified
@ -95,7 +103,7 @@ public class ExcelAntWorkbookUtil extends Typedef {
+ ". Make sure the path and file permissions are correct.", e); + ". Make sure the path and file permissions are correct.", e);
} }
return workbook ; return workbook;
} }
/** /**
@ -106,11 +114,11 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @throws InstantiationException * @throws InstantiationException
* @throws IllegalAccessException * @throws IllegalAccessException
*/ */
public void addFunction( String name, String clazzName ) throws ClassNotFoundException, InstantiationException, IllegalAccessException { public void addFunction(String name, String clazzName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> clazzInst = Class.forName( clazzName ) ; Class<?> clazzInst = Class.forName(clazzName);
Object newInst = clazzInst.newInstance() ; Object newInst = clazzInst.newInstance();
if( newInst instanceof FreeRefFunction ) { if(newInst instanceof FreeRefFunction) {
addFunction( name, (FreeRefFunction)newInst ) ; addFunction(name, (FreeRefFunction)newInst);
} }
} }
@ -136,13 +144,10 @@ public class ExcelAntWorkbookUtil extends Typedef {
String[] names = new String[xlsMacroList.size()]; String[] names = new String[xlsMacroList.size()];
FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()]; FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
Iterator<String> keysIt = xlsMacroList.keySet().iterator();
int x = 0; int x = 0;
while (keysIt.hasNext()) { for(Map.Entry<String, FreeRefFunction> entry : xlsMacroList.entrySet()) {
String name = keysIt.next(); names[x] = entry.getKey();
FreeRefFunction function = xlsMacroList.get(name); functions[x] = entry.getValue();
names[x] = name;
functions[x] = function;
} }
UDFFinder udff1 = new DefaultUDFFinder(names, functions); UDFFinder udff1 = new DefaultUDFFinder(names, functions);
@ -159,26 +164,26 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param fileName * @param fileName
* @return * @return
*/ */
protected FormulaEvaluator getEvaluator( String fileName ) { protected FormulaEvaluator getEvaluator(String fileName) {
FormulaEvaluator evaluator ; FormulaEvaluator evaluator;
if (fileName.endsWith(".xlsx")) { if (fileName.endsWith(".xlsx")) {
if( xlsMacroList.size() > 0 ) { if(xlsMacroList.size() > 0) {
evaluator = XSSFFormulaEvaluator.create( (XSSFWorkbook) workbook, evaluator = XSSFFormulaEvaluator.create((XSSFWorkbook) workbook,
null, null,
getFunctions() ) ; getFunctions());
} }
evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook); evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
} else { } else {
if( xlsMacroList.size() > 0 ) { if(xlsMacroList.size() > 0) {
evaluator = HSSFFormulaEvaluator.create( (HSSFWorkbook)workbook, evaluator = HSSFFormulaEvaluator.create((HSSFWorkbook)workbook,
null, null,
getFunctions() ) ; getFunctions());
} }
evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook); evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
} }
return evaluator ; return evaluator;
} }
@ -206,16 +211,16 @@ public class ExcelAntWorkbookUtil extends Typedef {
* *
* @return * @return
*/ */
public ArrayList<String> getSheets() { public List<String> getSheets() {
ArrayList<String> sheets = new ArrayList<String>() ; ArrayList<String> sheets = new ArrayList<String>();
int sheetCount = workbook.getNumberOfSheets() ; int sheetCount = workbook.getNumberOfSheets();
for( int x=0; x<sheetCount; x++ ) { for(int x=0; x<sheetCount; x++) {
sheets.add( workbook.getSheetName( x ) ) ; sheets.add(workbook.getSheetName(x));
} }
return sheets ; return sheets;
} }
/** /**
@ -241,7 +246,7 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param cellName * @param cellName
* @param value * @param value
*/ */
public void setStringValue( String cellName, String value ) { public void setStringValue(String cellName, String value) {
Cell cell = getCell(cellName); Cell cell = getCell(cellName);
cell.setCellValue(value); cell.setCellValue(value);
} }
@ -252,9 +257,9 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param cellName * @param cellName
* @param formula * @param formula
*/ */
public void setFormulaValue( String cellName, String formula ) { public void setFormulaValue(String cellName, String formula) {
Cell cell = getCell(cellName); Cell cell = getCell(cellName);
cell.setCellFormula( formula ); cell.setCellFormula(formula);
} }
/** /**
@ -262,9 +267,9 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param cellName * @param cellName
* @param date * @param date
*/ */
public void setDateValue( String cellName, Date date ) { public void setDateValue(String cellName, Date date) {
Cell cell = getCell(cellName); Cell cell = getCell(cellName);
cell.setCellValue( date ) ; cell.setCellValue(date);
} }
/** /**
* Uses a String in standard Excel format (SheetName!CellId) to locate a * Uses a String in standard Excel format (SheetName!CellId) to locate a
@ -281,7 +286,7 @@ public class ExcelAntWorkbookUtil extends Typedef {
Cell cell = getCell(cellName); Cell cell = getCell(cellName);
FormulaEvaluator evaluator = getEvaluator( excelFileName ); FormulaEvaluator evaluator = getEvaluator(excelFileName);
CellValue resultOfEval = evaluator.evaluate(cell); CellValue resultOfEval = evaluator.evaluate(cell);
@ -294,20 +299,19 @@ public class ExcelAntWorkbookUtil extends Typedef {
evalResults = new ExcelAntEvaluationResult(false, false, evalResults = new ExcelAntEvaluationResult(false, false,
resultOfEval.getNumberValue(), resultOfEval.getNumberValue(),
"Results was out of range based on precision " + " of " "Results was out of range based on precision " + " of "
+ precision + ". Delta was actually " + delta, delta, cellName ); + precision + ". Delta was actually " + delta, delta, cellName);
} else { } else {
evalResults = new ExcelAntEvaluationResult(false, true, evalResults = new ExcelAntEvaluationResult(false, true,
resultOfEval.getNumberValue(), resultOfEval.getNumberValue(),
"Evaluation passed without error within in range.", delta, cellName ); "Evaluation passed without error within in range.", delta, cellName);
} }
} else { } else {
String errorMeaning = null ; String errorMeaning = null;
try { try {
errorMeaning = ErrorConstants.getText( resultOfEval errorMeaning = FormulaError.forInt(resultOfEval.getErrorValue()).getString();
.getErrorValue() ) ; } catch(IllegalArgumentException iae) {
} catch( IllegalArgumentException iae ) {
errorMeaning = "unknown error code: " + errorMeaning = "unknown error code: " +
Byte.toString( resultOfEval.getErrorValue() ) ; Byte.toString(resultOfEval.getErrorValue());
} }
evalResults = new ExcelAntEvaluationResult(true, false, evalResults = new ExcelAntEvaluationResult(true, false,
@ -315,7 +319,7 @@ public class ExcelAntWorkbookUtil extends Typedef {
"Evaluation failed due to an evaluation error of " "Evaluation failed due to an evaluation error of "
+ resultOfEval.getErrorValue() + resultOfEval.getErrorValue()
+ " which is " + " which is "
+ errorMeaning, 0, cellName ); + errorMeaning, 0, cellName);
} }
return evalResults; return evalResults;
@ -327,9 +331,9 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param cellName * @param cellName
* @return * @return
*/ */
public String getCellAsString( String cellName ) { public String getCellAsString(String cellName) {
Cell cell = getCell( cellName ) ; Cell cell = getCell(cellName);
return cell.getStringCellValue() ; return cell.getStringCellValue();
} }
@ -339,9 +343,9 @@ public class ExcelAntWorkbookUtil extends Typedef {
* @param cellName * @param cellName
* @return * @return
*/ */
public double getCellAsDouble( String cellName ) { public double getCellAsDouble(String cellName) {
Cell cell = getCell( cellName ) ; Cell cell = getCell(cellName);
return cell.getNumericCellValue() ; return cell.getNumericCellValue();
} }
/** /**
* Returns a cell reference based on a String in standard Excel format * Returns a cell reference based on a String in standard Excel format
@ -363,14 +367,14 @@ public class ExcelAntWorkbookUtil extends Typedef {
int colIdx = cellRef.getCol(); int colIdx = cellRef.getCol();
Row row = sheet.getRow(rowIdx); Row row = sheet.getRow(rowIdx);
if( row == null ) { if(row == null) {
row = sheet.createRow( rowIdx ) ; row = sheet.createRow(rowIdx);
} }
Cell cell = row.getCell(colIdx); Cell cell = row.getCell(colIdx);
if( cell == null ) { if(cell == null) {
cell = row.createCell( colIdx ) ; cell = row.createCell(colIdx);
} }
return cell; return cell;

View File

@ -18,24 +18,22 @@
package org.apache.poi.ss.excelant.util; package org.apache.poi.ss.excelant.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
* This is a factory class maps file names to WorkbookUtil instances. This * This is a factory class maps file names to WorkbookUtil instances. This
* helps ExcelAnt be more efficient when being run many times in an Ant build. * helps ExcelAnt be more efficient when being run many times in an Ant build.
* *
* @author Jon Svede ( jon [at] loquatic [dot] com ) * @author Jon Svede (jon [at] loquatic [dot] com)
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
* *
*/ */
public class ExcelAntWorkbookUtilFactory { public final class ExcelAntWorkbookUtilFactory {
private static HashMap<String, ExcelAntWorkbookUtil> workbookUtilMap ; private static Map<String, ExcelAntWorkbookUtil> workbookUtilMap;
private static ExcelAntWorkbookUtilFactory factory ;
private ExcelAntWorkbookUtilFactory() { private ExcelAntWorkbookUtilFactory() {
workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>() ;
} }
/** /**
@ -45,19 +43,17 @@ public class ExcelAntWorkbookUtilFactory {
* @param fileName * @param fileName
* @return * @return
*/ */
public static ExcelAntWorkbookUtil getInstance( String fileName ) { public static ExcelAntWorkbookUtil getInstance(String fileName) {
if(workbookUtilMap == null) {
if( factory == null ) { workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>();
factory = new ExcelAntWorkbookUtilFactory() ;
} }
if( workbookUtilMap != null && if(workbookUtilMap != null &&
workbookUtilMap.containsKey( fileName ) ) { workbookUtilMap.containsKey(fileName)) {
return workbookUtilMap.get( fileName ) ; return workbookUtilMap.get(fileName);
} }
ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil( fileName ) ; ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName);
workbookUtilMap.put( fileName, wbu ) ; workbookUtilMap.put(fileName, wbu);
return wbu ; return wbu;
} }
} }

View File

@ -16,52 +16,61 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.excelant.util; 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 { import org.junit.After;
import org.junit.Before;
import org.junit.Test;
private ExcelAntEvaluationResult fixture ; public class TestExcelAntEvaluationResult {
private ExcelAntEvaluationResult fixture;
private boolean completedWithError = false ; private boolean completedWithError = false;
private boolean passed = false ; private boolean passed = false;
private double retValue = 1.1 ; private double retValue = 1.1;
private String errMessage = "error message" ; private String errMessage = "error message";
private double delta = 2.2 ; private double delta = 2.2;
private String cellId = "testCell!$F$1" ; private String cellId = "testCell!$F$1";
@Before
public void setUp() { public void setUp() {
fixture = new ExcelAntEvaluationResult( completedWithError, fixture = new ExcelAntEvaluationResult(completedWithError,
passed, passed,
retValue, retValue,
errMessage, errMessage,
delta, delta,
cellId ) ; cellId);
} }
@After
public void tearDown() { public void tearDown() {
fixture = null ; fixture = null;
} }
@Test
public void testCompletedWithErrorMessage() { public void testCompletedWithErrorMessage() {
String errMsg = fixture.getErrorMessage() ; String errMsg = fixture.getErrorMessage();
assertNotNull( errMsg ) ; assertNotNull(errMsg);
assertEquals( errMsg, errMessage ) ; assertEquals(errMsg, errMessage);
} }
@Test
public void testPassed() { public void testPassed() {
boolean passedValue = fixture.didTestPass() ; boolean passedValue = fixture.didTestPass();
assertEquals( passedValue, passed ) ; assertEquals(passedValue, passed);
} }
@Test
public void testDelta() { public void testDelta() {
double deltaValue = fixture.getDelta() ; double deltaValue = fixture.getDelta();
assertEquals(deltaValue, delta, 0.0 ) ; assertEquals(deltaValue, delta, 0.0);
} }
@Test
public void testCellId() { public void testCellId() {
String cellIdValue = fixture.getCellName() ; String cellIdValue = fixture.getCellName();
assertNotNull( cellIdValue ) ; assertNotNull(cellIdValue);
assertEquals( cellIdValue, cellId ) ; assertEquals(cellIdValue, cellId);
} }
} }

View File

@ -19,10 +19,8 @@ package org.apache.poi.ss.excelant.util;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.examples.formula.CalculateMortgageFunction; import org.apache.poi.ss.examples.formula.CalculateMortgageFunction;
@ -34,6 +32,8 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import junit.framework.TestCase;
public class TestExcelAntWorkbookUtil extends TestCase { public class TestExcelAntWorkbookUtil extends TestCase {
private static final String mortgageCalculatorFileName = private static final String mortgageCalculatorFileName =
@ -198,9 +198,10 @@ public class TestExcelAntWorkbookUtil extends TestCase {
precision); precision);
//System.out.println(result); //System.out.println(result);
assertTrue(result.toString().contains("evaluationCompletedWithError=false")); assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=false"));
assertTrue(result.toString().contains("returnValue=790.79")); assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=790.79"));
assertTrue(result.toString().contains("cellName='MortgageCalculator'!B4")); assertTrue("Had:" + result.toString(), result.toString().contains("cellName='MortgageCalculator'!B4"));
assertFalse(result.toString().contains("#N/A"));
assertFalse(result.evaluationCompleteWithError()); assertFalse(result.evaluationCompleteWithError());
assertTrue(result.didTestPass()); assertTrue(result.didTestPass());
@ -219,9 +220,10 @@ public class TestExcelAntWorkbookUtil extends TestCase {
precision); precision);
//System.out.println(result); //System.out.println(result);
assertTrue(result.toString().contains("evaluationCompletedWithError=false")); assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=false"));
assertTrue(result.toString().contains("returnValue=790.79")); assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=790.79"));
assertTrue(result.toString().contains("cellName='MortgageCalculator'!B4")); 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.evaluationCompleteWithError());
assertFalse(result.didTestPass()); assertFalse(result.didTestPass());
@ -240,9 +242,10 @@ public class TestExcelAntWorkbookUtil extends TestCase {
precision); precision);
System.out.println(result); System.out.println(result);
assertTrue(result.toString().contains("evaluationCompletedWithError=true")); assertTrue("Had:" + result.toString(), result.toString().contains("evaluationCompletedWithError=true"));
assertTrue(result.toString().contains("returnValue=0.0")); assertTrue("Had:" + result.toString(), result.toString().contains("returnValue=0.0"));
assertTrue(result.toString().contains("cellName='ErrorCell'!A1")); assertTrue("Had:" + result.toString(), result.toString().contains("cellName='ErrorCell'!A1"));
assertTrue("Had:" + result.toString(), result.toString().contains("#N/A"));
assertTrue(result.evaluationCompleteWithError()); assertTrue(result.evaluationCompleteWithError());
assertFalse(result.didTestPass()); assertFalse(result.didTestPass());
@ -252,7 +255,7 @@ public class TestExcelAntWorkbookUtil extends TestCase {
fixture = new ExcelAntWorkbookUtilTestHelper( fixture = new ExcelAntWorkbookUtilTestHelper(
mortgageCalculatorFileName); mortgageCalculatorFileName);
ArrayList<String> sheets = fixture.getSheets(); List<String> sheets = fixture.getSheets();
assertNotNull(sheets); assertNotNull(sheets);
assertEquals(sheets.size(), 3); assertEquals(sheets.size(), 3);

View File

@ -16,19 +16,21 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.excelant.util; 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.apache.poi.ss.excelant.BuildFileTest;
import org.junit.Test;
/** /**
* Tests for the ExcelAntWorbookUtilFactory. * Tests for the ExcelAntWorbookUtilFactory.
* *
* @author Jon Svede ( jon [at] loquatic [dot] com ) * @author Jon Svede (jon [at] loquatic [dot] com)
* @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) * @author Brian Bush (brian [dot] bush [at] nrel [dot] gov)
* *
*/ */
public class TestExcelAntWorkbookUtilFactory extends TestCase{ public class TestExcelAntWorkbookUtilFactory {
private static final String mortgageCalculatorWorkbookFile = private static final String mortgageCalculatorWorkbookFile =
BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ;
@ -38,13 +40,12 @@ public class TestExcelAntWorkbookUtilFactory extends TestCase{
* Simple test to determine if the factory properly returns an non-null * Simple test to determine if the factory properly returns an non-null
* instance of the ExcelAntWorkbookUtil class. * instance of the ExcelAntWorkbookUtil class.
*/ */
@Test
public void testGetNewWorkbookUtilInstance() { public void testGetNewWorkbookUtilInstance() {
ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance( ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance(
mortgageCalculatorWorkbookFile ) ; mortgageCalculatorWorkbookFile) ;
assertNotNull( util ) ;
assertNotNull(util) ;
} }
@ -53,19 +54,19 @@ public class TestExcelAntWorkbookUtilFactory extends TestCase{
* to an ExcelAnt WorkbookUtil when two different Strings, that point to * to an ExcelAnt WorkbookUtil when two different Strings, that point to
* the same resource, are passed in. * the same resource, are passed in.
*/ */
@Test
public void testVerifyEquivalence() { public void testVerifyEquivalence() {
String sameFileName = BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; String sameFileName = BuildFileTest.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ;
ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance( ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance(
mortgageCalculatorWorkbookFile ) ; mortgageCalculatorWorkbookFile) ;
ExcelAntWorkbookUtil util2 = ExcelAntWorkbookUtilFactory.getInstance( ExcelAntWorkbookUtil util2 = ExcelAntWorkbookUtilFactory.getInstance(
sameFileName ) ; sameFileName) ;
assertNotNull( util ) ; assertNotNull(util) ;
assertNotNull( util2 ) ; assertNotNull(util2) ;
assertEquals( util, util2 ) ; assertEquals(util, util2) ;
} }
} }

View File

@ -53,40 +53,48 @@ final class FunctionMetadataReader {
private static final Set<String> DIGIT_ENDING_FUNCTION_NAMES_SET = new HashSet<String>(Arrays.asList(DIGIT_ENDING_FUNCTION_NAMES)); private static final Set<String> DIGIT_ENDING_FUNCTION_NAMES_SET = new HashSet<String>(Arrays.asList(DIGIT_ENDING_FUNCTION_NAMES));
public static FunctionMetadataRegistry createRegistry() { public static FunctionMetadataRegistry createRegistry() {
InputStream is = FunctionMetadataReader.class.getResourceAsStream(METADATA_FILE_NAME); try {
if (is == null) { InputStream is = FunctionMetadataReader.class.getResourceAsStream(METADATA_FILE_NAME);
throw new RuntimeException("resource '" + METADATA_FILE_NAME + "' not found"); if (is == null) {
} throw new RuntimeException("resource '" + METADATA_FILE_NAME + "' not found");
}
BufferedReader br; try {
try { BufferedReader br;
br = new BufferedReader(new InputStreamReader(is,"UTF-8")); try {
} catch(UnsupportedEncodingException e) { br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
throw new RuntimeException(e); } catch(UnsupportedEncodingException e) {
} throw new RuntimeException(e);
FunctionDataBuilder fdb = new FunctionDataBuilder(400); }
try { try {
while (true) { FunctionDataBuilder fdb = new FunctionDataBuilder(400);
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(); 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) { private static void processLine(FunctionDataBuilder fdb, String line) {

View File

@ -31,6 +31,7 @@ import org.junit.Test;
* @author Evgeniy Berlog * @author Evgeniy Berlog
* @date 25.06.12 * @date 25.06.12
*/ */
@SuppressWarnings("deprecation")
public class TestText { public class TestText {
@Test @Test