]> source.dussan.org Git - poi.git/commitdiff
unit tests for SharedStringsTable
authorYegor Kozlov <yegor@apache.org>
Tue, 16 Sep 2008 12:27:05 +0000 (12:27 +0000)
committerYegor Kozlov <yegor@apache.org>
Tue, 16 Sep 2008 12:27:05 +0000 (12:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@695833 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java [new file with mode: 0755]

diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java
new file mode 100755 (executable)
index 0000000..c9043a0
--- /dev/null
@@ -0,0 +1,144 @@
+/* ====================================================================\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.model;\r
+\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 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.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;\r
+\r
+import junit.framework.TestCase;\r
+\r
+/**\r
+ * Test 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
+               SharedStringsTable sst = new SharedStringsTable();\r
+               \r
+        CTRst st;\r
+        int idx;\r
+\r
+        // Check defaults\r
+               assertNotNull(sst.getItems());\r
+               assertEquals(0, sst.getItems().size());\r
+        assertEquals(0, sst.getCount());\r
+        assertEquals(0, sst.getUniqueCount());\r
+\r
+        st = CTRst.Factory.newInstance();\r
+        st.setT("Hello, World!");\r
+\r
+        idx = sst.addEntry(st);\r
+        assertEquals(0, idx);\r
+        assertEquals(1, sst.getCount());\r
+        assertEquals(1, sst.getUniqueCount());\r
+\r
+        //add the same entry egain\r
+        idx = sst.addEntry(st);\r
+        assertEquals(0, idx);\r
+        assertEquals(2, sst.getCount());\r
+        assertEquals(1, sst.getUniqueCount());\r
+\r
+        //and egain\r
+        idx = sst.addEntry(st);\r
+        assertEquals(0, idx);\r
+        assertEquals(3, sst.getCount());\r
+        assertEquals(1, sst.getUniqueCount());\r
+\r
+        st = CTRst.Factory.newInstance();\r
+        st.setT("Second string");\r
+\r
+        idx = sst.addEntry(st);\r
+        assertEquals(1, idx);\r
+        assertEquals(4, sst.getCount());\r
+        assertEquals(2, sst.getUniqueCount());\r
+\r
+        //add the same entry egain\r
+        idx = sst.addEntry(st);\r
+        assertEquals(1, idx);\r
+        assertEquals(5, sst.getCount());\r
+        assertEquals(2, sst.getUniqueCount());\r
+\r
+        st = CTRst.Factory.newInstance();\r
+        CTRElt r = st.addNewR();\r
+        CTRPrElt pr = r.addNewRPr();\r
+        pr.addNewColor().setRgb(new byte[]{(byte)0xFF, 0, 0}); //red\r
+        pr.addNewI().setVal(true);  //bold\r
+        pr.addNewB().setVal(true);  //italic\r
+        r.setT("Second string");\r
+\r
+        idx = sst.addEntry(st);\r
+        assertEquals(2, idx);\r
+        assertEquals(6, sst.getCount());\r
+        assertEquals(3, sst.getUniqueCount());\r
+\r
+        idx = sst.addEntry(st);\r
+        assertEquals(2, idx);\r
+        assertEquals(7, sst.getCount());\r
+        assertEquals(3, sst.getUniqueCount());\r
+\r
+        //ok. the sst table is filled, check the contents\r
+        assertEquals(3, sst.getItems().size());\r
+        assertEquals("Hello, World!", new XSSFRichTextString(sst.getEntryAt(0)).toString());\r
+        assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(1)).toString());\r
+        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
+        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
+        assertEquals(sst1.getCount(), sst2.getCount());\r
+        assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());\r
+\r
+        List<CTRst> items1 = sst1.getItems();\r
+        List<CTRst> items2 = sst2.getItems();\r
+        assertEquals(items1.size(), items2.size());\r
+        for (int i = 0; i < items1.size(); i++) {\r
+            CTRst st1 = items1.get(i);\r
+            CTRst st2 = items2.get(i);\r
+            assertEquals(st1.toString(), st2.toString());\r
+        }\r
+    }\r
+}\r