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 TransientLifecycleTrackerComponentFunctionalTestCase 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       */
39      @Test
40      public void testSpringBeanServiceLifecycle() throws Exception
41      {
42          testComponentLifecycle("SpringBeanService",
43              "[setProperty, setMuleContext, springInitialize, setService, start, stop, springDestroy]");
44      }
45  
46      /**
47       * ASSERT:
48       * - Mule stop/start lifecycle methods invoked
49       * - Mule initialize/dipose lifecycle methods NOT invoked
50       * - Spring lifecycle methods NOT invoked
51       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
52       * NOTE: muleContext is injected twice, once by registry and once by lifecycleAdaptor
53       */
54      @Test
55      public void testSpringBeanService2Lifecycle() throws Exception
56      {
57          testComponentLifecycle("SpringBeanService2",
58              "[setProperty, setMuleContext, setService, start, stop]");
59      }
60  
61      /**
62       * ASSERT:
63       * - Mule lifecycle methods invoked
64       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
65       */
66      @Test
67      public void testSingletonServiceLifecycle() throws Exception
68      {
69          testComponentLifecycle("MuleSingletonService",
70              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
71      }
72  
73      /**
74       * ASSERT:
75       * - Mule lifecycle methods invoked
76       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
77       */
78      @Test
79      public void testMulePrototypeServiceLifecycle() throws Exception
80      {
81          testComponentLifecycle("MulePrototypeService",
82              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
83      }
84  
85      /**
86       * ASSERT:
87       * - Mule lifecycle methods invoked
88       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
89       */
90      @Test
91      public void testMulePooledPrototypeServiceLifecycle() throws Exception
92      {
93          testComponentLifecycle("MulePooledPrototypeService", 
94              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
95      }
96  
97      /**
98       * ASSERT:
99       * - Mule lifecycle methods invoked each time singleton is used to create new object in pool
100      * NOTE: injecting service/muleContext only once since this is a pooled singleton
101      */
102     @Test
103     public void testMulePooledSingletonServiceLifecycle() throws Exception
104     {
105         testComponentLifecycle("MulePooledSingletonService", 
106             "[setProperty, setService, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]");
107     }
108 
109     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
110         throws Exception
111     {
112         AbstractLifecycleTracker tracker = exerciseComponent(serviceName);
113 
114         muleContext.dispose();
115 
116         assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
117     }
118 
119     private AbstractLifecycleTracker exerciseComponent(final String serviceName) throws Exception
120     {
121         MuleClient muleClient = new MuleClient(muleContext);
122         final AbstractLifecycleTracker ltc = (AbstractLifecycleTracker) muleClient.send(
123             "vm://" + serviceName + ".In", null, null).getPayload();
124 
125         assertNotNull(ltc);
126 
127         return ltc;
128     }
129 }