From: PJ Fanning Date: Fri, 30 Jul 2021 12:20:08 +0000 (+0000) Subject: test sum with N/A X-Git-Tag: REL_5_1_0~93 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=cd606046a2a62092e23c505d25434d5e1d390e99;p=poi.git test sum with N/A git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891897 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java index 8edf44340e..ccc2b8b772 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestConcat.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test; import java.io.IOException; +import static org.apache.poi.ss.util.Utils.addRow; import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -95,13 +96,6 @@ final class TestConcat { return wb; } - private void addRow(HSSFSheet sheet, int rownum, String... values) { - HSSFRow row = sheet.createRow(rownum); - for (int i = 0; i < values.length; i++) { - row.createCell(i).setCellValue(values[i]); - } - } - private static void confirmResult(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, String expectedResult) { cell.setCellFormula(formulaText); fe.notifyUpdateCell(cell); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSum.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSum.java new file mode 100644 index 0000000000..bd90ccb22d --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSum.java @@ -0,0 +1,84 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.ss.formula.functions; + +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.CellValue; +import org.apache.poi.ss.usermodel.FormulaError; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static org.apache.poi.ss.util.Utils.addRow; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test cases for SUM() -- there are plenty of additional cases in other test classes + */ +final class TestSum { + + @Test + void testSum() throws IOException { + try (HSSFWorkbook wb = initWorkbook1()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); + confirmDouble(fe, cell, "SUM(B2:B5)", 70000); + } + } + + @Test + void testSumWithNA() throws IOException { + try (HSSFWorkbook wb = initWorkbook1()) { + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); + HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100); + confirmError(fe, cell, "SUM(B2:B6)", FormulaError.NA); + } + } + + private HSSFWorkbook initWorkbook1() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + addRow(sheet, 0, "Property Value", "Commission", "Data"); + addRow(sheet, 1, 100000, 7000, 250000); + addRow(sheet, 2, 200000, 14000); + addRow(sheet, 3, 300000, 21000); + addRow(sheet, 4, 400000, 28000); + addRow(sheet, 5, 500000, FormulaError.NA); + return wb; + } + + private static void confirmDouble(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, double expectedResult) { + cell.setCellFormula(formulaText); + fe.notifyUpdateCell(cell); + CellValue result = fe.evaluate(cell); + assertEquals(result.getCellType(), CellType.NUMERIC); + assertEquals(expectedResult, result.getNumberValue()); + } + + private static void confirmError(HSSFFormulaEvaluator fe, HSSFCell cell, String formulaText, FormulaError expectedError) { + cell.setCellFormula(formulaText); + fe.notifyUpdateCell(cell); + CellValue result = fe.evaluate(cell); + assertEquals(result.getCellType(), CellType.ERROR); + assertEquals(expectedError.getCode(), result.getErrorValue()); + } +} diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java index d960224c4e..f8b06b7d5b 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestSumif.java @@ -17,6 +17,7 @@ package org.apache.poi.ss.formula.functions; +import static org.apache.poi.ss.util.Utils.addRow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -153,7 +154,7 @@ final class TestSumif { private HSSFWorkbook initWorkbook1WithNA() { HSSFWorkbook wb = initWorkbook1(); HSSFSheet sheet = wb.getSheetAt(0); - addRow(sheet, 5, 500000, "#N/A"); + addRow(sheet, 5, 500000, FormulaError.NA); return wb; } @@ -170,22 +171,6 @@ final class TestSumif { return wb; } - private void addRow(HSSFSheet sheet, int rownum, Object... values) { - HSSFRow row = sheet.createRow(rownum); - for (int i = 0; i < values.length; i++) { - Cell cell = row.createCell(i); - if (values[i] instanceof Integer) { - cell.setCellValue((Integer)values[i]); - } else if (values[i] == null) { - cell.setBlank(); - } else if (values[i] == "#N/A") { - cell.setCellErrorValue(FormulaError.NA.getCode()); - } else { - cell.setCellValue(values[i].toString()); - } - } - } - private static void confirm(double expectedResult, ValueEval...args) { confirmDouble(expectedResult, invokeSumif(-1, -1, args)); } diff --git a/poi/src/test/java/org/apache/poi/ss/util/Utils.java b/poi/src/test/java/org/apache/poi/ss/util/Utils.java new file mode 100644 index 0000000000..09e4d428d0 --- /dev/null +++ b/poi/src/test/java/org/apache/poi/ss/util/Utils.java @@ -0,0 +1,25 @@ +package org.apache.poi.ss.util; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.FormulaError; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; + +public class Utils { + public static void addRow(Sheet sheet, int rownum, Object... values) { + Row row = sheet.createRow(rownum); + for (int i = 0; i < values.length; i++) { + Cell cell = row.createCell(i); + if (values[i] instanceof Integer) { + cell.setCellValue((Integer) values[i]); + } else if (values[i] instanceof FormulaError) { + cell.setCellErrorValue(((FormulaError)values[i]).getCode()); + } else if (values[i] == null) { + cell.setBlank(); + } else { + cell.setCellValue(values[i].toString()); + } + } + } + +}