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
This commit is contained in:
Nick Burch 2013-06-26 00:18:41 +00:00
parent 90b74675d5
commit 82df889469
2 changed files with 34 additions and 3 deletions

View File

@ -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<String> excepts = new ArrayList<String>(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);
}
}

View File

@ -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());
}
}