aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2016-07-20 12:01:36 +0000
committerNick Burch <nick@apache.org>2016-07-20 12:01:36 +0000
commitfcf3162b54973769b286f22bdfd4bd8bfe4b90ed (patch)
tree31834ca42b0deee09763660ddf7d389a21e9cdaf
parent551ca753ca0b99ab9807d85adf2b0252e03c1db4 (diff)
downloadpoi-fcf3162b54973769b286f22bdfd4bd8bfe4b90ed.tar.gz
poi-fcf3162b54973769b286f22bdfd4bd8bfe4b90ed.zip
Sync the file-based "past-the-end" exception to match the stream one, so that extending works, then tests for HSSF writing to a new empty POIFS file #57919
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753489 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java4
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java6
-rw-r--r--src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java23
-rw-r--r--src/testcases/org/apache/poi/poifs/nio/TestDataSource.java2
4 files changed, 28 insertions, 7 deletions
diff --git a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
index 6a661af625..1b86606448 100644
--- a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
+++ b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
@@ -85,7 +85,7 @@ public class FileBackedDataSource extends DataSource {
@Override
public ByteBuffer read(int length, long position) throws IOException {
if(position >= size()) {
- throw new IllegalArgumentException("Position " + position + " past the end of the file");
+ throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
}
// Do we read or map (for read/write?
@@ -103,7 +103,7 @@ public class FileBackedDataSource extends DataSource {
// Check
if(worked == -1) {
- throw new IllegalArgumentException("Position " + position + " past the end of the file");
+ throw new IndexOutOfBoundsException("Position " + position + " past the end of the file");
}
// Ready it for reading
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
index 1b0d1f7345..6eb4700161 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
@@ -37,6 +37,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import junit.framework.AssertionFailedError;
+
import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hpsf.ClassID;
@@ -71,11 +73,8 @@ 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;
-
/**
* Tests for {@link HSSFWorkbook}
*/
@@ -1280,7 +1279,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
}
@Test
- @Ignore("Not currently working, bug in POIFS creating empty FS")
public void testWriteToNewFile() throws Exception {
// Open from a Stream
HSSFWorkbook wb = new HSSFWorkbook(
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
index ed598ed6cc..1833b184f3 100644
--- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
+++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
@@ -27,6 +27,7 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
@@ -42,6 +43,7 @@ import org.apache.poi.poifs.property.Property;
import org.apache.poi.poifs.property.RootProperty;
import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.TempFile;
import org.junit.Test;
/**
@@ -101,6 +103,13 @@ public final class TestNPOIFSFileSystem {
original.close();
return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray()));
}
+ protected static NPOIFSFileSystem writeOutFileAndReadBack(NPOIFSFileSystem original) throws IOException {
+ final File file = TempFile.createTempFile("TestPOIFS", ".ole2");
+ final FileOutputStream fout = new FileOutputStream(file);
+ original.writeFilesystem(fout);
+ original.close();
+ return new NPOIFSFileSystem(file, false);
+ }
@Test
public void basicOpen() throws Exception {
@@ -959,6 +968,20 @@ public final class TestNPOIFSFileSystem {
assertEquals(0, fs._get_property_table().getStartBlock());
+ // Check the same but with saving to a file
+ fs = new NPOIFSFileSystem();
+ fs = writeOutFileAndReadBack(fs);
+
+ // Same, no change, SBAT remains empty
+ assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(0));
+ assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(1));
+ assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(2));
+ assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(3));
+ assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock());
+ assertEquals(0, fs._get_property_table().getStartBlock());
+
+
+
// Put everything within a new directory
DirectoryEntry testDir = fs.createDirectory("Test Directory");
diff --git a/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java b/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java
index 198ed7e752..1bc3f4532e 100644
--- a/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java
+++ b/src/testcases/org/apache/poi/poifs/nio/TestDataSource.java
@@ -177,7 +177,7 @@ public class TestDataSource extends TestCase
if(!writeable) {
fail("Shouldn't be able to read off the end of the file");
}
- } catch (IllegalArgumentException e) {
+ } catch (IndexOutOfBoundsException e) {
// expected here
}
}