1   /*
2    * $Id: SmtpConnectorTestCase.java 12017 2008-06-12 09:04:04Z rossmason $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleMessage;
17  import org.mule.api.MuleSession;
18  import org.mule.api.endpoint.EndpointBuilder;
19  import org.mule.api.endpoint.InboundEndpoint;
20  import org.mule.api.endpoint.OutboundEndpoint;
21  import org.mule.api.service.Service;
22  import org.mule.api.transport.Connector;
23  import org.mule.endpoint.EndpointURIEndpointBuilder;
24  import org.mule.routing.outbound.OutboundPassThroughRouter;
25  import org.mule.tck.testmodels.fruit.Apple;
26  import org.mule.tck.functional.FunctionalTestComponent;
27  import org.mule.transport.email.MailProperties;
28  import org.mule.transport.email.SmtpConnector;
29  
30  import com.icegreen.greenmail.util.ServerSetup;
31  
32  import javax.mail.internet.MimeMessage;
33  
34  /**
35   * Send a message via SMTP to a (greenmail) server.
36   */
37  public class SmtpConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
38  {    
39      public static final long DELIVERY_DELAY_MS = 5000;
40  
41      public SmtpConnectorTestCase()
42      {
43          this(ServerSetup.PROTOCOL_SMTP, 50007);
44      } 
45      
46      public SmtpConnectorTestCase(String protocol, int port)
47      {
48          super(NO_INITIAL_EMAIL, protocol, port);
49      }
50  
51      public Connector createConnector() throws Exception
52      {
53          SmtpConnector c = new SmtpConnector();
54          c.setName("SmtpConnector");
55          return c;
56      }
57  
58      /**
59       * The SmtpConnector does not accept listeners, so the test in the
60       * superclass makes no sense here.  Instead, we simply check that
61       * a listener is rejected.
62       */
63      // @Override
64      public void testConnectorListenerSupport() throws Exception
65      {
66          Connector connector = getConnector();
67          assertNotNull(connector);
68  
69          Service service = getTestService("anApple", Apple.class);
70          //muleContext.getRegistry().registerComponent(service);
71          EndpointBuilder builder = new EndpointURIEndpointBuilder(getTestEndpointURI(), muleContext);
72          builder.setName("test");
73          InboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getInboundEndpoint(
74              builder);
75          try
76          {
77              connector.registerListener(service, endpoint);
78              fail("SMTP connector does not accept listeners");
79          }
80          catch (Exception e)
81          {
82              assertNotNull("expected", e);
83          }
84      }
85  
86      public void testSend() throws Exception
87      {
88          //muleContext.getRegistry().registerConnector(createConnector(false));
89          OutboundEndpoint endpoint = muleContext.getRegistry().lookupEndpointFactory().getOutboundEndpoint(
90              getTestEndpointURI());
91          
92          Service service = getTestService(uniqueName("testComponent"), FunctionalTestComponent.class);
93          // TODO Simplify this API for adding an outbound endpoint.
94          ((OutboundPassThroughRouter) service.getOutboundRouter().getRouters().get(0)).addEndpoint(endpoint);
95          //muleContext.getRegistry().registerComponent(service);
96  
97          MuleMessage message = new DefaultMuleMessage(MESSAGE);
98          message.setStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, EMAIL);
99          MuleSession session = getTestSession(getTestService("apple", Apple.class), muleContext);
100         DefaultMuleEvent event = new DefaultMuleEvent(message, endpoint, session, true, new ResponseOutputStream(System.out));
101         endpoint.dispatch(event);
102 
103         getServers().waitForIncomingEmail(DELIVERY_DELAY_MS, 1);
104         MimeMessage[] messages = getServers().getReceivedMessages();
105         assertNotNull("did not receive any messages", messages);
106         assertEquals("did not receive 1 mail", 1, messages.length);
107         assertMessageOk(messages[0]);
108     }
109 
110 
111     // MULE-2130 (Impossible to re-initialise SMTP connector)
112     public void testConnectorReinitialise() throws Exception
113     {
114         Connector c = getConnector();
115 
116         c.start();
117         assertTrue(c.isStarted());
118 
119         c.stop();
120         assertFalse(c.isStarted());
121 
122         // are these supposed to work?
123         // - initialise() without dispose()
124         // - initialise() after dispose()
125 
126 //        c.dispose();
127 //        assertFalse(c.isStarted());
128 //        assertTrue(c.isDisposed());
129 //
130 //        c.initialise();
131 //        assertFalse(c.isDisposed());
132 //        assertFalse(c.isStarted());
133 
134         c.start();
135         assertFalse(c.isDisposed());
136         assertTrue(c.isStarted());
137     }
138 
139 }