]> source.dussan.org Git - poi.git/commitdiff
Bug #44733 - DPRODUCT function should be index 189, not 191
authorJosh Micich <josh@apache.org>
Tue, 1 Apr 2008 22:18:25 +0000 (22:18 +0000)
committerJosh Micich <josh@apache.org>
Tue, 1 Apr 2008 22:18:25 +0000 (22:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@643625 13f79535-47bb-0310-9956-ffa450edef68

src/resources/main/org/apache/poi/hssf/record/formula/function/functionMetadata.txt
src/testcases/org/apache/poi/hssf/data/missingFuncs44675.xls
src/testcases/org/apache/poi/hssf/record/formula/AllFormulaTests.java
src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java
src/testcases/org/apache/poi/hssf/record/formula/function/TestReadMissingBuiltInFuncs.java

index 237fbf094f20c231cce044f7401d33e259493c14..590b455c4716267790689ba849b8f12b99419abb 100644 (file)
 169    COUNTA  0       30      V       R               \r
 183    PRODUCT 0       30      V       R               \r
 184    FACT    1       1       V       V               \r
+189    DPRODUCT        3       3       V       R R R           \r
 190    ISNONTEXT       1       1       V       V               \r
-191    DPRODUCT        3       3       V       R R R           \r
 193    STDEVP  1       30      V       R               \r
 194    VARP    1       30      V       R               \r
 195    DSTDEVP 3       3       V       R R R           \r
index 71f874370ffe246151e5ed0b07a5a6f3b13d641b..4a68472450d05e10e9947ac06d505f2944726e8a 100644 (file)
Binary files a/src/testcases/org/apache/poi/hssf/data/missingFuncs44675.xls and b/src/testcases/org/apache/poi/hssf/data/missingFuncs44675.xls differ
index 645709bb6e355e79fff7023b7f8dea005ffa8116..3a704502230367f8bc7a7fcaebd254415cf076cf 100644 (file)
@@ -29,7 +29,7 @@ import junit.framework.TestSuite;
  * 
  * @author Josh Micich
  */
-public class AllFormulaTests {
+public final class AllFormulaTests {
        
        public static Test suite() {
                TestSuite result = new TestSuite(AllFormulaTests.class.getName());
@@ -50,7 +50,6 @@ public class AllFormulaTests {
                result.addTestSuite(TestReferencePtg.class);
                result.addTestSuite(TestSheetNameFormatter.class);
                result.addTestSuite(TestUnionPtg.class);
-               result.addTest(AllFormulaFunctionTests.suite());
                return result;
        }
 }
index 3671d37c1a8382f1b835696d345eaf366711c0ae..fdad5f44d8f9ef7c8788e105af648404066ad15d 100644 (file)
@@ -27,6 +27,9 @@ import org.apache.poi.hssf.record.formula.FuncPtg;
 import org.apache.poi.hssf.record.formula.FuncVarPtg;
 import org.apache.poi.hssf.record.formula.Ptg;
 /**
+ * Tests parsing of some built-in functions that were not properly
+ * registered in POI as bug #44675, #44733 (March/April 2008).
+ * 
  * @author Josh Micich
  */
 public final class TestParseMissingBuiltInFuncs extends TestCase {
@@ -39,6 +42,7 @@ public final class TestParseMissingBuiltInFuncs extends TestCase {
                Ptg[] ptgs = parse(formula);
                Ptg ptgF = ptgs[ptgs.length-1];  // func is last RPN token in all these formulas
                
+               // Check critical things in the Ptg array encoding.
                if(!(ptgF instanceof AbstractFunctionPtg)) {
                    throw new RuntimeException("function token missing");
                }
@@ -47,11 +51,15 @@ public final class TestParseMissingBuiltInFuncs extends TestCase {
                        throw new AssertionFailedError("Failed to recognise built-in function in formula '" 
                                        + formula + "'");
                }
-               
                assertEquals(expPtgArraySize, ptgs.length);
                assertEquals(funcIx, func.getFunctionIndex());
                Class expCls = isVarArgFunc ? FuncVarPtg.class : FuncPtg.class;
                assertEquals(expCls, ptgF.getClass());
+               
+               // check that parsed Ptg array converts back to formula text OK
+               Workbook book = Workbook.createWorkbook();
+               String reRenderedFormula = FormulaParser.toFormulaString(book, ptgs);
+               assertEquals(formula, reRenderedFormula);
        }
        
        public void testDatedif() {
@@ -76,4 +84,7 @@ public final class TestParseMissingBuiltInFuncs extends TestCase {
        public void testIsnontext() {
                confirmFunc("ISNONTEXT(\"abc\")", 2, false, 190);
        }
+       public void testDproduct() {
+               confirmFunc("DPRODUCT(C1:E5,\"HarvestYield\",G1:H2)", 4, false, 189);
+       }
 }
index f1e6bcfacf22d35eae424d8ee48c4ebccfc01764..14799bd0a77ed0467aa97e583ece3e3e8dd8c2dd 100644 (file)
@@ -21,7 +21,9 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
 
+import org.apache.poi.hssf.record.RecordFormatException;
 import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
@@ -29,24 +31,31 @@ import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 /**
  * Tests reading from a sample spreadsheet some built-in functions that were not properly
- * registered in POI as bug #44675 (March 2008).
+ * registered in POI as bug #44675, #44733 (March/April 2008).
  * 
  * @author Josh Micich
  */
 public final class TestReadMissingBuiltInFuncs extends TestCase {
 
-       private HSSFSheet sht;
+       /**
+        * This spreadsheet has examples of calls to the interesting built-in functions in cells A1:A7
+        */
+       private static final String SAMPLE_SPREADSHEET_FILE_NAME = "missingFuncs44675.xls";
+       private static HSSFSheet _sheet;
 
-       protected void setUp() {
-               String cwd = System.getProperty("HSSF.testdata.path");
-               HSSFWorkbook wb;
-               try {
-                       InputStream is = new FileInputStream(new File(cwd, "missingFuncs44675.xls"));
-                       wb = new HSSFWorkbook(is);
-               } catch (IOException e) {
-                       throw new RuntimeException(e);
+       private static HSSFSheet getSheet() {
+               if (_sheet == null) {
+                       String cwd = System.getProperty("HSSF.testdata.path");
+                       HSSFWorkbook wb;
+                       try {
+                               InputStream is = new FileInputStream(new File(cwd, SAMPLE_SPREADSHEET_FILE_NAME));
+                               wb = new HSSFWorkbook(is);
+                       } catch (IOException e) {
+                               throw new RuntimeException(e);
+                       }
+                       _sheet = wb.getSheetAt(0);
                }
-               sht = wb.getSheetAt(0);
+               return _sheet;
        }
 
        public void testDatedif() {
@@ -128,9 +137,30 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
                }
                assertEquals("ISNONTEXT(\"abc\")", formula);
        }
+       public void testDproduct() {
+               
+               String formula = getCellFormula(6);
+               assertEquals("DPRODUCT(C1:E5,\"HarvestYield\",G1:H2)", formula);
+       }
 
        private String getCellFormula(int rowIx) {
-               String result = sht.getRow(rowIx).getCell((short)0).getCellFormula();
+               HSSFSheet sheet;
+               try {
+                       sheet = getSheet();
+               } catch (RecordFormatException e) {
+                       if(e.getCause() instanceof InvocationTargetException) {
+                               InvocationTargetException ite = (InvocationTargetException) e.getCause();
+                               if(ite.getTargetException() instanceof RuntimeException) {
+                                       RuntimeException re = (RuntimeException) ite.getTargetException();
+                                       if(re.getMessage().equals("Invalid built-in function index (189)")) {
+                                               throw afe("DPRODUCT() registered with wrong index");
+                                       }
+                               }
+                       }
+                       // some other unexpected error
+                       throw e;
+               }
+               String result = sheet.getRow(rowIx).getCell((short)0).getCellFormula();
                if (false) {
                        System.err.println(result);
                }