From 85cca5bc2d27c219a69e143f687b2a22620c6ea8 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 26 Jun 2013 00:18:41 +0000 Subject: [PATCH] Add a write method to HPSFPropertiesOnlyDocument, and use this to finish the unit tests for bug #54233 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1496683 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hpsf/HPSFPropertiesOnlyDocument.java | 23 +++++++++++++++++-- .../apache/poi/hpsf/basic/TestHPSFBugs.java | 14 ++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/HPSFPropertiesOnlyDocument.java b/src/java/org/apache/poi/hpsf/HPSFPropertiesOnlyDocument.java index 196cabbbb5..7e341a896d 100644 --- a/src/java/org/apache/poi/hpsf/HPSFPropertiesOnlyDocument.java +++ b/src/java/org/apache/poi/hpsf/HPSFPropertiesOnlyDocument.java @@ -1,8 +1,12 @@ package org.apache.poi.hpsf; +import java.io.IOException; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import org.apache.poi.POIDocument; +import org.apache.poi.poifs.filesystem.EntryUtils; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -20,7 +24,22 @@ public class HPSFPropertiesOnlyDocument extends POIDocument { super(fs); } - public void write(OutputStream out) { - throw new IllegalStateException("Unable to write, only for properties!"); + /** + * Write out, with any properties changes, but nothing else + */ + public void write(OutputStream out) throws IOException { + POIFSFileSystem fs = new POIFSFileSystem(); + + // For tracking what we've written out, so far + List excepts = new ArrayList(1); + + // Write out our HPFS properties, with any changes + writeProperties(fs, excepts); + + // Copy over everything else unchanged + EntryUtils.copyNodes(directory, fs.getRoot(), excepts); + + // Save the resultant POIFSFileSystem to the output stream + fs.writeFilesystem(out); } } \ No newline at end of file diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java b/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java index c27b369c80..05c8d8b99f 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestHPSFBugs.java @@ -24,7 +24,9 @@ import java.util.Date; import junit.framework.TestCase; import org.apache.poi.POIDataSamples; +import org.apache.poi.POIDocument; import org.apache.poi.hpsf.DocumentSummaryInformation; +import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument; import org.apache.poi.hpsf.PropertySetFactory; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -124,6 +126,16 @@ public final class TestHPSFBugs extends TestCase { // Write out and read back, should still be valid - // TODO + POIDocument doc = new HPSFPropertiesOnlyDocument(fs); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + doc.write(baos); + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + doc = new HPSFPropertiesOnlyDocument(new POIFSFileSystem(bais)); + + // Check properties are still there + assertEquals("Microsoft Word 10.0", si.getApplicationName()); + assertEquals("", si.getTitle()); + assertEquals("", si.getAuthor()); + assertEquals("Cour de Justice", dsi.getCompany()); } }