From 4cd56aefdbc87fb9a05837f62e1158d66e21bb4f Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 7 Jun 2016 23:15:40 +0000 Subject: [PATCH] Add XSSF support to ForkedEvaluator git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1747326 13f79535-47bb-0310-9956-ffa450edef68 --- .../formula/eval/forked/ForkedEvaluator.java | 27 +++++++++--------- .../xssf/usermodel/AllXSSFUsermodelTests.java | 4 ++- .../usermodel/TestXSSFForkedEvaluator.java | 28 +++++++++++++++++++ .../eval/forked/TestForkedEvaluator.java | 27 ++++++++++-------- 4 files changed, 61 insertions(+), 25 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFForkedEvaluator.java diff --git a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java index 21fa576384..9ae19a1917 100644 --- a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java @@ -23,6 +23,9 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.udf.UDFFinder; + +import java.lang.reflect.Method; + import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment; @@ -40,8 +43,6 @@ import org.apache.poi.ss.usermodel.Workbook; * This class enables a 'master workbook' to be loaded just once and shared between many evaluation * clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values * that will be used for local evaluations (and don't disturb evaluations on other evaluators). - * - * @author Josh Micich */ public final class ForkedEvaluator { @@ -55,19 +56,19 @@ public final class ForkedEvaluator { private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) { if (wb instanceof HSSFWorkbook) { return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb); + } else { + try { + // TODO: check if this is Java 9 compatible ... + Class evalWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook"); + Class xssfWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFWorkbook"); + Method createM = evalWB.getDeclaredMethod("create", xssfWB); + return (EvaluationWorkbook)createM.invoke(null, wb); + } catch (Exception e) { + throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ") - check for poi-ooxml and poi-ooxml schemas jar in the classpath", e); + } } -// TODO rearrange POI build to allow this -// if (wb instanceof XSSFWorkbook) { -// return XSSFEvaluationWorkbook.create((XSSFWorkbook) wb); -// } - throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ")"); - } - /** - * @deprecated (Sep 2009) (reduce overloading) use {@link #create(Workbook, IStabilityClassifier, UDFFinder)} - */ - public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier) { - return create(wb, stabilityClassifier, null); } + /** * @param udfFinder pass null for default (AnalysisToolPak only) */ diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java index 3469205165..96b363283b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java @@ -17,6 +17,7 @@ package org.apache.poi.xssf.usermodel; +import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator; import org.apache.poi.xssf.usermodel.extensions.TestXSSFBorder; import org.apache.poi.xssf.usermodel.extensions.TestXSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.TestXSSFSheetComments; @@ -58,7 +59,8 @@ import org.junit.runners.Suite; TestXSSFSheetComments.class, TestColumnHelper.class, TestHeaderFooterHelper.class, - TestXSSFPivotTable.class + TestXSSFPivotTable.class, + TestForkedEvaluator.class }) public final class AllXSSFUsermodelTests { } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFForkedEvaluator.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFForkedEvaluator.java new file mode 100644 index 0000000000..6de609a5ed --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFForkedEvaluator.java @@ -0,0 +1,28 @@ +/* ==================================================================== + 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.xssf.usermodel; + +import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator; +import org.apache.poi.ss.usermodel.Workbook; + +public class TestXSSFForkedEvaluator extends TestForkedEvaluator { + + protected Workbook newWorkbook() { + return new XSSFWorkbook(); + } +} diff --git a/src/testcases/org/apache/poi/ss/formula/eval/forked/TestForkedEvaluator.java b/src/testcases/org/apache/poi/ss/formula/eval/forked/TestForkedEvaluator.java index fc945c7ff5..ea1f97b729 100644 --- a/src/testcases/org/apache/poi/ss/formula/eval/forked/TestForkedEvaluator.java +++ b/src/testcases/org/apache/poi/ss/formula/eval/forked/TestForkedEvaluator.java @@ -21,29 +21,34 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; -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.IStabilityClassifier; import org.apache.poi.ss.formula.eval.NumberEval; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -public final class TestForkedEvaluator { +public class TestForkedEvaluator { @Rule public ExpectedException expectedEx = ExpectedException.none(); + protected Workbook newWorkbook() { + return new HSSFWorkbook(); + } + /** * set up a calculation workbook with input cells nicely segregated on a * sheet called "Inputs" */ - private static HSSFWorkbook createWorkbook() { - HSSFWorkbook wb = new HSSFWorkbook(); - HSSFSheet sheet1 = wb.createSheet("Inputs"); - HSSFSheet sheet2 = wb.createSheet("Calculations"); - HSSFRow row; + protected Workbook createWorkbook() { + Workbook wb = newWorkbook(); + Sheet sheet1 = wb.createSheet("Inputs"); + Sheet sheet2 = wb.createSheet("Calculations"); + Row row; row = sheet2.createRow(0); row.createCell(0).setCellFormula("B1*Inputs!A1-Inputs!B1"); row.createCell(1).setCellValue(5.0); // Calculations!B1 @@ -60,7 +65,7 @@ public final class TestForkedEvaluator { */ @Test public void testBasic() throws IOException { - HSSFWorkbook wb = createWorkbook(); + Workbook wb = createWorkbook(); // The stability classifier is useful to reduce memory consumption of caching logic IStabilityClassifier stabilityClassifier = new IStabilityClassifier() { @@ -99,11 +104,11 @@ public final class TestForkedEvaluator { * read-only with respect to the ForkedEvaluator. */ @Test - public void testMissingInputCell() throws IOException { + public void testMissingInputCellH() throws IOException { expectedEx.expect(UnsupportedOperationException.class); expectedEx.expectMessage("Underlying cell 'A2' is missing in master sheet."); - HSSFWorkbook wb = createWorkbook(); + Workbook wb = createWorkbook(); try { ForkedEvaluator fe = ForkedEvaluator.create(wb, null, null); -- 2.39.5