View Javadoc

1   /*
2    * $Id: SmtpConnectorTestCase.java 22700 2011-08-19 01:24: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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertNull;
17  import static org.junit.Assert.assertTrue;
18  import static org.junit.Assert.fail;
19  
20  import org.mule.DefaultMuleEvent;
21  import org.mule.DefaultMuleMessage;
22  import org.mule.MessageExchangePattern;
23  import org.mule.ResponseOutputStream;
24  import org.mule.api.MuleException;
25  import org.mule.api.MuleMessage;
26  import org.mule.api.MuleSession;
27  import org.mule.api.endpoint.EndpointBuilder;
28  import org.mule.api.endpoint.EndpointException;
29  import org.mule.api.endpoint.OutboundEndpoint;
30  import org.mule.api.routing.OutboundRouterCollection;
31  import org.mule.api.service.Service;
32  import org.mule.api.transport.Connector;
33  import org.mule.endpoint.EndpointURIEndpointBuilder;
34  import org.mule.routing.outbound.OutboundPassThroughRouter;
35  import org.mule.tck.functional.FunctionalTestComponent;
36  import org.mule.tck.testmodels.fruit.Apple;
37  import org.mule.transport.email.AbstractMailConnector;
38  import org.mule.transport.email.MailProperties;
39  import org.mule.transport.email.SmtpConnector;
40  import org.mule.transport.email.functional.AbstractEmailFunctionalTestCase;
41  
42  import com.icegreen.greenmail.util.ServerSetup;
43  
44  import javax.mail.URLName;
45  import javax.mail.internet.MimeMessage;
46  
47  import org.junit.Test;
48  
49  /**
50   * Send a message via SMTP to a (greenmail) server.
51   */
52  public class SmtpConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
53  {    
54      public SmtpConnectorTestCase()
55      {
56          this(NO_INITIAL_EMAIL, ServerSetup.PROTOCOL_SMTP);
57          setStartContext(true);
58      } 
59      
60      protected SmtpConnectorTestCase(boolean initialEmail, String protocol)
61      {
62          super(initialEmail, protocol);
63      }
64      
65      @Override
66      public Connector createConnector() throws Exception
67      {
68          SmtpConnector c = new SmtpConnector(muleContext);
69          c.setName("SmtpConnector");
70          return c;
71      }
72      
73      @Override
74      protected boolean transformInboundMessage()
75      {
76          return true;
77      }
78  
79      /**
80       * The SmtpConnector does not accept listeners, so the test in the superclass makes no sense 
81       * here. The SMTP transport does not even support inbound endpoints, as SMTP is an outbound
82       * transport only so you cannot even create an inbound endpoint to register as listener.
83       */
84      @Override
85      public void testConnectorListenerSupport() throws Exception
86      {
87          // do nothing
88      }
89  
90      @Test
91      public void testSmtpDoesNotSupportOutboundEndpoints() throws MuleException
92      {
93          EndpointBuilder builder = new EndpointURIEndpointBuilder(getTestEndpointURI(), muleContext);
94          builder.setName("test");
95  
96          try
97          {
98              muleContext.getEndpointFactory().getInboundEndpoint(builder);
99              fail("Inbound SMTP endpoints are not supported");
100         }
101         catch (EndpointException ex)
102         {
103             // expected
104         }
105     }
106 
107     @Test
108     public void testSend() throws Exception
109     {
110         //muleContext.getRegistry().registerConnector(createConnector(false));
111         OutboundEndpoint endpoint = muleContext.getEndpointFactory().getOutboundEndpoint(
112             getTestEndpointURI());
113         
114         Service service = getTestService(uniqueName("testComponent"), FunctionalTestComponent.class);
115         // TODO Simplify this API for adding an outbound endpoint.
116         OutboundPassThroughRouter passThroughRouter = new OutboundPassThroughRouter();
117         passThroughRouter.addRoute(endpoint);
118         ((OutboundRouterCollection) service.getOutboundMessageProcessor()).addRoute(passThroughRouter);
119         //muleContext.getRegistry().registerComponent(service);
120 
121         MuleMessage message = new DefaultMuleMessage(MESSAGE, muleContext);
122         message.setOutboundProperty(MailProperties.TO_ADDRESSES_PROPERTY, EMAIL);
123         MuleSession session = getTestSession(getTestService("apple", Apple.class), muleContext);
124         DefaultMuleEvent event = new DefaultMuleEvent(message, MessageExchangePattern.ONE_WAY, session,
125             new ResponseOutputStream(System.out));
126         endpoint.process(event);
127 
128         getServers().waitForIncomingEmail(AbstractEmailFunctionalTestCase.DELIVERY_DELAY_MS, 1);
129         MimeMessage[] messages = getServers().getReceivedMessages();
130         assertNotNull("did not receive any messages", messages);
131         assertEquals("did not receive 1 mail", 1, messages.length);
132         assertMessageOk(messages[0]);
133     }
134 
135 
136     // MULE-2130 (Impossible to re-initialise SMTP connector)
137     @Test
138     public void testConnectorRestart() throws Exception
139     {
140         Connector c = getConnector();
141         assertTrue(c.isStarted());
142 
143         c.stop();
144         assertFalse(c.isStarted());
145 
146         assertFalse(c.isStarted());
147 
148         c.start();
149         assertFalse(c.isDisposed());
150         assertTrue(c.isStarted());
151     }
152 
153     @Test
154     public void testNullUsernameAndPassword() throws Exception
155     {
156         OutboundEndpoint endpoint = muleContext.getEndpointFactory().getOutboundEndpoint("smtp://localhost:23");
157         URLName name = ((AbstractMailConnector)getConnector()).urlFromEndpoint(endpoint);
158         assertNull(name.getUsername());
159         assertNull(name.getPassword());
160 
161         endpoint = muleContext.getEndpointFactory().getOutboundEndpoint("smtp://george@localhost:23");
162         name = ((AbstractMailConnector)getConnector()).urlFromEndpoint(endpoint);
163         assertEquals("george", name.getUsername());
164         assertNull(name.getPassword());
165     }
166 }