]> source.dussan.org Git - poi.git/commitdiff
When writing out a workbook, skip a WORKBOOK stream (if there is one), since we alway...
authorNick Burch <nick@apache.org>
Thu, 23 Aug 2007 17:40:24 +0000 (17:40 +0000)
committerNick Burch <nick@apache.org>
Thu, 23 Aug 2007 17:40:24 +0000 (17:40 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@569082 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java

index a50d890bb42a23c209d948c146ee930d60405dea..5273917d1240683d4a34c6f0a900ade420297bba 100644 (file)
@@ -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);
index 0ed967db7b2a47d2cbbec629daf6a2c6962d0f3a..951e6440153e51e640741591bef8a666c540a38a 100644 (file)
 */
 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);
+       }
 }