View Javadoc

1   /*
2    * $Id: TransientLifecycleTrackerComponentFunctionalTestCase.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 TransientLifecycleTrackerComponentFunctionalTestCase 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 TransientLifecycleTrackerComponentFunctionalTestCase(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      @Test
54      public void testSpringBeanServiceLifecycle() throws Exception
55      {
56          String expectedLifeCycle;
57  
58          if (variant.equals(ConfigVariant.FLOW))
59          {
60              expectedLifeCycle = "[setProperty, setMuleContext, springInitialize, start, stop, springDestroy]";
61          }
62          else
63          {
64              expectedLifeCycle = "[setProperty, setMuleContext, springInitialize, setService, start, stop, springDestroy]";
65          }
66  
67          testComponentLifecycle("SpringBeanService", expectedLifeCycle);
68      }
69  
70      /**
71       * ASSERT: - Mule stop/start lifecycle methods invoked - Mule initialize/dipose
72       * lifecycle methods NOT invoked - Spring lifecycle methods NOT invoked - Service
73       * and muleContext injected (Component implements ServiceAware/MuleContextAware)
74       * NOTE: muleContext is injected twice, once by registry and once by
75       * lifecycleAdaptor
76       */
77      @Test
78      public void testSpringBeanService2Lifecycle() throws Exception
79      {
80          String expectedLifeCycle;
81  
82          if (variant.equals(ConfigVariant.FLOW))
83          {
84              expectedLifeCycle = "[setProperty, setMuleContext, start, stop]";
85          }
86          else
87          {
88              expectedLifeCycle = "[setProperty, setMuleContext, setService, start, stop]";
89          }
90  
91          testComponentLifecycle("SpringBeanService2", expectedLifeCycle);
92      }
93  
94      /**
95       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
96       * (Component implements ServiceAware/MuleContextAware)
97       */
98      @Test
99      public void testSingletonServiceLifecycle() throws Exception
100     {
101         String expectedLifeCycle;
102 
103         if (variant.equals(ConfigVariant.FLOW))
104         {
105             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
106         }
107         else
108         {
109             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
110         }
111 
112         testComponentLifecycle("MuleSingletonService", expectedLifeCycle);
113     }
114 
115     /**
116      * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
117      * (Component implements ServiceAware/MuleContextAware)
118      */
119     @Test
120     public void testMulePrototypeServiceLifecycle() throws Exception
121     {
122         String expectedLifeCycle;
123 
124         if (variant.equals(ConfigVariant.FLOW))
125         {
126             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
127         }
128         else
129         {
130             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
131         }
132 
133         testComponentLifecycle("MulePrototypeService", expectedLifeCycle);
134     }
135 
136     /**
137      * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
138      * (Component implements ServiceAware/MuleContextAware)
139      */
140     @Test
141     public void testMulePooledPrototypeServiceLifecycle() throws Exception
142     {
143         String expectedLifeCycle;
144 
145         if (variant.equals(ConfigVariant.FLOW))
146         {
147             expectedLifeCycle = "[setProperty, setMuleContext, initialise, start, stop, dispose]";
148         }
149         else
150         {
151             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, start, stop, dispose]";
152         }
153 
154         testComponentLifecycle("MulePooledPrototypeService", expectedLifeCycle);
155     }
156 
157     /**
158      * ASSERT: - Mule lifecycle methods invoked each time singleton is used to create
159      * new object in pool NOTE: injecting service/muleContext only once since this is
160      * a pooled singleton
161      */
162     @Test
163     public void testMulePooledSingletonServiceLifecycle() throws Exception
164     {
165         String expectedLifeCycle;
166 
167         if (variant.equals(ConfigVariant.FLOW))
168         {
169             expectedLifeCycle = "[setProperty, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]";
170         }
171         else
172         {
173             expectedLifeCycle = "[setProperty, setService, setMuleContext, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]";
174         }
175 
176         testComponentLifecycle("MulePooledSingletonService", expectedLifeCycle);
177     }
178 
179     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
180         throws Exception
181     {
182         AbstractLifecycleTracker tracker = exerciseComponent(serviceName);
183 
184         muleContext.dispose();
185 
186         assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
187     }
188 
189     private AbstractLifecycleTracker exerciseComponent(final String serviceName) throws Exception
190     {
191         MuleClient muleClient = new MuleClient(muleContext);
192         final AbstractLifecycleTracker ltc = (AbstractLifecycleTracker) muleClient.send(
193             "vm://" + serviceName + ".In", null, null).getPayload();
194 
195         assertNotNull(ltc);
196 
197         return ltc;
198     }
199 }