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
This commit is contained in:
Nick Burch 2016-07-20 12:01:36 +00:00
parent dfd3bbee21
commit 4dee08213a
4 changed files with 28 additions and 7 deletions

View File

@ -85,7 +85,7 @@ public class FileBackedDataSource extends DataSource {
@Override @Override
public ByteBuffer read(int length, long position) throws IOException { public ByteBuffer read(int length, long position) throws IOException {
if(position >= size()) { 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? // Do we read or map (for read/write?
@ -103,7 +103,7 @@ public class FileBackedDataSource extends DataSource {
// Check // Check
if(worked == -1) { 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 // Ready it for reading

View File

@ -37,6 +37,8 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import junit.framework.AssertionFailedError;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.ddf.EscherBSERecord; import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.hpsf.ClassID; 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.LittleEndian;
import org.apache.poi.util.RecordFormatException; import org.apache.poi.util.RecordFormatException;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import junit.framework.AssertionFailedError;
/** /**
* Tests for {@link HSSFWorkbook} * Tests for {@link HSSFWorkbook}
*/ */
@ -1280,7 +1279,6 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook {
} }
@Test @Test
@Ignore("Not currently working, bug in POIFS creating empty FS")
public void testWriteToNewFile() throws Exception { public void testWriteToNewFile() throws Exception {
// Open from a Stream // Open from a Stream
HSSFWorkbook wb = new HSSFWorkbook( HSSFWorkbook wb = new HSSFWorkbook(

View File

@ -27,6 +27,7 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Iterator; 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.property.RootProperty;
import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.poifs.storage.HeaderBlock;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.TempFile;
import org.junit.Test; import org.junit.Test;
/** /**
@ -101,6 +103,13 @@ public final class TestNPOIFSFileSystem {
original.close(); original.close();
return new NPOIFSFileSystem(new ByteArrayInputStream(baos.toByteArray())); 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 @Test
public void basicOpen() throws Exception { public void basicOpen() throws Exception {
@ -959,6 +968,20 @@ public final class TestNPOIFSFileSystem {
assertEquals(0, fs._get_property_table().getStartBlock()); 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 // Put everything within a new directory
DirectoryEntry testDir = fs.createDirectory("Test Directory"); DirectoryEntry testDir = fs.createDirectory("Test Directory");

View File

@ -177,7 +177,7 @@ public class TestDataSource extends TestCase
if(!writeable) { if(!writeable) {
fail("Shouldn't be able to read off the end of the file"); fail("Shouldn't be able to read off the end of the file");
} }
} catch (IllegalArgumentException e) { } catch (IndexOutOfBoundsException e) {
// expected here // expected here
} }
} }