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.

TestAttachments.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 java.io.IOException;
  20. import java.text.DateFormat;
  21. import java.text.SimpleDateFormat;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import org.apache.poi.util.LocaleUtil;
  25. import org.junit.BeforeClass;
  26. import org.junit.Test;
  27. public final class TestAttachments {
  28. private static HMEFMessage quick;
  29. @BeforeClass
  30. public static void setUp() throws IOException {
  31. quick = openSample("quick-winmail.dat");
  32. }
  33. /**
  34. * Check the file is as we expect
  35. */
  36. @Test
  37. public void testCounts() {
  38. // Should have 5 attachments
  39. assertEquals(5, quick.getAttachments().size());
  40. }
  41. /**
  42. * Check some basic bits about the attachments
  43. */
  44. @Test
  45. public void testBasicAttachments() {
  46. List<Attachment> attachments = quick.getAttachments();
  47. // Word first
  48. assertEquals("quick.doc", attachments.get(0).getFilename());
  49. assertEquals("quick.doc", attachments.get(0).getLongFilename());
  50. assertEquals(".doc", attachments.get(0).getExtension());
  51. // Then HTML
  52. assertEquals("QUICK~1.HTM", attachments.get(1).getFilename());
  53. assertEquals("quick.html", attachments.get(1).getLongFilename());
  54. assertEquals(".html", attachments.get(1).getExtension());
  55. // Then PDF
  56. assertEquals("quick.pdf", attachments.get(2).getFilename());
  57. assertEquals("quick.pdf", attachments.get(2).getLongFilename());
  58. assertEquals(".pdf", attachments.get(2).getExtension());
  59. // Then Text
  60. assertEquals("quick.txt", attachments.get(3).getFilename());
  61. assertEquals("quick.txt", attachments.get(3).getLongFilename());
  62. assertEquals(".txt", attachments.get(3).getExtension());
  63. // And finally XML
  64. assertEquals("quick.xml", attachments.get(4).getFilename());
  65. assertEquals("quick.xml", attachments.get(4).getLongFilename());
  66. assertEquals(".xml", attachments.get(4).getExtension());
  67. }
  68. /**
  69. * Query the attachments in detail, and check we see
  70. * the right values for key things
  71. */
  72. @Test
  73. public void testAttachmentDetails() {
  74. List<Attachment> attachments = quick.getAttachments();
  75. assertEquals(5, attachments.size());
  76. // Pick a predictable date format + timezone
  77. DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss", Locale.UK);
  78. fmt.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  79. for (Attachment attachment : attachments) {
  80. // They should all have the same date on them
  81. assertEquals("28-Apr-2010 12:40:56", fmt.format(attachment.getModifiedDate()));
  82. // They should all have a 3512 byte metafile rendered version
  83. byte[] meta = attachment.getRenderedMetaFile();
  84. assertNotNull(meta);
  85. assertEquals(3512, meta.length);
  86. }
  87. }
  88. /**
  89. * Ensure the attachment contents come back as they should do
  90. */
  91. @Test
  92. public void testAttachmentContents() throws Exception {
  93. List<Attachment> attachments = quick.getAttachments();
  94. assertContents("quick.doc", attachments.get(0));
  95. assertContents("quick.html", attachments.get(1));
  96. assertContents("quick.pdf", attachments.get(2));
  97. assertContents("quick.txt", attachments.get(3));
  98. assertContents("quick.xml", attachments.get(4));
  99. }
  100. static void assertContents(String filename, Attachment attachment) throws IOException {
  101. assertEquals(filename, attachment.getLongFilename());
  102. TestHMEFMessage.assertContents(filename, attachment.getContents());
  103. }
  104. }