]> source.dussan.org Git - poi.git/commitdiff
Minor fixes to get junits working after r695832. Updates to align with new sample...
authorJosh Micich <josh@apache.org>
Wed, 17 Sep 2008 19:52:55 +0000 (19:52 +0000)
committerJosh Micich <josh@apache.org>
Wed, 17 Sep 2008 19:52:55 +0000 (19:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@696415 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/SharedStringSource.java [deleted file]
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java [deleted file]
src/ooxml/java/org/apache/poi/xssf/model/SharedStringSource.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java [new file with mode: 0644]
src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

diff --git a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/SharedStringSource.java b/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/SharedStringSource.java
deleted file mode 100644 (file)
index 1c1075a..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-/* ====================================================================
-   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.usermodel;
-
-public interface SharedStringSource {}
diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java
deleted file mode 100644 (file)
index 6d56abc..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/* ====================================================================
-   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.usermodel;
-
-/**
- * Allows the getting and saving of shared strings
- */
-public interface SharedStringSource {
-    
-}
diff --git a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringSource.java b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringSource.java
new file mode 100644 (file)
index 0000000..55ad3eb
--- /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.model;
+
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
+
+/**
+ * Allows the getting and saving of shared strings
+ */
+public interface SharedStringSource {
+       CTRst getEntryAt(int ref);
+       int addEntry(CTRst rst);  
+}
index 1712e3c94f0997961875f849625c56271806c84b..515706c772d0c4b31c5f013dd83f693eb6c20a4c 100644 (file)
@@ -20,16 +20,16 @@ package org.apache.poi.xssf.model;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
-import org.apache.poi.ss.usermodel.SharedStringSource;
-import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
 
 
 /**
@@ -204,5 +204,4 @@ public class SharedStringsTable implements SharedStringSource, XSSFModel {
         sst.setSiArray(ctr);
         doc.save(out, options);
     }
-
 }
index 658d4ebbdd315b1a170ddb4b6fe20a9ca5f39758..4b77bd10f0a8b6302ed336bcdedb949374b92e76 100644 (file)
@@ -26,13 +26,12 @@ import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Comment;
 import org.apache.poi.ss.usermodel.Hyperlink;
 import org.apache.poi.ss.usermodel.RichTextString;
-import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
+import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.xssf.model.SharedStringsTable;
+import org.apache.poi.xssf.model.SharedStringSource;
 import org.apache.poi.xssf.model.StylesTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
@@ -48,7 +47,7 @@ public final class XSSFCell implements Cell {
     private final CTCell cell;
     private final XSSFRow row;
     private int cellNum;
-    private SharedStringsTable sharedStringSource;
+    private SharedStringSource sharedStringSource;
     private StylesTable stylesSource;
 
     private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
@@ -67,7 +66,7 @@ public final class XSSFCell implements Cell {
         if (cell.getR() != null) {
             this.cellNum = parseCellNum(cell.getR());
         }
-        this.sharedStringSource = (SharedStringsTable) row.getSheet().getWorkbook().getSharedStringSource();
+        this.sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource();
         this.stylesSource = (StylesTable)row.getSheet().getWorkbook().getStylesSource();
     }
 
index e3d770fe598e14bef9690b538bae9cd29ac4fb9b..53b13ce3023fd88b71ad61d854cbbff3eaba5376 100644 (file)
@@ -34,7 +34,6 @@ import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.Palette;
 import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -44,6 +43,7 @@ import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.Control;
 import org.apache.poi.xssf.model.Drawing;
+import org.apache.poi.xssf.model.SharedStringSource;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.model.XSSFModel;
@@ -64,7 +64,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java
new file mode 100644 (file)
index 0000000..aa1d78f
--- /dev/null
@@ -0,0 +1,73 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.openxml4j.exceptions.InvalidFormatException;\r
+import org.openxml4j.opc.Package;\r
+\r
+/**\r
+ * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder. \r
+ * \r
+ * @author Josh Micich\r
+ */\r
+public class XSSFTestDataSamples {\r
+       public static final XSSFWorkbook openSampleWorkbook(String sampleName) {\r
+               InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName);\r
+               try {\r
+                       Package pkg = Package.open(is);\r
+                       return new XSSFWorkbook(pkg);\r
+               } catch (InvalidFormatException e) {\r
+                       throw new RuntimeException(e);\r
+               } catch (IOException e) {\r
+                       throw new RuntimeException(e);\r
+               }\r
+       }\r
+    public static <R extends Workbook> R writeOutAndReadBack(R wb) {\r
+       ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);\r
+       Workbook result;\r
+               try {\r
+               wb.write(baos);\r
+               InputStream is = new ByteArrayInputStream(baos.toByteArray());\r
+               if (wb instanceof HSSFWorkbook) {\r
+                       result = new HSSFWorkbook(is);\r
+               } else if (wb instanceof XSSFWorkbook) {\r
+                       Package pkg = Package.open(is);\r
+                       result = new XSSFWorkbook(pkg);\r
+               } else {\r
+                       throw new RuntimeException("Unexpected workbook type (" \r
+                                       + wb.getClass().getName() + ")");\r
+               }\r
+               } catch (InvalidFormatException e) {\r
+                       throw new RuntimeException(e);\r
+               } catch (IOException e) {\r
+                       throw new RuntimeException(e);\r
+               }\r
+               @SuppressWarnings("unchecked")\r
+               R r = (R) result;\r
+               return r;\r
+    }\r
+}\r
index e553084c36c7334d496dbab4e9c729aea47c3a51..1f170d27908d9491e0d741faf053adf66caa0108 100644 (file)
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
+
 package org.apache.poi.xssf.extractor;
 
-import java.io.File;
-import java.io.FileInputStream;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import junit.framework.TestCase;
 
 import org.apache.poi.POITextExtractor;
+import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.extractor.ExcelExtractor;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.poi.xssf.XSSFTestDataSamples;
 
 /**
- * Tests for XSSFExcelExtractor
+ * Tests for {@link XSSFExcelExtractor}
  */
-public class TestXSSFExcelExtractor extends TestCase {
-       /**
-        * A very simple file
-        */
-       private File xmlA;
-       /**
-        * A fairly complex file
-        */
-       private File xmlB;
-       
-       /**
-        * A fairly simple file - ooxml
-        */
-       private File simpleXLSX; 
-       /**
-        * A fairly simple file - ole2
-        */
-       private File simpleXLS;
+public final class TestXSSFExcelExtractor extends TestCase {
 
-       protected void setUp() throws Exception {
-               super.setUp();
-               
-               xmlA = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "sample.xlsx"
-               );
-               assertTrue(xmlA.exists());
-               xmlB = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "AverageTaxRates.xlsx"
-               );
-               assertTrue(xmlB.exists());
-               
-               simpleXLSX = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "SampleSS.xlsx"
-               );
-               simpleXLS = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "SampleSS.xls"
-               );
-               assertTrue(simpleXLS.exists());
-               assertTrue(simpleXLSX.exists());
+
+       private static final XSSFExcelExtractor getExtractor(String sampleName) {
+               return new XSSFExcelExtractor(XSSFTestDataSamples.openSampleWorkbook(sampleName));
        }
 
        /**
         * Get text out of the simple file
         */
-       public void testGetSimpleText() throws Exception {
-               new XSSFExcelExtractor(xmlA.toString());
-               new XSSFExcelExtractor(new XSSFWorkbook(xmlA.toString()));
-               
-               XSSFExcelExtractor extractor = 
-                       new XSSFExcelExtractor(xmlA.toString());
+       public void testGetSimpleText() {
+               // a very simple file
+               XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
                extractor.getText();
                
                String text = extractor.getText();
@@ -97,73 +55,52 @@ public class TestXSSFExcelExtractor extends TestCase {
                // Now without, will have text
                extractor.setIncludeSheetNames(false);
                text = extractor.getText();
+               String CHUNK1 =
+                       "Lorem\t111\n" + 
+               "ipsum\t222\n" + 
+               "dolor\t333\n" + 
+               "sit\t444\n" + 
+               "amet\t555\n" + 
+               "consectetuer\t666\n" + 
+               "adipiscing\t777\n" + 
+               "elit\t888\n" + 
+               "Nunc\t999\n";
+               String CHUNK2 =
+                       "The quick brown fox jumps over the lazy dog\n" +
+                       "hello, xssf    hello, xssf\n" +
+                       "hello, xssf    hello, xssf\n" +
+                       "hello, xssf    hello, xssf\n" +
+                       "hello, xssf    hello, xssf\n";
                assertEquals(
-                               "Lorem\t111\n" +
-                               "ipsum\t222\n" +
-                               "dolor\t333\n" +
-                               "sit\t444\n" +
-                               "amet\t555\n" +
-                               "consectetuer\t666\n" +
-                               "adipiscing\t777\n" +
-                               "elit\t888\n" +
-                               "Nunc\t999\n" +
-                               "at\t4995\n" +
-                "The quick brown fox jumps over the lazy dog\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n", text);
+                               CHUNK1 + 
+                               "at\t4995\n" + 
+                               CHUNK2
+                               , text);
                
                // Now get formulas not their values
                extractor.setFormulasNotResults(true);
                text = extractor.getText();
                assertEquals(
-                               "Lorem\t111\n" +
-                               "ipsum\t222\n" +
-                               "dolor\t333\n" +
-                               "sit\t444\n" +
-                               "amet\t555\n" +
-                               "consectetuer\t666\n" +
-                               "adipiscing\t777\n" +
-                               "elit\t888\n" +
-                               "Nunc\t999\n" +
-                               "at\tSUM(B1:B9)\n" +
-                "The quick brown fox jumps over the lazy dog\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n", text);
+                               CHUNK1 +
+                               "at\tSUM(B1:B9)\n" + 
+                               CHUNK2, text);
                
                // With sheet names too
                extractor.setIncludeSheetNames(true);
                text = extractor.getText();
                assertEquals(
                                "Sheet1\n" +
-                               "Lorem\t111\n" +
-                               "ipsum\t222\n" +
-                               "dolor\t333\n" +
-                               "sit\t444\n" +
-                               "amet\t555\n" +
-                               "consectetuer\t666\n" +
-                               "adipiscing\t777\n" +
-                               "elit\t888\n" +
-                               "Nunc\t999\n" +
-                               "at\tSUM(B1:B9)\n" +
+                               CHUNK1 +
+                               "at\tSUM(B1:B9)\n" + 
                                "rich test\n" +
-                "The quick brown fox jumps over the lazy dog\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
-                "hello, xssf\thello, xssf\n" +
+                               CHUNK2 +
                                "Sheet3\n"
                                , text);
        }
        
-       public void testGetComplexText() throws Exception {
-               new XSSFExcelExtractor(xmlB.toString());
-               
-               XSSFExcelExtractor extractor = 
-                       new XSSFExcelExtractor(new XSSFWorkbook(xmlB.toString()));
+       public void testGetComplexText() {
+               // A fairly complex file
+               XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
                extractor.getText();
                
                String text = extractor.getText();
@@ -182,12 +119,12 @@ public class TestXSSFExcelExtractor extends TestCase {
         *  ExcelExtractor does, when we're both passed
         *  the same file, just saved as xls and xlsx
         */
-       public void testComparedToOLE2() throws Exception {
-               XSSFExcelExtractor ooxmlExtractor =
-                       new XSSFExcelExtractor(simpleXLSX.toString());
+       public void testComparedToOLE2() {
+               // A fairly simple file - ooxml
+               XSSFExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
+
                ExcelExtractor ole2Extractor =
-                       new ExcelExtractor(new HSSFWorkbook(
-                                       new FileInputStream(simpleXLS)));
+                       new ExcelExtractor(HSSFTestDataSamples.openSampleWorkbook("SampleSS.xls"));
                
                POITextExtractor[] extractors =
                        new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
@@ -207,39 +144,26 @@ public class TestXSSFExcelExtractor extends TestCase {
        /**
         * From bug #45540
         */
-       public void testHeaderFooter() throws Exception {
+       public void testHeaderFooter() {
                String[] files = new String[] {
                        "45540_classic_Header.xlsx", "45540_form_Header.xlsx",
                        "45540_classic_Footer.xlsx", "45540_form_Footer.xlsx",
                };
-               for(String file : files) {
-                       File xml = new File(
-                                       System.getProperty("HSSF.testdata.path") +
-                                       File.separator + file
-                       );
-                       assertTrue(xml.exists());
-                       
-                       XSSFExcelExtractor extractor = 
-                               new XSSFExcelExtractor(new XSSFWorkbook(xml.toString()));
+               for(String sampleName : files) {
+                       XSSFExcelExtractor extractor = getExtractor(sampleName);
                        String text = extractor.getText();
                        
-                       assertTrue("Unable to find expected word in text from " + file + "\n" + text, text.contains("testdoc"));
-               assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); 
+                       assertTrue("Unable to find expected word in text from " + sampleName + "\n" + text, text.contains("testdoc"));
+                       assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); 
                }
        }
 
        /**
         * From bug #45544
         */
-       public void testComments() throws Exception {
-               File xml = new File(
-                               System.getProperty("HSSF.testdata.path") +
-                               File.separator + "45544.xlsx"
-               );
-               assertTrue(xml.exists());
+       public void testComments() {
                
-               XSSFExcelExtractor extractor = 
-                       new XSSFExcelExtractor(new XSSFWorkbook(xml.toString()));
+               XSSFExcelExtractor extractor = getExtractor("45544.xlsx");
                String text = extractor.getText();
 
                // No comments there yet
index c9043a045bba6fea6e8c7b142eaa785a9bea9260..838a0fbbfac15dcc85157b7b90c1f2bb795686a0 100755 (executable)
@@ -19,37 +19,26 @@ package org.apache.poi.xssf.model;
 \r
 import java.io.ByteArrayInputStream;\r
 import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
 import java.io.IOException;\r
 import java.util.List;\r
 \r
-import org.apache.poi.xssf.usermodel.XSSFCellStyle;\r
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import junit.framework.TestCase;\r
+\r
+import org.apache.poi.xssf.XSSFTestDataSamples;\r
 import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;\r
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;\r
-\r
-import junit.framework.TestCase;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;\r
 \r
 /**\r
- * Test SharedStringsTable, the cache of strings in a workbook\r
+ * Test {@link SharedStringsTable}, the cache of strings in a workbook\r
  *\r
  * @author Yegor Kozlov\r
  */\r
-public class TestSharedStringsTable extends TestCase {\r
-       private File xml;\r
-       \r
-       protected void setUp() throws Exception {\r
-               xml = new File(\r
-                               System.getProperty("HSSF.testdata.path") +\r
-                               File.separator + "sample.xlsx"\r
-               );\r
-               assertTrue(xml.exists());\r
-       }\r
-\r
-       public void testCreateNew() throws Exception {\r
+public final class TestSharedStringsTable extends TestCase {\r
+\r
+       public void testCreateNew() {\r
                SharedStringsTable sst = new SharedStringsTable();\r
                \r
         CTRst st;\r
@@ -120,15 +109,19 @@ public class TestSharedStringsTable extends TestCase {
         assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());\r
     }\r
        \r
-       public void testReadWrite() throws Exception {\r
-        XSSFWorkbook wb = new XSSFWorkbook(xml.getPath());\r
+       public void testReadWrite() {\r
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");\r
         SharedStringsTable sst1 = (SharedStringsTable)wb.getSharedStringSource();\r
 \r
-        ByteArrayOutputStream out = new ByteArrayOutputStream();\r
-        sst1.writeTo(out);\r
-\r
         //serialize, read back and compare with the original\r
-        SharedStringsTable sst2 = new SharedStringsTable(new ByteArrayInputStream(out.toByteArray()));\r
+               SharedStringsTable sst2;\r
+               try {\r
+                       ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+                       sst1.writeTo(out);\r
+                       sst2 = new SharedStringsTable(new ByteArrayInputStream(out.toByteArray()));\r
+               } catch (IOException e) {\r
+                       throw new RuntimeException(e);\r
+               }\r
         assertEquals(sst1.getCount(), sst2.getCount());\r
         assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());\r
 \r
index 1a1cf9a3a291133833c957e4d16c3d19a7cc8328..58b8ee730f73b414185e8bc3ab0f6db08f2fde10 100644 (file)
@@ -17,8 +17,6 @@
 
 package org.apache.poi.xssf.usermodel;
 
-import java.io.File;
-import java.io.FileOutputStream;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
@@ -30,19 +28,24 @@ import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Comment;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.model.SharedStringSource;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
 
-
-public class TestXSSFCell extends TestCase {
+/**
+ * Tests for {@link XSSFCell}
+ *
+ */
+public final class TestXSSFCell extends TestCase {
     
     private static final String TEST_C10_AUTHOR = "test C10 author";
 
@@ -160,7 +163,7 @@ public class TestXSSFCell extends TestCase {
         assertEquals("Foo2", cell.getRichStringCellValue().getString());
     }
     
-    public void testSetGetStringShared() throws Exception {
+    public void testSetGetStringShared() {
         XSSFRow row = createParentObjects();
         XSSFCell cell = new XSSFCell(row);
 
@@ -176,7 +179,7 @@ public class TestXSSFCell extends TestCase {
     /**
      * Test that empty cells (no v element) return default values.
      */
-    public void testGetEmptyCellValue() throws Exception {
+    public void testGetEmptyCellValue() {
         XSSFRow row = createParentObjects();
         XSSFCell cell = new XSSFCell(row);
         cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
@@ -216,12 +219,12 @@ public class TestXSSFCell extends TestCase {
     }
     
     public static class DummySharedStringSource implements SharedStringSource {
-        ArrayList<String> strs = new ArrayList<String>();
-        public String getSharedStringAt(int idx) {
+        ArrayList<CTRst> strs = new ArrayList<CTRst>();
+        public CTRst getEntryAt(int idx) {
             return strs.get(idx);
         }
 
-        public synchronized int putSharedString(String s) {
+        public synchronized int addEntry(CTRst s) {
             if(strs.contains(s)) {
                 return strs.indexOf(s);
             }
@@ -241,8 +244,8 @@ public class TestXSSFCell extends TestCase {
         
         // Create C10 cell
         Row row = sheet.createRow(9);
-        Cell cell = row.createCell((short)2);
-        Cell cell3 = row.createCell((short)3);
+        row.createCell(2);
+        row.createCell(3);
         
         
         // Set a comment for C10 cell
@@ -267,8 +270,8 @@ public class TestXSSFCell extends TestCase {
         
         // Create C10 cell
         Row row = sheet.createRow(9);
-        Cell cell = row.createCell((short)2);
-        Cell cell3 = row.createCell((short)3);
+        Cell cell = row.createCell(2);
+        row.createCell(3);
         
         // Create a comment
         Comment comment = comments.addComment();
@@ -295,10 +298,11 @@ public class TestXSSFCell extends TestCase {
        assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
     }
     
+    
     /**
      * Tests that cell formatting stuff works as expected
      */
-    public void testCellFormatting() throws Exception {
+    public void testCellFormatting() {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        CreationHelper creationHelper = workbook.getCreationHelper();
@@ -324,18 +328,13 @@ public class TestXSSFCell extends TestCase {
        
        
        // Save, re-load, and test again
-       File tmp = File.createTempFile("poi", "xlsx");
-       FileOutputStream out = new FileOutputStream(tmp);
-       workbook.write(out);
-       out.close();
-       
-       Workbook wb2 = new XSSFWorkbook(tmp.toString());
+       Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
        Cell c2 = wb2.getSheetAt(0).getRow(0).getCell(0);
        assertEquals(new Date(654321), c2.getDateCellValue());
        assertEquals("yyyy/mm/dd", c2.getCellStyle().getDataFormatString());
     }
 
-    private XSSFRow createParentObjects() {
+    private static XSSFRow createParentObjects() {
         XSSFWorkbook wb = new XSSFWorkbook();
         wb.setSharedStringSource(new DummySharedStringSource());
         XSSFSheet sheet = new XSSFSheet(wb);
@@ -347,12 +346,12 @@ public class TestXSSFCell extends TestCase {
      * Test to ensure we can only assign cell styles that belong
      *  to our workbook, and not those from other workbooks.
      */
-    public void testCellStyleWorkbookMatch() throws Exception {
+    public void testCellStyleWorkbookMatch() {
        XSSFWorkbook wbA = new XSSFWorkbook();
        XSSFWorkbook wbB = new XSSFWorkbook();
        
-       XSSFCellStyle styA = (XSSFCellStyle)wbA.createCellStyle();
-       XSSFCellStyle styB = (XSSFCellStyle)wbB.createCellStyle();
+       XSSFCellStyle styA = wbA.createCellStyle();
+       XSSFCellStyle styB = wbB.createCellStyle();
        
        styA.verifyBelongsToStylesSource(wbA.getStylesSource());
        styB.verifyBelongsToStylesSource(wbB.getStylesSource());