View Javadoc

1   /*
2    * $Id: GuiceLifecyceTestCase.java 20321 2010-11-24 15:21:24Z dfeist $
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.module.guice;
12  
13  import org.mule.module.client.MuleClient;
14  import org.mule.registry.AbstractLifecycleTracker;
15  import org.mule.tck.AbstractMuleTestCase;
16  
17  import com.google.inject.AbstractModule;
18  
19  import org.junit.Assert;
20  
21  public class GuiceLifecyceTestCase extends AbstractMuleTestCase
22  {
23      @Override
24      protected void doSetUp() throws Exception
25      {
26          GuiceConfigurationBuilder cb = new GuiceConfigurationBuilder(new GuiceLifecycleModule());
27          cb.configure(muleContext);
28      }
29  
30      /**
31       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
32       * (Component implements ServiceAware/MuleContextAware)
33       * 
34       * @throws Exception
35       */
36      public void testSingletonServiceLifecycle() throws Exception
37      {
38          testComponentLifecycle("MuleSingletonService",
39              "[setProperty, setMuleContext, setService, initialise, start, stop, dispose]");
40      }
41  
42      /**
43       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
44       * (Component implements ServiceAware/MuleContextAware)
45       * 
46       * @throws Exception
47       */
48      public void testMulePrototypeServiceLifecycle() throws Exception
49      {
50          testComponentLifecycle("MulePrototypeService",
51              "[setProperty, setMuleContext, setService, initialise, start, stop, dispose]");
52      }
53  
54      /**
55       * ASSERT: - Mule lifecycle methods invoked each time singleton is used to create
56       * new object in pool - Service and muleContext injected each time singleton is
57       * used to create new object in pool (Component implements
58       * ServiceAware/MuleContextAware)
59       * 
60       * @throws Exception
61       */
62      public void testMulePooledSingletonServiceLifecycle() throws Exception
63      {
64          // Initialisation policy not enabled in iBeans
65          // testComponentLifecycle("MulePooledSingletonService",
66          // "[setProperty, setMuleContext, setService, initialise, initialise, initialise, start, start, start, stop, stop, stop, dispose, dispose, dispose]");
67          testComponentLifecycle("MulePooledSingletonService",
68              "[setProperty, setMuleContext, setService, initialise, start, stop, dispose]");
69      }
70  
71      private void testComponentLifecycle(final String serviceName, final String expectedLifeCycle)
72          throws Exception
73      {
74  
75          final AbstractLifecycleTracker tracker = exerciseComponent(serviceName);
76  
77          muleContext.dispose();
78  
79          Assert.assertEquals(serviceName, expectedLifeCycle, tracker.getTracker().toString());
80      }
81  
82      private AbstractLifecycleTracker exerciseComponent(final String serviceName) throws Exception
83      {
84          MuleClient muleClient = new MuleClient(muleContext);
85          final AbstractLifecycleTracker ltc = (AbstractLifecycleTracker)muleClient.send(
86              "vm://" + serviceName + ".In", null, null).getPayload();
87  
88          Assert.assertNotNull(ltc);
89  
90          return ltc;
91      }
92  
93      public class GuiceLifecycleModule extends AbstractModule
94      {
95          @Override
96          protected void configure()
97          {
98  
99          }
100 
101         // @Provides @AnnotatedService
102         // public PrototypeService createPrototypeService()
103         // {
104         // PrototypeService service = new PrototypeService();
105         // service.setProperty("mps");
106         // return service;
107         // }
108         //
109         // @Provides @AnnotatedService @Singleton
110         // public SingletonService createSingletonService()
111         // {
112         // SingletonService service = new SingletonService();
113         // service.setProperty("mms");
114         // return service;
115         // }
116         //
117         // @Provides @AnnotatedService
118         // public PooledService createPooledService()
119         // {
120         // PooledService service = new PooledService();
121         // service.setProperty("mmps");
122         // return service;
123         // }
124     }
125 }