]> source.dussan.org Git - poi.git/commitdiff
Add XSSF support to ForkedEvaluator
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 7 Jun 2016 23:15:40 +0000 (23:15 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 7 Jun 2016 23:15:40 +0000 (23:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1747326 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFForkedEvaluator.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/formula/eval/forked/TestForkedEvaluator.java

index 21fa5763840fb8870455f66adbe250bad4dcf8d2..9ae19a1917dde8b3a6a58f4a3a7d749407b6d566 100644 (file)
@@ -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 <code>null</code> for default (AnalysisToolPak only)
         */
index 3469205165ad10768a1df2a5df6966fce3d26c32..96b363283bbdee409468c813eab2f26e3692b251 100644 (file)
@@ -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 (file)
index 0000000..6de609a
--- /dev/null
@@ -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();
+    }
+}
index fc945c7ff547ad036de59e8a57fb0da412cdeaa8..ea1f97b7296b498c4b0a187910882354b94f78cd 100644 (file)
@@ -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 {
         * <i>read-only</i> 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);