1   /*
2    * $Id: SmtpConnectorTestCase.java 7976 2007-08-21 14:26:13Z 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.provider.UMOConnector;
28  
29  import java.util.HashMap;
30  
31  import javax.mail.internet.MimeMessage;
32  
33  /**
34   * Send a message via SMTP to a (greenmail) server.
35   */
36  public class SmtpConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
37  {
38      
39      public static final long DELIVERY_DELAY_MS = 5000;
40      
41      public SmtpConnectorTestCase() 
42      {
43          this("SmtpConnector");
44      }
45  
46      SmtpConnectorTestCase(String connectorName) 
47      {
48          super(false, connectorName);
49      }
50  
51     public UMOConnector getConnector(boolean init) throws Exception
52      {
53          SmtpConnector c = new SmtpConnector();
54          c.setName(getConnectorName());
55          if (init)
56          {
57              c.initialise();
58          }
59          return c;
60      }
61  
62      public String getTestEndpointURI()
63      {
64          return getSmtpTestEndpointURI();
65      }
66  
67      /**
68       * The SmtpConnector does not accept listeners, so the test in the
69       * superclass makes no sense here.  Instead, we simply check that
70       * a listener is rejected.
71       */
72      // @Override
73      public void testConnectorListenerSupport() throws Exception
74      {
75          assertNotNull(connector);
76          MuleDescriptor d = getTestDescriptor("anApple", Apple.class.getName());
77          UMOComponent component = model.registerComponent(d);
78          UMOEndpoint endpoint = 
79              new MuleEndpoint("test", new MuleEndpointURI(getTestEndpointURI()), connector,
80                  null, UMOEndpoint.ENDPOINT_TYPE_SENDER, 0, null, new HashMap());
81          try
82          {
83              connector.registerListener(component, endpoint);
84              fail("SMTP connector does not accept listeners");
85          }
86          catch (Exception e)
87          {
88              assertNotNull("expected", e);
89          }
90      }
91  
92      public void testSend() throws Exception
93      {
94          repeatTest("doTestSend");
95      }
96  
97      public void doTestSend() throws Exception
98      {
99          HashMap props = new HashMap();
100 
101         QuickConfigurationBuilder builder = new QuickConfigurationBuilder();
102         builder.getManager().registerConnector(getConnector(false));
103         UMOEndpoint endpoint = new MuleEndpoint(getTestEndpointURI(), false);
104         builder.registerComponent(FunctionalTestComponent.class.getName(), 
105             "testComponent", null, endpoint, props);
106 
107         logger.debug("starting mule");
108         MuleManager.getInstance().start();
109 
110         // default transformer is string to mail message, so send string
111         UMOMessage message = new MuleMessage(AbstractGreenMailSupport.MESSAGE);
112         message.setStringProperty(MailProperties.TO_ADDRESSES_PROPERTY, TO);
113         UMOSession session = 
114             getTestSession(getTestComponent(getTestDescriptor("apple", Apple.class.getName())));
115         MuleEvent event = 
116             new MuleEvent(message, endpoint, session, true, 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 }