View Javadoc

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