1   /*
2    * $Id: AbstractReceivingMailConnectorTestCase.java 10961 2008-02-22 19:01:02Z dfeist $
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.api.MuleEventContext;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.routing.InboundRouterCollection;
17  import org.mule.api.service.Service;
18  import org.mule.routing.inbound.DefaultInboundRouterCollection;
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, int port)
41      {
42          super(SEND_INITIAL_EMAIL, protocol, port);
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().getOrginalPayload());
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          InboundEndpoint ep = 
72              muleContext.getRegistry().lookupEndpointFactory()
73                  .getInboundEndpoint(getTestEndpointURI());
74          InboundRouterCollection inboundRouter = new DefaultInboundRouterCollection();
75          inboundRouter.addEndpoint(ep);
76          service.setInboundRouter(inboundRouter);
77          muleContext.getRegistry().registerService(service);
78          //muleContext.applyLifecycle(service);
79          if (!muleContext.isStarted())
80          {
81              muleContext.start();
82          }
83  
84          logger.debug("waiting for count down");
85          assertTrue(countDown.await(WAIT_PERIOD_MS, TimeUnit.MILLISECONDS));
86      }
87  
88      protected static Map newEmailToStringServiceOverrides()
89      {
90          Map serviceOverrides = new HashMap();
91          serviceOverrides.put(MuleProperties.CONNECTOR_INBOUND_TRANSFORMER,
92                  EmailMessageToString.class.getName());
93          return serviceOverrides;
94      }
95  
96  }