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
This commit is contained in:
Josh Micich 2008-09-17 19:52:55 +00:00
parent e2c8b4f4d4
commit bb3bf964bd
9 changed files with 183 additions and 214 deletions

View File

@ -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 {}

View File

@ -15,11 +15,14 @@
limitations under the License. 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 * Allows the getting and saving of shared strings
*/ */
public interface SharedStringSource { public interface SharedStringSource {
CTRst getEntryAt(int ref);
int addEntry(CTRst rst);
} }

View File

@ -20,16 +20,16 @@ package org.apache.poi.xssf.model;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; 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.XmlException;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSst;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.SstDocument; 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); sst.setSiArray(ctr);
doc.save(out, options); doc.save(out, options);
} }
} }

View 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.Comment;
import org.apache.poi.ss.usermodel.Hyperlink; import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString; 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.Sheet;
import org.apache.poi.ss.usermodel.StylesSource; 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.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
import org.apache.poi.ss.util.CellReference; 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.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
@ -48,7 +47,7 @@ public final class XSSFCell implements Cell {
private final CTCell cell; private final CTCell cell;
private final XSSFRow row; private final XSSFRow row;
private int cellNum; private int cellNum;
private SharedStringsTable sharedStringSource; private SharedStringSource sharedStringSource;
private StylesTable stylesSource; private StylesTable stylesSource;
private POILogger logger = POILogFactory.getLogger(XSSFCell.class); private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
@ -67,7 +66,7 @@ public final class XSSFCell implements Cell {
if (cell.getR() != null) { if (cell.getR() != null) {
this.cellNum = parseCellNum(cell.getR()); 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(); this.stylesSource = (StylesTable)row.getSheet().getWorkbook().getStylesSource();
} }

View 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.Palette;
import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row; 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.Sheet;
import org.apache.poi.ss.usermodel.StylesSource; import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook; 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.CommentsTable;
import org.apache.poi.xssf.model.Control; import org.apache.poi.xssf.model.Control;
import org.apache.poi.xssf.model.Drawing; 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.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.XSSFModel; 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.CTDefinedName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet; 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.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;

View File

@ -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 extends Workbook> 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;
}
}

View File

@ -14,77 +14,35 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.extractor; package org.apache.poi.xssf.extractor;
import java.io.File;
import java.io.FileInputStream;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.POITextExtractor; import org.apache.poi.POITextExtractor;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.extractor.ExcelExtractor; import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/** /**
* Tests for XSSFExcelExtractor * Tests for {@link XSSFExcelExtractor}
*/ */
public class TestXSSFExcelExtractor extends TestCase { public final 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;
protected void setUp() throws Exception {
super.setUp(); private static final XSSFExcelExtractor getExtractor(String sampleName) {
return new XSSFExcelExtractor(XSSFTestDataSamples.openSampleWorkbook(sampleName));
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());
} }
/** /**
* Get text out of the simple file * Get text out of the simple file
*/ */
public void testGetSimpleText() throws Exception { public void testGetSimpleText() {
new XSSFExcelExtractor(xmlA.toString()); // a very simple file
new XSSFExcelExtractor(new XSSFWorkbook(xmlA.toString())); XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
XSSFExcelExtractor extractor =
new XSSFExcelExtractor(xmlA.toString());
extractor.getText(); extractor.getText();
String text = extractor.getText(); String text = extractor.getText();
@ -97,73 +55,52 @@ public class TestXSSFExcelExtractor extends TestCase {
// Now without, will have text // Now without, will have text
extractor.setIncludeSheetNames(false); extractor.setIncludeSheetNames(false);
text = extractor.getText(); 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( assertEquals(
"Lorem\t111\n" + CHUNK1 +
"ipsum\t222\n" + "at\t4995\n" +
"dolor\t333\n" + CHUNK2
"sit\t444\n" + , text);
"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);
// Now get formulas not their values // Now get formulas not their values
extractor.setFormulasNotResults(true); extractor.setFormulasNotResults(true);
text = extractor.getText(); text = extractor.getText();
assertEquals( assertEquals(
"Lorem\t111\n" + CHUNK1 +
"ipsum\t222\n" + "at\tSUM(B1:B9)\n" +
"dolor\t333\n" + CHUNK2, text);
"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);
// With sheet names too // With sheet names too
extractor.setIncludeSheetNames(true); extractor.setIncludeSheetNames(true);
text = extractor.getText(); text = extractor.getText();
assertEquals( assertEquals(
"Sheet1\n" + "Sheet1\n" +
"Lorem\t111\n" + CHUNK1 +
"ipsum\t222\n" + "at\tSUM(B1:B9)\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" +
"rich test\n" + "rich test\n" +
"The quick brown fox jumps over the lazy dog\n" + CHUNK2 +
"hello, xssf\thello, xssf\n" +
"hello, xssf\thello, xssf\n" +
"hello, xssf\thello, xssf\n" +
"hello, xssf\thello, xssf\n" +
"Sheet3\n" "Sheet3\n"
, text); , text);
} }
public void testGetComplexText() throws Exception { public void testGetComplexText() {
new XSSFExcelExtractor(xmlB.toString()); // A fairly complex file
XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
XSSFExcelExtractor extractor =
new XSSFExcelExtractor(new XSSFWorkbook(xmlB.toString()));
extractor.getText(); extractor.getText();
String text = extractor.getText(); String text = extractor.getText();
@ -182,12 +119,12 @@ public class TestXSSFExcelExtractor extends TestCase {
* ExcelExtractor does, when we're both passed * ExcelExtractor does, when we're both passed
* the same file, just saved as xls and xlsx * the same file, just saved as xls and xlsx
*/ */
public void testComparedToOLE2() throws Exception { public void testComparedToOLE2() {
XSSFExcelExtractor ooxmlExtractor = // A fairly simple file - ooxml
new XSSFExcelExtractor(simpleXLSX.toString()); XSSFExcelExtractor ooxmlExtractor = getExtractor("SampleSS.xlsx");
ExcelExtractor ole2Extractor = ExcelExtractor ole2Extractor =
new ExcelExtractor(new HSSFWorkbook( new ExcelExtractor(HSSFTestDataSamples.openSampleWorkbook("SampleSS.xls"));
new FileInputStream(simpleXLS)));
POITextExtractor[] extractors = POITextExtractor[] extractors =
new POITextExtractor[] { ooxmlExtractor, ole2Extractor }; new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
@ -207,39 +144,26 @@ public class TestXSSFExcelExtractor extends TestCase {
/** /**
* From bug #45540 * From bug #45540
*/ */
public void testHeaderFooter() throws Exception { public void testHeaderFooter() {
String[] files = new String[] { String[] files = new String[] {
"45540_classic_Header.xlsx", "45540_form_Header.xlsx", "45540_classic_Header.xlsx", "45540_form_Header.xlsx",
"45540_classic_Footer.xlsx", "45540_form_Footer.xlsx", "45540_classic_Footer.xlsx", "45540_form_Footer.xlsx",
}; };
for(String file : files) { for(String sampleName : files) {
File xml = new File( XSSFExcelExtractor extractor = getExtractor(sampleName);
System.getProperty("HSSF.testdata.path") +
File.separator + file
);
assertTrue(xml.exists());
XSSFExcelExtractor extractor =
new XSSFExcelExtractor(new XSSFWorkbook(xml.toString()));
String text = extractor.getText(); 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 from " + sampleName + "\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\n" + text, text.contains("test phrase"));
} }
} }
/** /**
* From bug #45544 * From bug #45544
*/ */
public void testComments() throws Exception { public void testComments() {
File xml = new File(
System.getProperty("HSSF.testdata.path") +
File.separator + "45544.xlsx"
);
assertTrue(xml.exists());
XSSFExcelExtractor extractor = XSSFExcelExtractor extractor = getExtractor("45544.xlsx");
new XSSFExcelExtractor(new XSSFWorkbook(xml.toString()));
String text = extractor.getText(); String text = extractor.getText();
// No comments there yet // No comments there yet

View File

@ -19,37 +19,26 @@ package org.apache.poi.xssf.model;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; 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 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 * @author Yegor Kozlov
*/ */
public class TestSharedStringsTable extends TestCase { public final 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 void testCreateNew() throws Exception { public void testCreateNew() {
SharedStringsTable sst = new SharedStringsTable(); SharedStringsTable sst = new SharedStringsTable();
CTRst st; CTRst st;
@ -120,15 +109,19 @@ public class TestSharedStringsTable extends TestCase {
assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString()); assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());
} }
public void testReadWrite() throws Exception { public void testReadWrite() {
XSSFWorkbook wb = new XSSFWorkbook(xml.getPath()); XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
SharedStringsTable sst1 = (SharedStringsTable)wb.getSharedStringSource(); SharedStringsTable sst1 = (SharedStringsTable)wb.getSharedStringSource();
ByteArrayOutputStream out = new ByteArrayOutputStream();
sst1.writeTo(out);
//serialize, read back and compare with the original //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.getCount(), sst2.getCount());
assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount()); assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());

View File

@ -17,8 +17,6 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; 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.Comment;
import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row; 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.Sheet;
import org.apache.poi.ss.usermodel.Workbook; 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.CommentsTable;
import org.apache.poi.xssf.model.SharedStringSource;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; 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.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; 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"; private static final String TEST_C10_AUTHOR = "test C10 author";
@ -160,7 +163,7 @@ public class TestXSSFCell extends TestCase {
assertEquals("Foo2", cell.getRichStringCellValue().getString()); assertEquals("Foo2", cell.getRichStringCellValue().getString());
} }
public void testSetGetStringShared() throws Exception { public void testSetGetStringShared() {
XSSFRow row = createParentObjects(); XSSFRow row = createParentObjects();
XSSFCell cell = new XSSFCell(row); XSSFCell cell = new XSSFCell(row);
@ -176,7 +179,7 @@ public class TestXSSFCell extends TestCase {
/** /**
* Test that empty cells (no v element) return default values. * Test that empty cells (no v element) return default values.
*/ */
public void testGetEmptyCellValue() throws Exception { public void testGetEmptyCellValue() {
XSSFRow row = createParentObjects(); XSSFRow row = createParentObjects();
XSSFCell cell = new XSSFCell(row); XSSFCell cell = new XSSFCell(row);
cell.setCellType(Cell.CELL_TYPE_BOOLEAN); cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
@ -216,12 +219,12 @@ public class TestXSSFCell extends TestCase {
} }
public static class DummySharedStringSource implements SharedStringSource { public static class DummySharedStringSource implements SharedStringSource {
ArrayList<String> strs = new ArrayList<String>(); ArrayList<CTRst> strs = new ArrayList<CTRst>();
public String getSharedStringAt(int idx) { public CTRst getEntryAt(int idx) {
return strs.get(idx); return strs.get(idx);
} }
public synchronized int putSharedString(String s) { public synchronized int addEntry(CTRst s) {
if(strs.contains(s)) { if(strs.contains(s)) {
return strs.indexOf(s); return strs.indexOf(s);
} }
@ -241,8 +244,8 @@ public class TestXSSFCell extends TestCase {
// Create C10 cell // Create C10 cell
Row row = sheet.createRow(9); Row row = sheet.createRow(9);
Cell cell = row.createCell((short)2); row.createCell(2);
Cell cell3 = row.createCell((short)3); row.createCell(3);
// Set a comment for C10 cell // Set a comment for C10 cell
@ -267,8 +270,8 @@ public class TestXSSFCell extends TestCase {
// Create C10 cell // Create C10 cell
Row row = sheet.createRow(9); Row row = sheet.createRow(9);
Cell cell = row.createCell((short)2); Cell cell = row.createCell(2);
Cell cell3 = row.createCell((short)3); row.createCell(3);
// Create a comment // Create a comment
Comment comment = comments.addComment(); Comment comment = comments.addComment();
@ -295,10 +298,11 @@ public class TestXSSFCell extends TestCase {
assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell()); assertEquals("A1", ctWorksheet.getSheetViews().getSheetViewArray(0).getSelectionArray(0).getActiveCell());
} }
/** /**
* Tests that cell formatting stuff works as expected * Tests that cell formatting stuff works as expected
*/ */
public void testCellFormatting() throws Exception { public void testCellFormatting() {
Workbook workbook = new XSSFWorkbook(); Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(); Sheet sheet = workbook.createSheet();
CreationHelper creationHelper = workbook.getCreationHelper(); CreationHelper creationHelper = workbook.getCreationHelper();
@ -324,18 +328,13 @@ public class TestXSSFCell extends TestCase {
// Save, re-load, and test again // Save, re-load, and test again
File tmp = File.createTempFile("poi", "xlsx"); Workbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
FileOutputStream out = new FileOutputStream(tmp);
workbook.write(out);
out.close();
Workbook wb2 = new XSSFWorkbook(tmp.toString());
Cell c2 = wb2.getSheetAt(0).getRow(0).getCell(0); Cell c2 = wb2.getSheetAt(0).getRow(0).getCell(0);
assertEquals(new Date(654321), c2.getDateCellValue()); assertEquals(new Date(654321), c2.getDateCellValue());
assertEquals("yyyy/mm/dd", c2.getCellStyle().getDataFormatString()); assertEquals("yyyy/mm/dd", c2.getCellStyle().getDataFormatString());
} }
private XSSFRow createParentObjects() { private static XSSFRow createParentObjects() {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
wb.setSharedStringSource(new DummySharedStringSource()); wb.setSharedStringSource(new DummySharedStringSource());
XSSFSheet sheet = new XSSFSheet(wb); 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 * Test to ensure we can only assign cell styles that belong
* to our workbook, and not those from other workbooks. * to our workbook, and not those from other workbooks.
*/ */
public void testCellStyleWorkbookMatch() throws Exception { public void testCellStyleWorkbookMatch() {
XSSFWorkbook wbA = new XSSFWorkbook(); XSSFWorkbook wbA = new XSSFWorkbook();
XSSFWorkbook wbB = new XSSFWorkbook(); XSSFWorkbook wbB = new XSSFWorkbook();
XSSFCellStyle styA = (XSSFCellStyle)wbA.createCellStyle(); XSSFCellStyle styA = wbA.createCellStyle();
XSSFCellStyle styB = (XSSFCellStyle)wbB.createCellStyle(); XSSFCellStyle styB = wbB.createCellStyle();
styA.verifyBelongsToStylesSource(wbA.getStylesSource()); styA.verifyBelongsToStylesSource(wbA.getStylesSource());
styB.verifyBelongsToStylesSource(wbB.getStylesSource()); styB.verifyBelongsToStylesSource(wbB.getStylesSource());