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.JSR250LifecycleTrackerComponent;
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  public class JSR250LifecycleTrackerComponentFunctionalTestCase extends FunctionalTestCase
19  {
20  
21      @Override
22      protected String getConfigResources()
23      {
24          return "org/mule/test/integration/components/jsr250-component-lifecycle-config.xml";
25      }
26  
27      /**
28       * ASSERT:
29       * - Mule lifecycle methods invoked
30       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
31       * @throws Exception
32       */
33      @Test
34      public void testSingletonServiceLifecycle() throws Exception
35      {
36          testComponentLifecycle("MuleSingletonService",
37              "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
38      }
39  
40      /**
41       * ASSERT:
42       * - Mule lifecycle methods invoked
43       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
44       * @throws Exception
45       */
46      @Test
47      public void testMulePrototypeServiceLifecycle() throws Exception
48      {
49          testComponentLifecycle("MulePrototypeService",
50              "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
51      }
52  
53      /**
54       * ASSERT:
55       * - Mule lifecycle methods invoked
56       * - Service and muleContext injected (Component implements ServiceAware/MuleContextAware)
57       * @throws Exception
58       */
59      @Test
60      public void testMulePooledPrototypeServiceLifecycle() throws Exception
61      {
62          testComponentLifecycle("MulePooledPrototypeService", "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
63      }
64      
65      /**
66       * ASSERT:
67       * - Mule lifecycle methods invoked each time singleton is used to create new object in pool
68       * - Service and muleContext injected each time singleton is used to create new object in pool (Component implements ServiceAware/MuleContextAware)
69       * @throws Exception
70       */
71      @Test
72      public void testMulePooledSingletonServiceLifecycle() throws Exception
73      {
74          testComponentLifecycle("MulePooledSingletonService", "[setProperty, setService, setMuleContext, jsr250 initialise, jsr250 initialise, jsr250 initialise, start, start, start, stop, stop, stop, jsr250 dispose, jsr250 dispose, jsr250 dispose]");
75      }
76  
77      private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
78          throws Exception
79      {
80  
81          final JSR250LifecycleTrackerComponent tracker = exerciseComponent(serviceName);
82  
83          muleContext.dispose();
84  
85          assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
86      }
87  
88      private JSR250LifecycleTrackerComponent exerciseComponent(final String serviceName) throws Exception
89      {
90          MuleClient muleClient = new MuleClient(muleContext);
91          final JSR250LifecycleTrackerComponent ltc = (JSR250LifecycleTrackerComponent) muleClient.send(
92              "vm://" + serviceName + ".In", null, null).getPayload();
93  
94          assertNotNull(ltc);
95  
96          return ltc;
97      }
98  }