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.

TestFunctionRegistry.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.ss.formula;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertThrows;
  22. import static org.junit.jupiter.api.Assertions.assertTrue;
  23. import java.io.IOException;
  24. import org.apache.poi.hssf.usermodel.HSSFCell;
  25. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  26. import org.apache.poi.hssf.usermodel.HSSFRow;
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.ss.formula.atp.AnalysisToolPak;
  30. import org.apache.poi.ss.formula.eval.ErrorEval;
  31. import org.apache.poi.ss.formula.eval.FunctionEval;
  32. import org.apache.poi.ss.formula.eval.NotImplementedException;
  33. import org.apache.poi.ss.formula.eval.ValueEval;
  34. import org.apache.poi.ss.usermodel.CellValue;
  35. import org.junit.jupiter.api.AfterEach;
  36. import org.junit.jupiter.api.BeforeEach;
  37. import org.junit.jupiter.api.MethodOrderer;
  38. import org.junit.jupiter.api.Test;
  39. import org.junit.jupiter.api.TestMethodOrder;
  40. @TestMethodOrder(MethodOrderer.MethodName.class)
  41. class TestFunctionRegistry {
  42. HSSFWorkbook wb;
  43. HSSFSheet sheet;
  44. HSSFRow row;
  45. HSSFFormulaEvaluator fe;
  46. @BeforeEach
  47. void setup() {
  48. wb = new HSSFWorkbook();
  49. sheet = wb.createSheet("Sheet1");
  50. row = sheet.createRow(0);
  51. fe = new HSSFFormulaEvaluator(wb);
  52. }
  53. @AfterEach
  54. void teardown() throws IOException {
  55. wb.close();
  56. wb = null;
  57. sheet = null;
  58. row = null;
  59. fe = null;
  60. }
  61. @Test
  62. void testRegisterInRuntimeA() {
  63. HSSFCell cellA = row.createCell(0);
  64. cellA.setCellFormula("FISHER(A5)");
  65. assertThrows(NotImplementedException.class, () -> fe.evaluate(cellA));
  66. }
  67. @Test
  68. void testRegisterInRuntimeB() {
  69. HSSFCell cellA = row.createCell(0);
  70. cellA.setCellFormula("FISHER(A5)");
  71. FunctionEval.registerFunction("FISHER", (args, srcRowIndex, srcColumnIndex) -> ErrorEval.NA);
  72. CellValue cv = fe.evaluate(cellA);
  73. assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
  74. }
  75. @Test
  76. void testRegisterInRuntimeC() {
  77. HSSFCell cellB = row.createCell(1);
  78. cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
  79. assertThrows(NotImplementedException.class, () -> fe.evaluate(cellB));
  80. }
  81. @Test
  82. void testRegisterInRuntimeD() {
  83. HSSFCell cellB = row.createCell(1);
  84. cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
  85. AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", (args, ec) -> ErrorEval.NUM_ERROR);
  86. CellValue cv = fe.evaluate(cellB);
  87. assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
  88. }
  89. private static ValueEval na(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  90. return ErrorEval.NA;
  91. }
  92. @Test
  93. void testExceptionsA() {
  94. IllegalArgumentException ex = assertThrows(
  95. IllegalArgumentException.class,
  96. () -> FunctionEval.registerFunction("SUM", TestFunctionRegistry::na)
  97. );
  98. assertEquals("POI already implements SUM. You cannot override POI's implementations of Excel functions", ex.getMessage());
  99. }
  100. @Test
  101. void testExceptionsB() {
  102. IllegalArgumentException ex = assertThrows(
  103. IllegalArgumentException.class,
  104. () -> FunctionEval.registerFunction("SUMXXX", TestFunctionRegistry::na)
  105. );
  106. assertTrue(ex.getMessage().contains("Unknown function: SUMXXX"));
  107. }
  108. @Test
  109. void testExceptionsC() {
  110. IllegalArgumentException ex = assertThrows(
  111. IllegalArgumentException.class,
  112. () -> FunctionEval.registerFunction("ISODD", TestFunctionRegistry::na)
  113. );
  114. assertEquals("ISODD is a function from the Excel Analysis Toolpack. " +
  115. "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.",
  116. ex.getMessage());
  117. }
  118. private static ValueEval atpFunc(ValueEval[] args, OperationEvaluationContext ec) {
  119. return ErrorEval.NUM_ERROR;
  120. }
  121. @Test
  122. void testExceptionsD() {
  123. IllegalArgumentException ex = assertThrows(
  124. IllegalArgumentException.class,
  125. () -> AnalysisToolPak.registerFunction("ISODD", TestFunctionRegistry::atpFunc)
  126. );
  127. assertEquals("POI already implements ISODD. You cannot override POI's implementations of Excel functions", ex.getMessage());
  128. }
  129. @Test
  130. void testExceptionsE() {
  131. IllegalArgumentException ex = assertThrows(
  132. IllegalArgumentException.class,
  133. () -> AnalysisToolPak.registerFunction("ISODDXXX", TestFunctionRegistry::atpFunc)
  134. );
  135. assertEquals("ISODDXXX is not a function from the Excel Analysis Toolpack.", ex.getMessage());
  136. }
  137. @Test
  138. void testExceptionsF() {
  139. IllegalArgumentException ex = assertThrows(
  140. IllegalArgumentException.class,
  141. () -> AnalysisToolPak.registerFunction("SUM", TestFunctionRegistry::atpFunc)
  142. );
  143. assertEquals("SUM is a built-in Excel function. " +
  144. "Use FunctionEval.registerFunction(String name, Function func) instead.",
  145. ex.getMessage());
  146. }
  147. }