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