View Javadoc
1   /**
2    * Copyright (c) 2014-2017, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.email.stamp;
31  
32  import java.io.UnsupportedEncodingException;
33  import java.util.Properties;
34  import javax.mail.Message;
35  import javax.mail.Session;
36  import javax.mail.internet.InternetAddress;
37  import javax.mail.internet.MimeMessage;
38  import javax.mail.internet.MimeUtility;
39  import org.hamcrest.MatcherAssert;
40  import org.hamcrest.Matchers;
41  import org.junit.Test;
42  
43  /**
44   * Test case for {@link StCc}.
45   *
46   * @author Yegor Bugayenko (yegor256@gmail.com)
47   * @version $Id: 49038877c7183172a57d2349f3e8a9f43c4aafdc $
48   * @since 1.3.1
49   */
50  public final class StCcTest {
51  
52      /**
53       * StCC can add a CC recipient to the message.
54       * @throws Exception If fails
55       */
56      @Test
57      public void addsCopyToMessage() throws Exception {
58          final Message msg = new MimeMessage(
59              Session.getDefaultInstance(new Properties())
60          );
61          new StCc("Jeff Петровский", "jeff@gmail.com").attach(msg);
62          new StCc("Jeff Again", "jeff+again@gmail.com").attach(msg);
63          MatcherAssert.assertThat(
64              new InternetAddress(
65                  msg.getRecipients(Message.RecipientType.CC)[0].toString()
66              ).getPersonal(),
67              Matchers.containsString("Петровский")
68          );
69          MatcherAssert.assertThat(
70              new InternetAddress(
71                  msg.getRecipients(Message.RecipientType.CC)[1].toString()
72              ).getPersonal(),
73              Matchers.containsString("Again")
74          );
75      }
76  
77      /**
78       * StCC can add a CC recipient with custom encoding to the message.
79       * @throws Exception If fails
80       */
81      @Test
82      public void addsCopyToMessageWithCustomCharset() throws Exception {
83          final String charset = "KOI8-R";
84          final Message msg = new MimeMessage(
85              Session.getDefaultInstance(new Properties())
86          );
87          final String name = "Ivan Иванов";
88          final String family = "Иванов";
89          new StCc(name, "ivan@gmail.com", charset).attach(msg);
90          MatcherAssert.assertThat(
91              new InternetAddress(
92                  msg.getRecipients(Message.RecipientType.CC)[0].toString()
93              ).getPersonal(),
94              Matchers.containsString(family)
95          );
96          MatcherAssert.assertThat(
97              msg.getRecipients(Message.RecipientType.CC)[0].toString(),
98              Matchers.not(Matchers.containsString(family))
99          );
100         MatcherAssert.assertThat(
101             msg.getRecipients(Message.RecipientType.CC)[0].toString(),
102             Matchers.not(Matchers.containsString("UTF"))
103         );
104         MatcherAssert.assertThat(
105             msg.getRecipients(Message.RecipientType.CC)[0].toString(),
106             Matchers.containsString(charset)
107         );
108         MatcherAssert.assertThat(
109             msg.getRecipients(Message.RecipientType.CC)[0].toString(),
110             Matchers.containsString(encodeText(name, charset))
111         );
112     }
113 
114     /**
115      * Encode text into MIME with encoding.
116      * @param text Text
117      * @param charset Text charset
118      * @return Encoded text
119      * @throws UnsupportedEncodingException if fail
120      */
121     private static String encodeText(
122         final String text,
123         final String charset
124     ) throws UnsupportedEncodingException {
125         final String encoded = MimeUtility.encodeWord(text, charset, null);
126         final int last = encoded.lastIndexOf('?');
127         final int prev = encoded.lastIndexOf('?', last - 1);
128         return encoded.substring(prev + 1, last);
129     }
130 
131 }