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.

Test7BitCodepage.java 4.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 java.io.IOException;
  17. import junit.framework.TestCase;
  18. import org.apache.poi.POIDataSamples;
  19. /**
  20. * Tests to verify if code page for general properties like subject,
  21. * text body and html body is evaluated correctly.
  22. */
  23. public final class Test7BitCodepage extends TestCase {
  24. private final MAPIMessage ascii_cp1251_lcid1049;
  25. private final MAPIMessage ascii_utf_8_cp1252_lcid1031;
  26. private final MAPIMessage ascii_utf_8_cp1252_lcid1031_html;
  27. private final MAPIMessage htmlbodybinary_cp1251;
  28. private final MAPIMessage htmlbodybinary_utf_8;
  29. /**
  30. * Initialize this test, load up the messages.
  31. * @throws Exception
  32. */
  33. public Test7BitCodepage() throws IOException {
  34. POIDataSamples samples = POIDataSamples.getHSMFInstance();
  35. ascii_cp1251_lcid1049 = new MAPIMessage(samples.openResourceAsStream("ASCII_CP1251_LCID1049.msg"));
  36. ascii_utf_8_cp1252_lcid1031 = new MAPIMessage(samples.openResourceAsStream("ASCII_UTF-8_CP1252_LCID1031.msg"));
  37. ascii_utf_8_cp1252_lcid1031_html = new MAPIMessage(samples.openResourceAsStream("ASCII_UTF-8_CP1252_LCID1031_HTML.msg"));
  38. htmlbodybinary_cp1251 = new MAPIMessage(samples.openResourceAsStream("HTMLBodyBinary_CP1251.msg"));
  39. htmlbodybinary_utf_8 = new MAPIMessage(samples.openResourceAsStream("HTMLBodyBinary_UTF-8.msg"));
  40. }
  41. /**
  42. * Evaluate encoding and check if the subject, text body and html body is decoded correctly.
  43. */
  44. public void test7BitEncoding() throws Exception {
  45. ascii_cp1251_lcid1049.guess7BitEncoding();
  46. ascii_cp1251_lcid1049.setReturnNullOnMissingChunk(true);
  47. ascii_utf_8_cp1252_lcid1031.guess7BitEncoding();
  48. ascii_utf_8_cp1252_lcid1031.setReturnNullOnMissingChunk(true);
  49. ascii_utf_8_cp1252_lcid1031_html.guess7BitEncoding();
  50. ascii_utf_8_cp1252_lcid1031_html.setReturnNullOnMissingChunk(true);
  51. htmlbodybinary_cp1251.guess7BitEncoding();
  52. htmlbodybinary_cp1251.setReturnNullOnMissingChunk(true);
  53. htmlbodybinary_utf_8.guess7BitEncoding();
  54. htmlbodybinary_utf_8.setReturnNullOnMissingChunk(true);
  55. assertEquals("Subject автоматически Subject", ascii_cp1251_lcid1049.getSubject());
  56. assertEquals("Body автоматически Body", ascii_cp1251_lcid1049.getTextBody());
  57. assertEquals("<!DOCTYPE html><html><meta charset=\\\"windows-1251\\\"><body>HTML автоматически</body></html>", ascii_cp1251_lcid1049.getHtmlBody());
  58. assertEquals("Subject öäü Subject", ascii_utf_8_cp1252_lcid1031.getSubject());
  59. assertEquals("Body öäü Body", ascii_utf_8_cp1252_lcid1031.getTextBody());
  60. assertNull(ascii_utf_8_cp1252_lcid1031.getHtmlBody());
  61. assertEquals("Subject öäü Subject", ascii_utf_8_cp1252_lcid1031_html.getSubject());
  62. assertEquals("Body öäü Body", ascii_utf_8_cp1252_lcid1031_html.getTextBody());
  63. assertEquals("<!DOCTYPE html><html><meta charset=\\\"utf-8\\\"><body>HTML öäü</body></html>", ascii_utf_8_cp1252_lcid1031_html.getHtmlBody());
  64. assertEquals("Subject öäü Subject", htmlbodybinary_cp1251.getSubject());
  65. assertNull(htmlbodybinary_cp1251.getTextBody());
  66. assertEquals("<!DOCTYPE html><html><meta charset=\\\"utf-8\\\"><body>HTML автоматически</body></html>", htmlbodybinary_cp1251.getHtmlBody());
  67. assertEquals("Subject öäü Subject", htmlbodybinary_utf_8.getSubject());
  68. assertNull(htmlbodybinary_utf_8.getTextBody());
  69. assertEquals("<!DOCTYPE html><html><meta charset=\\\"utf-8\\\"><body>HTML öäü</body></html>", htmlbodybinary_utf_8.getHtmlBody());
  70. }
  71. }