View Javadoc

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