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.

TestMAPIAttributes.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.hmef.attribute;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.InputStream;
  20. import java.text.DateFormat;
  21. import java.text.SimpleDateFormat;
  22. import java.util.Locale;
  23. import org.apache.poi.POIDataSamples;
  24. import org.apache.poi.hmef.HMEFMessage;
  25. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  26. import org.apache.poi.util.LittleEndian;
  27. import org.apache.poi.util.LocaleUtil;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. public final class TestMAPIAttributes {
  31. private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
  32. private HMEFMessage quick;
  33. @Before
  34. public void setUp() throws Exception {
  35. try (InputStream stream = _samples.openResourceAsStream("quick-winmail.dat")) {
  36. quick = new HMEFMessage(stream);
  37. }
  38. }
  39. /**
  40. * Test counts
  41. */
  42. @Test
  43. public void testCounts() throws Exception {
  44. // Message should have 54
  45. assertEquals(54, quick.getMessageMAPIAttributes().size());
  46. // First attachment should have 22
  47. assertEquals(22, quick.getAttachments().get(0).getMAPIAttributes().size());
  48. }
  49. /**
  50. * Test various general ones
  51. */
  52. @Test
  53. public void testBasics() throws Exception {
  54. // Try constructing two attributes
  55. byte[] data = new byte[]{
  56. // Level one, id 36867, type 6
  57. 0x01, 0x03, (byte) 0x90, 0x06, 0x00,
  58. // Length 24
  59. 0x24, 0x00, 0x00, 0x00,
  60. // Three attributes
  61. 0x03, 0x00, 0x00, 0x00,
  62. // AlternateRecipientAllowed = 01 00
  63. 0x0B, 0x00, 0x02, 0x00,
  64. 0x01, 0x00, 0x00, 0x00,
  65. // Priority = 00 00 00 00
  66. 0x03, 0x00, 0x26, 0x00,
  67. 0x00, 0x00, 0x00, 0x00,
  68. // ConversationTopic = Test
  69. 0x1E, 0x00, 0x70, 0x00,
  70. 0x01, 0x00, 0x00, 0x00,
  71. 0x04, 0x00, 0x00, 0x00,
  72. (byte) 'T', (byte) 'e',
  73. (byte) 's', (byte) 't',
  74. // Checksum (may be wrong...)
  75. 0x01, 0x00
  76. };
  77. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  78. // Create it
  79. int level = bais.read();
  80. assertEquals(1, level);
  81. TNEFAttribute attr = TNEFAttribute.create(bais);
  82. // Check it
  83. assertNotNull(attr);
  84. assertEquals(TNEFMAPIAttribute.class, attr.getClass());
  85. TNEFMAPIAttribute mapi = (TNEFMAPIAttribute) attr;
  86. assertEquals(3, mapi.getMAPIAttributes().size());
  87. assertEquals(
  88. MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED,
  89. mapi.getMAPIAttributes().get(0).getProperty()
  90. );
  91. assertEquals(1, LittleEndian.getUShort(
  92. mapi.getMAPIAttributes().get(0).getData()
  93. ));
  94. assertEquals(
  95. MAPIProperty.PRIORITY,
  96. mapi.getMAPIAttributes().get(1).getProperty()
  97. );
  98. assertEquals(0, LittleEndian.getUShort(
  99. mapi.getMAPIAttributes().get(1).getData()
  100. ));
  101. assertEquals(
  102. MAPIProperty.CONVERSATION_TOPIC,
  103. mapi.getMAPIAttributes().get(2).getProperty()
  104. );
  105. assertEquals(
  106. "Test",
  107. ((MAPIStringAttribute) mapi.getMAPIAttributes().get(2)).getDataString()
  108. );
  109. }
  110. /**
  111. * Test String, Date and RTF ones
  112. */
  113. @Test
  114. public void testTyped() throws Exception {
  115. MAPIAttribute attr;
  116. // String
  117. // ConversationTopic -> This is a test message
  118. attr = quick.getMessageMAPIAttribute(MAPIProperty.CONVERSATION_TOPIC);
  119. assertNotNull(attr);
  120. assertEquals(MAPIStringAttribute.class, attr.getClass());
  121. MAPIStringAttribute str = (MAPIStringAttribute) attr;
  122. assertEquals("This is a test message", str.getDataString());
  123. // Date
  124. // (Unknown/Custom) 32955 -> Wed Dec 15 2010 @ 14:46:31 UTC
  125. attr = null;
  126. for (MAPIAttribute a : quick.getMessageMAPIAttributes()) {
  127. if (a.getProperty().id == 32955) {
  128. attr = a;
  129. break;
  130. }
  131. }
  132. assertNotNull(attr);
  133. assertEquals(MAPIDateAttribute.class, attr.getClass());
  134. MAPIDateAttribute date = (MAPIDateAttribute) attr;
  135. DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK);
  136. fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  137. assertEquals("15-Dec-2010 14:46:31", fmt.format(date.getDate()));
  138. // RTF
  139. // RtfCompressed -> {\rtf1...
  140. attr = quick.getMessageMAPIAttribute(MAPIProperty.RTF_COMPRESSED);
  141. assertNotNull(attr);
  142. assertEquals(MAPIRtfAttribute.class, attr.getClass());
  143. MAPIRtfAttribute rtf = (MAPIRtfAttribute) attr;
  144. assertEquals("{\\rtf1", rtf.getDataString().substring(0, 6));
  145. }
  146. /**
  147. * Check common ones via helper accessors
  148. */
  149. @Test
  150. public void testCommon() throws Exception {
  151. assertEquals("This is a test message", quick.getSubject());
  152. assertEquals("quick.doc", quick.getAttachments().get(0).getLongFilename());
  153. assertEquals(".doc", quick.getAttachments().get(0).getExtension());
  154. }
  155. }