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.util.pool;
8   
9   import org.mule.api.object.ObjectFactory;
10  import org.mule.config.PoolingProfile;
11  import org.mule.object.PrototypeObjectFactory;
12  import org.mule.tck.testmodels.fruit.WaterMelon;
13  
14  import org.junit.Test;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  public class DefaultLifecycleEnabledObjectPoolTestCase extends AbstractPoolingTestCase
19  {
20  
21      @Test
22      public void testPoolStart() throws Exception
23      {
24          DefaultLifecycleEnabledObjectPool pool = createObjectPool();
25  
26          // pool was not started yet, objects must be uninitialized
27          WaterMelon borrowed = borrow(pool);
28          assertEquals("void", borrowed.getState());
29  
30          pool.start();
31          assertEquals("started", borrowed.getState());
32      }
33      
34      @Test
35      public void testPoolStop() throws Exception
36      {
37          DefaultLifecycleEnabledObjectPool pool = createObjectPool();
38          pool.start();
39          
40          WaterMelon borrowed = borrow(pool);
41          
42          pool.stop();
43          assertEquals("stopped", borrowed.getState());
44      }
45  
46      private DefaultLifecycleEnabledObjectPool createObjectPool() throws Exception
47      {
48          PoolingProfile poolingProfile = createDefaultPoolingProfile();
49          ObjectFactory objectFactory = createDefaultObjectFactory();
50          DefaultLifecycleEnabledObjectPool pool =
51              new DefaultLifecycleEnabledObjectPool(objectFactory, poolingProfile, muleContext);
52          
53          pool.initialise();
54          
55          return pool;
56      }
57  
58      private ObjectFactory createDefaultObjectFactory()
59      {
60          // WaterMelon implements some lifecycle methods
61          PrototypeObjectFactory factory = new PrototypeObjectFactory(WaterMelon.class);
62          return factory;
63      }
64      
65      private WaterMelon borrow(DefaultLifecycleEnabledObjectPool pool) throws Exception
66      {
67          return (WaterMelon) pool.borrowObject();
68      }
69  }
70  
71