]> source.dussan.org Git - poi.git/commitdiff
Sync the file-based "past-the-end" exception to match the stream one, so that extendi...
authorNick Burch <nick@apache.org>
Wed, 20 Jul 2016 12:01:36 +0000 (12:01 +0000)
committerNick Burch <nick@apache.org>
Wed, 20 Jul 2016 12:01:36 +0000 (12:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1753489 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
src/testcases/org/apache/poi/poifs/nio/TestDataSource.java

index 6a661af62589fdc3fdd4a269ec0e85e9d736d973..1b8660644863b2094f33790a416b819c40c59c5e 100644 (file)
@@ -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
index 1b0d1f73454adad17954ca1f69da446228d1c88c..6eb4700161f494385188eace1f581cc3e5828b2c 100644 (file)
@@ -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(
index ed598ed6cc0c3e840cd04163eb23892d47751ba0..1833b184f35010ae688a4ab0f9cda28ca533c3e4 100644 (file)
@@ -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");
       
index 198ed7e752f86cfcc2db33e6bbc23ddd05dfa0b4..1bc3f4532eb780f3cc91017a8012208f0ef0c1b9 100644 (file)
@@ -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
         }
     }