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.integration.components;
8   
9   import org.mule.lifecycle.AbstractLifecycleTracker;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.junit4.FunctionalTestCase;
12  
13  import org.junit.Test;
14  
15  import static org.junit.Assert.assertEquals;
16  import static org.junit.Assert.assertNotNull;
17  
18  /**
19   * @author David Dossot (david@dossot.net)
20   * See http://mule.mulesoft.org/jira/browse/MULE-3846
21   */
22  public class LifecycleTrackerComponentFunctionalTestCase extends FunctionalTestCase
23  {
24  
25      @Override
26      protected String getConfigResources()
27      {
28          return "org/mule/test/integration/components/component-lifecycle-config.xml";
29      }
30  
31      /**
32       * ASSERT:
33       * - Mule stop/start lifecycle methods invoked
34       * - Mule initialize/dipose lifecycle methods NOT invoked
35       * - Spring lifecycle methods invoked
36       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
37       * NOTE: muleContext is injected twice, once by registry and once by lifecycleAdaptor
38       * @throws Exception
39       */
40      @Test
41      public void testSpringBeanServiceLifecycle() throws Exception
42      {
43          testComponentLifecycle(
44              "SpringBeanService",
45              "[setProperty, setMuleContext, springInitialize, setService, start, stop, springDestroy]");
46      }
47  
48      /**
49       * ASSERT:
50       * - Mule stop/start lifecycle methods invoked
51       * - Mule initialize/dipose lifecycle methods NOT invoked
52       * - Spring lifecycle methods NOT invoked
53       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
54       * NOTE: muleContext is injected twice, once by registry and once by lifecycleAdaptor
55       * @throws Exception
56       */
57      @Test
58      public void testSpringBeanService2Lifecycle() throws Exception
59      {
60          testComponentLifecycle(
61              "SpringBeanService2",
62              "[setProperty, setMuleContext, setService, start, stop]");
63      }
64      
65      /**
66       * ASSERT:
67       * - Mule lifecycle methods invoked
68       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
69       * @throws Exception
70       */
71      @Test
72      public void testSingletonServiceLifecycle() throws Exception
73      {
74          testComponentLifecycle("MuleSingletonService",
75              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
76      }
77  
78      /**
79       * ASSERT:
80       * - Mule lifecycle methods invoked
81       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
82       * @throws Exception
83       */
84      @Test
85      public void testMulePrototypeServiceLifecycle() throws Exception
86      {
87          testComponentLifecycle("MulePrototypeService",
88              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
89      }
90  
91      /**
92       * ASSERT:
93       * - Mule lifecycle methods invoked
94       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
95       * @throws Exception
96       */
97      @Test
98      public void testMulePooledPrototypeServiceLifecycle() throws Exception
99      {
100         testComponentLifecycle("MulePooledPrototypeService", "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
101     }
102     
103     /**
104      * ASSERT:
105      * - Mule lifecycle methods invoked each time singleton is used to create new object in pool
106      * - Service and muleContext injected each time singleton is used to create new object in pool (Component implements ServiceAware/MuleContextAware)
107      * @throws Exception
108      */
109     @Test
110     public void testMulePooledSingletonServiceLifecycle() throws Exception
111     {
112         testComponentLifecycle("MulePooledSingletonService", "[setProperty, setService, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]");
113     }
114 
115     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
116         throws Exception
117     {
118 
119         final AbstractLifecycleTracker tracker = exerciseComponent(serviceName);
120 
121         muleContext.dispose();
122 
123         assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
124     }
125 
126     private AbstractLifecycleTracker exerciseComponent(final String serviceName) throws Exception
127     {
128         MuleClient muleClient = new MuleClient(muleContext);
129         final AbstractLifecycleTracker ltc = (AbstractLifecycleTracker) muleClient.send(
130             "vm://" + serviceName + ".In", null, null).getPayload();
131 
132         assertNotNull(ltc);
133 
134         return ltc;
135     }
136 }