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.

TestTNEFAttributes.java 7.0KB

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.hmef.attribute;
  16. import java.io.ByteArrayInputStream;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Locale;
  20. import org.apache.poi.POIDataSamples;
  21. import org.apache.poi.hmef.Attachment;
  22. import org.apache.poi.hmef.HMEFMessage;
  23. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  24. import org.apache.poi.util.LittleEndian;
  25. import org.apache.poi.util.LocaleUtil;
  26. import junit.framework.TestCase;
  27. public final class TestTNEFAttributes extends TestCase {
  28. private static final POIDataSamples _samples = POIDataSamples.getHMEFInstance();
  29. private HMEFMessage quick;
  30. @Override
  31. protected void setUp() throws Exception {
  32. super.setUp();
  33. quick = new HMEFMessage(
  34. _samples.openResourceAsStream("quick-winmail.dat")
  35. );
  36. }
  37. /**
  38. * Test counts
  39. */
  40. public void testCounts() throws Exception {
  41. // The message should have 4 attributes
  42. assertEquals(4, quick.getMessageAttributes().size());
  43. // Each attachment should have 6 attributes
  44. for(Attachment attach : quick.getAttachments()) {
  45. assertEquals(6, attach.getAttributes().size());
  46. }
  47. }
  48. /**
  49. * Test the basics
  50. */
  51. public void testBasics() throws Exception {
  52. // An int one
  53. assertEquals(
  54. 0x010000,
  55. LittleEndian.getInt( quick.getMessageAttribute(TNEFProperty.ID_TNEFVERSION).getData() )
  56. );
  57. // Claims not to be text, but really is
  58. assertEquals(
  59. "IPM.Microsoft Mail.Note\0",
  60. new String(quick.getMessageAttribute(TNEFProperty.ID_MESSAGECLASS).getData(), "ASCII")
  61. );
  62. // Try constructing two attributes
  63. byte[] data = new byte[] {
  64. // Level one, id 36870, type 8
  65. 0x01, 0x06, (byte)0x90, 0x08, 0x00,
  66. // Length 4
  67. 0x04, 0x00, 0x00, 0x00,
  68. // Data
  69. 0x00, 0x00, 0x01, 0x00,
  70. // Checksum
  71. 0x01, 0x00,
  72. // level one, id 36871, type 6
  73. 0x01, 0x07, (byte)0x90, 0x06, 0x00,
  74. // Length 8
  75. 0x08, 0x00, 0x00, 0x00,
  76. // Data
  77. (byte)0xe4, 0x04, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00,
  79. // Checksum
  80. (byte)0xe8, 0x00
  81. };
  82. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  83. // Create them
  84. int level = bais.read();
  85. assertEquals(1, level);
  86. TNEFAttribute attr1 = TNEFAttribute.create(bais);
  87. level = bais.read();
  88. assertEquals(1, level);
  89. TNEFAttribute attr2 = TNEFAttribute.create(bais);
  90. assertEquals(-1, bais.read());
  91. // Check them
  92. assertEquals(TNEFProperty.ID_TNEFVERSION, attr1.getProperty());
  93. assertEquals(8, attr1.getType());
  94. assertEquals(4, attr1.getData().length);
  95. assertEquals(0x010000, LittleEndian.getInt( attr1.getData() ));
  96. assertEquals(TNEFProperty.ID_OEMCODEPAGE, attr2.getProperty());
  97. assertEquals(6, attr2.getType());
  98. assertEquals(8, attr2.getData().length);
  99. assertEquals(0x04e4, LittleEndian.getInt( attr2.getData() ));
  100. }
  101. /**
  102. * Test string based ones
  103. */
  104. public void testString() throws Exception {
  105. TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
  106. TNEFProperty.ID_ATTACHTITLE
  107. );
  108. assertNotNull(attr);
  109. assertEquals(TNEFStringAttribute.class, attr.getClass());
  110. // It is a null terminated string
  111. assertEquals("quick.doc\u0000", new String(attr.getData(), "ASCII"));
  112. // But when we ask for the string, that is sorted for us
  113. TNEFStringAttribute str = (TNEFStringAttribute)attr;
  114. assertEquals("quick.doc", str.getString());
  115. }
  116. /**
  117. * Test date based ones
  118. */
  119. public void testDate() throws Exception {
  120. TNEFAttribute attr = quick.getAttachments().get(0).getAttribute(
  121. TNEFProperty.ID_ATTACHMODIFYDATE
  122. );
  123. assertNotNull(attr);
  124. assertEquals(TNEFDateAttribute.class, attr.getClass());
  125. // It is a series of date parts
  126. // Weds 28th April 2010 @ 12:40:56 UTC
  127. assertEquals(2010, LittleEndian.getUShort(attr.getData(), 0));
  128. assertEquals(04, LittleEndian.getUShort(attr.getData(), 2));
  129. assertEquals(28, LittleEndian.getUShort(attr.getData(), 4));
  130. assertEquals(12, LittleEndian.getUShort(attr.getData(), 6));
  131. assertEquals(40, LittleEndian.getUShort(attr.getData(), 8));
  132. assertEquals(56, LittleEndian.getUShort(attr.getData(), 10));
  133. assertEquals(3, LittleEndian.getUShort(attr.getData(), 12)); // Weds
  134. // Ask for it as a Java date, and have it converted
  135. // Pick a predictable format + location + timezone
  136. TNEFDateAttribute date = (TNEFDateAttribute)attr;
  137. DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss", Locale.UK);
  138. fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  139. assertEquals("28-Apr-2010 12:40:56", fmt.format(date.getDate()));
  140. }
  141. /**
  142. * Test a bit of mapi
  143. */
  144. public void testMAPI() throws Exception {
  145. // Message MAPI
  146. TNEFAttribute attr = quick.getMessageAttribute(
  147. TNEFProperty.ID_MAPIPROPERTIES
  148. );
  149. assertNotNull(attr);
  150. assertEquals(TNEFMAPIAttribute.class, attr.getClass());
  151. TNEFMAPIAttribute mapi = (TNEFMAPIAttribute)attr;
  152. assertEquals(54, mapi.getMAPIAttributes().size());
  153. assertEquals(
  154. MAPIProperty.ALTERNATE_RECIPIENT_ALLOWED,
  155. mapi.getMAPIAttributes().get(0).getProperty()
  156. );
  157. // Attribute MAPI
  158. attr = quick.getAttachments().get(0).getAttribute(
  159. TNEFProperty.ID_ATTACHMENT
  160. );
  161. assertNotNull(attr);
  162. assertEquals(TNEFMAPIAttribute.class, attr.getClass());
  163. mapi = (TNEFMAPIAttribute)attr;
  164. assertEquals(22, mapi.getMAPIAttributes().size());
  165. assertEquals(
  166. MAPIProperty.ATTACH_SIZE,
  167. mapi.getMAPIAttributes().get(0).getProperty()
  168. );
  169. }
  170. /**
  171. * Test common ones via helpers
  172. */
  173. public void testCommon() throws Exception {
  174. assertEquals("This is a test message", quick.getSubject());
  175. assertEquals("quick.doc", quick.getAttachments().get(0).getFilename());
  176. }
  177. }