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 com.jcabi.email.stamp.StRecipient}.
45 *
46 * @author Yegor Bugayenko (yegor256@gmail.com)
47 * @version $Id: 33f5e930b564307a69def16b7f6249effabce059 $
48 * @since 1.3.1
49 */
50 public final class StRecipientTest {
51
52 /**
53 * StRecipient can add a recipient to the message.
54 * @throws Exception If fails
55 */
56 @Test
57 public void addsRecipientToMessage() throws Exception {
58 final Message msg = new MimeMessage(
59 Session.getDefaultInstance(new Properties())
60 );
61 new StRecipient(
62 "Jeff Петровский", "jeff@gmail.com"
63 ).attach(msg);
64 MatcherAssert.assertThat(
65 new InternetAddress(
66 msg.getRecipients(Message.RecipientType.TO)[0].toString()
67 ).getAddress(),
68 Matchers.endsWith("@gmail.com")
69 );
70 }
71
72 /**
73 * StRecipient can add a recipient with custom encoding to the message.
74 * @throws Exception If fails
75 */
76 @Test
77 public void addsRecipientToMessageWithCustomCharset() throws Exception {
78 final String charset = "KOI8-R";
79 final Message msg = new MimeMessage(
80 Session.getDefaultInstance(new Properties())
81 );
82 final String name = "Ivan Иванов";
83 final String family = "Иванов";
84 new StRecipient(name, "ivan@gmail.com", charset).attach(msg);
85 MatcherAssert.assertThat(
86 new InternetAddress(
87 msg.getRecipients(Message.RecipientType.TO)[0].toString()
88 ).getPersonal(),
89 Matchers.containsString(family)
90 );
91 MatcherAssert.assertThat(
92 msg.getRecipients(Message.RecipientType.TO)[0].toString(),
93 Matchers.not(Matchers.containsString(family))
94 );
95 MatcherAssert.assertThat(
96 msg.getRecipients(Message.RecipientType.TO)[0].toString(),
97 Matchers.not(Matchers.containsString("UTF"))
98 );
99 MatcherAssert.assertThat(
100 msg.getRecipients(Message.RecipientType.TO)[0].toString(),
101 Matchers.containsString(charset)
102 );
103 MatcherAssert.assertThat(
104 msg.getRecipients(Message.RecipientType.TO)[0].toString(),
105 Matchers.containsString(encodeText(name, charset))
106 );
107 }
108
109 /**
110 * Encode text into MIME with encoding.
111 * @param text Text
112 * @param charset Text charset
113 * @return Encoded text
114 * @throws UnsupportedEncodingException if fail
115 */
116 private static String encodeText(
117 final String text,
118 final String charset
119 ) throws UnsupportedEncodingException {
120 final String encoded = MimeUtility.encodeWord(text, charset, null);
121 final int last = encoded.lastIndexOf('?');
122 final int prev = encoded.lastIndexOf('?', last - 1);
123 return encoded.substring(prev + 1, last);
124 }
125
126 }