View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.components;
8   
9   import static org.junit.Assert.*;
10  
11  import org.mule.api.transport.MessageReceiver;
12  import org.mule.construct.AbstractFlowConstruct;
13  import org.mule.tck.junit4.FunctionalTestCase;
14  import org.mule.transport.AbstractConnector;
15  
16  import org.junit.Test;
17  
18  public class FlowStateTestCase extends FunctionalTestCase
19  {
20  
21      @Override
22      protected String getConfigResources()
23      {
24          return "org/mule/test/components/flow-initial-state.xml";
25      }
26  
27      @Test
28      public void testDefaultInitialstate() throws Exception
29      {
30          doTestStarted("default");
31      }
32  
33      @Test
34      public void testStartedInitialstate() throws Exception
35      {
36          doTestStarted("started");
37      }
38  
39      protected void doTestStarted(String flowName) throws Exception
40      {
41          AbstractFlowConstruct f = (AbstractFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
42              flowName + "Flow");
43          // Flow initially started
44          assertTrue(f.isStarted());
45          assertFalse(f.isStopped());
46  
47          // The listeners should be registered and started.
48          doListenerTests(flowName, 1, true);
49      }
50  
51      @Test
52      public void testInitialStateStopped() throws Exception
53      {
54          AbstractFlowConstruct f = (AbstractFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
55              "stoppedFlow");
56          assertEquals("stopped", f.getInitialState());
57          // Flow initially stopped
58          assertFalse(f.isStarted());
59          assertTrue(f.isStopped());
60  
61          // The connector should be started, but with no listeners registered.
62          doListenerTests("stopped", 0, true);
63  
64          f.start();
65          assertTrue(f.isStarted());
66          assertFalse(f.isStopped());
67  
68          // The listeners should now be registered and started.
69          doListenerTests("stopped", 1, true);
70      }
71  
72      protected void doListenerTests(String receiverName, int expectedCount, boolean isConnected)
73      {
74          AbstractConnector connector = (AbstractConnector) muleContext.getRegistry().lookupConnector(
75              "connector.test.mule.default");
76          assertNotNull(connector);
77          assertTrue(connector.isStarted());
78          MessageReceiver[] receivers = connector.getReceivers("*" + receiverName + "*");
79          assertEquals(expectedCount, receivers.length);
80          for (int i = 0; i < expectedCount; i++)
81          {
82              assertEquals(isConnected, receivers[0].isConnected());
83          }
84      }
85  
86  }