Coverage Report - org.mule.tck.model.AbstractPoolTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPoolTestCase
95%
83/87
56%
10/18
1.5
AbstractPoolTestCase$Borrower
75%
12/16
50%
2/4
1.5
 
 1  
 /*
 2  
  * $Id: AbstractPoolTestCase.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.tck.model;
 12  
 
 13  
 import org.mule.config.PoolingProfile;
 14  
 import org.mule.impl.MuleDescriptor;
 15  
 import org.mule.impl.model.seda.SedaModel;
 16  
 import org.mule.tck.AbstractMuleTestCase;
 17  
 import org.mule.tck.testmodels.fruit.Orange;
 18  
 import org.mule.umo.UMODescriptor;
 19  
 import org.mule.umo.lifecycle.InitialisationException;
 20  
 import org.mule.umo.model.UMOPoolFactory;
 21  
 import org.mule.util.ExceptionUtils;
 22  
 import org.mule.util.ObjectPool;
 23  
 
 24  
 public abstract class AbstractPoolTestCase extends AbstractMuleTestCase
 25  
 {
 26  
     public static final byte FAIL_WHEN_EXHAUSTED = 0;
 27  
     public static final byte GROW_WHEN_EXHAUSTED = 1;
 28  
     public static final byte BLOCK_WHEN_EXHAUSTED = 2;
 29  
 
 30  
     public static final byte DEFAULT_POOL_SIZE = 3;
 31  
     public static final long DEFAULT_WAIT = 1500;
 32  
 
 33  
     public AbstractPoolTestCase()
 34  
     {
 35  16
         super();
 36  
 
 37  16
     }
 38  
 
 39  
     // @Override
 40  
     protected void doSetUp() throws Exception
 41  
     {
 42  
         // Initialise the manager
 43  16
         getManager(true);
 44  16
     }
 45  
 
 46  
     public void testCreatePool() throws Exception
 47  
     {
 48  
 
 49  4
         MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName());
 50  2
         ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED);
 51  
 
 52  2
         assertNotNull(pool);
 53  2
         assertEquals(0, pool.getSize());
 54  
 
 55  2
         Object borrowed = pool.borrowObject();
 56  2
         assertNotNull(borrowed);
 57  2
         assertEquals(1, pool.getSize());
 58  2
         pool.returnObject(borrowed);
 59  
 
 60  2
         borrowed = pool.borrowObject();
 61  2
         assertNotNull(borrowed);
 62  2
         assertEquals(1, pool.getSize());
 63  2
         Object borrowed2 = pool.borrowObject();
 64  2
         assertNotNull(borrowed2);
 65  2
         assertEquals(2, pool.getSize());
 66  2
     }
 67  
 
 68  
     public void testFailOnExhaust() throws Exception
 69  
     {
 70  2
         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED);
 71  2
         Object borrowed = null;
 72  
 
 73  8
         for (int i = 0; i < pool.getMaxSize(); i++)
 74  
         {
 75  6
             borrowed = pool.borrowObject();
 76  6
             assertNotNull(borrowed);
 77  6
             assertEquals(pool.getSize(), i + 1);
 78  
         }
 79  
 
 80  
         try
 81  
         {
 82  2
             borrowed = pool.borrowObject();
 83  0
             fail("Should have thrown an Exception");
 84  
         }
 85  2
         catch (Exception e)
 86  
         {
 87  
             // expected
 88  0
         }
 89  2
     }
 90  
 
 91  
     public void testBlockExpiryOnExhaust() throws Exception
 92  
     {
 93  2
         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()),
 94  
             BLOCK_WHEN_EXHAUSTED);
 95  2
         Object borrowed = null;
 96  
 
 97  2
         assertEquals(0, pool.getSize());
 98  2
         borrowed = pool.borrowObject();
 99  2
         assertNotNull(borrowed);
 100  2
         borrowed = pool.borrowObject();
 101  2
         assertNotNull(borrowed);
 102  2
         borrowed = pool.borrowObject();
 103  2
         assertNotNull(borrowed);
 104  2
         assertEquals(3, pool.getSize());
 105  
 
 106  
         try
 107  
         {
 108  2
             borrowed = pool.borrowObject();
 109  0
             fail("Should have thrown an Exception");
 110  
         }
 111  2
         catch (Exception e)
 112  
         {
 113  
             // expected
 114  0
         }
 115  2
     }
 116  
 
 117  
     public void testBlockOnExhaust() throws Exception
 118  
     {
 119  2
         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()),
 120  
             BLOCK_WHEN_EXHAUSTED);
 121  2
         Object borrowed = null;
 122  
 
 123  2
         assertEquals(0, pool.getSize());
 124  
 
 125  2
         borrowed = pool.borrowObject();
 126  2
         borrowed = pool.borrowObject();
 127  2
         assertEquals(2, pool.getSize());
 128  
 
 129  2
         long borrowerWait = 500;
 130  2
         Borrower borrower = new Borrower(pool, borrowerWait);
 131  2
         borrower.start();
 132  
 
 133  
         // Make sure the borrower borrows first
 134  2
         Thread.sleep(200);
 135  
 
 136  2
         borrowed = pool.borrowObject();
 137  2
         assertNotNull(borrowed);
 138  2
     }
 139  
 
 140  
     public void testGrowOnExhaust() throws Exception
 141  
     {
 142  2
         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), GROW_WHEN_EXHAUSTED);
 143  
 
 144  2
         Object borrowed = pool.borrowObject();
 145  2
         borrowed = pool.borrowObject();
 146  2
         borrowed = pool.borrowObject();
 147  2
         assertEquals(3, pool.getSize());
 148  2
         assertEquals(3, pool.getMaxSize());
 149  
 
 150  
         // Should now grow
 151  2
         borrowed = pool.borrowObject();
 152  2
         assertNotNull(borrowed);
 153  
 
 154  2
         assertEquals(4, pool.getSize());
 155  2
     }
 156  
 
 157  
     public void testClearPool() throws Exception
 158  
     {
 159  2
         ObjectPool pool = createPool(getTestDescriptor("orange", Orange.class.getName()), FAIL_WHEN_EXHAUSTED);
 160  
 
 161  2
         Object borrowed = pool.borrowObject();
 162  2
         assertEquals(1, pool.getSize());
 163  2
         pool.returnObject(borrowed);
 164  
 
 165  2
         pool.clearPool();
 166  2
         assertEquals(0, pool.getSize());
 167  
 
 168  2
         borrowed = pool.borrowObject();
 169  2
         assertEquals(1, pool.getSize());
 170  2
     }
 171  
 
 172  
     public void testCreateFromFactory() throws Exception
 173  
     {
 174  2
         UMODescriptor descriptor = getTestDescriptor("orange", Orange.class.getName());
 175  2
         UMOPoolFactory factory = getPoolFactory();
 176  2
         ObjectPool pool = factory.createPool(descriptor, new SedaModel(), new PoolingProfile());
 177  2
         assertNotNull(pool);
 178  2
     }
 179  
 
 180  
     public void testPoolLifecycle() throws Exception
 181  
     {
 182  2
         MuleDescriptor d = getTestDescriptor("orange", Orange.class.getName());
 183  2
         ObjectPool pool = createPool(d, FAIL_WHEN_EXHAUSTED);
 184  
 
 185  2
         assertNotNull(pool);
 186  2
         assertEquals(0, pool.getSize());
 187  2
     }
 188  
 
 189  
     private class Borrower extends Thread
 190  
     {
 191  
         private ObjectPool pool;
 192  
         private long time;
 193  
 
 194  
         public Borrower(ObjectPool pool, long time)
 195  2
         {
 196  2
             super("Borrower");
 197  2
             if (pool == null)
 198  
             {
 199  0
                 throw new IllegalArgumentException("Pool cannot be null");
 200  
             }
 201  2
             this.pool = pool;
 202  2
             if (time < 500)
 203  
             {
 204  0
                 time = 500;
 205  
             }
 206  2
             this.time = time;
 207  2
         }
 208  
 
 209  
         /*
 210  
          * (non-Javadoc)
 211  
          * 
 212  
          * @see java.lang.Runnable#run()
 213  
          */
 214  
         public void run()
 215  
         {
 216  
             try
 217  
             {
 218  2
                 Object object = pool.borrowObject();
 219  2
                 sleep(time);
 220  2
                 pool.returnObject(object);
 221  
             }
 222  0
             catch (Exception e)
 223  
             {
 224  0
                 fail("Borrower thread failed:\n" + ExceptionUtils.getStackTrace(e));
 225  2
             }
 226  2
         }
 227  
 
 228  
     }
 229  
 
 230  
     public abstract ObjectPool createPool(MuleDescriptor descriptor, byte action)
 231  
         throws InitialisationException;
 232  
 
 233  
     public abstract UMOPoolFactory getPoolFactory();
 234  
 }