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;
31  
32  import com.google.common.base.Joiner;
33  import com.jcabi.email.enclosure.EnHtml;
34  import com.jcabi.email.enclosure.EnPlain;
35  import com.jcabi.email.stamp.StRecipient;
36  import com.jcabi.email.stamp.StSender;
37  import com.jcabi.email.stamp.StSubject;
38  import java.io.ByteArrayOutputStream;
39  import java.util.Properties;
40  import javax.mail.Message;
41  import javax.mail.Multipart;
42  import javax.mail.Session;
43  import javax.mail.internet.MimeMessage;
44  import org.hamcrest.MatcherAssert;
45  import org.hamcrest.Matchers;
46  import org.junit.Test;
47  import org.mockito.Mockito;
48  
49  /**
50   * Test case for {@link Envelope}.
51   *
52   * @author Yegor Bugayenko (yegor256@gmail.com)
53   * @version $Id: 5384edd88dfea4457dbba76f33d3e9ef162d2d8c $
54   * @since 1.4
55   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
56   */
57  public final class EnvelopeTest {
58  
59      /**
60       * Envelope.Constant can cache.
61       * @throws Exception If fails
62       */
63      @Test
64      public void cachesPreviousCall() throws Exception {
65          final Envelope origin = Mockito.mock(Envelope.class);
66          Mockito.doReturn(
67              new MimeMessage(Session.getDefaultInstance(new Properties()))
68          ).when(origin).unwrap();
69          final Envelope envelope = new Envelope.Constant(origin);
70          envelope.unwrap();
71          envelope.unwrap();
72          Mockito.verify(origin, Mockito.times(1)).unwrap();
73      }
74  
75      /**
76       * Envelope should handle non-Latin characters.
77       * @throws Exception Thrown in case of problem of writing a message to
78       *  string.
79       */
80      @Test
81      public void handlesUnicodeCorrectly() throws Exception {
82          final Envelope env = new Envelope.Mime()
83              .with(new StSender("from <test-from@jcabi.com>"))
84              .with(new StRecipient("to", "test-to@jcabi.com"))
85              .with(new StSubject("test subject: test me"))
86              .with(new EnHtml("<html><body>привет</body></html>"));
87          final ByteArrayOutputStream stream =
88              new ByteArrayOutputStream();
89          env.unwrap().writeTo(stream);
90          final String msgtxt = stream.toString();
91          MatcherAssert.assertThat(
92              msgtxt,
93              Matchers.allOf(
94                  Matchers.containsString(
95                      "Content-Type: text/html;charset=\"UTF-8\""
96                  ),
97                  Matchers.containsString(
98                      "Content-Transfer-Encoding: quoted-printable"
99                  ),
100                 Matchers.containsString(
101                     Joiner.on("").join(
102                         "<html><body>",
103                         "=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D1=82</body></html>"
104                     )
105                 )
106             )
107         );
108     }
109 
110     /**
111      * Envelope.MIME can wrap another envelope.
112      * @throws Exception If fails
113      */
114     @Test
115     public void wrapsAnotherEnvelope() throws Exception {
116         final Envelope origin = new Envelope.Mime().with(
117             new StSender("jack@example.com")
118         );
119         final Message message = new Envelope.Mime(origin).with(
120             new StRecipient("paul@example.com")
121         ).unwrap();
122         MatcherAssert.assertThat(
123             message.getAllRecipients().length,
124             Matchers.equalTo(1)
125         );
126         MatcherAssert.assertThat(
127             message.getFrom().length,
128             Matchers.equalTo(1)
129         );
130     }
131 
132     /**
133      * Envelope.MIME can wrap another envelope with enclosures.
134      * @throws Exception If fails
135      */
136     @Test
137     public void wrapsAnotherEnvelopeWithEnclosures() throws Exception {
138         final Envelope origin = new Envelope.Mime().with(
139             new EnPlain("first enclosure")
140         );
141         final Message message = new Envelope.Mime(origin).with(
142             new EnPlain("second enclosure")
143         ).unwrap();
144         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
145         Multipart.class.cast(message.getContent()).writeTo(baos);
146         MatcherAssert.assertThat(
147             baos.toString(),
148             Matchers.allOf(
149                 Matchers.containsString("first"),
150                 Matchers.containsString("second")
151             )
152         );
153     }
154 }