From 551ca753ca0b99ab9807d85adf2b0252e03c1db4 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 20 Jul 2016 11:14:05 +0000 Subject: [PATCH] Start on unit testing for HSSFWorkbook.write(File), bug in POIFS to fix first git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753487 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/POIFSFileSystem.java | 19 ++++++++++++++++++- .../poi/hssf/usermodel/TestHSSFWorkbook.java | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index e78f18f9f1..cc482536b5 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -87,6 +87,23 @@ public class POIFSFileSystem super(stream); } + /** + *

Creates a POIFSFileSystem from a File. This uses less memory than + * creating from an InputStream.

+ * + *

Note that with this constructor, you will need to call {@link #close()} + * when you're done to have the underlying file closed, as the file is + * kept open during normal operation to read the data out.

+ * @param readOnly whether the POIFileSystem will only be used in read-only mode + * + * @param file the File from which to read the data + * + * @exception IOException on errors reading, or on invalid data + */ + public POIFSFileSystem(File file, boolean readOnly) throws IOException { + super(file, readOnly); + } + /** *

Creates a POIFSFileSystem from a File. This uses less memory than * creating from an InputStream. The File will be opened read-only

@@ -141,7 +158,7 @@ public class POIFSFileSystem tmp.close(); // Open it up again backed by the file - return new POIFSFileSystem(file); + return new POIFSFileSystem(file, false); } /** diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index c757ffbf50..1b0d1f7345 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -71,6 +71,7 @@ import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.RecordFormatException; import org.apache.poi.util.TempFile; +import org.junit.Ignore; import org.junit.Test; import junit.framework.AssertionFailedError; @@ -1277,4 +1278,22 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { assertEquals(1, wb.getNumberOfSheets()); assertEquals("Changed!", wb.getSheetAt(0).getRow(0).getCell(0).toString()); } + + @Test + @Ignore("Not currently working, bug in POIFS creating empty FS") + public void testWriteToNewFile() throws Exception { + // Open from a Stream + HSSFWorkbook wb = new HSSFWorkbook( + POIDataSamples.getSpreadSheetInstance().openResourceAsStream("SampleSS.xls")); + + // Save to a new temp file + final File file = TempFile.createTempFile("TestHSSFWorkbook", ".xls"); + wb.write(file); + wb.close(); + + // Read and check + wb = new HSSFWorkbook(new NPOIFSFileSystem(file)); + assertEquals(3, wb.getNumberOfSheets()); + wb.close(); + } } -- 2.39.5