poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1649311 - in /poi/trunk: src/excelant/java/org/apache/poi/ss/excelant/util/ src/excelant/testcases/org/apache/poi/ss/excelant/util/ test-data/spreadsheet/
Date Sun, 04 Jan 2015 10:23:45 GMT
Author: centic
Date: Sun Jan  4 10:23:45 2015
New Revision: 1649311

URL: http://svn.apache.org/r1649311
Log:
Update tests for ExcelAntWorkbookUtil, remove some unnecessary null-checks that can never
happen, fix setting failed state on error in evaluation

Modified:
    poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntEvaluationResult.java
    poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtil.java
    poi/trunk/src/excelant/testcases/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java
    poi/trunk/test-data/spreadsheet/excelant.xls

Modified: poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntEvaluationResult.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntEvaluationResult.java?rev=1649311&r1=1649310&r2=1649311&view=diff
==============================================================================
--- poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntEvaluationResult.java
(original)
+++ poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntEvaluationResult.java
Sun Jan  4 10:23:45 2015
@@ -30,7 +30,7 @@ public class ExcelAntEvaluationResult {
 	/**
 	 * This boolean flag is used to determine if the evaluation completed
 	 * without error.  This alone doesn't ensure that the evaluation was 
-	 * sucessful.
+	 * successful.
 	 */
 	private boolean evaluationCompletedWithError ;
 	
@@ -108,7 +108,5 @@ public class ExcelAntEvaluationResult {
 				+ ", returnValue=" + returnValue + ", errorMessage="
 				+ errorMessage + ", actualDelta=" + actualDelta + ", cellName="
 				+ cellName + "]";
-	}
-
-	
+	}	
 }

Modified: poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtil.java?rev=1649311&r1=1649310&r2=1649311&view=diff
==============================================================================
--- poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtil.java
(original)
+++ poi/trunk/src/excelant/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtil.java
Sun Jan  4 10:23:45 2015
@@ -52,7 +52,7 @@ public class ExcelAntWorkbookUtil extend
 
     private Workbook workbook;
 
-    private HashMap<String, FreeRefFunction> xlsMacroList;
+    private final HashMap<String, FreeRefFunction> xlsMacroList = new HashMap<String,
FreeRefFunction>();
 
     /**
      * Constructs an instance using a String that contains the fully qualified
@@ -63,7 +63,6 @@ public class ExcelAntWorkbookUtil extend
      */
     protected ExcelAntWorkbookUtil(String fName) {
         excelFileName = fName;
-        xlsMacroList = new HashMap<String, FreeRefFunction>() ;
         loadWorkbook();
 
     }
@@ -75,7 +74,6 @@ public class ExcelAntWorkbookUtil extend
      */
     protected ExcelAntWorkbookUtil(Workbook wb) {
         workbook = wb;
-        xlsMacroList = new HashMap<String, FreeRefFunction>() ;
     }
 
     /**
@@ -164,14 +162,14 @@ public class ExcelAntWorkbookUtil extend
     protected FormulaEvaluator getEvaluator( String fileName ) {
         FormulaEvaluator evaluator ;
         if (fileName.endsWith(".xlsx")) {
-            if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
+            if( xlsMacroList.size() > 0 ) {
                 evaluator = XSSFFormulaEvaluator.create( (XSSFWorkbook) workbook,
                                                          null,
                                                          getFunctions() ) ;
             }
             evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
         } else {
-            if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
+            if( xlsMacroList.size() > 0 ) {
                 evaluator = HSSFFormulaEvaluator.create( (HSSFWorkbook)workbook,
                                                          null,
                                                          getFunctions() ) ;
@@ -312,7 +310,7 @@ public class ExcelAntWorkbookUtil extend
                                 Byte.toString( resultOfEval.getErrorValue() ) ;
             }
 
-            evalResults = new ExcelAntEvaluationResult(false, false,
+            evalResults = new ExcelAntEvaluationResult(true, false,
                     resultOfEval.getNumberValue(),
                     "Evaluation failed due to an evaluation error of "
                             + resultOfEval.getErrorValue()
@@ -331,10 +329,7 @@ public class ExcelAntWorkbookUtil extend
      */
     public String getCellAsString( String cellName ) {
     	Cell cell = getCell( cellName ) ;
-    	if( cell != null ) {
-    		return cell.getStringCellValue() ;
-    	}
-    	return "" ;
+		return cell.getStringCellValue() ;
     }
 
 
@@ -346,10 +341,7 @@ public class ExcelAntWorkbookUtil extend
      */
     public double getCellAsDouble( String cellName ) {
     	Cell cell = getCell( cellName ) ;
-    	if( cell != null ) {
-    		return cell.getNumericCellValue() ;
-    	}
-    	return 0.0 ;
+		return cell.getNumericCellValue() ;
     }
     /**
      * Returns a cell reference based on a String in standard Excel format
@@ -360,7 +352,6 @@ public class ExcelAntWorkbookUtil extend
      * @return
      */
     private Cell getCell(String cellName) {
-
         CellReference cellRef = new CellReference(cellName);
         String sheetName = cellRef.getSheetName();
         Sheet sheet = workbook.getSheet(sheetName);
@@ -384,5 +375,4 @@ public class ExcelAntWorkbookUtil extend
 
         return cell;
     }
-
 }

Modified: poi/trunk/src/excelant/testcases/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/excelant/testcases/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java?rev=1649311&r1=1649310&r2=1649311&view=diff
==============================================================================
--- poi/trunk/src/excelant/testcases/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java
(original)
+++ poi/trunk/src/excelant/testcases/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java
Sun Jan  4 10:23:45 2015
@@ -48,15 +48,15 @@ public class TestExcelAntWorkbookUtil ex
 	}
 
 	public void testStringConstructor() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-				                                  mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+				                                  mortgageCalculatorFileName);
 		
-		assertNotNull( fixture ) ;
+		assertNotNull(fixture);
 	}
 
 	public void testLoadNotExistingFile() {
 		try {
-			assertNotNull(new ExcelAntWorkbookUtilTestHelper( 
+			assertNotNull(new ExcelAntWorkbookUtilTestHelper(
 				                                  "notexistingFile" ));
 			fail("Should catch exception here");
 		} catch (BuildException e) {
@@ -69,152 +69,287 @@ public class TestExcelAntWorkbookUtil ex
         FileInputStream fis = new FileInputStream(workbookFile);
         Workbook workbook = WorkbookFactory.create(fis);
 
-		fixture = new ExcelAntWorkbookUtilTestHelper( workbook ) ;
-		
-		assertNotNull( fixture ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(workbook);
 		
+		assertNotNull(fixture);
 	}
 	
 	public void testAddFunction() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 
-		assertNotNull( fixture ) ;
+		assertNotNull(fixture);
 		
-		fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction() ) ;
+		fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction());
 		
-		UDFFinder functions = fixture.getFunctions() ;
+		UDFFinder functions = fixture.getFunctions();
 		
-		assertNotNull( functions ) ;
-	}
+		assertNotNull(functions);
+		assertNotNull(functions.findFunction("h2_ZFactor"));
+	}
+
+    public void testAddFunctionClassName() throws Exception {
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+
+        assertNotNull(fixture);
+        
+        fixture.addFunction("h2_ZFactor", CalculateMortgageFunction.class.getName());
+        
+        UDFFinder functions = fixture.getFunctions();
+        
+        assertNotNull(functions);
+        assertNotNull(functions.findFunction("h2_ZFactor"));
+    }
+
+    public void testAddFunctionInvalidClassName() throws Exception {
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+
+        assertNotNull(fixture);
+        
+        fixture.addFunction("h2_ZFactor", String.class.getName());
+        
+        UDFFinder functions = fixture.getFunctions();
+        
+        assertNotNull(functions);
+        assertNull(functions.findFunction("h2_ZFactor"));
+    }
 
 	public void testGetWorkbook() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		assertNotNull( fixture ) ;
+		assertNotNull(fixture);
 		
-		Workbook workbook = fixture.getWorkbook() ;
+		Workbook workbook = fixture.getWorkbook();
 		
-		assertNotNull( workbook ) ;
+		assertNotNull(workbook);
 	}
 	
 	public void testFileName() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		assertNotNull( fixture ) ;
+		assertNotNull(fixture);
 
-		String fileName = fixture.getFileName() ;
+		String fileName = fixture.getFileName();
 		
-		assertNotNull( fileName ) ;
+		assertNotNull(fileName);
 		
-		assertEquals( mortgageCalculatorFileName, fileName ) ;
+		assertEquals(mortgageCalculatorFileName, fileName);
 		
 	}
 	
 	public void testGetEvaluator() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
-		
-		FormulaEvaluator evaluator = fixture.getEvaluator( 
-				                                  mortgageCalculatorFileName ) ;
-		
-		assertNotNull( evaluator ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
+		FormulaEvaluator evaluator = fixture.getEvaluator(
+				                                  mortgageCalculatorFileName);
 		
+		assertNotNull(evaluator);
  	}
 
+    public void testGetEvaluatorWithUDF() {
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+
+        fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction());
+        
+        FormulaEvaluator evaluator = fixture.getEvaluator(
+                                                  mortgageCalculatorFileName);
+        
+        assertNotNull(evaluator);
+    }
+	
 	public void testGetEvaluatorXLSX() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx") ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx");
 		
-		FormulaEvaluator evaluator = fixture.getEvaluator( 
-				BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx" ) ;
+		FormulaEvaluator evaluator = fixture.getEvaluator(
+				BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx");
 		
-		assertNotNull( evaluator ) ;
+		assertNotNull(evaluator);
  	}
 
+    public void testGetEvaluatorXLSXWithFunction() {
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx");
+        
+        fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction());
+        
+        FormulaEvaluator evaluator = fixture.getEvaluator(
+                BuildFileTest.getDataDir() + "/spreadsheet/sample.xlsx");
+        
+        assertNotNull(evaluator);
+    }
+
 	public void testEvaluateCell() {
 		String cell = "'MortgageCalculator'!B4" ;
 		double expectedValue = 790.79 ;
 		double precision = 0.1 ;
 
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 
-		ExcelAntEvaluationResult result = fixture.evaluateCell( cell, 
+		ExcelAntEvaluationResult result = fixture.evaluateCell(cell, 
 				                                                expectedValue, 
-				                                                precision ) ;
-		
-		System.out.println(  result ) ;
-		
-		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"));
+
+		assertFalse(result.evaluationCompleteWithError());
+		assertTrue(result.didTestPass());
 	}
 	
+    public void testEvaluateCellFailedPrecision() {
+        String cell = "'MortgageCalculator'!B4" ;
+        double expectedValue = 790.79 ;
+        double precision = 0.0000000000001 ;
+
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+
+        ExcelAntEvaluationResult result = fixture.evaluateCell(cell, 
+                                                                expectedValue, 
+                                                                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"));
+
+        assertFalse(result.evaluationCompleteWithError());
+        assertFalse(result.didTestPass());
+    }
+    
+    public void testEvaluateCellWithError() {
+        String cell = "'ErrorCell'!A1" ;
+        double expectedValue = 790.79 ;
+        double precision = 0.1 ;
+
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+
+        ExcelAntEvaluationResult result = fixture.evaluateCell(cell, 
+                                                                expectedValue, 
+                                                                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(result.evaluationCompleteWithError());
+        assertFalse(result.didTestPass());
+    }
+    
 	public void testGetSheets() {
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		ArrayList<String> sheets = fixture.getSheets() ;
+		ArrayList<String> sheets = fixture.getSheets();
 		
-		assertNotNull( sheets ) ;
-		assertEquals( sheets.size(), 3 ) ; 
+		assertNotNull(sheets);
+		assertEquals(sheets.size(), 3); 
 	}
 	
 	public void testSetString() {
 		String cell = "'MortgageCalculator'!C14" ;
 		String cellValue = "testString" ;
 		
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
-		
-		fixture.setStringValue( cell, cellValue ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		String value = fixture.getCellAsString( cell ) ;
+		fixture.setStringValue(cell, cellValue);
 		
-		assertNotNull( value ) ;
-		
-		assertEquals( cellValue, value ) ;
+		String value = fixture.getCellAsString(cell);
 		
+		assertNotNull(value);
+		assertEquals(cellValue, value);
 	}
 	
+    public void testSetNotExistingSheet() {
+        String cell = "'NotexistingSheet'!C14" ;
+        
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+        try {
+            fixture.setStringValue(cell, "some");
+            fail("Should catch exception here");
+        } catch (BuildException e) {
+            assertTrue(e.getMessage().contains("NotexistingSheet"));
+        }
+    }
+
+    public void testSetFormula() {
+        String cell = "'MortgageCalculator'!C14" ;
+        String cellValue = "SUM(B14:B18)" ;
+        
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+        
+        fixture.setFormulaValue(cell, cellValue);
+        
+        double value = fixture.getCellAsDouble(cell);
+        
+        assertNotNull(value);
+        assertEquals(0.0, value);
+    }
+    
+    public void testSetDoubleValue() {
+        String cell = "'MortgageCalculator'!C14" ;
+        double cellValue = 1.2;
+        
+        fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
+        
+        fixture.setDoubleValue(cell, cellValue);
+        
+        Double value = fixture.getCellAsDouble(cell);
+        
+        assertNotNull(value);
+        assertEquals(cellValue, value);
+    }
+    
 	public void testSetDate() {
 		String cell = "'MortgageCalculator'!C14" ;
 		Date cellValue = new Date();
 		
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
-		
-		fixture.setDateValue( cell, cellValue ) ;
-		
-		double value = fixture.getCellAsDouble( cell ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		assertNotNull( value ) ;
+		fixture.setDateValue(cell, cellValue);
 		
-		assertEquals( DateUtil.getExcelDate(cellValue, false), value ) ;
+		double value = fixture.getCellAsDouble(cell);
 		
+		assertNotNull(value);
+		assertEquals(DateUtil.getExcelDate(cellValue, false), value);
 	}
 
 	public void testGetNonexistingString() {
 		String cell = "'MortgageCalculator'!C33" ;
 		
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		String value = fixture.getCellAsString( cell ) ;
+		String value = fixture.getCellAsString(cell);
 		
-		assertEquals( "", value ) ;
+		assertEquals("", value);
 	}
 
 	public void testGetNonexistingDouble() {
 		String cell = "'MortgageCalculator'!C33" ;
 		
-		fixture = new ExcelAntWorkbookUtilTestHelper( 
-                mortgageCalculatorFileName ) ;
+		fixture = new ExcelAntWorkbookUtilTestHelper(
+                mortgageCalculatorFileName);
 		
-		double value = fixture.getCellAsDouble( cell ) ;
+		double value = fixture.getCellAsDouble(cell);
 		
-		assertEquals( 0.0, value ) ;
+		assertEquals(0.0, value);
 	}
 }

Modified: poi/trunk/test-data/spreadsheet/excelant.xls
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/excelant.xls?rev=1649311&r1=1649310&r2=1649311&view=diff
==============================================================================
Binary files - no diff available.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message