1   /*
2    * $Id: AbstractReceivingMailConnectorTestCase.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.tck.functional.EventCallback;
16  import org.mule.tck.functional.FunctionalTestComponent;
17  import org.mule.umo.UMOEventContext;
18  
19  import java.util.HashMap;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
22  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
23  
24  /**
25   * Given an endpoint ({@link #getTestEndpointURI()}) this waits for up to 10 seconds,
26   * hoping to receive the message stored in the mail server.  It also runs the unit tests
27   * defined way down in {@link org.mule.tck.providers.AbstractConnectorTestCase}.
28   */
29  public abstract class AbstractReceivingMailConnectorTestCase extends AbstractMailConnectorFunctionalTestCase
30  {
31  
32      public static final int POLL_PERIOD_MS = 1000; 
33      public static final int WAIT_PERIOD_MS = 3 * POLL_PERIOD_MS;
34  
35      protected AbstractReceivingMailConnectorTestCase(String connectorName)
36      {
37          super(true, connectorName);
38      }
39  
40      public void testReceiver() throws Exception
41      {
42          repeatTest("doTestReceiver");
43      }
44  
45      public void doTestReceiver() 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(UMOEventContext 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().getPayload());
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          QuickConfigurationBuilder builder = new QuickConfigurationBuilder();
71          builder.getManager().registerConnector(getConnector(false));
72          builder.registerComponent(
73              FunctionalTestComponent.class.getName(), "testComponent", getTestEndpointURI(), null, props);
74  
75          logger.debug("starting mule");
76          MuleManager.getInstance().start();
77  
78          logger.debug("waiting for count down");
79          assertTrue(countDown.await(WAIT_PERIOD_MS, TimeUnit.MILLISECONDS));
80      }
81  
82  }