2008-03-03 10:10:46 -05:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
2017-01-22 19:31:27 -05:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2017-07-24 10:16:34 -04:00
|
|
|
import static org.junit.Assert.assertNotNull;
|
2017-01-22 19:31:27 -05:00
|
|
|
|
2008-03-03 10:10:46 -05:00
|
|
|
import java.io.ByteArrayInputStream;
|
2008-04-06 23:02:03 -04:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2017-07-24 10:16:34 -04:00
|
|
|
import java.io.IOException;
|
2008-04-06 23:02:03 -04:00
|
|
|
import java.io.InputStream;
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-09-16 04:20:25 -04:00
|
|
|
import org.apache.poi.hpsf.*;
|
2008-04-06 23:02:03 -04:00
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
2017-08-01 02:26:44 -04:00
|
|
|
import org.apache.poi.util.HexDump;
|
2017-01-22 19:31:27 -05:00
|
|
|
import org.junit.Test;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-03-03 10:10:46 -05:00
|
|
|
/**
|
|
|
|
* Old-style setting of POIFS properties doesn't work with POI 3.0.2
|
|
|
|
*/
|
2017-01-22 19:31:27 -05:00
|
|
|
public class TestPOIFSProperties {
|
2008-04-06 23:02:03 -04:00
|
|
|
private static final String title = "Testing POIFS properties";
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-01-22 19:31:27 -05:00
|
|
|
@Test
|
2008-03-03 10:10:46 -05:00
|
|
|
public void testFail() throws Exception {
|
2017-07-24 10:16:34 -04:00
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
{ // read the workbook, adjust the SummaryInformation and write the data to a byte array
|
|
|
|
POIFSFileSystem fs = openFileSystem();
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(fs);
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
//set POIFS properties after constructing HSSFWorkbook
|
|
|
|
//(a piece of code that used to work up to POI 3.0.2)
|
|
|
|
setTitle(fs);
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
//save the workbook and read the property
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
wb.close();
|
|
|
|
}
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
// process the byte array
|
|
|
|
checkFromByteArray(out.toByteArray());
|
2008-03-03 10:10:46 -05:00
|
|
|
}
|
|
|
|
|
2017-01-22 19:31:27 -05:00
|
|
|
@Test
|
2008-03-03 10:10:46 -05:00
|
|
|
public void testOK() throws Exception {
|
2017-07-24 10:16:34 -04:00
|
|
|
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 {
|
2008-04-06 23:02:03 -04:00
|
|
|
InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
|
2008-03-03 10:10:46 -05:00
|
|
|
POIFSFileSystem fs = new POIFSFileSystem(is);
|
2017-01-22 19:31:27 -05:00
|
|
|
is.close();
|
2017-07-24 10:16:34 -04:00
|
|
|
return fs;
|
|
|
|
}
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
private void setTitle(POIFSFileSystem fs) throws NoPropertySetStreamException, MarkUnsupportedException, IOException, WritingNotSupportedException {
|
|
|
|
SummaryInformation summary1 = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
|
|
|
|
assertNotNull(summary1);
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
summary1.setTitle(title);
|
|
|
|
//write the modified property back to POIFS
|
2008-03-03 10:10:46 -05:00
|
|
|
fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
|
|
|
|
fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
|
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
// check that the information was added successfully to the filesystem object
|
|
|
|
SummaryInformation summaryCheck = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
|
|
|
|
assertNotNull(summaryCheck);
|
|
|
|
}
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
private void checkFromByteArray(byte[] bytes) throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
|
2017-08-01 02:26:44 -04:00
|
|
|
// on some environments in CI we see strange failures, let's verify that the size is exactly right
|
|
|
|
// this can be removed again after the problem is identified
|
|
|
|
assertEquals("Had: " + HexDump.toHex(bytes), 5120, bytes.length);
|
|
|
|
|
2017-07-24 10:16:34 -04:00
|
|
|
POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(bytes));
|
|
|
|
SummaryInformation summary2 = (SummaryInformation) PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
|
|
|
|
assertNotNull(summary2);
|
2008-03-03 10:10:46 -05:00
|
|
|
|
|
|
|
assertEquals(title, summary2.getTitle());
|
2017-01-22 19:31:27 -05:00
|
|
|
fs2.close();
|
2008-03-03 10:10:46 -05:00
|
|
|
}
|
|
|
|
}
|