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.assertEquals;
10  import static org.junit.Assert.assertFalse;
11  import static org.junit.Assert.assertNotNull;
12  import static org.junit.Assert.assertTrue;
13  
14  import org.mule.api.transport.MessageReceiver;
15  import org.mule.construct.AbstractFlowConstruct;
16  import org.mule.tck.junit4.FunctionalTestCase;
17  import org.mule.transport.AbstractConnector;
18  
19  import org.junit.Test;
20  
21  public class PatternFlowStateTestCase extends FunctionalTestCase
22  {
23  
24      @Override
25      protected String getConfigResources()
26      {
27          return "org/mule/test/components/pattern-flows-initial-state.xml";
28      }
29  
30      @Test
31      public void testDefaultInitialstate() throws Exception
32      {
33          doTestStarted("defaultSimpleService", "in.simple.default");
34      }
35  
36      @Test
37      public void testStartedInitialstate() throws Exception
38      {
39          doTestStarted("startedSimpleService", "in.simple.started");
40      }
41  
42      @Test
43      public void testInitialStateStopped() throws Exception
44      {
45          doTestStopped("stoppedSimpleService", "in.simple.stopped");
46      }
47  
48      @Test
49      public void testBridgeDefaultInitialstate() throws Exception
50      {
51          doTestStarted("defaultBridge", "in.bridge.default");
52      }
53  
54      @Test
55      public void testBridgeStartedInitialstate() throws Exception
56      {
57          doTestStarted("startedBridge", "in.bridge.started");
58      }
59  
60      @Test
61      public void testBridgeInitialStateStopped() throws Exception
62      {
63          doTestStopped("stoppedBridge", "in.bridge.stopped");
64      }
65  
66      @Test
67      public void testValidatorDefaultInitialstate() throws Exception
68      {
69          doTestStarted("defaultValidator", "in.validator.default");
70      }
71  
72      @Test
73      public void testValidatorStartedInitialstate() throws Exception
74      {
75          doTestStarted("startedValidator", "in.validator.started");
76      }
77  
78      @Test
79      public void testValidatorInitialStateStopped() throws Exception
80      {
81          doTestStopped("stoppedValidator", "in.validator.stopped");
82      }
83  
84      protected void doTestStarted(String flowName, String endpointName) throws Exception
85      {
86          AbstractFlowConstruct f = (AbstractFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
87              flowName);
88          // Flow initially started
89          assertTrue(f.isStarted());
90          assertFalse(f.isStopped());
91  
92          // The listeners should be registered and started.
93          doListenerTests(endpointName, 1, true);
94      }
95  
96      public void doTestStopped(String flowName, String endpointName) throws Exception
97      {
98          AbstractFlowConstruct f = (AbstractFlowConstruct) muleContext.getRegistry().lookupFlowConstruct(
99              flowName);
100         assertEquals("stopped", f.getInitialState());
101         // Flow initially stopped
102         assertFalse(f.isStarted());
103         assertTrue(f.isStopped());
104 
105         // The connector should be started, but with no listeners registered.
106         doListenerTests(endpointName, 0, true);
107 
108         f.start();
109         assertTrue(f.isStarted());
110         assertFalse(f.isStopped());
111 
112         // The listeners should now be registered and started.
113         doListenerTests(endpointName, 1, true);
114     }
115 
116     protected void doListenerTests(String receiverName, int expectedCount, boolean isConnected)
117     {
118         AbstractConnector connector = (AbstractConnector) muleContext.getRegistry().lookupConnector(
119             "connector.test.mule.default");
120         assertNotNull(connector);
121         assertTrue(connector.isStarted());
122         MessageReceiver[] receivers = connector.getReceivers("*" + receiverName + "*");
123         assertEquals(expectedCount, receivers.length);
124         for (int i = 0; i < expectedCount; i++)
125         {
126             assertEquals(isConnected, receivers[0].isConnected());
127         }
128     }
129 
130 }