aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases
diff options
context:
space:
mode:
authorJosh Micich <josh@apache.org>2009-09-17 00:00:57 +0000
committerJosh Micich <josh@apache.org>2009-09-17 00:00:57 +0000
commit30dcd662576e1fbbf52a6551c149582de2f1fef9 (patch)
tree05e02da2ec8e47921ea051f3d3ec75c96036ea28 /src/testcases
parenta82e264754e5a6e74968ab1032892283764538f3 (diff)
downloadpoi-30dcd662576e1fbbf52a6551c149582de2f1fef9.tar.gz
poi-30dcd662576e1fbbf52a6551c149582de2f1fef9.zip
Improvements to patch 47809 (support for UDFs)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@816016 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r--src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java136
-rw-r--r--src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java2
-rw-r--r--src/testcases/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java6
3 files changed, 63 insertions, 81 deletions
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java b/src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java
index b2937b0fcb..2cfbbfec41 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/eval/TestExternalFunction.java
@@ -17,97 +17,79 @@
package org.apache.poi.hssf.record.formula.eval;
-import java.io.IOException;
-
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
-import org.apache.poi.hssf.record.formula.toolpack.DefaultToolPack;
-import org.apache.poi.hssf.record.formula.toolpack.MainToolPacksHandler;
-import org.apache.poi.hssf.record.formula.toolpack.ToolPack;
+import org.apache.poi.hssf.record.formula.udf.DefaultUDFFinder;
+import org.apache.poi.hssf.record.formula.udf.AggregatingUDFFinder;
+import org.apache.poi.hssf.record.formula.udf.UDFFinder;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.OperationEvaluationContext;
-import org.apache.poi.ss.usermodel.Workbook;
/**
- *
* @author Josh Micich
- *
- * Modified 09/14/09 by Petr Udalau - Test of registering UDFs in workbook and
- * using ToolPacks.
+ * @author Petr Udalau - registering UDFs in workbook and using ToolPacks.
*/
public final class TestExternalFunction extends TestCase {
- private static class MyFunc implements FreeRefFunction {
- public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
- if (args.length != 1 || !(args[0] instanceof StringEval)) {
- return ErrorEval.VALUE_INVALID;
- } else {
- StringEval input = (StringEval) args[0];
- return new StringEval(input.getStringValue() + "abc");
- }
- }
- }
-
- private static class MyFunc2 implements FreeRefFunction {
- public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
- if (args.length != 1 || !(args[0] instanceof StringEval)) {
- return ErrorEval.VALUE_INVALID;
- } else {
- StringEval input = (StringEval) args[0];
- return new StringEval(input.getStringValue() + "abc2");
- }
- }
- }
-
- /**
- * Creates and registers user-defined function "MyFunc()" directly with POI.
- * This is VB function defined in "testNames.xls". In future there must be
- * some parser of VBA scripts which will register UDFs.
- */
- private void registerMyFunc(Workbook workbook) {
- workbook.registerUserDefinedFunction("myFunc", new MyFunc());
- }
-
- /**
- * Creates example ToolPack which contains function "MyFunc2()".
- */
- private void createExampleToolPack() {
- ToolPack exampleToolPack = new DefaultToolPack();
- exampleToolPack.addFunction("myFunc2", new MyFunc2());
- MainToolPacksHandler.instance().addToolPack(exampleToolPack);
- }
-
- /**
- * Checks that an external function can get invoked from the formula
- * evaluator.
- *
- * @throws IOException
- *
- */
- public void testInvoke() {
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
- HSSFSheet sheet = wb.getSheetAt(0);
-
- registerMyFunc(wb);
- createExampleToolPack();
-
- HSSFRow row = sheet.getRow(0);
- HSSFCell myFuncCell = row.getCell(1); //=myFunc("_")
-
- HSSFCell myFunc2Cell = row.getCell(2); //=myFunc2("_")
-
- HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
- try {
- assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
- assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
- } catch (Exception e) {
- assertFalse(true);
- }
- }
+ private static class MyFunc implements FreeRefFunction {
+ public MyFunc() {
+ //
+ }
+
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1 || !(args[0] instanceof StringEval)) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ StringEval input = (StringEval) args[0];
+ return new StringEval(input.getStringValue() + "abc");
+ }
+ }
+
+ private static class MyFunc2 implements FreeRefFunction {
+ public MyFunc2() {
+ //
+ }
+
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
+ if (args.length != 1 || !(args[0] instanceof StringEval)) {
+ return ErrorEval.VALUE_INVALID;
+ }
+ StringEval input = (StringEval) args[0];
+ return new StringEval(input.getStringValue() + "abc2");
+ }
+ }
+
+ /**
+ * Checks that an external function can get invoked from the formula
+ * evaluator.
+ */
+ public void testInvoke() {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
+ HSSFSheet sheet = wb.getSheetAt(0);
+
+ /**
+ * register the two test UDFs in a UDF finder, to be passed to the evaluator
+ */
+ UDFFinder udff1 = new DefaultUDFFinder(new String[] { "myFunc", },
+ new FreeRefFunction[] { new MyFunc(), });
+ UDFFinder udff2 = new DefaultUDFFinder(new String[] { "myFunc2", },
+ new FreeRefFunction[] { new MyFunc2(), });
+ UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
+
+
+ HSSFRow row = sheet.getRow(0);
+ HSSFCell myFuncCell = row.getCell(1); // =myFunc("_")
+
+ HSSFCell myFunc2Cell = row.getCell(2); // =myFunc2("_")
+
+ HSSFFormulaEvaluator fe = HSSFFormulaEvaluator.create(wb, null, udff);
+ assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
+ assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
+ }
}
diff --git a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
index 9035ecbedb..41aa0ab57b 100644
--- a/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
+++ b/src/testcases/org/apache/poi/ss/formula/TestWorkbookEvaluator.java
@@ -44,7 +44,7 @@ public class TestWorkbookEvaluator extends TestCase {
private static ValueEval evaluateFormula(Ptg[] ptgs) {
OperationEvaluationContext ec = new OperationEvaluationContext(null, null, 0, 0, 0, null);
- return new WorkbookEvaluator(null, null).evaluateFormula(ec, ptgs);
+ return new WorkbookEvaluator(null, null, null).evaluateFormula(ec, ptgs);
}
/**
diff --git a/src/testcases/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java b/src/testcases/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java
index b8b5de991e..673cb9500c 100644
--- a/src/testcases/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java
+++ b/src/testcases/org/apache/poi/ss/formula/WorkbookEvaluatorTestHelper.java
@@ -22,7 +22,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Allows tests to execute {@link WorkbookEvaluator}s and track the internal workings.
- *
+ *
* @author Josh Micich
*/
public final class WorkbookEvaluatorTestHelper {
@@ -30,8 +30,8 @@ public final class WorkbookEvaluatorTestHelper {
private WorkbookEvaluatorTestHelper() {
// no instances of this class
}
-
+
public static WorkbookEvaluator createEvaluator(HSSFWorkbook wb, EvaluationListener listener) {
- return new WorkbookEvaluator(HSSFEvaluationWorkbook.create(wb), listener, null);
+ return new WorkbookEvaluator(HSSFEvaluationWorkbook.create(wb), listener, null, null);
}
}