1   /*
2    * $Id: SmtpConnectorTestCase.java 10724 2008-02-06 15:34:56Z dirk.olmes $
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.providers.email;
12  
13  import org.mule.MuleManager;
14  import org.mule.config.builders.QuickConfigurationBuilder;
15  import org.mule.impl.MuleDescriptor;
16  import org.mule.impl.MuleEvent;
17  import org.mule.impl.MuleMessage;
18  import org.mule.impl.ResponseOutputStream;
19  import org.mule.impl.endpoint.MuleEndpoint;
20  import org.mule.impl.endpoint.MuleEndpointURI;
21  import org.mule.tck.functional.FunctionalTestComponent;
22  import org.mule.tck.testmodels.fruit.Apple;
23  import org.mule.umo.UMOComponent;
24  import org.mule.umo.UMOMessage;
25  import org.mule.umo.UMOSession;
26  import org.mule.umo.endpoint.UMOEndpoint;
27  import org.mule.umo.model.UMOModel;
28  import org.mule.umo.provider.UMOConnector;
29  
30  import java.util.HashMap;
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  
40      public static final long DELIVERY_DELAY_MS = 5000;
41  
42      public SmtpConnectorTestCase()
43      {
44          this("SmtpConnector");
45      }
46  
47      SmtpConnectorTestCase(String connectorName)
48      {
49          super(false, connectorName);
50      }
51  
52      public UMOConnector createConnector(boolean init) throws Exception
53      {
54          SmtpConnector c = new SmtpConnector();
55          c.setName(getConnectorName());
56          if (init)
57          {
58              c.initialise();
59          }
60          return c;
61      }
62  
63      public String getTestEndpointURI()
64      {
65          return getSmtpTestEndpointURI();
66      }
67  
68      /**
69       * The SmtpConnector does not accept listeners, so the test in the superclass makes no
70       * sense here. Instead, we simply check that a listener is rejected.
71       */
72      // @Override
73      public void testConnectorListenerSupport() throws Exception
74      {
75          UMOConnector connector = getConnector();
76          assertNotNull(connector);
77          MuleDescriptor d = getTestDescriptor("anApple", Apple.class.getName());
78          UMOModel model = getModel();
79          UMOComponent component = model.registerComponent(d);
80          UMOEndpoint endpoint = new MuleEndpoint("test", new MuleEndpointURI(getTestEndpointURI()), connector,
81              null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, new HashMap());
82          try
83          {
84              connector.registerListener(component, endpoint);
85              fail("SMTP connector does not accept listeners");
86          }
87          catch (Exception e)
88          {
89              assertNotNull("expected", e);
90          }
91      }
92  
93      public void testSend() throws Exception
94      {
95          repeatTest("doTestSend");
96      }
97  
98      public void doTestSend() throws Exception
99      {
100         HashMap props = new HashMap();
101 
102         QuickConfigurationBuilder builder = new QuickConfigurationBuilder();
103         builder.getManager().registerConnector(createConnector(false));
104         UMOEndpoint endpoint = new MuleEndpoint(getTestEndpointURI(), false);
105         builder.registerComponent(FunctionalTestComponent.class.getName(), "testComponent", null, endpoint,
106             props);
107 
108         logger.debug("starting mule");
109         MuleManager.getInstance().start();
110 
111         // default transformer is string to mail message, so send string
112         UMOMessage message = new MuleMessage(AbstractGreenMailSupport.MESSAGE);
113         message.setStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, TO);
114         UMOSession session = getTestSession(getTestComponent(getTestDescriptor("apple", Apple.class.getName())));
115         MuleEvent event = new MuleEvent(message, endpoint, session, true,
116             new ResponseOutputStream(System.out));
117         endpoint.dispatch(event);
118 
119         getServers().waitForIncomingEmail(DELIVERY_DELAY_MS, 1);
120         MimeMessage[] messages = getServers().getReceivedMessages();
121         assertNotNull("did not receive any messages", messages);
122         assertEquals("did not receive 1 mail", 1, messages.length);
123         assertMessageOk(messages[0]);
124     }
125 
126     // MULE-2130 (Impossible to re-initialise SMTP connector)
127     public void testConnectorReinitialise() throws Exception
128     {
129         UMOConnector c = getConnector();
130 
131         c.startConnector();
132         assertTrue(c.isStarted());
133 
134         c.stopConnector();
135         assertFalse(c.isStarted());
136 
137         // The test provided in the JIRA calls dispose(), after which the connector is
138         // considered dead forever. Calling initialise() itself should be enough to zap
139         // the internal state.
140 
141         c.initialise();
142         assertFalse(c.isDisposed());
143         assertFalse(c.isStarted());
144 
145         c.startConnector();
146         assertFalse(c.isDisposed());
147         assertTrue(c.isStarted());
148     }
149 
150 }