You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseTestExternalFunctions.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.ss.ITestDataProvider;
  18. import org.apache.poi.ss.formula.eval.ErrorEval;
  19. import org.apache.poi.ss.formula.eval.NotImplementedException;
  20. import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
  21. import org.apache.poi.ss.formula.eval.StringEval;
  22. import org.apache.poi.ss.formula.eval.ValueEval;
  23. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  24. import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
  25. import org.apache.poi.ss.formula.udf.UDFFinder;
  26. import org.apache.poi.ss.usermodel.Cell;
  27. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  28. import org.apache.poi.ss.usermodel.Sheet;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. /**
  31. * Test setting / evaluating of Analysis Toolpack and user-defined functions
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public class BaseTestExternalFunctions extends TestCase {
  36. // define two custom user-defined functions
  37. private static class MyFunc implements FreeRefFunction {
  38. public MyFunc() {
  39. //
  40. }
  41. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  42. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  43. return ErrorEval.VALUE_INVALID;
  44. }
  45. StringEval input = (StringEval) args[0];
  46. return new StringEval(input.getStringValue() + "abc");
  47. }
  48. }
  49. private static class MyFunc2 implements FreeRefFunction {
  50. public MyFunc2() {
  51. //
  52. }
  53. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  54. if (args.length != 1 || !(args[0] instanceof StringEval)) {
  55. return ErrorEval.VALUE_INVALID;
  56. }
  57. StringEval input = (StringEval) args[0];
  58. return new StringEval(input.getStringValue() + "abc2");
  59. }
  60. }
  61. /**
  62. * register the two test UDFs in a UDF finder, to be passed to the workbook
  63. */
  64. private static UDFFinder customToolpack = new DefaultUDFFinder(
  65. new String[] { "myFunc", "myFunc2"},
  66. new FreeRefFunction[] { new MyFunc(), new MyFunc2()}
  67. );
  68. protected final ITestDataProvider _testDataProvider;
  69. /**
  70. * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
  71. */
  72. protected BaseTestExternalFunctions(ITestDataProvider testDataProvider) {
  73. _testDataProvider = testDataProvider;
  74. }
  75. public void testExternalFunctions() {
  76. Workbook wb = _testDataProvider.createWorkbook();
  77. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  78. Sheet sh = wb.createSheet();
  79. Cell cell1 = sh.createRow(0).createCell(0);
  80. cell1.setCellFormula("ISODD(1)+ISEVEN(2)"); // functions from the Excel Analysis Toolpack
  81. assertEquals("ISODD(1)+ISEVEN(2)", cell1.getCellFormula());
  82. Cell cell2 = sh.createRow(1).createCell(0);
  83. cell2.setCellFormula("MYFUNC(\"B1\")"); //unregistered functions are parseable and renderable, but may not be evaluateable
  84. try {
  85. evaluator.evaluate(cell2);
  86. fail("Expected NotImplementedFunctionException/NotImplementedException");
  87. } catch (final NotImplementedException e) {
  88. if (!(e.getCause() instanceof NotImplementedFunctionException))
  89. throw e;
  90. // expected
  91. // Alternatively, a future implementation of evaluate could return #NAME? error to align behavior with Excel
  92. // assertEquals(ErrorEval.NAME_INVALID, ErrorEval.valueOf(evaluator.evaluate(cell2).getErrorValue()));
  93. }
  94. wb.addToolPack(customToolpack);
  95. cell2.setCellFormula("MYFUNC(\"B1\")");
  96. assertEquals("MYFUNC(\"B1\")", cell2.getCellFormula());
  97. Cell cell3 = sh.createRow(2).createCell(0);
  98. cell3.setCellFormula("MYFUNC2(\"C1\")&\"-\"&A2"); //where A2 is defined above
  99. assertEquals("MYFUNC2(\"C1\")&\"-\"&A2", cell3.getCellFormula());
  100. assertEquals(2.0, evaluator.evaluate(cell1).getNumberValue());
  101. assertEquals("B1abc", evaluator.evaluate(cell2).getStringValue());
  102. assertEquals("C1abc2-B1abc", evaluator.evaluate(cell3).getStringValue());
  103. }
  104. /**
  105. * test invoking saved ATP functions
  106. *
  107. * @param testFile either atp.xls or atp.xlsx
  108. */
  109. public void baseTestInvokeATP(String testFile){
  110. Workbook wb = _testDataProvider.openSampleWorkbook(testFile);
  111. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  112. Sheet sh = wb.getSheetAt(0);
  113. // these two are not imlemented in r
  114. assertEquals("DELTA(1.3,1.5)", sh.getRow(0).getCell(1).getCellFormula());
  115. assertEquals("COMPLEX(2,4)", sh.getRow(1).getCell(1).getCellFormula());
  116. Cell cell2 = sh.getRow(2).getCell(1);
  117. assertEquals("ISODD(2)", cell2.getCellFormula());
  118. assertEquals(false, evaluator.evaluate(cell2).getBooleanValue());
  119. assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(cell2));
  120. Cell cell3 = sh.getRow(3).getCell(1);
  121. assertEquals("ISEVEN(2)", cell3.getCellFormula());
  122. assertEquals(true, evaluator.evaluate(cell3).getBooleanValue());
  123. assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(cell3));
  124. }
  125. }