View Javadoc

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