Author: centic
Date: Mon Jul 24 14:16:34 2017
New Revision: 1802817
URL: http://svn.apache.org/viewvc?rev=1802817&view=rev
Log:
Refactor test somewhat to make it easier to see what it actually verifies
Check some more to see why it fails sometimes in CI, e.g. on node qnode1
Modified:
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestPOIFSProperties.java
Modified:
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestPOIFSProperties.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestPOIFSProperties.java?rev=1802817&r1=1802816&r2=1802817&view=diff
==============================================================================
---
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestPOIFSProperties.java
(original)
+++
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestPOIFSProperties.java
Mon Jul 24 14:16:34 2017
@@ -18,13 +18,18 @@
package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.InputStream;
+import org.apache.poi.hpsf.MarkUnsupportedException;
+import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
+import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Test;
@@ -33,62 +38,76 @@ import org.junit.Test;
* Old-style setting of POIFS properties doesn't work with POI 3.0.2
*/
public class TestPOIFSProperties {
-
private static final String title = "Testing POIFS properties";
@Test
public void testFail() throws Exception {
- InputStream is =
HSSFTestDataSamples.openSampleFileStream("Simple.xls");
- POIFSFileSystem fs = new POIFSFileSystem(is);
- is.close();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ { // read the workbook, adjust the SummaryInformation and write the
data to a byte array
+ POIFSFileSystem fs = openFileSystem();
- HSSFWorkbook wb = new HSSFWorkbook(fs);
+ HSSFWorkbook wb = new HSSFWorkbook(fs);
- //set POIFS properties after constructing HSSFWorkbook
- //(a piece of code that used to work up to POI 3.0.2)
- SummaryInformation summary1 =
(SummaryInformation)PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
- summary1.setTitle(title);
- //write the modified property back to POIFS
- fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
- fs.createDocument(summary1.toInputStream(),
SummaryInformation.DEFAULT_STREAM_NAME);
+ //set POIFS properties after constructing HSSFWorkbook
+ //(a piece of code that used to work up to POI 3.0.2)
+ setTitle(fs);
+
+ //save the workbook and read the property
+ wb.write(out);
+ out.close();
+ wb.close();
+ }
+
+ // process the byte array
+ checkFromByteArray(out.toByteArray());
+ }
- //save the workbook and read the property
+ @Test
+ public void testOK() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
- wb.write(out);
- out.close();
- wb.close();
+ { // read the workbook, adjust the SummaryInformation and write the
data to a byte array
+ POIFSFileSystem fs = openFileSystem();
- POIFSFileSystem fs2 = new POIFSFileSystem(new
ByteArrayInputStream(out.toByteArray()));
- SummaryInformation summary2 =
(SummaryInformation)PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
+ //set POIFS properties before constructing HSSFWorkbook
+ setTitle(fs);
- //failing assertion
- assertEquals(title, summary2.getTitle());
- fs2.close();
+ HSSFWorkbook wb = new HSSFWorkbook(fs);
+
+ wb.write(out);
+ out.close();
+ wb.close();
+ }
+
+ // process the byte array
+ checkFromByteArray(out.toByteArray());
}
- @Test
- public void testOK() throws Exception {
+ private POIFSFileSystem openFileSystem() throws IOException {
InputStream is =
HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
+ return fs;
+ }
- //set POIFS properties before constructing HSSFWorkbook
- SummaryInformation summary1 =
(SummaryInformation)PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
- summary1.setTitle(title);
+ private void setTitle(POIFSFileSystem fs) throws
NoPropertySetStreamException, MarkUnsupportedException, IOException,
WritingNotSupportedException {
+ SummaryInformation summary1 = (SummaryInformation)
PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
+ assertNotNull(summary1);
+ summary1.setTitle(title);
+ //write the modified property back to POIFS
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
fs.createDocument(summary1.toInputStream(),
SummaryInformation.DEFAULT_STREAM_NAME);
- HSSFWorkbook wb = new HSSFWorkbook(fs);
+ // check that the information was added successfully to the filesystem
object
+ SummaryInformation summaryCheck = (SummaryInformation)
PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
+ assertNotNull(summaryCheck);
+ }
+
+ private void checkFromByteArray(byte[] bytes) throws IOException,
NoPropertySetStreamException, MarkUnsupportedException {
+ POIFSFileSystem fs2 = new POIFSFileSystem(new
ByteArrayInputStream(bytes));
+ SummaryInformation summary2 = (SummaryInformation)
PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
+ assertNotNull(summary2);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- wb.write(out);
- out.close();
- wb.close();
-
- //read the property
- POIFSFileSystem fs2 = new POIFSFileSystem(new
ByteArrayInputStream(out.toByteArray()));
- SummaryInformation summary2 =
(SummaryInformation)PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
assertEquals(title, summary2.getTitle());
fs2.close();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]