View Javadoc

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