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.registry;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleException;
11  import org.mule.construct.SimpleFlowConstruct;
12  import org.mule.tck.junit4.AbstractMuleContextTestCase;
13  import org.mule.tck.testmodels.mule.TestConnector;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertEquals;
18  
19  public class RegistryBrokerTestCase extends AbstractMuleContextTestCase
20  {
21  
22      private String tracker;
23  
24      @Override
25      protected void doSetUp() throws Exception
26      {
27          super.doSetUp();
28          tracker = new String();
29      }
30  
31      @Override
32      protected boolean isStartContext()
33      {
34          return false;
35      }
36  
37      @Test
38      public void testCrossRegistryLifecycleOrder() throws MuleException
39      {
40  
41          TransientRegistry reg1 = new TransientRegistry(muleContext);
42          reg1.initialise();
43          TransientRegistry reg2 = new TransientRegistry(muleContext);
44          reg2.initialise();
45  
46          reg1.registerObject("conn", new LifecycleTrackerConnector("conn", muleContext));
47          reg2.registerObject("conn2", new LifecycleTrackerConnector("conn2", muleContext));
48          reg1.registerObject("flow", new LifecycleTrackerFlow("flow", muleContext));
49          reg2.registerObject("flow2", new LifecycleTrackerFlow("flow2", muleContext));
50  
51          muleContext.addRegistry(reg1);
52          muleContext.addRegistry(reg2);
53  
54          muleContext.start();
55  
56          // Both connectors are started before either flow
57          assertEquals("conn2-start conn-start flow2-start flow-start ", tracker.toString());
58  
59          tracker = new String();
60          muleContext.stop();
61  
62          // Both services are stopped before either connector
63          assertEquals("flow2-stop flow-stop conn2-stop conn-stop ", tracker);
64      }
65  
66      class LifecycleTrackerConnector extends TestConnector
67      {
68  
69          public LifecycleTrackerConnector(String name, MuleContext context)
70          {
71              super(context);
72              this.name = name;
73          }
74  
75          @Override
76          protected void doStart()
77          {
78              super.doStart();
79              tracker += name + "-start ";
80          }
81  
82          @Override
83          protected void doStop()
84          {
85              super.doStop();
86              tracker += name + "-stop ";
87          }
88      }
89  
90      class LifecycleTrackerFlow extends SimpleFlowConstruct
91      {
92  
93          public LifecycleTrackerFlow(String name, MuleContext muleContext)
94          {
95              super(name, muleContext);
96          }
97  
98          @Override
99          protected void doStart() throws MuleException
100         {
101             super.doStart();
102             tracker += name + "-start ";
103         }
104 
105         @Override
106         protected void doStop() throws MuleException
107         {
108             super.doStop();
109             tracker += name + "-stop ";
110         }
111     }
112 
113 }