You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rw...@apache.org on 2002/03/19 18:27:54 UTC

cvs commit: jakarta-commons/pool/src/test/org/apache/commons/pool/impl TestSoftReferenceObjectPool.java TestAll.java

rwaldhoff    02/03/19 09:27:54

  Modified:    pool/src/test/org/apache/commons/pool/impl TestAll.java
  Added:       pool/src/java/org/apache/commons/pool/impl
                        SoftReferenceObjectPool.java
               pool/src/test/org/apache/commons/pool/impl
                        TestSoftReferenceObjectPool.java
  Log:
  adding a simple SoftReference-based ObjectPool, and tests for it
  
  Revision  Changes    Path
  1.1                  jakarta-commons/pool/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java
  
  Index: SoftReferenceObjectPool.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/SoftReferenceObjectPool.java,v 1.1 2002/03/19 17:27:54 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2002/03/19 17:27:54 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.pool.impl;
  
  import org.apache.commons.pool.ObjectPool;
  import org.apache.commons.pool.PoolableObjectFactory;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.NoSuchElementException;
  import java.util.Iterator;
  import java.lang.ref.SoftReference;
  
  /**
   * A {@link java.lang.ref.SoftReference SoftReference} based
   * {@link ObjectPool}.
   *
   * @author Rodney Waldhoff
   * @version $Revision: 1.1 $ $Date: 2002/03/19 17:27:54 $
   */
  public class SoftReferenceObjectPool implements ObjectPool {
      public SoftReferenceObjectPool() {
          _pool = new ArrayList();
          _factory = null;
      }
  
      public SoftReferenceObjectPool(PoolableObjectFactory factory) {
          _pool = new ArrayList();
          _factory = factory;
      }
  
      public SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize) throws Exception {
          _pool = new ArrayList();
          _factory = factory;
          if(null != _factory) {
              for(int i=0;i<initSize;i++) {
                  Object obj = _factory.makeObject();
                  _factory.passivateObject(obj);
                  _pool.add(new SoftReference(obj));
              }
          }
      }
  
      public synchronized Object borrowObject() throws Exception {        
          Object obj = null;
          while(null == obj) {
              if(_pool.isEmpty()) {
                  if(null == _factory) {
                      throw new NoSuchElementException();
                  } else {
                      obj = _factory.makeObject();
                  }
              } else {
                  SoftReference ref = (SoftReference)(_pool.remove(_pool.size() - 1));
                  obj = ref.get();
                  if(null == obj) {
                      System.out.println("Found a SoftReference " + ref + " in the pool, but its refferant was null.");
                  }
              }
          }
          if(null != _factory && null != obj) {
              _factory.activateObject(obj);
          }
          _numActive++;
          return obj;
      }
  
      public synchronized void returnObject(Object obj) throws Exception {
          _numActive--;
          if(null == _factory || _factory.validateObject(obj)) {
              if(null != _factory) {
                  try {
                      _factory.passivateObject(obj);
                  } catch(Exception e) {
                      _factory.destroyObject(obj);
                      return;
                  }
              }
          }
          _pool.add(new SoftReference(obj));
      }
  
      /** Returns an approximation not less than the of the number of idle instances in the pool. */
      public int numIdle() {
          return _pool.size();
      }
  
      public int numActive() {
          return _numActive;
      }
  
      public synchronized void clear() {
          if(null != _factory) {
              Iterator iter = _pool.iterator();
              while(iter.hasNext()) {
                  try {
                      Object obj = ((SoftReference)iter.next()).get();
                      if(null != obj) {
                          _factory.destroyObject(obj);
                      }
                  } catch(Exception e) {
                      // ignore error, keep destroying the rest
                  }
              }
          }
          _pool.clear();
      }
  
      synchronized public void close() throws Exception {
          clear();
          _pool = null;
          _factory = null;
      }
  
      synchronized public void setFactory(PoolableObjectFactory factory) throws IllegalStateException {
          if(0 < numActive()) {
              throw new IllegalStateException("Objects are already active");
          } else {
              clear();
              _factory = factory;
          }
      }
  
      /** My pool. */
      private List _pool = null;
  
      /** My {@link PoolableObjectFactory}. */
      private PoolableObjectFactory _factory = null;
  
      /** Number of active objects. */
      private int _numActive = 0;
  }
  
  
  
  1.2       +5 -4      jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestAll.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestAll.java	14 Apr 2001 16:42:02 -0000	1.1
  +++ TestAll.java	19 Mar 2002 17:27:54 -0000	1.2
  @@ -1,13 +1,13 @@
   /*
  - * $Header: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestAll.java,v 1.1 2001/04/14 16:42:02 rwaldhoff Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/04/14 16:42:02 $
  + * $Header: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestAll.java,v 1.2 2002/03/19 17:27:54 rwaldhoff Exp $
  + * $Revision: 1.2 $
  + * $Date: 2002/03/19 17:27:54 $
    *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -68,7 +68,7 @@
    * package.
    *
    * @author Rodney Waldhoff
  - * @version $Id: TestAll.java,v 1.1 2001/04/14 16:42:02 rwaldhoff Exp $
  + * @version $Revision: 1.2 $ $Date: 2002/03/19 17:27:54 $
    */
   public class TestAll extends TestCase {
       public TestAll(String testName) {
  @@ -81,6 +81,7 @@
           suite.addTest(TestStackKeyedObjectPool.suite());
           suite.addTest(TestGenericObjectPool.suite());
           suite.addTest(TestGenericKeyedObjectPool.suite());
  +        suite.addTest(TestSoftReferenceObjectPool.suite());
           return suite;
       }
   
  
  
  
  1.1                  jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestSoftReferenceObjectPool.java
  
  Index: TestSoftReferenceObjectPool.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestSoftReferenceObjectPool.java,v 1.1 2002/03/19 17:27:54 rwaldhoff Exp $
   * $Revision: 1.1 $
   * $Date: 2002/03/19 17:27:54 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.pool.impl;
  
  import junit.framework.*;
  import org.apache.commons.pool.ObjectPool;
  import org.apache.commons.pool.PoolableObjectFactory;
  
  /**
   * @author Rodney Waldhoff
   * @version $Revision: 1.1 $ $Date: 2002/03/19 17:27:54 $
   */
  public class TestSoftReferenceObjectPool extends TestCase {
      public TestSoftReferenceObjectPool(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          return new TestSuite(TestSoftReferenceObjectPool.class);
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestSoftReferenceObjectPool.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      private SoftReferenceObjectPool pool = null;
  
      public void setUp() {
          pool = new SoftReferenceObjectPool(
              new PoolableObjectFactory()  {
                  int counter = 0;
                  public Object makeObject() { return String.valueOf(counter++); }
                  public void destroyObject(Object obj) { }
                  public boolean validateObject(Object obj) { return true; }
                  public void activateObject(Object obj) { }
                  public void passivateObject(Object obj) { }
              }
              );
      }
  
      public void testBorrow() throws Exception {
          Object obj0 = pool.borrowObject();
          assertEquals("0",obj0);
          Object obj1 = pool.borrowObject();
          assertEquals("1",obj1);
          Object obj2 = pool.borrowObject();
          assertEquals("2",obj2);
      }
  
      public void testBorrowReturn() throws Exception {
          Object obj0 = pool.borrowObject();
          assertEquals("borrowObject from an empty pool should create a new instance.","0",obj0);
          Object obj1 = pool.borrowObject();
          assertEquals("A second borrowObject from an empty pool should create a second instance.","1",obj1);
          Object obj2 = pool.borrowObject();
          assertEquals("A third borrowObject from an empty pool should create a third instance.","2",obj2);
  
          pool.returnObject(obj2);
          obj2 = pool.borrowObject();
          assertEquals("Having returned the third instance to the empty pool, borrowObject should return it.","2",obj2);
  
          pool.returnObject(obj1);
          obj1 = pool.borrowObject();
          assertEquals("Having returned the second instance to the empty pool, borrowObject should return it.","1",obj1);
  
          pool.returnObject(obj0);
          pool.returnObject(obj2);
          obj2 = pool.borrowObject();
          assertEquals("Having returned the first, then third instance to the empty pool, borrowObject should return the third instance.","2",obj2);
          obj0 = pool.borrowObject();
          assertEquals("Having returned the first, then third instance to the empty pool, the second call to borrowObject should return the first instance.","0",obj0);
      }
  
      public void testNumActiveNumIdle() throws Exception {
          assertEquals(0,pool.numActive());
          assertEquals(0,pool.numIdle());
          Object obj0 = pool.borrowObject();
          assertEquals(1,pool.numActive());
          assertEquals(0,pool.numIdle());
          Object obj1 = pool.borrowObject();
          assertEquals(2,pool.numActive());
          assertEquals(0,pool.numIdle());
          pool.returnObject(obj1);
          assertEquals(1,pool.numActive());
          assertEquals(1,pool.numIdle());
          pool.returnObject(obj0);
          assertEquals(0,pool.numActive());
          assertEquals(2,pool.numIdle());
      }
  
      public void testClear() throws Exception {
          assertEquals(0,pool.numActive());
          assertEquals(0,pool.numIdle());
          Object obj0 = pool.borrowObject();
          Object obj1 = pool.borrowObject();
          assertEquals(2,pool.numActive());
          assertEquals(0,pool.numIdle());
          pool.returnObject(obj1);
          pool.returnObject(obj0);
          assertEquals(0,pool.numActive());
          assertEquals(2,pool.numIdle());
          pool.clear();
          assertEquals(0,pool.numActive());
          assertEquals(0,pool.numIdle());
          Object obj2 = pool.borrowObject();
          assertEquals("2",obj2);
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>