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.module.guice;
8   
9   import org.mule.module.client.MuleClient;
10  import org.mule.registry.AbstractLifecycleTracker;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  
13  import com.google.inject.AbstractModule;
14  
15  import org.junit.Assert;
16  import org.junit.Test;
17  
18  public class GuiceLifecyceTestCase extends AbstractMuleContextTestCase
19  {
20      @Override
21      protected void doSetUp() throws Exception
22      {
23          GuiceConfigurationBuilder cb = new GuiceConfigurationBuilder(new GuiceLifecycleModule());
24          cb.configure(muleContext);
25      }
26  
27      /**
28       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
29       * (Component implements ServiceAware/MuleContextAware)
30       * 
31       * @throws Exception
32       */
33      @Test
34      public void testSingletonServiceLifecycle() throws Exception
35      {
36          testComponentLifecycle("MuleSingletonService",
37              "[setProperty, setMuleContext, setService, initialise, start, stop, dispose]");
38      }
39  
40      /**
41       * ASSERT: - Mule lifecycle methods invoked - Service and muleContext injected
42       * (Component implements ServiceAware/MuleContextAware)
43       * 
44       * @throws Exception
45       */
46      @Test
47      public void testMulePrototypeServiceLifecycle() throws Exception
48      {
49          testComponentLifecycle("MulePrototypeService",
50              "[setProperty, setMuleContext, setService, initialise, start, stop, dispose]");
51      }
52  
53      /**
54       * ASSERT: - Mule lifecycle methods invoked each time singleton is used to create
55       * new object in pool - Service and muleContext injected each time singleton is
56       * used to create new object in pool (Component implements
57       * ServiceAware/MuleContextAware)
58       * 
59       * @throws Exception
60       */
61      @Test
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 }