View Javadoc

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