diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index a50d890bb..5273917d1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -908,7 +908,14 @@ public class HSSFWorkbook if (preserveNodes) { List excepts = new ArrayList(1); + + // Don't write out the old Workbook, we'll be doing our new one excepts.add("Workbook"); + // If the file had WORKBOOK instead of Workbook, we'll write it + // out correctly shortly, so don't include the old one + excepts.add("WORKBOOK"); + + // Copy over all the other nodes to our new poifs copyNodes(this.poifs,fs,excepts); } fs.writeFilesystem(stream); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java index 0ed967db7..951e64401 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java @@ -16,7 +16,10 @@ */ package org.apache.poi.hssf.usermodel; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.FileInputStream; +import java.io.FileNotFoundException; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -47,9 +50,76 @@ public class TestUppercaseWorkbook extends TestCase { // Ensure that we have a WORKBOOK entry fs.getRoot().getEntry("WORKBOOK"); + // And a summary + fs.getRoot().getEntry("\005SummaryInformation"); assertTrue(true); + + // But not a Workbook one + try { + fs.getRoot().getEntry("Workbook"); + fail(); + } catch(FileNotFoundException e) {} // Try to open the workbook HSSFWorkbook wb = new HSSFWorkbook(fs); } + + /** + * Test that when we write out, we go back to the correct case + */ + public void testWrite() throws Exception { + FileInputStream is = new FileInputStream(dirPath + "/" + xlsA); + POIFSFileSystem fs = new POIFSFileSystem(is); + + // Open the workbook, not preserving nodes + HSSFWorkbook wb = new HSSFWorkbook(fs); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wb.write(out); + + // Check now + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + POIFSFileSystem fs2 = new POIFSFileSystem(in); + + // Check that we have the new entries + fs2.getRoot().getEntry("Workbook"); + try { + fs2.getRoot().getEntry("WORKBOOK"); + fail(); + } catch(FileNotFoundException e) {} + + // And it can be opened + HSSFWorkbook wb2 = new HSSFWorkbook(fs2); + } + + /** + * Test that when we write out preserving nodes, we go back to the + * correct case + */ + public void testWritePreserve() throws Exception { + FileInputStream is = new FileInputStream(dirPath + "/" + xlsA); + POIFSFileSystem fs = new POIFSFileSystem(is); + + // Open the workbook, not preserving nodes + HSSFWorkbook wb = new HSSFWorkbook(fs,true); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wb.write(out); + + // Check now + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + POIFSFileSystem fs2 = new POIFSFileSystem(in); + + // Check that we have the new entries + fs2.getRoot().getEntry("Workbook"); + try { + fs2.getRoot().getEntry("WORKBOOK"); + fail(); + } catch(FileNotFoundException e) {} + + // As we preserved, should also have a few other streams + fs2.getRoot().getEntry("\005SummaryInformation"); + + // And it can be opened + HSSFWorkbook wb2 = new HSSFWorkbook(fs2); + } }