View Javadoc

1   /*
2    * $Id: DefaultLifecycleEnabledObjectPoolTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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  public class DefaultLifecycleEnabledObjectPoolTestCase extends AbstractPoolingTestCase
19  {
20      public void testPoolStart() throws Exception
21      {
22          DefaultLifecycleEnabledObjectPool pool = createObjectPool();
23  
24          // pool was not started yet, objects must be uninitialized
25          WaterMelon borrowed = borrow(pool);
26          assertEquals("void", borrowed.getState());
27  
28          pool.start();
29          assertEquals("started", borrowed.getState());
30      }
31      
32      public void testPoolStop() throws Exception
33      {
34          DefaultLifecycleEnabledObjectPool pool = createObjectPool();
35          pool.start();
36          
37          WaterMelon borrowed = borrow(pool);
38          
39          pool.stop();
40          assertEquals("stopped", borrowed.getState());
41      }
42  
43      private DefaultLifecycleEnabledObjectPool createObjectPool() throws Exception
44      {
45          PoolingProfile poolingProfile = createDefaultPoolingProfile();
46          ObjectFactory objectFactory = createDefaultObjectFactory();
47          DefaultLifecycleEnabledObjectPool pool =
48              new DefaultLifecycleEnabledObjectPool(objectFactory, poolingProfile, muleContext);
49          
50          pool.initialise();
51          
52          return pool;
53      }
54  
55      private ObjectFactory createDefaultObjectFactory()
56      {
57          // WaterMelon implements some lifecycle methods
58          PrototypeObjectFactory factory = new PrototypeObjectFactory(WaterMelon.class);
59          return factory;
60      }
61      
62      private WaterMelon borrow(DefaultLifecycleEnabledObjectPool pool) throws Exception
63      {
64          return (WaterMelon) pool.borrowObject();
65      }
66  }
67  
68