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.

TestBasic.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.hpsf.basic;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertSame;
  19. import static org.junit.jupiter.api.Assertions.assertTrue;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.File;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.io.UnsupportedEncodingException;
  26. import java.util.Date;
  27. import java.util.List;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.poi.hpsf.ClassID;
  30. import org.apache.poi.hpsf.DocumentSummaryInformation;
  31. import org.apache.poi.hpsf.Filetime;
  32. import org.apache.poi.hpsf.HPSFException;
  33. import org.apache.poi.hpsf.NoPropertySetStreamException;
  34. import org.apache.poi.hpsf.PropertySet;
  35. import org.apache.poi.hpsf.PropertySetFactory;
  36. import org.apache.poi.hpsf.Section;
  37. import org.apache.poi.hpsf.SummaryInformation;
  38. import org.apache.poi.hpsf.wellknown.PropertyIDMap;
  39. import org.junit.jupiter.api.BeforeEach;
  40. import org.junit.jupiter.api.Test;
  41. /**
  42. * <p>Tests the basic HPSF functionality.</p>
  43. */
  44. final class TestBasic {
  45. private static final POIDataSamples samples = POIDataSamples.getHPSFInstance();
  46. private static final String[] POI_FILES = {
  47. SummaryInformation.DEFAULT_STREAM_NAME,
  48. DocumentSummaryInformation.DEFAULT_STREAM_NAME,
  49. "WordDocument",
  50. "\001CompObj",
  51. "1Table"
  52. };
  53. private static final int BYTE_ORDER = 0xfffe;
  54. private static final int FORMAT = 0x0000;
  55. private static final int OS_VERSION = 0x00020A04;
  56. private static final ClassID CLASS_ID = new ClassID("{00000000-0000-0000-0000-000000000000}");
  57. private static final int[] SECTION_COUNT = {1, 2};
  58. private static final boolean[] IS_SUMMARY_INFORMATION = {true, false};
  59. private static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION = {false, true};
  60. private List<POIFile> poiFiles;
  61. /**
  62. * <p>Read a the test file from the "data" directory.</p>
  63. *
  64. * @throws FileNotFoundException if the file to be read does not exist.
  65. * @throws IOException if any other I/O exception occurs.
  66. */
  67. @BeforeEach
  68. void setUp() throws IOException {
  69. final File data = samples.getFile("TestGermanWord90.doc");
  70. poiFiles = Util.readPOIFiles(data);
  71. }
  72. /**
  73. * <p>Checks the names of the files in the POI filesystem. They
  74. * are expected to be in a certain order.</p>
  75. */
  76. @Test
  77. void testReadFiles() {
  78. String[] expected = POI_FILES;
  79. for (int i = 0; i < expected.length; i++) {
  80. assertEquals(poiFiles.get(i).getName(), expected[i]);
  81. }
  82. }
  83. /**
  84. * <p>Tests whether property sets can be created from the POI
  85. * files in the POI file system. This test case expects the first
  86. * file to be a {@link SummaryInformation}, the second file to be
  87. * a {@link DocumentSummaryInformation} and the rest to be no
  88. * property sets. In the latter cases a {@link
  89. * NoPropertySetStreamException} will be thrown when trying to
  90. * create a {@link PropertySet}.</p>
  91. *
  92. * @throws IOException if an I/O exception occurs.
  93. *
  94. * @throws UnsupportedEncodingException if a character encoding is not
  95. * supported.
  96. */
  97. @Test
  98. void testCreatePropertySets() throws IOException {
  99. Class<?>[] expected = {
  100. SummaryInformation.class,
  101. DocumentSummaryInformation.class,
  102. NoPropertySetStreamException.class,
  103. NoPropertySetStreamException.class,
  104. NoPropertySetStreamException.class
  105. };
  106. for (int i = 0; i < expected.length; i++) {
  107. InputStream in = new ByteArrayInputStream(poiFiles.get(i).getBytes());
  108. Object o;
  109. try {
  110. o = PropertySetFactory.create(in);
  111. } catch (NoPropertySetStreamException ex) {
  112. o = ex;
  113. }
  114. in.close();
  115. assertSame(expected[i], o.getClass());
  116. }
  117. }
  118. /**
  119. * <p>Tests the {@link PropertySet} methods. The test file has two
  120. * property sets: the first one is a {@link SummaryInformation},
  121. * the second one is a {@link DocumentSummaryInformation}.</p>
  122. *
  123. * @throws IOException if an I/O exception occurs
  124. * @throws HPSFException if any HPSF exception occurs
  125. */
  126. @Test
  127. void testPropertySetMethods() throws IOException, HPSFException {
  128. /* Loop over the two property sets. */
  129. for (int i = 0; i < 2; i++) {
  130. byte[] b = poiFiles.get(i).getBytes();
  131. PropertySet ps = PropertySetFactory.create(new ByteArrayInputStream(b));
  132. assertEquals(BYTE_ORDER, ps.getByteOrder());
  133. assertEquals(FORMAT, ps.getFormat());
  134. assertEquals(OS_VERSION, ps.getOSVersion());
  135. assertEquals(CLASS_ID, ps.getClassID());
  136. assertEquals(SECTION_COUNT[i], ps.getSectionCount());
  137. assertEquals(IS_SUMMARY_INFORMATION[i], ps.isSummaryInformation());
  138. assertEquals(IS_DOCUMENT_SUMMARY_INFORMATION[i], ps.isDocumentSummaryInformation());
  139. }
  140. }
  141. /**
  142. * <p>Tests the {@link Section} methods. The test file has two
  143. * property sets: the first one is a {@link SummaryInformation},
  144. * the second one is a {@link DocumentSummaryInformation}.</p>
  145. *
  146. * @throws IOException if an I/O exception occurs
  147. * @throws HPSFException if any HPSF exception occurs
  148. */
  149. @Test
  150. void testSectionMethods() throws IOException, HPSFException {
  151. InputStream is = new ByteArrayInputStream(poiFiles.get(0).getBytes());
  152. final SummaryInformation si = (SummaryInformation)PropertySetFactory.create(is);
  153. final List<Section> sections = si.getSections();
  154. final Section s = sections.get(0);
  155. assertEquals(SummaryInformation.FORMAT_ID, s.getFormatID());
  156. assertNotNull(s.getProperties());
  157. assertEquals(17, s.getPropertyCount());
  158. assertEquals("Titel", s.getProperty(PropertyIDMap.PID_TITLE));
  159. assertEquals(1764, s.getSize());
  160. }
  161. @Test
  162. void bug52117LastPrinted() throws IOException, HPSFException {
  163. File f = samples.getFile("TestBug52117.doc");
  164. POIFile poiFile = Util.readPOIFiles(f, new String[]{POI_FILES[0]}).get(0);
  165. InputStream in = new ByteArrayInputStream(poiFile.getBytes());
  166. SummaryInformation si = (SummaryInformation)PropertySetFactory.create(in);
  167. Date lastPrinted = si.getLastPrinted();
  168. long editTime = si.getEditTime();
  169. assertTrue(Filetime.isUndefined(lastPrinted));
  170. assertEquals(1800000000L, editTime);
  171. }
  172. @Test
  173. void bug61809() throws IOException, HPSFException {
  174. InputStream is_si = new ByteArrayInputStream(poiFiles.get(0).getBytes());
  175. final SummaryInformation si = (SummaryInformation)PropertySetFactory.create(is_si);
  176. final Section s_si = si.getSections().get(0);
  177. assertEquals("PID_TITLE", s_si.getPIDString(PropertyIDMap.PID_TITLE));
  178. assertEquals(PropertyIDMap.UNDEFINED, s_si.getPIDString(4711));
  179. InputStream is_dsi = new ByteArrayInputStream(poiFiles.get(1).getBytes());
  180. final DocumentSummaryInformation dsi = (DocumentSummaryInformation)PropertySetFactory.create(is_dsi);
  181. final Section s_dsi = dsi.getSections().get(0);
  182. assertEquals("PID_MANAGER", s_dsi.getPIDString(PropertyIDMap.PID_MANAGER));
  183. }
  184. }