View Javadoc

1   /*
2    * $Id: LifecycleTrackerComponentFunctionalTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.test.integration.components;
12  
13  import org.mule.lifecycle.AbstractLifecycleTracker;
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.FunctionalTestCase;
16  
17  /**
18   * @author David Dossot (david@dossot.net)
19   * See http://mule.mulesoft.org/jira/browse/MULE-3846
20   */
21  public class LifecycleTrackerComponentFunctionalTestCase extends FunctionalTestCase
22  {
23  
24      @Override
25      protected String getConfigResources()
26      {
27          return "org/mule/test/integration/components/component-lifecycle-config.xml";
28      }
29  
30      /**
31       * ASSERT:
32       * - Mule stop/start lifecycle methods invoked
33       * - Mule initialize/dipose lifecycle methods NOT invoked
34       * - Spring lifecycle methods invoked
35       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
36       * NOTE: muleContext is injected twice, once by registry and once by lifecycleAdaptor
37       * @throws Exception
38       */
39      public void testSpringBeanServiceLifecycle() throws Exception
40      {
41          testComponentLifecycle(
42              "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       * @throws Exception
54       */
55      public void testSpringBeanService2Lifecycle() throws Exception
56      {
57          testComponentLifecycle(
58              "SpringBeanService2",
59              "[setProperty, setMuleContext, setService, start, stop]");
60      }
61      
62      /**
63       * ASSERT:
64       * - Mule lifecycle methods invoked
65       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
66       * @throws Exception
67       */
68      public void testSingletonServiceLifecycle() throws Exception
69      {
70          testComponentLifecycle("MuleSingletonService",
71              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
72      }
73  
74      /**
75       * ASSERT:
76       * - Mule lifecycle methods invoked
77       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
78       * @throws Exception
79       */
80      public void testMulePrototypeServiceLifecycle() throws Exception
81      {
82          testComponentLifecycle("MulePrototypeService",
83              "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]");
84      }
85  
86      /**
87       * ASSERT:
88       * - Mule lifecycle methods invoked
89       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
90       * @throws Exception
91       */
92      public void testMulePooledPrototypeServiceLifecycle() throws Exception
93      {
94          testComponentLifecycle("MulePooledPrototypeService", "[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      * - Service and muleContext injected each time singleton is used to create new object in pool (Component implements ServiceAware/MuleContextAware)
101      * @throws Exception
102      */
103     public void testMulePooledSingletonServiceLifecycle() throws Exception
104     {
105         testComponentLifecycle("MulePooledSingletonService", "[setProperty, setService, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]");
106     }
107 
108     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
109         throws Exception
110     {
111 
112         final 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 }