View Javadoc

1   /*
2    * $Id: AbstractReceivingMailConnectorTestCase.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.api.MuleEventContext;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.endpoint.EndpointBuilder;
16  import org.mule.api.endpoint.InboundEndpoint;
17  import org.mule.api.service.Service;
18  import org.mule.api.source.CompositeMessageSource;
19  import org.mule.tck.MuleTestUtils;
20  import org.mule.tck.functional.EventCallback;
21  import org.mule.tck.functional.FunctionalTestComponent;
22  import org.mule.transport.email.transformers.EmailMessageToString;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
28  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
29  
30  /**
31   * Given an endpoint ({@link #getTestEndpointURI()}) this waits for up to 10 seconds,
32   * hoping to receive the message stored in the mail server.  It also runs the unit tests
33   * defined way down in {@link org.mule.transport.AbstractConnectorTestCase}.
34   */
35  public abstract class AbstractReceivingMailConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
36  {
37      public static final int POLL_PERIOD_MS = 2000;
38      public static final int WAIT_PERIOD_MS = 4 * POLL_PERIOD_MS;
39  
40      protected AbstractReceivingMailConnectorTestCase(String protocol)
41      {
42          super(SEND_INITIAL_EMAIL, protocol);
43      }
44  
45      public void testReceiver() throws Exception
46      {
47          final CountDownLatch countDown = new CountDownLatch(1);
48  
49          HashMap props = new HashMap();
50          props.put("eventCallback", new EventCallback()
51          {
52              public synchronized void eventReceived(MuleEventContext context, Object component)
53              {
54                  try 
55                  {
56                      logger.debug("woot - event received");
57                      logger.debug("context: " + context);
58                      logger.debug("component: " + component);
59                      assertMessageOk(context.getMessage().getOriginalPayload());
60                      countDown.countDown();
61                  } 
62                  catch (Exception e) 
63                  {
64                      // counter will not be incremented
65                      logger.error(e.getMessage(), e);
66                  }
67              }
68          });
69  
70          Service service = MuleTestUtils.getTestService(uniqueName("testComponent"), FunctionalTestComponent.class, props, muleContext, /*initialize*/false);
71          EndpointBuilder eb = muleContext.getEndpointFactory().getEndpointBuilder(getTestEndpointURI());
72          eb.setDisableTransportTransformer(true);
73          InboundEndpoint ep = eb.buildInboundEndpoint();
74          ((CompositeMessageSource) service.getMessageSource()).addSource(ep);
75          muleContext.getRegistry().registerService(service);
76          if (!muleContext.isStarted())
77          {
78              muleContext.start();
79          }
80  
81          logger.debug("waiting for count down");
82          assertTrue(countDown.await(WAIT_PERIOD_MS, TimeUnit.MILLISECONDS));
83      }    
84  
85      protected static Map newEmailToStringServiceOverrides()
86      {
87          Map serviceOverrides = new HashMap();
88          serviceOverrides.put(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER,
89                  EmailMessageToString.class.getName());
90          return serviceOverrides;
91      }
92  
93  }