diff --git a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java index 6a661af62..1b8660644 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 1b0d1f734..6eb470016 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 ed598ed6c..1833b184f 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 198ed7e75..1bc3f4532 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 } }