View Javadoc

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