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.wire;
31  
32  import com.icegreen.greenmail.util.DummySSLSocketFactory;
33  import com.icegreen.greenmail.util.GreenMail;
34  import com.icegreen.greenmail.util.ServerSetup;
35  import com.jcabi.email.Envelope;
36  import com.jcabi.email.Postman;
37  import com.jcabi.email.Protocol;
38  import com.jcabi.email.Token;
39  import com.jcabi.email.enclosure.EnPlain;
40  import com.jcabi.email.stamp.StRecipient;
41  import com.jcabi.email.stamp.StSender;
42  import com.jcabi.email.stamp.StSubject;
43  import java.io.IOException;
44  import java.net.ServerSocket;
45  import java.security.Security;
46  import javax.mail.Message;
47  import javax.mail.internet.MimeMessage;
48  import org.hamcrest.MatcherAssert;
49  import org.hamcrest.Matchers;
50  import org.junit.Test;
51  
52  /**
53   * Test case for {@link Smtps}.
54   * @author Mihai Andronache (amihaiemil@gmail.com)
55   * @version $Id: eb59f1dd16b2c80bce0dba920f340c1dbfee0022 $
56   * @since 1.9
57   * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
58   */
59  public final class SmtpsTest {
60  
61      /**
62       * SMTPS postman can send an email to the server through SMTPS wire.
63       * @throws Exception If fails
64       */
65      @Test
66      public void sendsEmailToTheServerThroughSmtps() throws Exception {
67          final String bind = "localhost";
68          final int received = 1;
69          final int port = SmtpsTest.port();
70          final int timeout = 3000;
71          Security.setProperty(
72              "ssl.SocketFactory.provider",
73              DummySSLSocketFactory.class.getName()
74          );
75          final ServerSetup setup = new ServerSetup(
76              port, bind, ServerSetup.PROTOCOL_SMTPS
77          );
78          setup.setServerStartupTimeout(timeout);
79          final GreenMail server = new GreenMail(setup);
80          server.start();
81          try {
82              new Postman.Default(
83                  new Smtps(
84                      new Token("", "")
85                          .access(new Protocol.Smtps(bind, port))
86                  )
87              ).send(
88                  new Envelope.Safe(
89                      new Envelope.Mime()
90                          .with(new StSender("from <test-from@jcabi.com>"))
91                          .with(new StRecipient("to", "test-to@jcabi.com"))
92                          .with(new StSubject("test subject: test me"))
93                          .with(new EnPlain("hello"))
94                  )
95              );
96              final MimeMessage[] messages = server.getReceivedMessages();
97              MatcherAssert.assertThat(
98                  messages.length, Matchers.is(received)
99              );
100             for (final Message msg : messages) {
101                 MatcherAssert.assertThat(
102                     msg.getFrom()[0].toString(),
103                     Matchers.containsString("<test-from@jcabi.com>")
104                 );
105                 MatcherAssert.assertThat(
106                     msg.getSubject(),  Matchers.containsString("test me")
107                 );
108             }
109         } finally {
110             server.stop();
111         }
112     }
113 
114     /**
115      * Allocate free port.
116      * @return Found port.
117      * @throws IOException In case of error.
118      */
119     private static int port() throws IOException {
120         try (ServerSocket socket = new ServerSocket(0)) {
121             return socket.getLocalPort();
122         }
123     }
124 
125 }