diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-04-07 21:40:33 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-04-07 21:40:33 +0000 |
commit | b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f (patch) | |
tree | ad9c7b312976c4ed113a7f3b5b4757bfe1b3eee6 /poi-excelant/src/test | |
parent | 6458acb931a0cc17b2d5ed205a1b3fbbb78b9193 (diff) | |
download | poi-b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f.tar.gz poi-b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f.zip |
65206 - Migrate ant / maven to gradle build
compile / jar / test of mrJars
don't include ants build.xml anymore
rename directories to match project and maven artifact names
refactor artifacts - so each project has one artifact
replace static references in hssf/dev tests with junit5 constructs, which had problems in parallel tests
increase gradle heap to 4gb because of OOM - maybe less would also work
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888488 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-excelant/src/test')
15 files changed, 1618 insertions, 0 deletions
diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/CalculateMortgageFunction.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/CalculateMortgageFunction.java new file mode 100644 index 0000000000..73eb984db7 --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/CalculateMortgageFunction.java @@ -0,0 +1,93 @@ +/* ==================================================================== + 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.excelant; + +import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.EvaluationException; +import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.functions.FreeRefFunction; + +/** + * A simple user-defined function to calculate principal and interest. + * + * Used by {@link org.apache.poi.ss.excelant.util.TestExcelAntWorkbookUtil}. + * + * @author Jon Svede ( jon [at] loquatic [dot] com ) + * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov ) + * + */ +public class CalculateMortgageFunction implements FreeRefFunction { + + @Override + public ValueEval evaluate( ValueEval[] args, OperationEvaluationContext ec ) { + + // verify that we have enough data + if (args.length != 3) { + return ErrorEval.VALUE_INVALID; + } + + // declare doubles for values + double principal, rate, years, result; + try { + // extract values as ValueEval + ValueEval v1 = OperandResolver.getSingleValue( args[0], + ec.getRowIndex(), + ec.getColumnIndex() ) ; + ValueEval v2 = OperandResolver.getSingleValue( args[1], + ec.getRowIndex(), + ec.getColumnIndex() ) ; + ValueEval v3 = OperandResolver.getSingleValue( args[2], + ec.getRowIndex(), + ec.getColumnIndex() ) ; + + // get data as doubles + principal = OperandResolver.coerceValueToDouble( v1 ) ; + rate = OperandResolver.coerceValueToDouble( v2 ) ; + years = OperandResolver.coerceValueToDouble( v3 ) ; + + result = calculateMortgagePayment( principal, rate, years ) ; + System.out.println( "Result = " + result ) ; + + checkValue(result); + + } catch (EvaluationException e) { + return e.getErrorEval(); + } + + return new NumberEval( result ) ; + } + + public double calculateMortgagePayment( double p, double r, double y ) { + double i = r / 12 ; + double n = y * 12 ; + + return p * (( i * Math.pow((1 + i),n ) ) / ( Math.pow((1 + i),n) - 1)); + } + /** + * Excel does not support infinities and NaNs, rather, it gives a #NUM! error in these cases + * + * @throws EvaluationException (#NUM!) if <tt>result</tt> is <tt>NaN</> or <tt>Infinity</tt> + */ + private void checkValue(double result) throws EvaluationException { + if (Double.isNaN(result) || Double.isInfinite(result)) { + throw new EvaluationException(ErrorEval.NUM_ERROR); + } + } +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/ExcelAntUserDefinedFunctionTestHelper.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/ExcelAntUserDefinedFunctionTestHelper.java new file mode 100644 index 0000000000..48086fc22d --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/ExcelAntUserDefinedFunctionTestHelper.java @@ -0,0 +1,34 @@ +/* ==================================================================== + 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.excelant; + +public class ExcelAntUserDefinedFunctionTestHelper extends + ExcelAntUserDefinedFunction { + + @Override + protected String getFunctionAlias() { + // TODO Auto-generated method stub + return super.getFunctionAlias(); + } + + @Override + protected String getClassName() { + // TODO Auto-generated method stub + return super.getClassName(); + } + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/MockExcelAntWorkbookHandler.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/MockExcelAntWorkbookHandler.java new file mode 100644 index 0000000000..d6a3a74b7d --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/MockExcelAntWorkbookHandler.java @@ -0,0 +1,39 @@ +/* + * 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.excelant; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.apache.poi.ss.usermodel.Workbook; + +public class MockExcelAntWorkbookHandler implements IExcelAntWorkbookHandler { + public static boolean executed; + public static Workbook workbook; + + + @Override + public void setWorkbook(Workbook workbook) { + MockExcelAntWorkbookHandler.workbook = workbook; + } + + @Override + public void execute() { + executed = true; + assertNotNull(workbook); + } +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestBuildFile.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestBuildFile.java new file mode 100644 index 0000000000..2a2b36b827 --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestBuildFile.java @@ -0,0 +1,375 @@ +/* + * 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.excelant; + +import static org.apache.poi.POITestCase.assertContains; +import static org.apache.poi.POITestCase.assertNotContained; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.File; +import java.io.PrintStream; + +import org.apache.poi.POIDataSamples; +import org.apache.poi.util.NullPrintStream; +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * JUnit test for the ExcelAnt tasks. + * Leverages Ant's test framework. + */ +public class TestBuildFile { + + protected Project project; + + private StringBuilder logBuffer; + private StringBuilder fullLogBuffer; + + + @BeforeEach + void setUp() { + String filename = TestBuildFile.getDataDir() + "/../poi-excelant/src/test/resources/tests.xml"; + int logLevel = Project.MSG_DEBUG; + + logBuffer = new StringBuilder(); + fullLogBuffer = new StringBuilder(); + project = new Project(); + project.init(); + project.setNewProperty("data.dir.name", getDataDir()); + File antFile = new File(System.getProperty("root"), filename); + project.setUserProperty("ant.file", antFile.getAbsolutePath()); + project.addBuildListener(new AntTestListener(logLevel)); + ProjectHelper.configureProject(project, antFile); + + } + + /** + * Automatically calls the target called "tearDown" + * from the build file tested if it exits. + * <p> + * This allows to use Ant tasks directly in the build file + * to clean up after each test. Note that no "setUp" target + * is automatically called, since it's trivial to have a + * test target depend on it. + */ + @AfterEach + void tearDown() { + if (project == null) { + /* + * Maybe the BuildFileTest was subclassed and there is + * no initialized project. So we could avoid getting a + * NPE. + * If there is an initialized project getTargets() does + * not return null as it is initialized by an empty + * HashSet. + */ + return; + } + final String tearDown = "tearDown"; + if (project.getTargets().containsKey(tearDown)) { + project.executeTarget(tearDown); + } + } + + /** + * run a target, expect for any build exception + * + * @param target target to run + * @param cause information string to reader of report + */ + void expectBuildException(String target, String cause) { + expectSpecificBuildException(target, cause, null); + } + + /** + * Assert that the given substring is in the log messages. + */ + void assertLogContaining(String substring) { + assertContains(getLog(), substring); + } + + /** + * Assert that the given substring is not in the log messages. + */ + void assertLogNotContaining(String substring) { + assertNotContained(getLog(), substring); + } + + /** + * Gets the log the BuildFileTest object. + * Only valid if configureProject() has been called. + * + * @return The log value + */ + public String getLog() { + return logBuffer.toString(); + } + + /** + * Executes a target we have set up + * + * @param targetName target to run + */ + void executeTarget(String targetName) { + PrintStream sysOut = System.out; + PrintStream sysErr = System.err; + try { + sysOut.flush(); + sysErr.flush(); + System.setOut(new NullPrintStream()); + System.setErr(new NullPrintStream()); + logBuffer = new StringBuilder(); + fullLogBuffer = new StringBuilder(); + project.executeTarget(targetName); + } finally { + System.setOut(sysOut); + System.setErr(sysErr); + } + + } + + /** + * Runs a target, wait for a build exception. + * + * @param target target to run + * @param cause information string to reader of report + * @param msg the message value of the build exception we are waiting + * for set to null for any build exception to be valid + */ + void expectSpecificBuildException(String target, String cause, String msg) { + try { + executeTarget(target); + } catch (org.apache.tools.ant.BuildException ex) { + assertTrue(msg == null || ex.getMessage().equals(msg), + "Should throw BuildException because '" + cause + "' with message '" + msg + "' (actual message '" + ex.getMessage() + "' instead)" + ); + return; + } + fail("Should throw BuildException because: " + cause); + } + + public static String getDataDir() { + String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY); + return dataDirName == null ? "test-data" : dataDirName; + } + + /** + * Our own personal build listener. + */ + private class AntTestListener implements BuildListener { + private final int logLevel; + + /** + * Constructs a test listener which will ignore log events + * above the given level. + */ + public AntTestListener(int logLevel) { + this.logLevel = logLevel; + } + + /** + * Fired before any targets are started. + */ + @Override + public void buildStarted(BuildEvent event) { + } + + /** + * Fired after the last target has finished. This event + * will still be thrown if an error occurred during the build. + * + * @see BuildEvent#getException() + */ + @Override + public void buildFinished(BuildEvent event) { + } + + /** + * Fired when a target is started. + * + * @see BuildEvent#getTarget() + */ + @Override + public void targetStarted(BuildEvent event) { + //System.out.println("targetStarted " + event.getTarget().getName()); + } + + /** + * Fired when a target has finished. This event will + * still be thrown if an error occurred during the build. + * + * @see BuildEvent#getException() + */ + @Override + public void targetFinished(BuildEvent event) { + //System.out.println("targetFinished " + event.getTarget().getName()); + } + + /** + * Fired when a task is started. + * + * @see BuildEvent#getTask() + */ + @Override + public void taskStarted(BuildEvent event) { + //System.out.println("taskStarted " + event.getTask().getTaskName()); + } + + /** + * Fired when a task has finished. This event will still + * be throw if an error occurred during the build. + * + * @see BuildEvent#getException() + */ + @Override + public void taskFinished(BuildEvent event) { + //System.out.println("taskFinished " + event.getTask().getTaskName()); + } + + /** + * Fired whenever a message is logged. + * + * @see BuildEvent#getMessage() + * @see BuildEvent#getPriority() + */ + @Override + public void messageLogged(BuildEvent event) { + if (event.getPriority() > logLevel) { + // ignore event + return; + } + + if (event.getPriority() == Project.MSG_INFO || + event.getPriority() == Project.MSG_WARN || + event.getPriority() == Project.MSG_ERR) { + logBuffer.append(event.getMessage()); + } + fullLogBuffer.append(event.getMessage()); + } + } + + @Test + void testMissingFilename() { + expectSpecificBuildException("test-nofile", "required argument not specified", + "fileName attribute must be set!"); + } + + @Test + void testFileNotFound() { + expectSpecificBuildException("test-filenotfound", "required argument not specified", + "Cannot load file invalid.xls. Make sure the path and file permissions are correct."); + } + + @Test + void testEvaluate() { + executeTarget("test-evaluate"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4."); + } + + @Test + void testEvaluateNoDetails() { + executeTarget("test-evaluate-nodetails"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogNotContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4."); + } + + @Test + void testPrecision() { + executeTarget("test-precision"); + + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4. " + + "It evaluated to 2285.5761494145563 when the value of 2285.576149 with precision of 1.0E-4"); + assertLogContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4. " + + "It evaluated to 2285.5761494145563 when the value of 2285.576149 with precision of 1.0E-5"); + assertLogContaining("Failed to evaluate cell 'MortgageCalculator'!$B$4. " + + "It evaluated to 2285.5761494145563 when the value of 2285.576149 with precision of 1.0E-10 was expected."); + assertLogContaining("2/3 tests passed"); + } + + @Test + void testPrecisionFail() { + expectSpecificBuildException("test-precision-fails", "precision not matched", + "\tFailed to evaluate cell 'MortgageCalculator'!$B$4. It evaluated to 2285.5761494145563 when the value of 2285.576149 with precision of 1.0E-10 was expected."); + } + + @Test + void testPassOnError() { + executeTarget("test-passonerror"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogContaining("Test named failonerror failed because 1 of 0 evaluations failed to evaluate correctly."); + } + + @Test + void testFailOnError() { + expectBuildException("test-failonerror", "fail on error"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogNotContaining("failed because 1 of 0 evaluations failed to evaluate correctly. Failed to evaluate cell 'MortageCalculatorFunction'!$D$3"); + } + + @Test + void testFailOnErrorNoDetails() { + expectBuildException("test-failonerror-nodetails", "fail on error"); + assertLogNotContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogNotContaining("failed because 1 of 0 evaluations failed to evaluate correctly. Failed to evaluate cell 'MortageCalculatorFunction'!$D$3"); + } + + @Test + void testUdf() { + executeTarget("test-udf"); + assertLogContaining("1/1 tests passed"); + } + + @Test + void testSetText() { + executeTarget("test-settext"); + assertLogContaining("1/1 tests passed"); + } + + @Test + void testAddHandler() { + executeTarget("test-addhandler"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4."); + + assertNotNull(MockExcelAntWorkbookHandler.workbook, "The workbook should have been passed to the handler"); + assertTrue(MockExcelAntWorkbookHandler.executed, "The handler should have been executed"); + } + + @Test + void testAddHandlerWrongClass() { + executeTarget("test-addhandler-wrongclass"); + assertLogContaining("Using input file: " + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls"); + assertLogContaining("Succeeded when evaluating 'MortgageCalculator'!$B$4."); + } + + @Test + void testAddHandlerFails() { + expectSpecificBuildException("test-addhandler-fails", "NullPointException", null); + } + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntPrecision.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntPrecision.java new file mode 100644 index 0000000000..2902144fde --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntPrecision.java @@ -0,0 +1,54 @@ +/* ==================================================================== + 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.excelant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntPrecision { + + private ExcelAntPrecision fixture ; + + @BeforeEach + void setUp() { + fixture = new ExcelAntPrecision() ; + } + + @AfterEach + void tearDown() { + fixture = null ; + } + + @Test + void testVerifyPrecision() { + + double value = 1.0E-1 ; + + fixture.setValue( value ) ; + + double result = fixture.getValue() ; + + assertTrue( result > 0 ) ; + + assertEquals( value, result, 0.0 ) ; + } + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSet.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSet.java new file mode 100644 index 0000000000..42f6cd256c --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSet.java @@ -0,0 +1,69 @@ +/* ==================================================================== + 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.excelant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil; +import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtilFactory; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntSet { + + + // This is abstract in nature, so we'll use a + // concrete instance to test the set methods. + private ExcelAntSet fixture ; + + private static final String mortgageCalculatorFileName = + TestBuildFile.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; + + @BeforeEach + void setUp() { + fixture = new ExcelAntSetDoubleCell() ; + } + + @AfterEach + void tearDown() { + fixture = null ; + } + + @Test + void testSetter() { + String cell = "simpleCellRef!$F$1" ; + + fixture.setCell( cell ) ; + + String cellStr = fixture.getCell() ; + + assertNotNull( cellStr ) ; + assertEquals( cell, cellStr ) ; + } + + @Test + void testSetWorkbookUtil() { + ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance( + mortgageCalculatorFileName ) ; + + assertNotNull( util ) ; + + fixture.setWorkbookUtil( util ) ; + } +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSetDoubleCell.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSetDoubleCell.java new file mode 100644 index 0000000000..45c4f53635 --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntSetDoubleCell.java @@ -0,0 +1,70 @@ +/* ==================================================================== + 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.excelant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil; +import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtilFactory; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntSetDoubleCell { + + private ExcelAntSetDoubleCell fixture ; + + private ExcelAntWorkbookUtil util ; + + private static final String mortgageCalculatorFileName = + TestBuildFile.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; + + @BeforeEach + void setUp() { + fixture = new ExcelAntSetDoubleCell() ; + util = ExcelAntWorkbookUtilFactory.getInstance(mortgageCalculatorFileName ) ; + fixture.setWorkbookUtil( util ) ; + } + + @AfterEach + void tearDown() { + fixture = null ; + } + + @Test + void testSetDouble() { + String cellId = "'Sheet3'!$A$1" ; + double testValue = 1.1 ; + + fixture.setCell( cellId ) ; + fixture.setValue( testValue ) ; + + double value = fixture.getCellValue() ; + + assertTrue( value > 0 ) ; + assertEquals( testValue, value, 0.0 ) ; + + fixture.execute() ; + + double setValue = util.getCellAsDouble( cellId ) ; + + assertEquals( setValue, testValue, 0.0 ) ; + } + + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntUserDefinedFunction.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntUserDefinedFunction.java new file mode 100644 index 0000000000..52b580ac6f --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/TestExcelAntUserDefinedFunction.java @@ -0,0 +1,57 @@ +/* ==================================================================== + 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.excelant; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntUserDefinedFunction { + + private ExcelAntUserDefinedFunctionTestHelper fixture ; + + @BeforeEach + void setUp() { + fixture = new ExcelAntUserDefinedFunctionTestHelper() ; + } + + @Test + void testSetClassName() { + String className = "simple.class.name" ; + + fixture.setClassName( className ) ; + String value = fixture.getClassName() ; + + assertNotNull( value ) ; + assertEquals( className, value ) ; + } + + @Test + void testSetFunction() { + String functionAlias = "alias" ; + + fixture.setFunctionAlias( functionAlias ) ; + + String alias = fixture.getFunctionAlias() ; + + assertNotNull( alias ) ; + assertEquals( functionAlias, alias ) ; + } + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilTestHelper.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilTestHelper.java new file mode 100644 index 0000000000..16189f1d50 --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilTestHelper.java @@ -0,0 +1,50 @@ +/* ==================================================================== + 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.excelant.util; + +import org.apache.poi.ss.formula.udf.UDFFinder; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Workbook; + +/** + * A helper class to allow testing of protected methods and constructors. + * + * @author jsvede + * + */ +public class ExcelAntWorkbookUtilTestHelper extends ExcelAntWorkbookUtil { + + public ExcelAntWorkbookUtilTestHelper(String fName) { + super(fName); + } + + public ExcelAntWorkbookUtilTestHelper(Workbook wb) { + super(wb); + } + + @Override + public UDFFinder getFunctions() { + return super.getFunctions(); + } + + @Override + public FormulaEvaluator getEvaluator(String excelFileName) { + return super.getEvaluator(excelFileName); + } + + +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntEvaluationResult.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntEvaluationResult.java new file mode 100644 index 0000000000..dcb044a2eb --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntEvaluationResult.java @@ -0,0 +1,76 @@ +/* ==================================================================== + 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.excelant.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntEvaluationResult { + private ExcelAntEvaluationResult fixture; + + private boolean completedWithError; + private boolean passed; + private double retValue = 1.1; + private String errMessage = "error message"; + private double delta = 2.2; + private String cellId = "testCell!$F$1"; + + @BeforeEach + void setUp() { + fixture = new ExcelAntEvaluationResult(completedWithError, + passed, + retValue, + errMessage, + delta, + cellId); + } + + @AfterEach + void tearDown() { + fixture = null; + } + + @Test + void testCompletedWithErrorMessage() { + String errMsg = fixture.getErrorMessage(); + assertNotNull(errMsg); + assertEquals(errMsg, errMessage); + } + + @Test + void testPassed() { + boolean passedValue = fixture.didTestPass(); + assertEquals(passedValue, passed); + } + + @Test + void testDelta() { + double deltaValue = fixture.getDelta(); + assertEquals(deltaValue, delta, 0.0); + } + + @Test + void testCellId() { + String cellIdValue = fixture.getCellName(); + assertNotNull(cellIdValue); + assertEquals(cellIdValue, cellId); + } +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java new file mode 100644 index 0000000000..4f46084c6a --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtil.java @@ -0,0 +1,373 @@ +/* ==================================================================== + 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.excelant.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import org.apache.poi.ss.excelant.CalculateMortgageFunction; +import org.apache.poi.ss.excelant.TestBuildFile; +import org.apache.poi.ss.formula.udf.UDFFinder; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FormulaEvaluator; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; +import org.apache.tools.ant.BuildException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +class TestExcelAntWorkbookUtil { + + private static final String mortgageCalculatorFileName = + TestBuildFile.getDataDir() + "/spreadsheet/excelant.xls" ; + + private ExcelAntWorkbookUtilTestHelper fixture ; + + + @AfterEach + void tearDown() { + fixture = null ; + } + + @Test + void testStringConstructor() { + fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName); + + assertNotNull(fixture); + } + + @Test + void testLoadNotExistingFile() { + BuildException e = assertThrows(BuildException.class, () -> new ExcelAntWorkbookUtilTestHelper("notexistingFile")); + assertTrue(e.getMessage().contains("notexistingFile")); + } + + @Test + void testWorkbookConstructor() throws IOException { + File workbookFile = new File(mortgageCalculatorFileName); + FileInputStream fis = new FileInputStream(workbookFile); + Workbook workbook = WorkbookFactory.create(fis); + + fixture = new ExcelAntWorkbookUtilTestHelper(workbook); + + assertNotNull(fixture); + } + + @Test + void testAddFunction() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + assertNotNull(fixture); + + fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction()); + + UDFFinder functions = fixture.getFunctions(); + + assertNotNull(functions); + assertNotNull(functions.findFunction("h2_ZFactor")); + } + + @Test + void testAddFunctionClassName() throws Exception { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + assertNotNull(fixture); + + fixture.addFunction("h2_ZFactor", CalculateMortgageFunction.class.getName()); + + UDFFinder functions = fixture.getFunctions(); + + assertNotNull(functions); + assertNotNull(functions.findFunction("h2_ZFactor")); + } + + @Test + void testAddFunctionInvalidClassName() throws Exception { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + assertNotNull(fixture); + + fixture.addFunction("h2_ZFactor", String.class.getName()); + + UDFFinder functions = fixture.getFunctions(); + + assertNotNull(functions); + assertNull(functions.findFunction("h2_ZFactor")); + } + + @Test + void testGetWorkbook() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + assertNotNull(fixture); + + Workbook workbook = fixture.getWorkbook(); + + assertNotNull(workbook); + } + + @Test + void testFileName() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + assertNotNull(fixture); + + String fileName = fixture.getFileName(); + + assertNotNull(fileName); + + assertEquals(mortgageCalculatorFileName, fileName); + + } + + @Test + void testGetEvaluator() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + FormulaEvaluator evaluator = fixture.getEvaluator( + mortgageCalculatorFileName); + + assertNotNull(evaluator); + } + + @Test + void testGetEvaluatorWithUDF() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction()); + + FormulaEvaluator evaluator = fixture.getEvaluator( + mortgageCalculatorFileName); + + assertNotNull(evaluator); + } + + @Test + void testGetEvaluatorXLSX() { + fixture = new ExcelAntWorkbookUtilTestHelper( + TestBuildFile.getDataDir() + "/spreadsheet/sample.xlsx"); + + FormulaEvaluator evaluator = fixture.getEvaluator( + TestBuildFile.getDataDir() + "/spreadsheet/sample.xlsx"); + + assertNotNull(evaluator); + } + + @Test + void testGetEvaluatorXLSXWithFunction() { + fixture = new ExcelAntWorkbookUtilTestHelper( + TestBuildFile.getDataDir() + "/spreadsheet/sample.xlsx"); + + fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction()); + + FormulaEvaluator evaluator = fixture.getEvaluator( + TestBuildFile.getDataDir() + "/spreadsheet/sample.xlsx"); + + assertNotNull(evaluator); + } + + @Test + void testEvaluateCell() { + String cell = "'MortgageCalculator'!B4" ; + double expectedValue = 790.79 ; + double precision = 0.1 ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + ExcelAntEvaluationResult result = fixture.evaluateCell(cell, + expectedValue, + precision); + + //System.out.println(result); + assertTrue( result.toString().contains("evaluationCompletedWithError=false"), "Had:" + result ); + assertTrue( result.toString().contains("returnValue=790.79"), "Had:" + result ); + assertTrue( result.toString().contains("cellName='MortgageCalculator'!B4"), "Had:" + result ); + assertFalse(result.toString().contains("#N/A")); + + assertFalse(result.evaluationCompleteWithError()); + assertTrue(result.didTestPass()); + } + + @Test + void testEvaluateCellFailedPrecision() { + String cell = "'MortgageCalculator'!B4" ; + double expectedValue = 790.79 ; + double precision = 0.0000000000001 ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + ExcelAntEvaluationResult result = fixture.evaluateCell(cell, + expectedValue, + precision); + + //System.out.println(result); + assertTrue( result.toString().contains("evaluationCompletedWithError=false"), "Had:" + result ); + assertTrue( result.toString().contains("returnValue=790.79"), "Had:" + result ); + assertTrue( result.toString().contains("cellName='MortgageCalculator'!B4"), "Had:" + result ); + assertFalse( result.toString().contains("#"), "Should not see an error, but had:" + result ); + + assertFalse(result.evaluationCompleteWithError()); + assertFalse(result.didTestPass()); + } + + @Test + void testEvaluateCellWithError() { + String cell = "'ErrorCell'!A1" ; + double expectedValue = 790.79 ; + double precision = 0.1 ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + ExcelAntEvaluationResult result = fixture.evaluateCell(cell, + expectedValue, + precision); + + System.out.println(result); + assertTrue( result.toString().contains("evaluationCompletedWithError=true"), "Had:" + result ); + assertTrue( result.toString().contains("returnValue=0.0"), "Had:" + result ); + assertTrue( result.toString().contains("cellName='ErrorCell'!A1"), "Had:" + result ); + assertTrue( result.toString().contains("#N/A"), "Had:" + result ); + + assertTrue(result.evaluationCompleteWithError()); + assertFalse(result.didTestPass()); + } + + @Test + void testGetSheets() { + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + List<String> sheets = fixture.getSheets(); + + assertNotNull(sheets); + assertEquals(sheets.size(), 3); + } + + @Test + void testSetString() { + String cell = "'MortgageCalculator'!C14" ; + String cellValue = "testString" ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + fixture.setStringValue(cell, cellValue); + + String value = fixture.getCellAsString(cell); + + assertNotNull(value); + assertEquals(cellValue, value); + } + + @Test + void testSetNotExistingSheet() { + String cell = "'NotexistingSheet'!C14" ; + + fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName); + BuildException e = assertThrows(BuildException.class, () -> fixture.setStringValue(cell, "some")); + assertTrue(e.getMessage().contains("NotexistingSheet")); + } + + @Test + void testSetFormula() { + String cell = "'MortgageCalculator'!C14" ; + String cellValue = "SUM(B14:B18)" ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + fixture.setFormulaValue(cell, cellValue); + + double value = fixture.getCellAsDouble(cell); + + assertEquals(0.0, value, 0); + } + + @Test + void testSetDoubleValue() { + String cell = "'MortgageCalculator'!C14" ; + double cellValue = 1.2; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + fixture.setDoubleValue(cell, cellValue); + + double value = fixture.getCellAsDouble(cell); + + assertEquals(cellValue, value, 0); + } + + @Test + void testSetDate() { + String cell = "'MortgageCalculator'!C14" ; + Date cellValue = new Date(); + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + fixture.setDateValue(cell, cellValue); + + double value = fixture.getCellAsDouble(cell); + + assertEquals(DateUtil.getExcelDate(cellValue, false), value, 0); + } + + @Test + void testGetNonexistingString() { + String cell = "'MortgageCalculator'!C33" ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + String value = fixture.getCellAsString(cell); + + assertEquals("", value); + } + + @Test + void testGetNonexistingDouble() { + String cell = "'MortgageCalculator'!C33" ; + + fixture = new ExcelAntWorkbookUtilTestHelper( + mortgageCalculatorFileName); + + double value = fixture.getCellAsDouble(cell); + + assertEquals(0.0, value, 0); + } +} diff --git a/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtilFactory.java b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtilFactory.java new file mode 100644 index 0000000000..26e700fa8b --- /dev/null +++ b/poi-excelant/src/test/java/org/apache/poi/ss/excelant/util/TestExcelAntWorkbookUtilFactory.java @@ -0,0 +1,68 @@ +/* ==================================================================== + 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.excelant.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.apache.poi.ss.excelant.TestBuildFile; +import org.junit.jupiter.api.Test; + + +/** + * Tests for the ExcelAntWorbookUtilFactory. + */ +class TestExcelAntWorkbookUtilFactory { + + private static final String mortgageCalculatorWorkbookFile = + TestBuildFile.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; + + + /** + * Simple test to determine if the factory properly returns an non-null + * instance of the ExcelAntWorkbookUtil class. + */ + @Test + void testGetNewWorkbookUtilInstance() { + ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance( + mortgageCalculatorWorkbookFile) ; + + assertNotNull(util) ; + } + + + /** + * Test whether or not the factory will properly return the same reference + * to an ExcelAnt WorkbookUtil when two different Strings, that point to + * the same resource, are passed in. + */ + @Test + void testVerifyEquivalence() { + String sameFileName = TestBuildFile.getDataDir() + "/spreadsheet/mortgage-calculation.xls" ; + + ExcelAntWorkbookUtil util = ExcelAntWorkbookUtilFactory.getInstance( + mortgageCalculatorWorkbookFile) ; + + ExcelAntWorkbookUtil util2 = ExcelAntWorkbookUtilFactory.getInstance( + sameFileName) ; + + assertNotNull(util) ; + assertNotNull(util2) ; + + assertEquals(util, util2) ; + } +} diff --git a/poi-excelant/src/test/java9/module-info.class b/poi-excelant/src/test/java9/module-info.class Binary files differnew file mode 100644 index 0000000000..c4cef92e79 --- /dev/null +++ b/poi-excelant/src/test/java9/module-info.class diff --git a/poi-excelant/src/test/java9/module-info.java b/poi-excelant/src/test/java9/module-info.java new file mode 100644 index 0000000000..a26d9bd04d --- /dev/null +++ b/poi-excelant/src/test/java9/module-info.java @@ -0,0 +1,34 @@ +/* ==================================================================== + 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. +==================================================================== */ + +module org.apache.poi.excelant { + + requires ant; + requires org.apache.poi.ooxml; + requires org.apache.poi.scratchpad; + + exports org.apache.poi.ss.excelant; + exports org.apache.poi.ss.excelant.util; + + opens org.apache.poi.ss.excelant; + + // test specific exports + requires org.junit.jupiter.api; + requires org.junit.jupiter.params; + + opens org.apache.poi.ss.excelant.util to org.junit.platform.commons; +}
\ No newline at end of file diff --git a/poi-excelant/src/test/resources/tests.xml b/poi-excelant/src/test/resources/tests.xml new file mode 100644 index 0000000000..0882195551 --- /dev/null +++ b/poi-excelant/src/test/resources/tests.xml @@ -0,0 +1,226 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!-- +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. +--> +<project name="excelant-tests" basedir="." xmlns:poi="antlib:org.apache.poi.ss.excelant"> + + <path id="excelant.classpath"> + <pathelement location="build/classes"/> + <pathelement location="build/ooxml-classes"/> + <pathelement location="build/excelant-classes"/> + <pathelement location="build/excelant-test-classes"/> <!-- test udf is in the test classes --> + </path> + + <typedef resource="org/apache/poi/ss/excelant/antlib.xml" + classpathref="excelant.classpath" + uri="antlib:org.apache.poi.ss.excelant"/> + + <!-- Should fail because the fileName attribute is missing --> + <target name="test-nofile"> + <poi:excelant> + + </poi:excelant> + </target> + + <!-- Should fail because the specified file is invalid --> + <target name="test-filenotfound"> + <poi:excelant fileName="invalid.xls"> + + </poi:excelant> + </target> + + <!-- basic evaluation test --> + <target name="test-evaluate"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="true"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> + + <target name="test-evaluate-nodetails"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="false"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> + + <target name="test-precision"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:precision value="1.0E-4"/> + + <poi:test name="global-precision" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149"/> + </poi:test> + + <poi:test name="custom-precision" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-5"/> + </poi:test> + + <poi:test name="tiny-precision" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-10"/> + </poi:test> + + </poi:excelant> + </target> + + <target name="test-precision-fails"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:precision value="1.0E-4"/> + + <poi:test name="tiny-precision" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-10" requiredToPass="true"/> + </poi:test> + + </poi:excelant> + </target> + + <!-- + By default ExcelAnt does not terminate execution if an error occurs + --> + <target name="test-passonerror"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + + <poi:test name="failonerror" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$1" value="1"/> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$2" value="2"/> + <poi:setFormula cell="'MortageCalculatorFunction'!$D$3" value ="SUM(D1:D2)"/> + <poi:evaluate showDelta="true" cell="'MortageCalculatorFunction'!$D$3" expectedValue="2"/> + </poi:test> + + </poi:excelant> + </target> + + <!-- + failOnError="true" forces ExcelAnt tot terminate execution if an error occurs + --> + <target name="test-failonerror"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls" failOnError="true"> + + <poi:test name="failonerror" showFailureDetail="true" showSuccessDetails="true"> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$1" value="1"/> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$2" value="2"/> + <poi:setFormula cell="'MortageCalculatorFunction'!$D$3" value ="SUM(D1:D2)"/> + <poi:evaluate showDelta="true" cell="'MortageCalculatorFunction'!$D$3" expectedValue="2"/> + </poi:test> + + </poi:excelant> + </target> + + <target name="test-failonerror-notdetails"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls" failOnError="true"> + + <poi:test name="failonerror" showFailureDetail="false" showSuccessDetails="false"> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$1" value="1"/> + <poi:setDouble cell="'MortageCalculatorFunction'!$D$2" value="2"/> + <poi:setFormula cell="'MortageCalculatorFunction'!$D$3" value ="SUM(D1:D2)"/> + <poi:evaluate showDelta="true" cell="'MortageCalculatorFunction'!$D$3" expectedValue="2"/> + </poi:test> + + </poi:excelant> + </target> + + <!-- Evaluation of user-defined functions --> + <target name="test-udf"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:udf functionAlias="calculatePayment" + className="org.apache.poi.ss.excelant.CalculateMortgageFunction"/> + <poi:test> + <poi:setDouble cell="'MortageCalculatorFunction'!$B$1" value="240000"/> + <poi:setDouble cell="'MortageCalculatorFunction'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortageCalculatorFunction'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortageCalculatorFunction'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> + + <!-- basic evaluation test --> + <target name="test-settext"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="true"> + <poi:setString cell="'MortgageCalculator'!$B$1" value="sometext"/> + <!-- How can we evaluate text? --> + </poi:test> + </poi:excelant> + </target> + + <target name="test-addhandler"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="true"> + <poi:handler className="org.apache.poi.ss.excelant.MockExcelAntWorkbookHandler"/> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> + + <target name="test-addhandler-wrongclass"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="true"> + <poi:handler className="java.lang.String"/> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> + + <target name="test-addhandler-fails"> + <poi:excelant fileName="${data.dir.name}/spreadsheet/excelant.xls"> + <poi:test showSuccessDetails="true"> + <poi:handler/> + <poi:setDouble cell="'MortgageCalculator'!$B$1" value="240000"/> + <poi:setDouble cell="'MortgageCalculator'!$B$2" value ="0.11"/> + <poi:setDouble cell="'MortgageCalculator'!$B$3" value ="30"/> + <poi:evaluate showDelta="true" cell="'MortgageCalculator'!$B$4" + expectedValue="2285.576149" precision="1.0E-4" /> + </poi:test> + </poi:excelant> + </target> +</project> |