When writing out a workbook, skip a WORKBOOK stream (if there is one), since we always write out as Workbook + test (bug #43055)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@569082 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6e2b416925
commit
490bdc3fe6
@ -908,7 +908,14 @@ public class HSSFWorkbook
|
|||||||
|
|
||||||
if (preserveNodes) {
|
if (preserveNodes) {
|
||||||
List excepts = new ArrayList(1);
|
List excepts = new ArrayList(1);
|
||||||
|
|
||||||
|
// Don't write out the old Workbook, we'll be doing our new one
|
||||||
excepts.add("Workbook");
|
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);
|
copyNodes(this.poifs,fs,excepts);
|
||||||
}
|
}
|
||||||
fs.writeFilesystem(stream);
|
fs.writeFilesystem(stream);
|
||||||
|
@ -16,7 +16,10 @@
|
|||||||
*/
|
*/
|
||||||
package org.apache.poi.hssf.usermodel;
|
package org.apache.poi.hssf.usermodel;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||||
|
|
||||||
@ -47,9 +50,76 @@ public class TestUppercaseWorkbook extends TestCase {
|
|||||||
|
|
||||||
// Ensure that we have a WORKBOOK entry
|
// Ensure that we have a WORKBOOK entry
|
||||||
fs.getRoot().getEntry("WORKBOOK");
|
fs.getRoot().getEntry("WORKBOOK");
|
||||||
|
// And a summary
|
||||||
|
fs.getRoot().getEntry("\005SummaryInformation");
|
||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
|
|
||||||
|
// But not a Workbook one
|
||||||
|
try {
|
||||||
|
fs.getRoot().getEntry("Workbook");
|
||||||
|
fail();
|
||||||
|
} catch(FileNotFoundException e) {}
|
||||||
|
|
||||||
// Try to open the workbook
|
// Try to open the workbook
|
||||||
HSSFWorkbook wb = new HSSFWorkbook(fs);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user