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.

TestFixedSizedProperties.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.hsmf;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.IOException;
  23. import java.io.PrintStream;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Calendar;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Locale;
  29. import java.util.Map;
  30. import java.util.TimeZone;
  31. import org.apache.poi.POIDataSamples;
  32. import org.apache.poi.hsmf.datatypes.ChunkBasedPropertyValue;
  33. import org.apache.poi.hsmf.datatypes.Chunks;
  34. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  35. import org.apache.poi.hsmf.datatypes.PropertyValue;
  36. import org.apache.poi.hsmf.datatypes.PropertyValue.LongPropertyValue;
  37. import org.apache.poi.hsmf.datatypes.PropertyValue.TimePropertyValue;
  38. import org.apache.poi.hsmf.dev.HSMFDump;
  39. import org.apache.poi.hsmf.extractor.OutlookTextExtractor;
  40. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  41. import org.apache.poi.util.LocaleUtil;
  42. import org.junit.AfterClass;
  43. import org.junit.BeforeClass;
  44. import org.junit.Test;
  45. /**
  46. * Tests that we can read fixed sized properties, as well as variable
  47. * ones, for example Submission Dates
  48. */
  49. public final class TestFixedSizedProperties {
  50. private static final String messageSucceeds = "53784_succeeds.msg";
  51. private static final String messageFails = "53784_fails.msg";
  52. private static MAPIMessage mapiMessageSucceeds;
  53. private static MAPIMessage mapiMessageFails;
  54. private static POIFSFileSystem fsMessageSucceeds;
  55. private static POIFSFileSystem fsMessageFails;
  56. private static SimpleDateFormat messageDateFormat;
  57. private static TimeZone userTimeZone;
  58. /**
  59. * Initialize this test, load up the messages.
  60. */
  61. @BeforeClass
  62. public static void initMapi() throws Exception {
  63. POIDataSamples samples = POIDataSamples.getHSMFInstance();
  64. fsMessageSucceeds = new POIFSFileSystem(samples.getFile(messageSucceeds));
  65. fsMessageFails = new POIFSFileSystem(samples.getFile(messageFails));
  66. mapiMessageSucceeds = new MAPIMessage(fsMessageSucceeds);
  67. mapiMessageFails = new MAPIMessage(fsMessageFails);
  68. messageDateFormat = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss", Locale.ROOT);
  69. messageDateFormat.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  70. userTimeZone = LocaleUtil.getUserTimeZone();
  71. LocaleUtil.setUserTimeZone(LocaleUtil.TIMEZONE_UTC);
  72. }
  73. @AfterClass
  74. public static void closeFS() throws Exception {
  75. LocaleUtil.setUserTimeZone(userTimeZone);
  76. fsMessageSucceeds.close();
  77. fsMessageFails.close();
  78. }
  79. /**
  80. * Check we can find a sensible number of properties on a few
  81. * of our test files
  82. */
  83. @Test
  84. public void testPropertiesFound() {
  85. Map<MAPIProperty,List<PropertyValue>> props;
  86. props = mapiMessageSucceeds.getMainChunks().getProperties();
  87. assertTrue(props.toString(), props.size() > 10);
  88. props = mapiMessageFails.getMainChunks().getProperties();
  89. assertTrue(props.toString(), props.size() > 10);
  90. }
  91. /**
  92. * Check we find properties of a variety of different types
  93. */
  94. @Test
  95. public void testPropertyValueTypes() {
  96. Chunks mainChunks = mapiMessageSucceeds.getMainChunks();
  97. // Ask to have the values looked up
  98. Map<MAPIProperty,List<PropertyValue>> props = mainChunks.getProperties();
  99. HashSet<Class<? extends PropertyValue>> seenTypes =
  100. new HashSet<>();
  101. for (List<PropertyValue> pvs : props.values()) {
  102. for (PropertyValue pv : pvs) {
  103. seenTypes.add(pv.getClass());
  104. }
  105. }
  106. assertTrue(seenTypes.toString(), seenTypes.size() > 3);
  107. assertTrue(seenTypes.toString(), seenTypes.contains(LongPropertyValue.class));
  108. assertTrue(seenTypes.toString(), seenTypes.contains(TimePropertyValue.class));
  109. assertFalse(seenTypes.toString(), seenTypes.contains(ChunkBasedPropertyValue.class));
  110. // Ask for the raw values
  111. seenTypes.clear();
  112. for (PropertyValue pv : mainChunks.getRawProperties().values()) {
  113. seenTypes.add(pv.getClass());
  114. }
  115. assertTrue(seenTypes.toString(), seenTypes.size() > 3);
  116. assertTrue(seenTypes.toString(), seenTypes.contains(LongPropertyValue.class));
  117. assertTrue(seenTypes.toString(), seenTypes.contains(TimePropertyValue.class));
  118. assertTrue(seenTypes.toString(), seenTypes.contains(ChunkBasedPropertyValue.class));
  119. }
  120. /**
  121. * Test to see if we can read the Date Chunk with OutlookTextExtractor.
  122. */
  123. @Test
  124. public void testReadMessageDateSucceedsWithOutlookTextExtractor() throws Exception {
  125. OutlookTextExtractor ext = new OutlookTextExtractor(mapiMessageSucceeds);
  126. ext.setCloseFilesystem(false);
  127. String text = ext.getText();
  128. assertContains(text, "Date: Fri, 22 Jun 2012 18:32:54 +0000\n");
  129. ext.close();
  130. }
  131. /**
  132. * Test to see if we can read the Date Chunk with OutlookTextExtractor.
  133. */
  134. @Test
  135. public void testReadMessageDateFailsWithOutlookTextExtractor() throws Exception {
  136. OutlookTextExtractor ext = new OutlookTextExtractor(mapiMessageFails);
  137. ext.setCloseFilesystem(false);
  138. String text = ext.getText();
  139. assertContains(text, "Date: Thu, 21 Jun 2012 14:14:04 +0000\n");
  140. ext.close();
  141. }
  142. /**
  143. * Test to see if we can read the Date Chunk with HSMFDump.
  144. */
  145. @Test
  146. public void testReadMessageDateSucceedsWithHSMFDump() throws IOException {
  147. PrintStream stream = new PrintStream(new ByteArrayOutputStream());
  148. HSMFDump dump = new HSMFDump(fsMessageSucceeds);
  149. dump.dump(stream);
  150. }
  151. /**
  152. * Test to see if we can read the Date Chunk with HSMFDump.
  153. */
  154. @Test
  155. public void testReadMessageDateFailsWithHSMFDump() throws Exception {
  156. PrintStream stream = new PrintStream(new ByteArrayOutputStream());
  157. HSMFDump dump = new HSMFDump(fsMessageFails);
  158. dump.dump(stream);
  159. }
  160. /**
  161. * Will be based on the ClientSubmit time
  162. */
  163. @Test
  164. public void testClientSubmitTime() throws Exception {
  165. // Check via the message date
  166. Calendar clientSubmitTime = mapiMessageSucceeds.getMessageDate();
  167. assertEquals(
  168. "Fri, 22 Jun 2012 18:32:54",
  169. messageDateFormat.format(clientSubmitTime.getTime()));
  170. // Fetch the property value directly
  171. Map<MAPIProperty,List<PropertyValue>> props =
  172. mapiMessageSucceeds.getMainChunks().getProperties();
  173. List<PropertyValue> pv = props.get(MAPIProperty.CLIENT_SUBMIT_TIME);
  174. assertNotNull(pv);
  175. assertEquals(1, pv.size());
  176. clientSubmitTime = (Calendar)pv.get(0).getValue();
  177. assertEquals(
  178. "Fri, 22 Jun 2012 18:32:54",
  179. messageDateFormat.format(clientSubmitTime.getTime()));
  180. }
  181. }