From bb3bf964bdcde9f8d212e0d25e69099d8a5212ff Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Wed, 17 Sep 2008 19:52:55 +0000 Subject: [PATCH] Minor fixes to get junits working after r695832. Updates to align with new sample.xlsx, and moved SharedStringSource out of usermodel API. git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@696415 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/usermodel/SharedStringSource.java | 20 -- .../poi/xssf/model}/SharedStringSource.java | 7 +- .../poi/xssf/model/SharedStringsTable.java | 9 +- .../apache/poi/xssf/usermodel/XSSFCell.java | 9 +- .../poi/xssf/usermodel/XSSFWorkbook.java | 3 +- .../apache/poi/xssf/XSSFTestDataSamples.java | 73 +++++++ .../extractor/TestXSSFExcelExtractor.java | 180 +++++------------- .../xssf/model/TestSharedStringsTable.java | 47 ++--- .../poi/xssf/usermodel/TestXSSFCell.java | 49 +++-- 9 files changed, 183 insertions(+), 214 deletions(-) delete mode 100644 src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/SharedStringSource.java rename src/ooxml/{interfaces-jdk15/org/apache/poi/ss/usermodel => java/org/apache/poi/xssf/model}/SharedStringSource.java (86%) create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.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 index 1c1075a47..000000000 --- a/src/ooxml/interfaces-jdk14/org/apache/poi/ss/usermodel/SharedStringSource.java +++ /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/java/org/apache/poi/xssf/model/SharedStringSource.java similarity index 86% rename from src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java rename to src/ooxml/java/org/apache/poi/xssf/model/SharedStringSource.java index 6d56abc2c..55ad3eba3 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/SharedStringSource.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringSource.java @@ -15,11 +15,14 @@ limitations under the License. ==================================================================== */ -package org.apache.poi.ss.usermodel; +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); } diff --git a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java index 1712e3c94..515706c77 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/SharedStringsTable.java @@ -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); } - } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 658d4ebbd..4b77bd10f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -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(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index e3d770fe5..53b13ce30 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 index 000000000..aa1d78fe5 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/XSSFTestDataSamples.java @@ -0,0 +1,73 @@ +/* ==================================================================== + 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; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.poi.hssf.HSSFTestDataSamples; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxml4j.exceptions.InvalidFormatException; +import org.openxml4j.opc.Package; + +/** + * Centralises logic for finding/opening sample files in the src/testcases/org/apache/poi/hssf/hssf/data folder. + * + * @author Josh Micich + */ +public class XSSFTestDataSamples { + public static final XSSFWorkbook openSampleWorkbook(String sampleName) { + InputStream is = HSSFTestDataSamples.openSampleFileStream(sampleName); + try { + Package pkg = Package.open(is); + return new XSSFWorkbook(pkg); + } catch (InvalidFormatException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static R writeOutAndReadBack(R wb) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); + Workbook result; + try { + wb.write(baos); + InputStream is = new ByteArrayInputStream(baos.toByteArray()); + if (wb instanceof HSSFWorkbook) { + result = new HSSFWorkbook(is); + } else if (wb instanceof XSSFWorkbook) { + Package pkg = Package.open(is); + result = new XSSFWorkbook(pkg); + } else { + throw new RuntimeException("Unexpected workbook type (" + + wb.getClass().getName() + ")"); + } + } catch (InvalidFormatException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } + @SuppressWarnings("unchecked") + R r = (R) result; + return r; + } +} diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java index e553084c3..1f170d279 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java @@ -14,77 +14,35 @@ 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 diff --git a/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java index c9043a045..838a0fbbf 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/model/TestSharedStringsTable.java @@ -19,37 +19,26 @@ package org.apache.poi.xssf.model; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.File; import java.io.IOException; import java.util.List; -import org.apache.poi.xssf.usermodel.XSSFCellStyle; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xssf.usermodel.XSSFRichTextString; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; - import junit.framework.TestCase; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.usermodel.XSSFRichTextString; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; + /** - * Test SharedStringsTable, the cache of strings in a workbook + * Test {@link SharedStringsTable}, the cache of strings in a workbook * * @author Yegor Kozlov */ -public class TestSharedStringsTable extends TestCase { - private File xml; - - protected void setUp() throws Exception { - xml = new File( - System.getProperty("HSSF.testdata.path") + - File.separator + "sample.xlsx" - ); - assertTrue(xml.exists()); - } +public final class TestSharedStringsTable extends TestCase { - public void testCreateNew() throws Exception { + public void testCreateNew() { SharedStringsTable sst = new SharedStringsTable(); CTRst st; @@ -120,15 +109,19 @@ public class TestSharedStringsTable extends TestCase { assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString()); } - public void testReadWrite() throws Exception { - XSSFWorkbook wb = new XSSFWorkbook(xml.getPath()); + public void testReadWrite() { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx"); SharedStringsTable sst1 = (SharedStringsTable)wb.getSharedStringSource(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - sst1.writeTo(out); - //serialize, read back and compare with the original - SharedStringsTable sst2 = new SharedStringsTable(new ByteArrayInputStream(out.toByteArray())); + SharedStringsTable sst2; + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + sst1.writeTo(out); + sst2 = new SharedStringsTable(new ByteArrayInputStream(out.toByteArray())); + } catch (IOException e) { + throw new RuntimeException(e); + } assertEquals(sst1.getCount(), sst2.getCount()); assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index 1a1cf9a3a..58b8ee730 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -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 strs = new ArrayList(); - public String getSharedStringAt(int idx) { + ArrayList strs = new ArrayList(); + 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());