1   /*
2    * $Id: PollingReceiversRestartTestCase.java 7963 2007-08-21 08:53:15Z 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  package org.mule.providers.http.functional;
11  
12  import org.mule.MuleManager;
13  import org.mule.MuleServer;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.tck.functional.EventCallback;
16  import org.mule.tck.functional.FunctionalTestComponent;
17  import org.mule.umo.UMOEventContext;
18  import org.mule.umo.manager.UMOManager;
19  import org.mule.umo.model.UMOModel;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
22  
23  public class PollingReceiversRestartTestCase extends AbstractMuleTestCase
24  {
25      private static final int WAIT_TIME = 2500;
26  
27      public void testPollingReceiversRestart() throws Exception
28      {
29          // we are going to stop and start Mule in this thread, make it
30          // a daemon so the test can exit properly
31          MuleServer mule = new MuleServer("polling-receivers-restart-test.xml");
32          mule.start(true);
33  
34          // well, no way to register notification on the MuleServer, only
35          // possible for MuleManager, so just sleep
36          Thread.sleep(WAIT_TIME);
37  
38          UMOManager manager = MuleManager.getInstance();
39  
40          UMOModel model = (UMOModel) MuleManager.getInstance().getModels().get("PollingHttpRestart");
41          FunctionalTestComponent ftc = (FunctionalTestComponent) model.getComponent("Test").getInstance();
42          assertNotNull("Functional Test Component not found in the model.", ftc);
43  
44          AtomicInteger pollCounter = new AtomicInteger(0);
45          ftc.setEventCallback(new PollingEventCallback(pollCounter));
46  
47          // should be enough to poll for 2 messages
48          Thread.sleep(WAIT_TIME);
49  
50          // stop
51          manager.stop();
52          assertTrue("No polls performed", pollCounter.get() > 0);
53  
54          // and restart
55          manager.start();
56  
57          pollCounter.set(0);
58          ftc.setEventCallback(new PollingEventCallback(pollCounter));
59  
60          Thread.sleep(WAIT_TIME);
61          manager.dispose();
62          assertTrue("No polls performed", pollCounter.get() > 0);
63      }
64  
65      private static class PollingEventCallback implements EventCallback
66      {
67          private final AtomicInteger pollCounter;
68  
69          public PollingEventCallback(final AtomicInteger pollCounter)
70          {
71              this.pollCounter = pollCounter;
72          }
73  
74          public void eventReceived(final UMOEventContext context, final Object component) throws Exception
75          {
76              pollCounter.incrementAndGet();
77              String message = context.getMessageAsString();
78              assertEquals("Here's your content.", message);
79          }
80      }
81  }
82