avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/util/test DataSourceFactoryRunner.java
Date Thu, 27 Jun 2002 14:33:38 GMT
leosutic    2002/06/27 07:33:38

  Modified:    microcontainer/src/test/org/apache/excalibur/microcontainer/test
                        ComposerTestCase.java
               microcontainer/src/java/org/apache/excalibur/microcontainer/util
                        DataSourceFactory.java
               microcontainer/src/test/org/apache/excalibur/microcontainer/util/test
                        DataSourceFactoryRunner.java
  Log:
  no message
  
  Revision  Changes    Path
  1.2       +59 -4     jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/test/ComposerTestCase.java
  
  Index: ComposerTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/test/ComposerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ComposerTestCase.java	24 Jun 2002 20:53:46 -0000	1.1
  +++ ComposerTestCase.java	27 Jun 2002 14:33:38 -0000	1.2
  @@ -1,9 +1,64 @@
  +/*
  +
  +============================================================================
  +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 modifica-
  +tion, 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  acknowledgment:  "This product includes  software
  +developed  by the  Apache Software Foundation  (http://www.apache.org/)."
  +Alternately, this  acknowledgment may  appear in the software itself,  if
  +and wherever such third-party acknowledgments normally appear.
  +
  +4. The names "Jakarta", "Avalon", "Excalibur" 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 name,  without prior written permission  of the
  +Apache Software Foundation.
  +
  +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 (INCLU-
  +DING, 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.excalibur.microcontainer.test;
   
   import junit.framework.TestCase;
   
   import org.apache.excalibur.microcontainer.MicroContainer;
   
  +/**
  + * Test case for composition.
  + *
  + * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
  + */ 
   public class ComposerTestCase extends TestCase
   {
       
  @@ -14,14 +69,14 @@
       
       public void testComposerImpl() throws Exception 
       {
  -        Supplier supplier = SupplierImpl.getInstance ();
  -        Composer comp = ComposerImpl.getInstance (supplier);
  +        Supplier supplier = SupplierImpl.getInstance();
  +        Composer comp = ComposerImpl.getInstance( supplier );
           
           comp.method();
           
           assertTrue( comp.success() );
           
  -        MicroContainer.release (comp);
  -        MicroContainer.release (supplier);
  +        MicroContainer.release( comp );
  +        MicroContainer.release( supplier );
       }    
   }
  
  
  
  1.2       +54 -0     jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/util/DataSourceFactory.java
  
  Index: DataSourceFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/java/org/apache/excalibur/microcontainer/util/DataSourceFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DataSourceFactory.java	27 Jun 2002 09:47:34 -0000	1.1
  +++ DataSourceFactory.java	27 Jun 2002 14:33:38 -0000	1.2
  @@ -1,3 +1,52 @@
  +/*
  +
  +============================================================================
  +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 modifica-
  +tion, 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  acknowledgment:  "This product includes  software
  +developed  by the  Apache Software Foundation  (http://www.apache.org/)."
  +Alternately, this  acknowledgment may  appear in the software itself,  if
  +and wherever such third-party acknowledgments normally appear.
  +
  +4. The names "Jakarta", "Avalon", "Excalibur" 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 name,  without prior written permission  of the
  +Apache Software Foundation.
  +
  +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 (INCLU-
  +DING, 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.excalibur.microcontainer.util;
   
   import org.apache.avalon.excalibur.datasource.DataSourceComponent;
  @@ -8,6 +57,11 @@
   import org.apache.excalibur.microcontainer.MicroContainer;
   import org.apache.excalibur.microcontainer.MicroContainerException;
   
  +/**
  + * Utility class to create <code>DataSourceComponent</code>s.
  + *
  + * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
  + */ 
   public class DataSourceFactory
   {
       private static final DefaultConfiguration configurationWithValue( String name, String
value ) 
  
  
  
  1.2       +163 -14   jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/util/test/DataSourceFactoryRunner.java
  
  Index: DataSourceFactoryRunner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/microcontainer/src/test/org/apache/excalibur/microcontainer/util/test/DataSourceFactoryRunner.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DataSourceFactoryRunner.java	27 Jun 2002 09:47:34 -0000	1.1
  +++ DataSourceFactoryRunner.java	27 Jun 2002 14:33:38 -0000	1.2
  @@ -1,6 +1,61 @@
  +/*
  +
  +============================================================================
  +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 modifica-
  +tion, 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  acknowledgment:  "This product includes  software
  +developed  by the  Apache Software Foundation  (http://www.apache.org/)."
  +Alternately, this  acknowledgment may  appear in the software itself,  if
  +and wherever such third-party acknowledgments normally appear.
  +
  +4. The names "Jakarta", "Avalon", "Excalibur" 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 name,  without prior written permission  of the
  +Apache Software Foundation.
  +
  +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 (INCLU-
  +DING, 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.excalibur.microcontainer.util.test;
   
  -import java.sql.*;
  +import java.sql.Connection;
  +import java.sql.Statement;
  +import java.sql.ResultSet;
  +import java.sql.ResultSetMetaData;
  +import java.sql.SQLException;
  +import java.util.StringTokenizer;
   
   import org.apache.avalon.excalibur.datasource.DataSourceComponent;
   import org.apache.avalon.excalibur.datasource.JdbcDataSource;
  @@ -8,22 +63,45 @@
   import org.apache.excalibur.microcontainer.MicroContainer;
   import org.apache.excalibur.microcontainer.util.DataSourceFactory;
   
  +/**
  + * Test case for the DataSourceFactory in the util package.
  + *
  + * @author <a href="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
  + */ 
   public class DataSourceFactoryRunner
   {
  +    /**
  +     * Main method.
  +     */
       public static void main( String[] args ) throws Exception 
       {
  +        String driver = args[0];
  +        String dbUrl  = args[1];
  +        String login  = args[2];
  +        String query  = args[3];
  +        
  +        StringTokenizer tok = new StringTokenizer( login, "," );
  +        String userName = tok.nextToken();
  +        String password = tok.hasMoreTokens() ? tok.nextToken() : "";
  +       
  +        System.out.println( "Attempting to create a DataSourceComponent with the following
parameters:" );
  +        System.out.println( "   Driver: " + driver );
  +        System.out.println( "   DB URL: " + dbUrl );
  +        System.out.println( "User name: " + userName );
  +        System.out.println( " Password: " + (password != null ? "(hidden)" : "(not specified)")
);
  +                
           DataSourceComponent ds = DataSourceFactory.getDataSource( 
  -            "org.gjt.mm.mysql.Driver", 
  -            "jdbc:mysql://localhost/",
  -            "sa",
  -            "",
  -            null,
  -            1,
  -            3,
  -            -1,
  -            true,
  -            false,
  -            null);
  +            driver,        // JDBC Driver
  +            dbUrl,         // Database URL
  +            userName,      // user name
  +            password,      // user's password
  +            null,          // Connection keep-alive command (none)
  +            1,             // Minimum number of connections in pool.
  +            3,             // Maximum number of connections in pool.
  +            -1,            // Connection timeout (disabled)
  +            true,          // Auto commit
  +            false,         // Using oracle (only relevant when supplying a keep-alive command)
  +            null);         // Override name of connection class (null == use default).
           
           Connection conn = null;
           Statement stmt = null;
  @@ -32,12 +110,22 @@
           {
               conn = ds.getConnection();
               stmt = conn.createStatement();
  -            rs = stmt.executeQuery( "SHOW DATABASES;" );
  +            
  +            System.out.println( "\nExecuting query: " + query + "\n");
  +            
  +            rs = stmt.executeQuery( query );
  +            ResultSetMetaData rsmd = rs.getMetaData();
  +            
  +            System.out.println( getHorizontalDivider( rsmd ) );
  +            System.out.println( getTableHeader( rsmd ) );
  +            System.out.println( getHorizontalDivider( rsmd ) );
               
               while (rs.next()) 
               {
  -                System.out.println( rs.getObject(1) );
  +                System.out.println( formatRow( rsmd, rs ) );
               }
  +            
  +            System.out.println( getHorizontalDivider( rsmd ) );
           }
           finally
           {
  @@ -58,5 +146,66 @@
               
               MicroContainer.release(ds);
           }
  +    }
  +    
  +    /**
  +     * Returns a horizontal dividing line (ASCII art) suitable for displaying the
  +     * result of a query.
  +     */
  +    private static String getHorizontalDivider( ResultSetMetaData rsmd ) throws SQLException
  +    {
  +        StringBuffer row = new StringBuffer();
  +        for( int i = 1; i <= rsmd.getColumnCount(); i++ )
  +        {
  +            row.append( '+' );
  +            for( int j = 0; j < rsmd.getColumnDisplaySize(i) + 2; j++ )
  +            {
  +                row.append( '-' );
  +            }
  +        }
  +        row.append( '+' );
  +        return row.toString();
  +    }
  +    
  +    /**
  +     * Returns a table header. The header consists of the column names separated by '|'.
  +     */
  +    private static String getTableHeader( ResultSetMetaData rsmd ) throws SQLException
  +    {
  +        StringBuffer row = new StringBuffer();
  +        for( int i = 1; i <= rsmd.getColumnCount(); i++ )
  +        {
  +            row.append( "| " );
  +            row.append( rsmd.getColumnName(i) );
  +            int padding = rsmd.getColumnDisplaySize(i) - rsmd.getColumnName(i).length()
+ 1;
  +            for( int j = 0; j < padding; j++ )
  +            {
  +                row.append( ' ' );
  +            }
  +        }
  +        row.append( '|' );
  +        return row.toString();
  +    }
  +    
  +    /**
  +     * Formats one row of a result set. The line consists of the column values separated
by '|' and padded
  +     * with spaces.
  +     */
  +    private static String formatRow( ResultSetMetaData rsmd, ResultSet rs ) throws SQLException
  +    {
  +        StringBuffer row = new StringBuffer();
  +        for( int i = 1; i <= rsmd.getColumnCount(); i++ )
  +        {
  +            row.append( "| " );
  +            String value = rs.getObject(i) != null ? rs.getObject(i).toString() : "null";
  +            row.append( value );
  +            int padding = rsmd.getColumnDisplaySize(i) - value.length() + 1;
  +            for( int j = 0; j < padding; j++ )
  +            {
  +                row.append( ' ' );
  +            }
  +        }
  +        row.append( '|' );
  +        return row.toString();
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message