You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by le...@apache.org on 2002/02/07 09:39:21 UTC

cvs commit: jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/datasource/test DataSourceJdbcTestCase.java DataSourceJdbcTestCase.xtest DataSourceTestCase.java DataSourceTestCase.xtest

leif        02/02/07 00:39:21

  Added:       src/test/org/apache/avalon/excalibur/datasource/test
                        DataSourceJdbcTestCase.java
                        DataSourceJdbcTestCase.xtest
  Removed:     src/test/org/apache/avalon/excalibur/datasource/test
                        DataSourceTestCase.java DataSourceTestCase.xtest
  Log:
  Make it possible to create new JDBC tests without having to modify
  the build file each time.
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/datasource/test/DataSourceJdbcTestCase.java
  
  Index: DataSourceJdbcTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.datasource.test;
  
  import java.sql.Connection;
  import java.sql.SQLException;
  import java.util.Random;
  import java.util.LinkedList;
  import java.util.Iterator;
  import org.apache.avalon.framework.component.Component;
  import org.apache.avalon.framework.component.ComponentException;
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.configuration.ConfigurationException;
  import org.apache.avalon.framework.configuration.DefaultConfiguration;
  import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
  import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
  import org.apache.avalon.excalibur.datasource.DataSourceComponent;
  import org.apache.avalon.excalibur.datasource.JdbcDataSource;
  import org.apache.avalon.excalibur.concurrent.ThreadBarrier;
  
  /**
   * Test the DataSource Component.  I don't know how to make this generic,
   * so I'll throw some bones out there, and hope someone can set this up
   * better.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   */
  public class DataSourceJdbcTestCase
      extends ExcaliburTestCase
  {
      protected boolean isSuccessful;
      protected ThreadBarrier barrier;
      protected int connectionCount;
  
      public DataSourceJdbcTestCase(String name)
      {
          super(name);
      }
  
      public void testOverAllocation()
      {
          DataSourceComponent ds = null;
          this.isSuccessful = false;
          LinkedList connectionList = new LinkedList();
  
          try
          {
              ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE );
  
              for( int i = 0; i < 11; i++ )
              {
                  connectionList.add( ds.getConnection() );
              }
          }
          catch ( SQLException se )
          {
              this.isSuccessful = true;
              getLogger().info( "The test was successful" );
          }
          catch ( ComponentException ce )
          {
              if ( getLogger().isDebugEnabled() )
              {
                  getLogger().debug( "There was an error in the OverAllocation test", ce );
              }
  
              throw new CascadingAssertionFailedError( "There was an error in the OverAllocation test", ce );
          }
          finally
          {
              assertTrue(  "The DataSourceComponent could not be retrieved.", null != ds );
  
              Iterator connections = connectionList.iterator();
  
              while ( connections.hasNext() )
              {
                  try
                  {
                      ( (Connection) connections.next() ).close();
                  }
                  catch ( SQLException se )
                  {
                      // ignore
                  }
              }
  
              connectionList.clear();
  
              manager.release( (Component) ds );
          }
  
          assertTrue( "Exception was not thrown when too many datasource components were retrieved.", this.isSuccessful );
      }
  
      public void testNormalUse()
      {
          DataSourceComponent ds = null;
          this.isSuccessful = true;
  
          try
          {
              ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE );
  
              this.connectionCount = 0;
  
              for (int i = 0; i < 10; i++)
              {
                  (new Thread( new ConnectionThread( this, ds ) ) ).start();
              }
  
              this.barrier = new ThreadBarrier(11);
              try
              {
                  this.barrier.barrierSynchronize();
              }
              catch(InterruptedException ie)
              {
                  // Ignore
              }
  
              getLogger().info( "The normal use test passed with " + this.connectionCount + " requests and 10 concurrent threads running" );
          }
          catch ( ComponentException ce )
          {
              if ( getLogger().isDebugEnabled() )
              {
                  getLogger().debug( "There was an error in the normal use test", ce );
              }
  
              throw new CascadingAssertionFailedError( "There was an error in the normal use test", ce );
          }
          finally
          {
              assertTrue(  "The DataSourceComponent could not be retrieved.", null != ds );
  
              manager.release( (Component) ds );
          }
  
          assertTrue( "Normal use test failed", this.isSuccessful );
      }
  
      class ConnectionThread
          implements Runnable
      {
          protected DataSourceComponent datasource;
          protected DataSourceJdbcTestCase testcase;
  
          ConnectionThread( DataSourceJdbcTestCase testcase,
                            final DataSourceComponent datasource )
          {
              this.datasource = datasource;
              this.testcase = testcase;
          }
  
          public void run()
          {
              long end = System.currentTimeMillis() + 5000; // run for 5 seconds
              Random rnd = new Random();
  
              while( System.currentTimeMillis() < end && this.testcase.isSuccessful )
              {
                  try
                  {
                      Connection con = this.datasource.getConnection();
                      Thread.sleep((long) rnd.nextInt(100)); // sleep for up to 100ms
                      con.close();
                      this.testcase.connectionCount++;
                  }
                  catch( final SQLException se )
                  {
                      this.testcase.isSuccessful = false;
                      this.testcase.getLogger().info( "Failed to get Connection, test failed", se );
                  }
                  catch( final InterruptedException ie )
                  {
                      // Ignore
                  }
              }
  
              try
              {
                  this.testcase.barrier.barrierSynchronize();
              }
              catch( final InterruptedException ie )
              {
                  // Ignore
              }
          }
      }
  }
  
  
  
  
  1.1                  jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/datasource/test/DataSourceJdbcTestCase.xtest
  
  Index: DataSourceJdbcTestCase.xtest
  ===================================================================
  <testcase>
    <annotation>
      <![CDATA[
        <title>DataSource Tests</title>
        <para>
          This series of tests excersize the JDBC DataSourceComponent provided by
          Excalibur.  The configuration is specified in the file located in
          <parameter>jakarta-avalon-excalibur/src/test/org/apache/avalon/excalibur/datasource/test/DataSourceJdbcTestCase.xtext</parameter>.
          You may edit the test to customize the settings.
        </para>
      ]]>
    </annotation>
  
    <logkit>
      <factories>
        <factory type="stream" 
                 class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/>
        <factory type="file" class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/>
      </factories>
  
      <targets>
        <stream id="console">
          <stream>System.out</stream>
          <format type="avalon">
            %7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\n%{throwable}
          </format>
        </stream>
        <file id="file">
          <filename>TEST-org.apache.avalon.excalibur.datasource.test.DataSourceJdbcTestCase.log</filename>
          <format type="extended">
            %7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): %{message}\n%{throwable}
          </format>
        </file>
      </targets>
  
      <categories>
        <category name="test" log-level="INFO">
          <log-target id-ref="console"/>
          <log-target id-ref="file"/>
        </category>
      </categories>
    </logkit>
  
    <roles>
      <role name="org.apache.avalon.excalibur.datasource.DataSourceComponent"
            shorthand="datasource"
            default-class="org.apache.avalon.excalibur.datasource.JdbcDataSource"/>
    </roles>
  
    <!--
      It is critical that you have a monitor by the name "active" and the
      name "passive"
    -->
    <components>
      <datasource logger="test">
        <pool-controller min="5" max="10"/>
        <driver>@test.jdbc.driver@</driver>
        <dburl>@test.jdbc.url@</dburl>
        <user>@test.jdbc.user@</user>
        <password>@test.jdbc.password@</password>
      </datasource>
    </components>
  </testcase>
  
  
  

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