db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker ConnectionFactoryTest.java AllTests.java
Date Fri, 09 May 2003 12:50:38 GMT
arminw      2003/05/09 05:50:37

  Modified:    src/test/org/apache/ojb/broker ConnectionFactoryTest.java
                        AllTests.java
  Log:
  add new test case
  
  Revision  Changes    Path
  1.2       +125 -3    db-ojb/src/test/org/apache/ojb/broker/ConnectionFactoryTest.java
  
  Index: ConnectionFactoryTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ConnectionFactoryTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ConnectionFactoryTest.java	5 May 2003 23:46:42 -0000	1.1
  +++ ConnectionFactoryTest.java	9 May 2003 12:50:37 -0000	1.2
  @@ -6,10 +6,17 @@
   import org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl;
   import org.apache.ojb.broker.accesslayer.ConnectionFactoryFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl;
  +import org.apache.ojb.broker.accesslayer.LookupException;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
   import org.apache.ojb.broker.metadata.MetadataManager;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.query.QueryFactory;
  +import org.apache.ojb.broker.util.sequence.HighLowSequence;
   
   import java.sql.Connection;
  +import java.util.ArrayList;
  +import java.util.Collection;
   
   /**
    * ConnectionFactory implementation related tests.
  @@ -68,7 +75,7 @@
               MetadataManager mm = MetadataManager.getInstance();
               JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils.clone(
                       broker.serviceConnectionManager().getConnectionDescriptor());
  -            jcd.setJcdAlias(factory.getName() + "_test_1");
  +            jcd.setJcdAlias(factory.getName() + "_test_checkFactory_a");
               jcd.setUseAutoCommit(2);
   
               mm.connectionRepository().addDescriptor(jcd);
  @@ -85,7 +92,7 @@
   
               jcd = (JdbcConnectionDescriptor) SerializationUtils.clone(
                       broker.serviceConnectionManager().getConnectionDescriptor());
  -            jcd.setJcdAlias(factory.getName() + "_test_2");
  +            jcd.setJcdAlias(factory.getName() + "_test_checkFactory_b");
               jcd.setUseAutoCommit(1);
   
               mm.connectionRepository().addDescriptor(jcd);
  @@ -96,6 +103,121 @@
           {
               if (oldFac != null) fac.setClassToServe(oldFac);
           }
  +    }
  +
  +    public void testExhaustedPool() throws Exception
  +    {
  +        checkFactoryPoolExhausted(ConnectionFactoryPooledImpl.class);
  +        checkFactoryPoolExhausted(ConnectionFactoryDBCPImpl.class);
  +    }
  +
  +    private void checkFactoryPoolExhausted(Class factory) throws Exception
  +    {
  +        Class oldFac = null;
  +        ConnectionFactoryFactory fac = null;
  +        try
  +        {
  +            fac = ConnectionFactoryFactory.getInstance();
  +            oldFac = fac.getClassToServe();
  +            fac.setClassToServe(factory);
  +            ConnectionFactory conFac = (ConnectionFactory) fac.createNewInstance();
  +
  +            MetadataManager mm = MetadataManager.getInstance();
  +            JdbcConnectionDescriptor jcd = (JdbcConnectionDescriptor) SerializationUtils.clone(
  +                    broker.serviceConnectionManager().getConnectionDescriptor());
  +            jcd.setJcdAlias(factory.getName() + "_test_checkFactoryPoolExhausted_1");
  +            jcd.setUseAutoCommit(1);
  +            jcd.getConnectionPoolDescriptor().setMaxActive(2);
  +            jcd.getConnectionPoolDescriptor().setConnectionFactory(factory);
  +            mm.connectionRepository().addDescriptor(jcd);
  +
  +            Connection con = null;
  +            Connection con2 = null;
  +            Connection con3 = null;
  +            try
  +            {
  +                con = conFac.lookupConnection(jcd);
  +                con2 = conFac.lookupConnection(jcd);
  +                try
  +                {
  +                    con3 = conFac.lookupConnection(jcd);
  +                    fail("We expect an exception indicating that the pool is exhausted");
  +                }
  +                catch (LookupException e)
  +                {
  +                    // we expected that
  +                    assertTrue(true);
  +                }
  +            }
  +            finally
  +            {
  +                try
  +                {
  +                    con.close();
  +                    con2.close();
  +                }
  +                catch (Exception e)
  +                {
  +                }
  +            }
  +        }
  +        finally
  +        {
  +            if (oldFac != null) fac.setClassToServe(oldFac);
  +        }
  +    }
  +
  +    public void testExhaustedPool2() throws Exception
  +    {
  +        PersistenceBroker pb = null;
  +        if (!(broker.serviceConnectionManager().
  +                getConnectionDescriptor().getConnectionPoolDescriptor().
  +                getWhenExhaustedAction() == 0))
  +        {
  +            // skip test
  +            System.out.println("** Skip " + this.getName() +
  +                    "#testExhaustedPool2 need whenExhaustedAction=0 in configuration **");
  +            return;
  +        }
  +
  +        PersistenceBroker[] pbArr = new PersistenceBroker[50];
  +        try
  +        {
  +            try
  +            {
  +                for (int i = 0; i < 50; i++)
  +                {
  +                    pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +                    pbArr[i] = pb;
  +                    doSampleQuery(pb);
  +                }
  +                fail("Exception expected - connection pool should be exhausted");
  +            }
  +            catch (PersistenceBrokerException e)
  +            {
  +                // expected
  +                assertTrue(true);
  +            }
  +        }
  +        finally
  +        {
  +            for (int i = 0; i < pbArr.length; i++)
  +            {
  +                PersistenceBroker broker = pbArr[i];
  +                if (broker != null) broker.close();
  +            }
  +        }
  +
  +    }
  +
  +    private Collection doSampleQuery(PersistenceBroker pb)
  +    {
  +        Criteria crit = new Criteria();
  +        Collection results = new ArrayList();
  +        crit.addGreaterOrEqualThan("grabSize", new Integer(5));
  +        QueryByCriteria q = QueryFactory.newQuery(HighLowSequence.class, crit);
  +        results = pb.getCollectionByQuery(q);
  +        return results;
       }
   
   
  
  
  
  1.26      +1 -0      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- AllTests.java	29 Apr 2003 15:43:26 -0000	1.25
  +++ AllTests.java	9 May 2003 12:50:37 -0000	1.26
  @@ -74,6 +74,7 @@
           suite.addTest(new TestSuite(ReferenceTest.class));
           suite.addTest(new TestSuite(ExtentAwarePathExpressionsTest.class));
           suite.addTest(new TestSuite(RepositoryElementsTest.class));
  +        suite.addTest(new TestSuite(ConnectionFactoryTest.class));
           return suite;
       }
   
  
  
  

Mime
View raw message