1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
51
52
53
54
55
56
57 public final class EnvelopeTest {
58
59
60
61
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
77
78
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
112
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
134
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 }