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.MimeMessage; 37 import javax.mail.internet.MimeUtility; 38 import org.hamcrest.MatcherAssert; 39 import org.hamcrest.Matchers; 40 import org.junit.Test; 41 42 /** 43 * Test case for {@link com.jcabi.email.stamp.StSubject}. 44 * 45 * @author Yegor Bugayenko (yegor256@gmail.com) 46 * @version $Id: 5ad163e196c27fb3fb7042828ea99f735ac0e699 $ 47 * @since 1.3.1 48 */ 49 public final class StSubjectTest { 50 51 /** 52 * StSubject can add a subject to the message. 53 * @throws Exception If fails 54 */ 55 @Test 56 public void addsSubjectToMessage() throws Exception { 57 final Message msg = new MimeMessage( 58 Session.getDefaultInstance(new Properties()) 59 ); 60 new StSubject("how are you, друг?").attach(msg); 61 MatcherAssert.assertThat( 62 msg.getSubject(), 63 Matchers.containsString("друг") 64 ); 65 } 66 67 /** 68 * StSubject can add a subject to the message with custom encoding. 69 * @throws Exception If fails 70 */ 71 @Test 72 public void addsSubjectToMessageWithCustomEncoding() throws Exception { 73 final String charset = "KOI8-R"; 74 final Message msg = new MimeMessage( 75 Session.getDefaultInstance(new Properties()) 76 ); 77 new StSubject("how are you, приятель?", charset).attach(msg); 78 final String substring = "приятель"; 79 MatcherAssert.assertThat( 80 msg.getSubject(), 81 Matchers.containsString(substring) 82 ); 83 final String subject = "Subject"; 84 MatcherAssert.assertThat( 85 msg.getHeader(subject)[0], 86 Matchers.not(Matchers.containsString("UTF")) 87 ); 88 MatcherAssert.assertThat( 89 msg.getHeader(subject)[0], 90 Matchers.containsString(charset) 91 ); 92 MatcherAssert.assertThat( 93 msg.getHeader(subject)[0], 94 Matchers.containsString(encodeText(substring, charset)) 95 ); 96 } 97 98 /** 99 * Encode text into MIME with encoding. 100 * @param text Text 101 * @param charset Text charset 102 * @return Encoded text 103 * @throws UnsupportedEncodingException if fail 104 */ 105 private static String encodeText( 106 final String text, 107 final String charset 108 ) throws UnsupportedEncodingException { 109 final String encoded = MimeUtility.encodeText(text, charset, "Q"); 110 final int last = encoded.lastIndexOf('?'); 111 final int prev = encoded.lastIndexOf('?', last - 1); 112 return encoded.substring(prev + 1, last); 113 } 114 115 }