View Javadoc

1   /*
2    * $Id: LifecycleTrackerComponentFunctionalTestCase.java 22421 2011-07-15 05:05:06Z dirk.olmes $
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.AbstractServiceAndFlowTestCase;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  
20  import org.junit.Test;
21  import org.junit.runners.Parameterized.Parameters;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  /**
27   * @author David Dossot (david@dossot.net) See
28   *         http://mule.mulesoft.org/jira/browse/MULE-3846
29   */
30  public class LifecycleTrackerComponentFunctionalTestCase extends AbstractServiceAndFlowTestCase
31  {
32      @Parameters
33      public static Collection<Object[]> parameters()
34      {
35          return Arrays.asList(new Object[][]{
36              {ConfigVariant.SERVICE,
37                  "org/mule/test/integration/components/component-lifecycle-config-service.xml"},
38              {ConfigVariant.FLOW, "org/mule/test/integration/components/component-lifecycle-config-flow.xml"}});
39      }
40  
41      public LifecycleTrackerComponentFunctionalTestCase(ConfigVariant variant, String configResources)
42      {
43          super(variant, configResources);
44      }
45  
46      /**
47       * ASSERT: - Mule stop/start lifecycle methods invoked - Mule initialize/dipose
48       * lifecycle methods NOT invoked - Spring lifecycle methods invoked - Service and
49       * muleContext injected (Component implements ServiceAware/MuleContextAware)
50       * NOTE: muleContext is injected twice, once by registry and once by
51       * lifecycleAdaptor
52       *
53       * @throws Exception
54       */
55      @Test
56      public void testSpringBeanServiceLifecycle() throws Exception
57      {
58          String expectedLifeCycle;
59  
60          if (variant.equals(ConfigVariant.FLOW))
61          {
62              expectedLifeCycle = "[setProperty, setMuleContext, springInitialize, start, stop, springDestroy]";
63          }
64          else
65          {
66              expectedLifeCycle = "[setProperty, setMuleContext, springInitialize, setService, start, stop, springDestroy]";
67          }
68  
69          testComponentLifecycle("SpringBeanService", expectedLifeCycle);
70      }
71  
72      /**
73       * ASSERT: - Mule stop/start lifecycle methods invoked - Mule initialize/dipose
74       * lifecycle methods NOT invoked - Spring lifecycle methods NOT invoked - Service
75       * and muleContext injected (Component implements ServiceAware/MuleContextAware)
76       * NOTE: muleContext is injected twice, once by registry and once by
77       * lifecycleAdaptor
78       *
79       * @throws Exception
80       */
81      @Test
82      public void testSpringBeanService2Lifecycle() throws Exception
83      {
84          String expectedLifeCycle;
85  
86          if (variant.equals(ConfigVariant.FLOW))
87          {
88              expectedLifeCycle = "[setProperty, setMuleContext, start, stop]";
89          }
90          else
91          {
92              expectedLifeCycle = "[setProperty, setMuleContext, setService, start, stop]";
93          }
94  
95          testComponentLifecycle("SpringBeanService2", expectedLifeCycle);
96      }
97  
98      /**
99       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
100      * (Component implements ServiceAware/MuleContextAware)
101      *
102      * @throws Exception
103      */
104     @Test
105     public void testSingletonServiceLifecycle() throws Exception
106     {
107         String expectedLifeCycle;
108 
109         if (variant.equals(ConfigVariant.FLOW))
110         {
111             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
112         }
113         else
114         {
115             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
116         }
117 
118         testComponentLifecycle("MuleSingletonService", expectedLifeCycle);
119     }
120 
121     /**
122      * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
123      * (Component implements ServiceAware/MuleContextAware)
124      *
125      * @throws Exception
126      */
127     @Test
128     public void testMulePrototypeServiceLifecycle() throws Exception
129     {
130         String expectedLifeCycle;
131 
132         if (variant.equals(ConfigVariant.FLOW))
133         {
134             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
135         }
136         else
137         {
138             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
139         }
140 
141         testComponentLifecycle("MulePrototypeService", expectedLifeCycle);
142     }
143 
144     /**
145      * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
146      * (Component implements ServiceAware/MuleContextAware)
147      *
148      * @throws Exception
149      */
150     @Test
151     public void testMulePooledPrototypeServiceLifecycle() throws Exception
152     {
153         String expectedLifeCycle;
154 
155         if (variant.equals(ConfigVariant.FLOW))
156         {
157             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
158         }
159         else
160         {
161             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
162         }
163 
164         testComponentLifecycle("MulePooledPrototypeService", expectedLifeCycle);
165     }
166 
167     /**
168      * ASSERT: - Mule lifecycle methods invoked each time singleton is used to create
169      * new object in pool - Service and muleContext injected each time singleton is
170      * used to create new object in pool (Component implements
171      * ServiceAware/MuleContextAware)
172      *
173      * @throws Exception
174      */
175     @Test
176     public void testMulePooledSingletonServiceLifecycle() throws Exception
177     {
178         String expectedLifeCycle;
179 
180         if (variant.equals(ConfigVariant.FLOW))
181         {
182             expectedLifeCycle = "[setProperty, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]";
183         }
184         else
185         {
186             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]";
187         }
188 
189         testComponentLifecycle("MulePooledSingletonService", expectedLifeCycle);
190     }
191 
192     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
193         throws Exception
194     {
195 
196         final AbstractLifecycleTracker tracker = exerciseComponent(serviceName);
197 
198         muleContext.dispose();
199 
200         assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
201     }
202 
203     private AbstractLifecycleTracker exerciseComponent(final String serviceName) throws Exception
204     {
205         MuleClient muleClient = new MuleClient(muleContext);
206         final AbstractLifecycleTracker ltc = (AbstractLifecycleTracker) muleClient.send(
207             "vm://" + serviceName + ".In", null, null).getPayload();
208 
209         assertNotNull(ltc);
210 
211         return ltc;
212     }
213 }