Reenable test on Windows which was disabled previously but should now work after a workaround was put in place via bug 58480.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1708613 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-10-14 15:00:19 +00:00
parent a00240f212
commit 392591e222

View File

@ -29,7 +29,6 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileFilter;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -76,7 +75,6 @@ import org.apache.poi.util.CodePageUtil;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
import org.junit.Assume;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -827,190 +825,179 @@ public class TestWrite
*/ */
@Test @Test
public void inPlaceNPOIFSWrite() throws Exception { public void inPlaceNPOIFSWrite() throws Exception {
try { NPOIFSFileSystem fs = null;
NPOIFSFileSystem fs = null; DirectoryEntry root = null;
DirectoryEntry root = null; DocumentNode sinfDoc = null;
DocumentNode sinfDoc = null; DocumentNode dinfDoc = null;
DocumentNode dinfDoc = null; SummaryInformation sinf = null;
SummaryInformation sinf = null; DocumentSummaryInformation dinf = null;
DocumentSummaryInformation dinf = null;
// We need to work on a File for in-place changes, so create a temp one // We need to work on a File for in-place changes, so create a temp one
final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit(); copy.deleteOnExit();
// Copy a test file over to our temp location // Copy a test file over to our temp location
InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc"); InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc");
FileOutputStream out = new FileOutputStream(copy); FileOutputStream out = new FileOutputStream(copy);
IOUtils.copy(inp, out); IOUtils.copy(inp, out);
inp.close(); inp.close();
out.close(); out.close();
// Open the copy in read/write mode // Open the copy in read/write mode
fs = new NPOIFSFileSystem(copy, false); fs = new NPOIFSFileSystem(copy, false);
root = fs.getRoot(); root = fs.getRoot();
// Read the properties in there // Read the properties in there
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
// Check they start as we expect // Check they start as we expect
assertEquals("Reiichiro Hori", sinf.getAuthor()); assertEquals("Reiichiro Hori", sinf.getAuthor());
assertEquals("Microsoft Word 9.0", sinf.getApplicationName()); assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
assertEquals("\u7b2c1\u7ae0", sinf.getTitle()); assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
assertEquals("", dinf.getCompany()); assertEquals("", dinf.getCompany());
assertEquals(null, dinf.getManager()); assertEquals(null, dinf.getManager());
// Do an in-place replace via an InputStream // Do an in-place replace via an InputStream
new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream()); new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream()); new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
// Check it didn't get changed // Check it didn't get changed
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
// Start again! // Start again!
fs.close(); fs.close();
inp = _samples.openResourceAsStream("TestShiftJIS.doc"); inp = _samples.openResourceAsStream("TestShiftJIS.doc");
out = new FileOutputStream(copy); out = new FileOutputStream(copy);
IOUtils.copy(inp, out); IOUtils.copy(inp, out);
inp.close(); inp.close();
out.close(); out.close();
fs = new NPOIFSFileSystem(copy, false); fs = new NPOIFSFileSystem(copy, false);
root = fs.getRoot(); root = fs.getRoot();
// Read the properties in once more // Read the properties in once more
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
// Have them write themselves in-place with no changes, as an OutputStream // Have them write themselves in-place with no changes, as an OutputStream
sinf.write(new NDocumentOutputStream(sinfDoc)); sinf.write(new NDocumentOutputStream(sinfDoc));
dinf.write(new NDocumentOutputStream(dinfDoc)); dinf.write(new NDocumentOutputStream(dinfDoc));
// And also write to some bytes for checking // And also write to some bytes for checking
ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream(); ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
sinf.write(sinfBytes); sinf.write(sinfBytes);
ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream(); ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
dinf.write(dinfBytes); dinf.write(dinfBytes);
// Check that the filesystem can give us back the same bytes // Check that the filesystem can give us back the same bytes
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
byte[] sinfData = IOUtils.toByteArray(new NDocumentInputStream(sinfDoc)); byte[] sinfData = IOUtils.toByteArray(new NDocumentInputStream(sinfDoc));
byte[] dinfData = IOUtils.toByteArray(new NDocumentInputStream(dinfDoc)); byte[] dinfData = IOUtils.toByteArray(new NDocumentInputStream(dinfDoc));
assertThat(sinfBytes.toByteArray(), equalTo(sinfData)); assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
assertThat(dinfBytes.toByteArray(), equalTo(dinfData)); assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
// Read back in as-is // Read back in as-is
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
assertEquals("Reiichiro Hori", sinf.getAuthor()); assertEquals("Reiichiro Hori", sinf.getAuthor());
assertEquals("Microsoft Word 9.0", sinf.getApplicationName()); assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
assertEquals("\u7b2c1\u7ae0", sinf.getTitle()); assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
assertEquals("", dinf.getCompany()); assertEquals("", dinf.getCompany());
assertEquals(null, dinf.getManager()); assertEquals(null, dinf.getManager());
// Now alter a few of them // Now alter a few of them
sinf.setAuthor("Changed Author"); sinf.setAuthor("Changed Author");
sinf.setTitle("Le titre \u00e9tait chang\u00e9"); sinf.setTitle("Le titre \u00e9tait chang\u00e9");
dinf.setManager("Changed Manager"); dinf.setManager("Changed Manager");
// Save this into the filesystem // Save this into the filesystem
sinf.write(new NDocumentOutputStream(sinfDoc)); sinf.write(new NDocumentOutputStream(sinfDoc));
dinf.write(new NDocumentOutputStream(dinfDoc)); dinf.write(new NDocumentOutputStream(dinfDoc));
// Read them back in again // Read them back in again
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
assertEquals("Changed Author", sinf.getAuthor()); assertEquals("Changed Author", sinf.getAuthor());
assertEquals("Microsoft Word 9.0", sinf.getApplicationName()); assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle()); assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
assertEquals("", dinf.getCompany()); assertEquals("", dinf.getCompany());
assertEquals("Changed Manager", dinf.getManager()); assertEquals("Changed Manager", dinf.getManager());
// Close the whole filesystem, and open it once more // Close the whole filesystem, and open it once more
fs.writeFilesystem(); fs.writeFilesystem();
fs.close(); fs.close();
fs = new NPOIFSFileSystem(copy); fs = new NPOIFSFileSystem(copy);
root = fs.getRoot(); root = fs.getRoot();
// Re-check on load // Re-check on load
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion()); assertEquals(131077, sinf.getOSVersion());
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc)); dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion()); assertEquals(131077, dinf.getOSVersion());
assertEquals("Changed Author", sinf.getAuthor()); assertEquals("Changed Author", sinf.getAuthor());
assertEquals("Microsoft Word 9.0", sinf.getApplicationName()); assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle()); assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
assertEquals("", dinf.getCompany()); assertEquals("", dinf.getCompany());
assertEquals("Changed Manager", dinf.getManager()); assertEquals("Changed Manager", dinf.getManager());
// Tidy up // Tidy up
fs.close(); fs.close();
copy.delete(); copy.delete();
} catch (FileNotFoundException e) {
// On Windows this might always fail, as the nio classes
// leave memory mapped buffers active, even when the corresponding channel is closed
// The buffers are closed on garbage-collection (but System.gc() can't be forced)
// or via sun.misc.Cleaner, but this is regarded unsafe
// http://stackoverflow.com/questions/2972986
// http://bugs.java.com/view_bug.do?bug_id=4724038
Assume.assumeFalse(System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win"));
throw e;
}
} }