From 7b134ca25a65331e5277fdf3795a49f45b23eae6 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 26 Apr 2014 22:50:00 +0000 Subject: [PATCH] More on the HPFS in-place NPOIFS write tests, and a note about what is still to do for them git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590319 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/nio/FileBackedDataSource.java | 4 ++ .../org/apache/poi/hpsf/basic/TestWrite.java | 53 +++++++++++++++++-- .../poifs/filesystem/TestNPOIFSMiniStore.java | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java index ae4caf0e61..1964986266 100644 --- a/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java +++ b/src/java/org/apache/poi/poifs/nio/FileBackedDataSource.java @@ -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; diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java index f6eba13303..e5ddc62fd4 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestWrite.java @@ -17,8 +17,10 @@ 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 diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java index 90daf160aa..4aa6abf14c 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java @@ -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(); -- 2.39.5