You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestPOIFSProperties.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import org.apache.poi.hpsf.*;
  23. import org.apache.poi.hssf.HSSFTestDataSamples;
  24. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  25. import org.apache.poi.util.HexDump;
  26. import org.junit.Test;
  27. /**
  28. * Old-style setting of POIFS properties doesn't work with POI 3.0.2
  29. */
  30. public class TestPOIFSProperties {
  31. private static final String title = "Testing POIFS properties";
  32. @Test
  33. public void testFail() throws Exception {
  34. ByteArrayOutputStream out = new ByteArrayOutputStream();
  35. { // read the workbook, adjust the SummaryInformation and write the data to a byte array
  36. POIFSFileSystem fs = openFileSystem();
  37. HSSFWorkbook wb = new HSSFWorkbook(fs);
  38. //set POIFS properties after constructing HSSFWorkbook
  39. //(a piece of code that used to work up to POI 3.0.2)
  40. setTitle(fs);
  41. //save the workbook and read the property
  42. wb.write(out);
  43. out.close();
  44. wb.close();
  45. }
  46. // process the byte array
  47. checkFromByteArray(out.toByteArray());
  48. }
  49. @Test
  50. public void testOK() throws Exception {
  51. ByteArrayOutputStream out = new ByteArrayOutputStream();
  52. { // read the workbook, adjust the SummaryInformation and write the data to a byte array
  53. POIFSFileSystem fs = openFileSystem();
  54. //set POIFS properties before constructing HSSFWorkbook
  55. setTitle(fs);
  56. HSSFWorkbook wb = new HSSFWorkbook(fs);
  57. wb.write(out);
  58. out.close();
  59. wb.close();
  60. }
  61. // process the byte array
  62. checkFromByteArray(out.toByteArray());
  63. }
  64. private POIFSFileSystem openFileSystem() throws IOException {
  65. InputStream is = HSSFTestDataSamples.openSampleFileStream("Simple.xls");
  66. POIFSFileSystem fs = new POIFSFileSystem(is);
  67. is.close();
  68. return fs;
  69. }
  70. private void setTitle(POIFSFileSystem fs) throws NoPropertySetStreamException, MarkUnsupportedException, IOException, WritingNotSupportedException {
  71. SummaryInformation summary1 = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
  72. assertNotNull(summary1);
  73. summary1.setTitle(title);
  74. //write the modified property back to POIFS
  75. fs.getRoot().getEntry(SummaryInformation.DEFAULT_STREAM_NAME).delete();
  76. fs.createDocument(summary1.toInputStream(), SummaryInformation.DEFAULT_STREAM_NAME);
  77. // check that the information was added successfully to the filesystem object
  78. SummaryInformation summaryCheck = (SummaryInformation) PropertySetFactory.create(fs.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
  79. assertNotNull(summaryCheck);
  80. }
  81. private void checkFromByteArray(byte[] bytes) throws IOException, NoPropertySetStreamException, MarkUnsupportedException {
  82. // on some environments in CI we see strange failures, let's verify that the size is exactly right
  83. // this can be removed again after the problem is identified
  84. assertEquals("Had: " + HexDump.toHex(bytes), 5120, bytes.length);
  85. POIFSFileSystem fs2 = new POIFSFileSystem(new ByteArrayInputStream(bytes));
  86. SummaryInformation summary2 = (SummaryInformation) PropertySetFactory.create(fs2.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
  87. assertNotNull(summary2);
  88. assertEquals(title, summary2.getTitle());
  89. fs2.close();
  90. }
  91. }