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.

TestPOIDocumentMain.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 static org.junit.Assert.assertNull;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.IOException;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  24. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  25. import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. /**
  29. * Tests that POIDocument correctly loads and saves the common
  30. * (hspf) Document Properties.
  31. *
  32. * This is part 1 of 2 of the tests - it only does the POIDocuments
  33. * which are part of the Main (not scratchpad)
  34. */
  35. public final class TestPOIDocumentMain {
  36. // The POI Documents to work on
  37. private POIDocument doc;
  38. private POIDocument doc2;
  39. /**
  40. * Set things up, two spreadsheets for our testing
  41. */
  42. @Before
  43. public void setUp() {
  44. doc = HSSFTestDataSamples.openSampleWorkbook("DateFormats.xls");
  45. doc2 = HSSFTestDataSamples.openSampleWorkbook("StringFormulas.xls");
  46. }
  47. @Test
  48. public void readProperties() {
  49. // We should have both sets
  50. assertNotNull(doc.getDocumentSummaryInformation());
  51. assertNotNull(doc.getSummaryInformation());
  52. // Check they are as expected for the test doc
  53. assertEquals("Administrator", doc.getSummaryInformation().getAuthor());
  54. assertEquals(0, doc.getDocumentSummaryInformation().getByteCount());
  55. }
  56. @Test
  57. public void readProperties2() {
  58. // Check again on the word one
  59. assertNotNull(doc2.getDocumentSummaryInformation());
  60. assertNotNull(doc2.getSummaryInformation());
  61. assertEquals("Avik Sengupta", doc2.getSummaryInformation().getAuthor());
  62. assertEquals(null, doc2.getSummaryInformation().getKeywords());
  63. assertEquals(0, doc2.getDocumentSummaryInformation().getByteCount());
  64. }
  65. @Test
  66. public void writeProperties() throws Exception {
  67. // Just check we can write them back out into a filesystem
  68. NPOIFSFileSystem outFS = new NPOIFSFileSystem();
  69. doc.readProperties();
  70. doc.writeProperties(outFS);
  71. // Should now hold them
  72. assertNotNull(
  73. outFS.createDocumentInputStream("\005SummaryInformation")
  74. );
  75. assertNotNull(
  76. outFS.createDocumentInputStream("\005DocumentSummaryInformation")
  77. );
  78. }
  79. @Test
  80. public void WriteReadProperties() throws Exception {
  81. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  82. // Write them out
  83. NPOIFSFileSystem outFS = new NPOIFSFileSystem();
  84. doc.readProperties();
  85. doc.writeProperties(outFS);
  86. outFS.writeFilesystem(baos);
  87. // Create a new version
  88. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  89. OPOIFSFileSystem inFS = new OPOIFSFileSystem(bais);
  90. // Check they're still there
  91. doc.directory = inFS.getRoot();
  92. doc.readProperties();
  93. // Delegate test
  94. readProperties();
  95. }
  96. @Test
  97. public void createNewProperties() throws IOException {
  98. POIDocument doc = new HSSFWorkbook();
  99. // New document won't have them
  100. assertNull(doc.getSummaryInformation());
  101. assertNull(doc.getDocumentSummaryInformation());
  102. // Add them in
  103. doc.createInformationProperties();
  104. assertNotNull(doc.getSummaryInformation());
  105. assertNotNull(doc.getDocumentSummaryInformation());
  106. // Write out and back in again, no change
  107. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  108. doc.write(baos);
  109. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  110. ((HSSFWorkbook)doc).close();
  111. doc = new HSSFWorkbook(bais);
  112. assertNotNull(doc.getSummaryInformation());
  113. assertNotNull(doc.getDocumentSummaryInformation());
  114. ((HSSFWorkbook)doc).close();
  115. }
  116. @Test
  117. public void createNewPropertiesOnExistingFile() throws IOException {
  118. POIDocument doc = new HSSFWorkbook();
  119. // New document won't have them
  120. assertNull(doc.getSummaryInformation());
  121. assertNull(doc.getDocumentSummaryInformation());
  122. // Write out and back in again, no change
  123. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  124. doc.write(baos);
  125. ((HSSFWorkbook)doc).close();
  126. ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  127. doc = new HSSFWorkbook(bais);
  128. assertNull(doc.getSummaryInformation());
  129. assertNull(doc.getDocumentSummaryInformation());
  130. // Create, and change
  131. doc.createInformationProperties();
  132. doc.getSummaryInformation().setAuthor("POI Testing");
  133. doc.getDocumentSummaryInformation().setCompany("ASF");
  134. // Save and re-load
  135. baos = new ByteArrayOutputStream();
  136. doc.write(baos);
  137. ((HSSFWorkbook)doc).close();
  138. bais = new ByteArrayInputStream(baos.toByteArray());
  139. doc = new HSSFWorkbook(bais);
  140. // Check
  141. assertNotNull(doc.getSummaryInformation());
  142. assertNotNull(doc.getDocumentSummaryInformation());
  143. assertEquals("POI Testing", doc.getSummaryInformation().getAuthor());
  144. assertEquals("ASF", doc.getDocumentSummaryInformation().getCompany());
  145. // Asking to re-create will make no difference now
  146. doc.createInformationProperties();
  147. assertNotNull(doc.getSummaryInformation());
  148. assertNotNull(doc.getDocumentSummaryInformation());
  149. assertEquals("POI Testing", doc.getSummaryInformation().getAuthor());
  150. assertEquals("ASF", doc.getDocumentSummaryInformation().getCompany());
  151. ((HSSFWorkbook)doc).close();
  152. }
  153. }