aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2011-07-15 15:19:17 +0000
committerNick Burch <nick@apache.org>2011-07-15 15:19:17 +0000
commitfbc18644054fbfe109f4886907ebbc49bad17f00 (patch)
tree4e4e8ae5a4cc8caefef222e5801955102b01e053 /src/testcases/org/apache/poi/hssf
parent432167c031a0bcf013ddcb0c969622cbcfe1a17c (diff)
downloadpoi-fbc18644054fbfe109f4886907ebbc49bad17f00.tar.gz
poi-fbc18644054fbfe109f4886907ebbc49bad17f00.zip
Add tests for bug #51514 - NPOIFS based loading
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1147188 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi/hssf')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index 387e9a93df..a780ae6085 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -33,6 +33,7 @@ import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.TempFile;
import org.apache.poi.ss.usermodel.BaseTestWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@@ -119,7 +120,7 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
b.cloneSheet(0);
assertEquals(2, b.getNumberOfSheets());
}
-
+
public void testReadWriteWithCharts() {
HSSFWorkbook b;
HSSFSheet s;
@@ -556,6 +557,54 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals("replaceMe", cell .getRichStringCellValue().getString());
}
}
+
+ public void testWordDocEmbeddedInXls() throws IOException {
+ // Open the two filesystems
+ DirectoryNode[] files = new DirectoryNode[2];
+ files[0] = (new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))).getRoot();
+ files[1] = (new NPOIFSFileSystem(HSSFTestDataSamples.getSampeFile("WithEmbeddedObjects.xls"))).getRoot();
+
+ // Check the embedded parts
+ for(DirectoryNode root : files) {
+ HSSFWorkbook hw = new HSSFWorkbook(root, true);
+ List<HSSFObjectData> objects = hw.getAllEmbeddedObjects();
+ boolean found = false;
+ for (int i = 0; i < objects.size(); i++) {
+ HSSFObjectData embeddedObject = objects.get(i);
+ if (embeddedObject.hasDirectoryEntry()) {
+ DirectoryEntry dir = embeddedObject.getDirectory();
+ if (dir instanceof DirectoryNode) {
+ DirectoryNode dNode = (DirectoryNode)dir;
+ if (hasEntry(dNode,"WordDocument")) {
+ found = true;
+ }
+ }
+ }
+ }
+ assertTrue(found);
+ }
+ }
+
+ /**
+ * Checks that we can open a workbook with NPOIFS, and write it out
+ * again (via POIFS) and have it be valid
+ * @throws IOException
+ */
+ public void testWriteWorkbookFromNPOIFS() throws IOException {
+ InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls");
+ NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
+
+ // Start as NPOIFS
+ HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
+ assertEquals(3, wb.getNumberOfSheets());
+ assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+
+ // Will switch to POIFS
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ assertEquals(3, wb.getNumberOfSheets());
+ assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
+ }
+
public void testCellStylesLimit() {
HSSFWorkbook wb = new HSSFWorkbook();
int numBuiltInStyles = wb.getNumCellStyles();
@@ -631,4 +680,13 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
assertEquals("'first sheet'!D1", cf.getRule(0).getFormula1());
assertEquals("'other sheet'!D1", cf.getRule(0).getFormula2());
}
+
+ private boolean hasEntry(DirectoryNode dirNode, String entryName) {
+ try {
+ dirNode.getEntry(entryName);
+ return true;
+ } catch (FileNotFoundException e) {
+ return false;
+ }
+ }
}