You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2003/08/13 00:46:57 UTC

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

dirkv       2003/08/12 15:46:57

  Modified:    pool/src/test/org/apache/commons/pool/impl
                        TestGenericObjectPool.java
  Log:
  Bugzilla Bug 22078
    [DBCP] testOnBorrow fails if setAutoCommit() throws an exception
  
  - junit tests
  
  Revision  Changes    Path
  1.16      +91 -22    jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
  
  Index: TestGenericObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- TestGenericObjectPool.java	24 Apr 2003 00:18:58 -0000	1.15
  +++ TestGenericObjectPool.java	12 Aug 2003 22:46:57 -0000	1.16
  @@ -72,6 +72,7 @@
   
   /**
    * @author Rodney Waldhoff
  + * @author Dirk Verbeeck
    * @version $Revision$ $Date$
    */
   public class TestGenericObjectPool extends TestObjectPool {
  @@ -105,6 +106,43 @@
           pool = null;
       }
   
  +    /**
  +     * Activation failure on existing object doesn't fail the borrow 
  +     */
  +    public void testActivationException() throws Exception {
  +        SimpleFactory factory = new SimpleFactory(true, false);
  +        factory.setThrowExceptionOnActivate(true);
  +        factory.setValidationEnabled(false);
  +        GenericObjectPool pool = new GenericObjectPool(factory);
  +        
  +        Object obj1 = pool.borrowObject();
  +        pool.returnObject(obj1);
  +
  +        // obj1 was returned to the pool but failed to activate the second borrow
  +        // a new object obj2 needs te be created
  +        Object obj2 = pool.borrowObject();
  +        assertTrue(obj1 != obj2);        
  +    }
  +
  +    /**
  +     * Activation failure on new object fails the borrow 
  +     */
  +    public void testActivationExceptionOnNewObject() throws Exception {
  +        SimpleFactory factory = new SimpleFactory(true, false);
  +        factory.setThrowExceptionOnActivate(true);
  +        factory.setValidationEnabled(false);
  +        GenericObjectPool pool = new GenericObjectPool(factory);
  +        
  +        Object obj1 = pool.borrowObject();
  +        try {
  +            Object obj2 = pool.borrowObject();
  +            System.out.println("obj1: " + obj1);
  +            System.out.println("obj2: " + obj2);
  +            fail("a second object should have been created and failed to activate");
  +        }
  +        catch (Exception e) {}
  +    }
  +
       public void testWhenExhaustedGrow() throws Exception {
           GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
           pool.setMaxActive(1);
  @@ -193,6 +231,7 @@
           pool.setMaxIdle(10);
           pool.setFactory(new SimpleFactory());
           Object obj = pool.borrowObject();
  +        assertNotNull(obj);
           try {
               pool.setFactory(null);
               fail("Expected IllegalStateException");
  @@ -683,7 +722,20 @@
       
       private GenericObjectPool pool = null;
   
  -    static class SimpleFactory implements PoolableObjectFactory {
  +    private void assertConfiguration(GenericObjectPool.Config expected, GenericObjectPool actual) throws Exception {
  +        assertEquals("testOnBorrow",expected.testOnBorrow,actual.getTestOnBorrow());
  +        assertEquals("testOnReturn",expected.testOnReturn,actual.getTestOnReturn());
  +        assertEquals("testWhileIdle",expected.testWhileIdle,actual.getTestWhileIdle());
  +        assertEquals("whenExhaustedAction",expected.whenExhaustedAction,actual.getWhenExhaustedAction());
  +        assertEquals("maxActive",expected.maxActive,actual.getMaxActive());
  +        assertEquals("maxIdle",expected.maxIdle,actual.getMaxIdle());
  +        assertEquals("maxWait",expected.maxWait,actual.getMaxWait());
  +        assertEquals("minEvictableIdleTimeMillis",expected.minEvictableIdleTimeMillis,actual.getMinEvictableIdleTimeMillis());
  +        assertEquals("numTestsPerEvictionRun",expected.numTestsPerEvictionRun,actual.getNumTestsPerEvictionRun());
  +        assertEquals("timeBetweenEvictionRunsMillis",expected.timeBetweenEvictionRunsMillis,actual.getTimeBetweenEvictionRunsMillis());
  +    }
  +
  +    public class SimpleFactory implements PoolableObjectFactory {
           public SimpleFactory() {
               this(true);
           }
  @@ -704,14 +756,27 @@
           void setOddValid(boolean valid) {
               oddValid = valid;
           }
  -        void setThrowExceptionOnPassivate(boolean bool) {
  +        public void setThrowExceptionOnPassivate(boolean bool) {
               exceptionOnPassivate = bool;
           }
  -        
  +    
           public Object makeObject() { return String.valueOf(makeCounter++); }
           public void destroyObject(Object obj) { }
  -        public boolean validateObject(Object obj) { return validateCounter++%2 == 0 ? evenValid : oddValid; }
  -        public void activateObject(Object obj) { }
  +        public boolean validateObject(Object obj) {
  +            if (enableValidation) { 
  +                return validateCounter++%2 == 0 ? evenValid : oddValid; 
  +            }
  +            else {
  +                return true;
  +            }
  +        }
  +        public void activateObject(Object obj) throws Exception {
  +            if (exceptionOnActivate) {
  +                if (!(validateCounter++%2 == 0 ? evenValid : oddValid)) {
  +                    throw new Exception();
  +                }
  +            }
  +        }
           public void passivateObject(Object obj) throws Exception {
               if(exceptionOnPassivate) {
                   throw new Exception();
  @@ -722,21 +787,25 @@
           boolean evenValid = true;
           boolean oddValid = true;
           boolean exceptionOnPassivate = false;
  -    }
  +        boolean exceptionOnActivate = false;
  +        boolean enableValidation = true;
   
  -    private void assertConfiguration(GenericObjectPool.Config expected, GenericObjectPool actual) throws Exception {
  -        assertEquals("testOnBorrow",expected.testOnBorrow,actual.getTestOnBorrow());
  -        assertEquals("testOnReturn",expected.testOnReturn,actual.getTestOnReturn());
  -        assertEquals("testWhileIdle",expected.testWhileIdle,actual.getTestWhileIdle());
  -        assertEquals("whenExhaustedAction",expected.whenExhaustedAction,actual.getWhenExhaustedAction());
  -        assertEquals("maxActive",expected.maxActive,actual.getMaxActive());
  -        assertEquals("maxIdle",expected.maxIdle,actual.getMaxIdle());
  -        assertEquals("maxWait",expected.maxWait,actual.getMaxWait());
  -        assertEquals("minEvictableIdleTimeMillis",expected.minEvictableIdleTimeMillis,actual.getMinEvictableIdleTimeMillis());
  -        assertEquals("numTestsPerEvictionRun",expected.numTestsPerEvictionRun,actual.getNumTestsPerEvictionRun());
  -        assertEquals("timeBetweenEvictionRunsMillis",expected.timeBetweenEvictionRunsMillis,actual.getTimeBetweenEvictionRunsMillis());
  -    }
  +        public boolean isThrowExceptionOnActivate() {
  +            return exceptionOnActivate;
  +        }
  +
  +        public void setThrowExceptionOnActivate(boolean b) {
  +            exceptionOnActivate = b;
  +        }
   
  +        public boolean isValidationEnabled() {
  +            return enableValidation;
  +        }
  +
  +        public void setValidationEnabled(boolean b) {
  +            enableValidation = b;
  +        }
  +    }
   }