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

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1802817 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-07-24 14:16:34 +00:00
parent 08b03d9547
commit cc0294d58b

View File

@ -18,13 +18,18 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; 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.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.junit.Test; 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 * Old-style setting of POIFS properties doesn't work with POI 3.0.2
*/ */
public class TestPOIFSProperties { public class TestPOIFSProperties {
private static final String title = "Testing POIFS properties"; private static final String title = "Testing POIFS properties";
@Test @Test
public void testFail() throws Exception { public void testFail() throws Exception {
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls"); ByteArrayOutputStream out = new ByteArrayOutputStream();
POIFSFileSystem fs = new POIFSFileSystem(is); { // read the workbook, adjust the SummaryInformation and write the data to a byte array
is.close(); POIFSFileSystem fs = openFileSystem();
HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFWorkbook wb = new HSSFWorkbook(fs);
//set POIFS properties after constructing HSSFWorkbook //set POIFS properties after constructing HSSFWorkbook
//(a piece of code that used to work up to POI 3.0.2) //(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)); setTitle(fs);
//save the workbook and read the property
wb.write(out);
out.close();
wb.close();
}
// process the byte array
checkFromByteArray(out.toByteArray());
}
@Test
public void testOK() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
{ // read the workbook, adjust the SummaryInformation and write the data to a byte array
POIFSFileSystem fs = openFileSystem();
//set POIFS properties before constructing HSSFWorkbook
setTitle(fs);
HSSFWorkbook wb = new HSSFWorkbook(fs);
wb.write(out);
out.close();
wb.close();
}
// process the byte array
checkFromByteArray(out.toByteArray());
}
private POIFSFileSystem openFileSystem() throws IOException {
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
POIFSFileSystem fs = new POIFSFileSystem(is);
is.close();
return fs;
}
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); summary1.setTitle(title);
//write the modified property back to POIFS //write the modified property back to POIFS
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete(); fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME); fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
//save the workbook and read the property // check that the information was added successfully to the filesystem object
ByteArrayOutputStream out = new ByteArrayOutputStream(); SummaryInformation summaryCheck = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
wb.write(out); assertNotNull(summaryCheck);
out.close();
wb.close();
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
SummaryInformation summary2 = (SummaryInformation)PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
//failing assertion
assertEquals(title, summary2.getTitle());
fs2.close();
} }
@Test private void checkFromByteArray(byte[] bytes) throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
public void testOK() throws Exception { POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(bytes));
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls"); SummaryInformation summary2 = (SummaryInformation) PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
POIFSFileSystem fs = new POIFSFileSystem(is); assertNotNull(summary2);
is.close();
//set POIFS properties before constructing HSSFWorkbook
SummaryInformation summary1 = (SummaryInformation)PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
summary1.setTitle(title);
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
HSSFWorkbook wb = new HSSFWorkbook(fs);
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()); assertEquals(title, summary2.getTitle());
fs2.close(); fs2.close();
} }