View Javadoc

1   /*
2    * $Id: JSR250LifecycleTrackerComponentFunctionalTestCase.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.JSR250LifecycleTrackerComponent;
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  public class JSR250LifecycleTrackerComponentFunctionalTestCase extends AbstractServiceAndFlowTestCase
27  {
28      @Parameters
29      public static Collection<Object[]> parameters()
30      {
31          return Arrays.asList(new Object[][]{
32              {ConfigVariant.SERVICE,
33                  "org/mule/test/integration/components/jsr250-component-lifecycle-config-service.xml"},
34              {ConfigVariant.FLOW,
35                  "org/mule/test/integration/components/jsr250-component-lifecycle-config-flow.xml"}});
36      }
37  
38      public JSR250LifecycleTrackerComponentFunctionalTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41      }
42  
43      /**
44       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
45       * (Component implements ServiceAware/MuleContextAware)
46       *
47       * @throws Exception
48       */
49      @Test
50      public void testSingletonServiceLifecycle() throws Exception
51      {
52          if (variant.equals(ConfigVariant.FLOW))
53          {
54              testComponentLifecycle("MuleSingletonService",
55                  "[setProperty, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
56          }
57          else
58          {
59              testComponentLifecycle("MuleSingletonService",
60                  "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
61          }
62      }
63  
64      /**
65       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
66       * (Component implements ServiceAware/MuleContextAware)
67       *
68       * @throws Exception
69       */
70      @Test
71      public void testMulePrototypeServiceLifecycle() throws Exception
72      {
73          if (variant.equals(ConfigVariant.FLOW))
74          {
75              testComponentLifecycle("MulePrototypeService",
76                  "[setProperty, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
77          }
78          else
79          {
80              testComponentLifecycle("MulePrototypeService",
81                  "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
82          }
83      }
84  
85      /**
86       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
87       * (Component implements ServiceAware/MuleContextAware)
88       *
89       * @throws Exception
90       */
91      @Test
92      public void testMulePooledPrototypeServiceLifecycle() throws Exception
93      {
94          if (variant.equals(ConfigVariant.FLOW))
95          {
96              testComponentLifecycle("MulePooledPrototypeService",
97                  "[setProperty, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
98          }
99          else
100         {
101             testComponentLifecycle("MulePooledPrototypeService",
102                 "[setProperty, setService, setMuleContext, jsr250 initialise, start, stop, jsr250 dispose]");
103         }
104     }
105 
106     /**
107      * ASSERT: - Mule lifecycle methods invoked each time singleton is used to create
108      * new object in pool - Service and muleContext injected each time singleton is
109      * used to create new object in pool (Component implements
110      * ServiceAware/MuleContextAware)
111      *
112      * @throws Exception
113      */
114     @Test
115     public void testMulePooledSingletonServiceLifecycle() throws Exception
116     {
117         if (variant.equals(ConfigVariant.FLOW))
118         {
119             testComponentLifecycle(
120                 "MulePooledSingletonService",
121                 "[setProperty, setMuleContext, jsr250 initialise, jsr250 initialise, jsr250 initialise, start, start, start, stop, stop, stop, jsr250 dispose, jsr250 dispose, jsr250 dispose]");
122         }
123         else
124         {
125             testComponentLifecycle(
126                 "MulePooledSingletonService",
127                 "[setProperty, setService, setMuleContext, jsr250 initialise, jsr250 initialise, jsr250 initialise, start, start, start, stop, stop, stop, jsr250 dispose, jsr250 dispose, jsr250 dispose]");
128         }
129     }
130 
131     private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
132         throws Exception
133     {
134 
135         final JSR250LifecycleTrackerComponent tracker = exerciseComponent(serviceName);
136 
137         muleContext.dispose();
138 
139         assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
140     }
141 
142     private JSR250LifecycleTrackerComponent exerciseComponent(final String serviceName) throws Exception
143     {
144         MuleClient muleClient = new MuleClient(muleContext);
145         final JSR250LifecycleTrackerComponent ltc = (JSR250LifecycleTrackerComponent) muleClient.send(
146             "vm://" + serviceName + ".In", null, null).getPayload();
147 
148         assertNotNull(ltc);
149 
150         return ltc;
151     }
152 }