Convert the HPSF test to JUnit 4, and add a bit more testing for NPOIFS

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590309 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-04-26 21:51:28 +00:00
parent 7f5f7d0440
commit 0496d8f330
2 changed files with 87 additions and 37 deletions

View File

@ -143,6 +143,8 @@ public final class NPOIFSDocument implements POIFSViewable {
os.write(buf, 0, readBytes); os.write(buf, 0, readBytes);
} }
os.close();
return length; return length;
} }

View File

@ -17,6 +17,11 @@
package org.apache.poi.hpsf.basic; package org.apache.poi.hpsf.basic;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -32,8 +37,6 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hpsf.ClassID; import org.apache.poi.hpsf.ClassID;
import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.DocumentSummaryInformation;
@ -68,14 +71,14 @@ 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.Assert; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
/** /**
* <p>Tests HPSF's writing functionality.</p> * <p>Tests HPSF's writing functionality.</p>
*
* @author Rainer Klute (klute@rainer-klute.de)
*/ */
public class TestWrite extends TestCase public class TestWrite
{ {
private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance(); private static final POIDataSamples _samples = POIDataSamples.getHPSFInstance();
@ -98,24 +101,20 @@ public class TestWrite extends TestCase
POIFile[] poiFiles; POIFile[] poiFiles;
@Before
/**
* @see TestCase#setUp()
*/
public void setUp() public void setUp()
{ {
VariantSupport.setLogUnsupportedTypes(false); VariantSupport.setLogUnsupportedTypes(false);
} }
/** /**
* <p>Writes an empty property set to a POIFS and reads it back * <p>Writes an empty property set to a POIFS and reads it back
* in.</p> * in.</p>
* *
* @exception IOException if an I/O exception occurs * @exception IOException if an I/O exception occurs
*/ */
public void testNoFormatID() throws IOException @Test
public void withoutAFormatID() throws IOException
{ {
final File filename = TempFile.createTempFile(POI_FS, ".doc"); final File filename = TempFile.createTempFile(POI_FS, ".doc");
@ -138,11 +137,11 @@ public class TestWrite extends TestCase
SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation.DEFAULT_STREAM_NAME);
poiFs.writeFilesystem(out); poiFs.writeFilesystem(out);
out.close(); out.close();
Assert.fail("Should have thrown a NoFormatIDException."); fail("Should have thrown a NoFormatIDException.");
} }
catch (Exception ex) catch (Exception ex)
{ {
Assert.assertTrue(ex instanceof NoFormatIDException); assertTrue(ex instanceof NoFormatIDException);
} }
finally finally
{ {
@ -160,7 +159,8 @@ public class TestWrite extends TestCase
* @exception UnsupportedVariantTypeException if HPSF does not yet support * @exception UnsupportedVariantTypeException if HPSF does not yet support
* a variant type to be written * a variant type to be written
*/ */
public void testWriteEmptyPropertySet() @Test
public void writeEmptyPropertySet()
throws IOException, UnsupportedVariantTypeException throws IOException, UnsupportedVariantTypeException
{ {
final File dataDir = _samples.getFile(""); final File dataDir = _samples.getFile("");
@ -200,7 +200,8 @@ public class TestWrite extends TestCase
* @exception UnsupportedVariantTypeException if HPSF does not yet support * @exception UnsupportedVariantTypeException if HPSF does not yet support
* a variant type to be written * a variant type to be written
*/ */
public void testWriteSimplePropertySet() @Test
public void writeSimplePropertySet()
throws IOException, UnsupportedVariantTypeException throws IOException, UnsupportedVariantTypeException
{ {
final String AUTHOR = "Rainer Klute"; final String AUTHOR = "Rainer Klute";
@ -249,14 +250,14 @@ public class TestWrite extends TestCase
}, },
SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation.DEFAULT_STREAM_NAME);
r.read(new FileInputStream(filename)); r.read(new FileInputStream(filename));
Assert.assertNotNull(psa[0]); assertNotNull(psa[0]);
Assert.assertTrue(psa[0].isSummaryInformation()); assertTrue(psa[0].isSummaryInformation());
final Section s = (Section) (psa[0].getSections().get(0)); final Section s = (Section) (psa[0].getSections().get(0));
Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR); Object p1 = s.getProperty(PropertyIDMap.PID_AUTHOR);
Object p2 = s.getProperty(PropertyIDMap.PID_TITLE); Object p2 = s.getProperty(PropertyIDMap.PID_TITLE);
Assert.assertEquals(AUTHOR, p1); assertEquals(AUTHOR, p1);
Assert.assertEquals(TITLE, p2); assertEquals(TITLE, p2);
} }
@ -269,7 +270,8 @@ public class TestWrite extends TestCase
* @exception WritingNotSupportedException if HPSF does not yet support * @exception WritingNotSupportedException if HPSF does not yet support
* a variant type to be written * a variant type to be written
*/ */
public void testWriteTwoSections() @Test
public void writeTwoSections()
throws WritingNotSupportedException, IOException throws WritingNotSupportedException, IOException
{ {
final String STREAM_NAME = "PropertySetStream"; final String STREAM_NAME = "PropertySetStream";
@ -326,14 +328,14 @@ public class TestWrite extends TestCase
}, },
STREAM_NAME); STREAM_NAME);
r.read(new FileInputStream(filename)); r.read(new FileInputStream(filename));
Assert.assertNotNull(psa[0]); assertNotNull(psa[0]);
Section s = (Section) (psa[0].getSections().get(0)); Section s = (Section) (psa[0].getSections().get(0));
assertEquals(s.getFormatID(), formatID); assertEquals(s.getFormatID(), formatID);
Object p = s.getProperty(2); Object p = s.getProperty(2);
Assert.assertEquals(SECTION1, p); assertEquals(SECTION1, p);
s = (Section) (psa[0].getSections().get(1)); s = (Section) (psa[0].getSections().get(1));
p = s.getProperty(2); p = s.getProperty(2);
Assert.assertEquals(SECTION2, p); assertEquals(SECTION2, p);
} }
@ -366,7 +368,8 @@ public class TestWrite extends TestCase
* <p>Writes and reads back various variant types and checks whether the * <p>Writes and reads back various variant types and checks whether the
* stuff that has been read back equals the stuff that was written.</p> * stuff that has been read back equals the stuff that was written.</p>
*/ */
public void testVariantTypes() @Test
public void variantTypes()
{ {
Throwable t = null; Throwable t = null;
final int codepage = CODEPAGE_DEFAULT; final int codepage = CODEPAGE_DEFAULT;
@ -451,7 +454,8 @@ public class TestWrite extends TestCase
* checks whether the stuff that has been read back equals the stuff that * checks whether the stuff that has been read back equals the stuff that
* was written.</p> * was written.</p>
*/ */
public void testCodepages() @Test
public void codepages()
{ {
Throwable thr = null; Throwable thr = null;
final int[] validCodepages = new int[] final int[] validCodepages = new int[]
@ -537,7 +541,8 @@ public class TestWrite extends TestCase
* <p>Tests whether writing 8-bit characters to a Unicode property * <p>Tests whether writing 8-bit characters to a Unicode property
* succeeds.</p> * succeeds.</p>
*/ */
public void testUnicodeWrite8Bit() @Test
public void unicodeWrite8Bit()
{ {
final String TITLE = "This is a sample title"; final String TITLE = "This is a sample title";
final MutablePropertySet mps = new MutablePropertySet(); final MutablePropertySet mps = new MutablePropertySet();
@ -673,7 +678,8 @@ public class TestWrite extends TestCase
* </ul> * </ul>
* @throws IOException * @throws IOException
*/ */
public void testRecreate() throws IOException @Test
public void recreate() throws IOException
{ {
final File dataDir = _samples.getFile(""); final File dataDir = _samples.getFile("");
final File[] fileList = dataDir.listFiles(new FileFilter() final File[] fileList = dataDir.listFiles(new FileFilter()
@ -757,7 +763,8 @@ public class TestWrite extends TestCase
* @throws IOException * @throws IOException
* @throws HPSFException * @throws HPSFException
*/ */
public void testDictionary() throws IOException, HPSFException @Test
public void dictionary() throws IOException, HPSFException
{ {
final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit(); copy.deleteOnExit();
@ -782,7 +789,7 @@ public class TestWrite extends TestCase
/* Read back: */ /* Read back: */
final POIFile[] psf = Util.readPropertySets(copy); final POIFile[] psf = Util.readPropertySets(copy);
Assert.assertEquals(1, psf.length); assertEquals(1, psf.length);
final byte[] bytes = psf[0].getBytes(); final byte[] bytes = psf[0].getBytes();
final InputStream in = new ByteArrayInputStream(bytes); final InputStream in = new ByteArrayInputStream(bytes);
final PropertySet ps2 = PropertySetFactory.create(in); final PropertySet ps2 = PropertySetFactory.create(in);
@ -800,7 +807,9 @@ public class TestWrite extends TestCase
* Tests that when using NPOIFS, we can do an in-place write * Tests that when using NPOIFS, we can do an in-place write
* without needing to stream in + out the whole kitchen sink * without needing to stream in + out the whole kitchen sink
*/ */
public void DISABLEDtestInPlaceNPOIFSWrite() throws Exception { @Test
@Ignore
public void inPlaceNPOIFSWrite() throws Exception {
NPOIFSFileSystem fs = null; NPOIFSFileSystem fs = null;
DirectoryEntry root = null; DirectoryEntry root = null;
DocumentNode sinfDoc = null; DocumentNode sinfDoc = null;
@ -808,20 +817,23 @@ public class TestWrite extends TestCase
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
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 a 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); fs = new NPOIFSFileSystem(copy);
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);
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc)); sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
@ -831,6 +843,7 @@ public class TestWrite extends TestCase
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());
@ -839,7 +852,41 @@ public class TestWrite extends TestCase
assertEquals("", dinf.getCompany()); assertEquals("", dinf.getCompany());
assertEquals(null, dinf.getManager()); assertEquals(null, dinf.getManager());
// Alter a few of them
// Have them write themselves in-place with no changes
sinf.write(new NDocumentOutputStream(sinfDoc));
dinf.write(new NDocumentOutputStream(dinfDoc));
// And also write to some bytes for checking
ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
sinf.write(sinfBytes);
ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
dinf.write(dinfBytes);
// Check that the filesystem can give us back the same bytes
sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
// TODO
// Read back in as-is
sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
assertEquals(131077, sinf.getOSVersion());
dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
assertEquals(131077, dinf.getOSVersion());
assertEquals("Reiichiro Hori", sinf.getAuthor());
assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
assertEquals("", dinf.getCompany());
assertEquals(null, dinf.getManager());
// 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");
@ -850,7 +897,7 @@ public class TestWrite extends TestCase
dinf.write(new NDocumentOutputStream(dinfDoc)); dinf.write(new NDocumentOutputStream(dinfDoc));
// Read as-is // 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());
@ -867,7 +914,7 @@ public class TestWrite extends TestCase
assertEquals("Changed Manager", dinf.getManager()); assertEquals("Changed Manager", dinf.getManager());
// Close, re-load // Close the whole filesystem, and open it once more
fs.writeFilesystem(); fs.writeFilesystem();
fs.close(); fs.close();
@ -903,7 +950,8 @@ public class TestWrite extends TestCase
* @throws IOException * @throws IOException
* @throws HPSFException * @throws HPSFException
*/ */
public void testDictionaryWithInvalidCodepage() throws IOException, HPSFException @Test
public void dictionaryWithInvalidCodepage() throws IOException, HPSFException
{ {
try try
{ {