]> source.dussan.org Git - poi.git/commitdiff
More on the HPFS in-place NPOIFS write tests, and a note about what is still to do...
authorNick Burch <nick@apache.org>
Sat, 26 Apr 2014 22:50:00 +0000 (22:50 +0000)
committerNick Burch <nick@apache.org>
Sat, 26 Apr 2014 22:50:00 +0000 (22:50 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590319 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java
src/testcases/org/apache/poi/hpsf/basic/TestWrite.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java

index ae4caf0e61130379c8164bd25fa92b4c0935a130..19649862665ac6e250af13df348cbaa03661014a 100644 (file)
@@ -31,6 +31,10 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * A POIFS {@link DataSource} backed by a File
+ * 
+ * TODO - Return the ByteBuffers in such a way that in RW mode,
+ *  changes to the buffer end up on the disk (will fix the HPSF TestWrite
+ *  currently failing unit test when done)
  */
 public class FileBackedDataSource extends DataSource {
    private FileChannel channel;
index f6eba133039ac2e2c745b2720c0aac593317db81..e5ddc62fd4f57aba65871f66fc6baa0722d90cfa 100644 (file)
 
 package org.apache.poi.hpsf.basic;
 
+import static org.hamcrest.core.IsEqual.equalTo;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -65,6 +67,7 @@ import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.DocumentNode;
 import org.apache.poi.poifs.filesystem.NDocumentInputStream;
 import org.apache.poi.poifs.filesystem.NDocumentOutputStream;
+import org.apache.poi.poifs.filesystem.NPOIFSDocument;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.CodePageUtil;
@@ -830,16 +833,17 @@ public class TestWrite
         
         
         // Open the copy in read/write mode
-        fs = new NPOIFSFileSystem(copy);
+        fs = new NPOIFSFileSystem(copy, false);
         root = fs.getRoot();
         
         
         // Read the properties in there
         sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
+        dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
+
         sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
         assertEquals(131077, sinf.getOSVersion());
         
-        dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
         dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
         assertEquals(131077, dinf.getOSVersion());
         
@@ -853,7 +857,45 @@ public class TestWrite
         assertEquals(null, dinf.getManager());
         
         
-        // Have them write themselves in-place with no changes
+        // Do an in-place replace via an InputStream
+        new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
+        new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
+        
+        
+        // Check it didn't get changed
+        sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
+        dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
+        
+        sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
+        assertEquals(131077, sinf.getOSVersion());
+        
+        dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
+        assertEquals(131077, dinf.getOSVersion());
+
+        
+        // Start again!
+        fs.close();
+        inp = _samples.openResourceAsStream("TestShiftJIS.doc");
+        out = new FileOutputStream(copy);
+        IOUtils.copy(inp, out);
+        inp.close();
+        out.close();
+        
+        fs = new NPOIFSFileSystem(copy, false);
+        root = fs.getRoot();
+        
+        // Read the properties in once more
+        sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
+        dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
+
+        sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
+        assertEquals(131077, sinf.getOSVersion());
+        
+        dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
+        assertEquals(131077, dinf.getOSVersion());
+        
+        
+        // Have them write themselves in-place with no changes, as an OutputStream
         sinf.write(new NDocumentOutputStream(sinfDoc));
         dinf.write(new NDocumentOutputStream(dinfDoc));
         
@@ -868,7 +910,10 @@ public class TestWrite
         sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
         dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
 
-        // TODO
+        byte[] sinfData = IOUtils.toByteArray(new NDocumentInputStream(sinfDoc));
+        byte[] dinfData = IOUtils.toByteArray(new NDocumentInputStream(dinfDoc));
+        assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
+        assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
 
         
         // Read back in as-is
index 90daf160aa4c0d198ec3d2145d940a41645955d3..4aa6abf14c209efe2a4fa7bbc33a3ccd44d021a6 100644 (file)
@@ -28,6 +28,7 @@ import org.apache.poi.poifs.common.POIFSConstants;
 /**
  * Tests for the Mini Store in the NIO POIFS
  */
+@SuppressWarnings("resource")
 public final class TestNPOIFSMiniStore extends TestCase {
    private static final POIDataSamples _inst = POIDataSamples.getPOIFSInstance();