View Javadoc

1   /*
2    * $Id: AbstractMailConnectorFunctionalTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email.connectors;
12  
13  import org.mule.tck.junit4.rule.DynamicPort;
14  import org.mule.tck.transformer.NoActionTransformer;
15  import org.mule.transport.AbstractConnectorTestCase;
16  import org.mule.transport.email.GreenMailUtilities;
17  
18  import com.icegreen.greenmail.util.GreenMail;
19  import com.icegreen.greenmail.util.ServerSetup;
20  
21  import java.util.concurrent.atomic.AtomicInteger;
22  
23  import javax.mail.Address;
24  import javax.mail.Message;
25  import javax.mail.internet.MimeMessage;
26  
27  import org.junit.Rule;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertTrue;
32  
33  /**
34   * Start a (greenmail) mail server with a known message, for use in subclasses.
35   */
36  public abstract class AbstractMailConnectorFunctionalTestCase extends AbstractConnectorTestCase
37  {
38  
39      public static final String LOCALHOST = "127.0.0.1";
40      public static final String USER = "bob";
41      public static final String PROVIDER = "example.com";
42      public static final String EMAIL = USER + "@" + PROVIDER;
43      public static final String PASSWORD = "secret";
44      public static final String MESSAGE = "Test Email Message";
45  
46      public static final int START_ATTEMPTS = 3;
47      public static final int TEST_ATTEMPTS = 5;
48      public static final long STARTUP_PERIOD_MS = 1000;
49  
50      // for constructor
51      public static final boolean SEND_INITIAL_EMAIL = true;
52      public static final boolean NO_INITIAL_EMAIL = false;
53  
54      private static final AtomicInteger nameCount = new AtomicInteger(0);
55  
56      private MimeMessage message;
57      private GreenMail servers;
58      private final boolean initialEmail;
59      private String protocol;
60      private int port;
61  
62      @Rule
63      public DynamicPort dynamicPort = new DynamicPort("port1");
64  
65      protected AbstractMailConnectorFunctionalTestCase(boolean initialEmail, String protocol)
66      {
67          super();
68          this.initialEmail = initialEmail;
69          this.protocol = protocol;
70      }
71  
72      @Override
73      protected synchronized void doSetUp() throws Exception
74      {
75          super.doSetUp();
76          //TODO(pablo.kraan): looks like port is redundant. Remove it
77          this.port = dynamicPort.getNumber();
78          startServers();
79          muleContext.getRegistry().registerObject("noActionTransformer", new NoActionTransformer());
80      }
81  
82      @Override
83      protected synchronized void doTearDown() throws Exception
84      {
85          stopServers();
86          super.doTearDown();
87      }
88  
89      private synchronized void storeEmail() throws Exception
90      {
91          GreenMailUtilities.storeEmail(servers.getManagers().getUserManager(), EMAIL, USER,
92                                        PASSWORD, (MimeMessage) getValidMessage());
93          assertEquals(1, servers.getReceivedMessages().length);
94      }
95  
96      private synchronized void startServers() throws Exception
97      {
98          servers = new GreenMail(getSetups());
99          GreenMailUtilities.robustStartup(servers, LOCALHOST, port, START_ATTEMPTS, TEST_ATTEMPTS, STARTUP_PERIOD_MS);
100         if (initialEmail)
101         {
102             storeEmail();
103         }
104     }
105 
106     private ServerSetup[] getSetups()
107     {
108         return new ServerSetup[] {new ServerSetup(port, null, protocol)};
109     }
110 
111     private synchronized void stopServers() throws Exception
112     {
113         if (null != servers)
114         {
115             try
116             {
117                 servers.stop();
118             }
119             catch (RuntimeException e)
120             {
121                 e.printStackTrace();
122             }
123         }
124     }
125 
126     protected synchronized GreenMail getServers()
127     {
128         return servers;
129     }
130 
131     @Override
132     public Object getValidMessage() throws Exception
133     {
134         if (null == message)
135         {
136             message = GreenMailUtilities.toMessage(MESSAGE, EMAIL, null);
137         }
138         return message;
139     }
140 
141     @Override
142     public String getTestEndpointURI()
143     {
144         String uri = protocol + "://" + USER + ":" + PASSWORD + "@" + LOCALHOST + ":" + port + "?connector="
145                      + connectorName;
146         if (!transformInboundMessage())
147         {
148             uri = uri + "&transformers=noActionTransformer";
149         }
150         return uri;
151     }
152 
153     protected boolean transformInboundMessage()
154     {
155         return false;
156     }
157 
158     protected void assertMessageOk(Object mailMessage) throws Exception
159     {
160         assertTrue("Did not receive a MimeMessage", mailMessage instanceof MimeMessage);
161 
162         MimeMessage received = (MimeMessage) mailMessage;
163 
164         // for some reason, something is adding a newline at the end of messages
165         // so we need to strip that out for comparison
166         assertTrue("Did not receive a message with String contents",
167                    received.getContent() instanceof String);
168 
169         String receivedText = ((String) received.getContent()).trim();
170         assertEquals(MESSAGE, receivedText);
171 
172         Address[] recipients = received.getRecipients(Message.RecipientType.TO);
173         assertNotNull(recipients);
174         assertEquals("recipients", 1, recipients.length);
175         assertEquals("recipient", EMAIL, recipients[0].toString());
176     }
177 
178     protected String uniqueName(String root)
179     {
180         return root + nameCount.incrementAndGet();
181     }
182 }