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.

TestBugs.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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;
  16. import static org.apache.poi.hmef.TestHMEFMessage.openSample;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertTrue;
  20. import java.io.IOException;
  21. import java.util.List;
  22. import org.apache.poi.hmef.attribute.MAPIAttribute;
  23. import org.apache.poi.hmef.attribute.TNEFAttribute;
  24. import org.apache.poi.hmef.attribute.TNEFProperty;
  25. import org.apache.poi.hsmf.datatypes.MAPIProperty;
  26. import org.apache.poi.util.LittleEndian;
  27. import org.junit.Test;
  28. public class TestBugs {
  29. @Test
  30. public void test52400ReadSimpleTNEF() throws IOException {
  31. HMEFMessage tnefDat = openSample("bug52400-winmail-simple.dat");
  32. MAPIAttribute bodyHtml = tnefDat.getMessageMAPIAttribute(MAPIProperty.BODY_HTML);
  33. assertNotNull(bodyHtml);
  34. String bodyStr = new String(bodyHtml.getData(), getEncoding(tnefDat));
  35. assertTrue(bodyStr.contains("This is the message body."));
  36. }
  37. @Test
  38. public void test52400ReadAttachedTNEF() throws IOException {
  39. HMEFMessage tnefDat = openSample("bug52400-winmail-with-attachments.dat");
  40. MAPIAttribute bodyHtml = tnefDat.getMessageMAPIAttribute(MAPIProperty.BODY_HTML);
  41. assertNotNull(bodyHtml);
  42. String bodyStr = new String(bodyHtml.getData(), getEncoding(tnefDat));
  43. assertTrue(bodyStr.contains("There are also two attachments."));
  44. assertEquals(2, tnefDat.getAttachments().size());
  45. }
  46. private String getEncoding(HMEFMessage tnefDat) {
  47. TNEFAttribute oemCP = tnefDat.getMessageAttribute(TNEFProperty.ID_OEMCODEPAGE);
  48. MAPIAttribute cpId = tnefDat.getMessageMAPIAttribute(MAPIProperty.INTERNET_CPID);
  49. int codePage = 1252;
  50. if (oemCP != null) {
  51. codePage = LittleEndian.getInt(oemCP.getData());
  52. } else if (cpId != null) {
  53. codePage = LittleEndian.getInt(cpId.getData());
  54. }
  55. switch (codePage) {
  56. // see http://en.wikipedia.org/wiki/Code_page for more
  57. case 1252: return "Windows-1252";
  58. case 20127: return "US-ASCII";
  59. default: return "cp"+codePage;
  60. }
  61. }
  62. @Test
  63. public void bug63955() throws IOException {
  64. HMEFMessage tnefDat = openSample("bug63955-winmail.dat");
  65. List<MAPIAttribute> atts = tnefDat.getMessageMAPIAttributes();
  66. assertEquals(96, atts.size());
  67. MAPIAttribute bodyHtml = tnefDat.getMessageMAPIAttribute(MAPIProperty.BODY_HTML);
  68. assertNotNull(bodyHtml);
  69. String bodyStr = new String(bodyHtml.getData(), getEncoding(tnefDat));
  70. assertEquals(1697, bodyStr.length());
  71. }
  72. }