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.

TestPOIDocumentScratchpad.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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;
  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 org.apache.poi.hpsf.DocumentSummaryInformation;
  22. import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
  23. import org.apache.poi.hpsf.SummaryInformation;
  24. import org.apache.poi.hslf.usermodel.HSLFSlideShowImpl;
  25. import org.apache.poi.hwpf.HWPFTestDataSamples;
  26. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. /**
  30. * Tests that POIDocument correctly loads and saves the common
  31. * (hspf) Document Properties
  32. *
  33. * This is part 2 of 2 of the tests - it only does the POIDocuments
  34. * which are part of the scratchpad (not main)
  35. */
  36. public final class TestPOIDocumentScratchpad {
  37. // The POI Documents to work on
  38. private POIDocument doc;
  39. private POIDocument doc2;
  40. /**
  41. * Set things up, using a PowerPoint document and
  42. * a Word Document for our testing
  43. */
  44. @Before
  45. public void setUp() throws IOException {
  46. doc = new HSLFSlideShowImpl(POIDataSamples.getSlideShowInstance().openResourceAsStream("basic_test_ppt_file.ppt"));
  47. doc2 = HWPFTestDataSamples.openSampleFile("test2.doc");
  48. }
  49. @Test
  50. public void testReadProperties() {
  51. testReadPropertiesHelper(doc);
  52. }
  53. private void testReadPropertiesHelper(POIDocument docPH) {
  54. // We should have both sets
  55. assertNotNull(docPH.getDocumentSummaryInformation());
  56. assertNotNull(docPH.getSummaryInformation());
  57. // Check they are as expected for the test doc
  58. assertEquals("Hogwarts", docPH.getSummaryInformation().getAuthor());
  59. assertEquals(10598, docPH.getDocumentSummaryInformation().getByteCount());
  60. }
  61. @Test
  62. public void testReadProperties2() {
  63. // Check again on the word one
  64. assertNotNull(doc2.getDocumentSummaryInformation());
  65. assertNotNull(doc2.getSummaryInformation());
  66. assertEquals("Hogwarts", doc2.getSummaryInformation().getAuthor());
  67. assertEquals("", doc2.getSummaryInformation().getKeywords());
  68. assertEquals(0, doc2.getDocumentSummaryInformation().getByteCount());
  69. }
  70. @Test
  71. public void testWriteProperties() throws IOException {
  72. // Just check we can write them back out into a filesystem
  73. POIFSFileSystem outFS = new POIFSFileSystem();
  74. doc.writeProperties(outFS);
  75. // Should now hold them
  76. assertNotNull(outFS.createDocumentInputStream(SummaryInformation.DEFAULT_STREAM_NAME));
  77. assertNotNull(outFS.createDocumentInputStream(DocumentSummaryInformation.DEFAULT_STREAM_NAME));
  78. outFS.close();
  79. }
  80. @Test
  81. public void testWriteReadProperties() throws IOException {
  82. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  83. // Write them out
  84. POIFSFileSystem outFS = new POIFSFileSystem();
  85. doc.writeProperties(outFS);
  86. outFS.writeFilesystem(baos);
  87. // Create a new version
  88. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  89. POIFSFileSystem inFS = new POIFSFileSystem(bais);
  90. // Check they're still there
  91. POIDocument ppt = new HPSFPropertiesOnlyDocument(inFS);
  92. ppt.readProperties();
  93. // Delegate test
  94. testReadPropertiesHelper(ppt);
  95. ppt.close();
  96. inFS.close();
  97. }
  98. }