JdbcDatabaseTester.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit;

  22. import java.sql.Connection;
  23. import java.sql.DriverManager;

  24. import org.dbunit.database.DatabaseConnection;
  25. import org.dbunit.database.IDatabaseConnection;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;

  28. /**
  29.  * DatabaseTester that uses JDBC's Driver Manager to create connections.<br>
  30.  *
  31.  * @author Andres Almiray (aalmiray@users.sourceforge.net)
  32.  * @author Felipe Leme (dbunit@felipeal.net)
  33.  * @version $Revision$
  34.  * @since 2.2
  35.  */
  36. public class JdbcDatabaseTester extends AbstractDatabaseTester
  37. {

  38.     /**
  39.      * Logger for this class
  40.      */
  41.     private static final Logger logger = LoggerFactory.getLogger(JdbcDatabaseTester.class);

  42.     private String connectionUrl;
  43.     private String driverClass;
  44.     private String password;
  45.     private String username;

  46.     /**
  47.      * Creates a new JdbcDatabaseTester with the specified properties.<br>
  48.      * Username and Password are set to null.
  49.      *
  50.      * @param driverClass the classname of the JDBC driver to use
  51.      * @param connectionUrl the connection url
  52.      * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
  53.      */
  54.     public JdbcDatabaseTester( String driverClass, String connectionUrl )
  55.     throws ClassNotFoundException
  56.     {
  57.         this( driverClass, connectionUrl, null, null );
  58.     }

  59.     /**
  60.      * Creates a new JdbcDatabaseTester with the specified properties.
  61.      *
  62.      * @param driverClass the classname of the JDBC driver to use
  63.      * @param connectionUrl the connection url
  64.      * @param username a username that can has access to the database
  65.      * @param password the user's password
  66.      * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
  67.      */
  68.     public JdbcDatabaseTester( String driverClass, String connectionUrl, String username,
  69.             String password )
  70.     throws ClassNotFoundException
  71.     {
  72.         this(driverClass, connectionUrl, username, password, null);
  73.     }

  74.     /**
  75.      * Creates a new JdbcDatabaseTester with the specified properties.
  76.      *
  77.      * @param driverClass the classname of the JDBC driver to use
  78.      * @param connectionUrl the connection url
  79.      * @param username a username that can has access to the database - can be <code>null</code>
  80.      * @param password the user's password - can be <code>null</code>
  81.      * @param schema the database schema to be tested - can be <code>null</code>
  82.      * @throws ClassNotFoundException If the given <code>driverClass</code> was not found
  83.      * @since 2.4.3
  84.      */
  85.     public JdbcDatabaseTester( String driverClass, String connectionUrl, String username,
  86.             String password, String schema )
  87.     throws ClassNotFoundException
  88.     {
  89.         super(schema);
  90.         this.driverClass = driverClass;
  91.         this.connectionUrl = connectionUrl;
  92.         this.username = username;
  93.         this.password = password;
  94.        
  95.         assertNotNullNorEmpty( "driverClass", driverClass );
  96.         Class.forName( driverClass );
  97.     }

  98.     public IDatabaseConnection getConnection() throws Exception
  99.     {
  100.         logger.debug("getConnection() - start");

  101.         assertNotNullNorEmpty( "connectionUrl", connectionUrl );
  102.         Connection conn = null;
  103.         if( username == null && password == null ){
  104.             conn = DriverManager.getConnection( connectionUrl );
  105.         }else{
  106.             conn = DriverManager.getConnection( connectionUrl, username, password );
  107.         }
  108.         return new DatabaseConnection( conn, getSchema() );
  109.     }

  110.     public String toString()
  111.     {
  112.         final StringBuilder sb = new StringBuilder();
  113.         sb.append(getClass().getName()).append("[");
  114.         sb.append("connectionUrl=").append(this.connectionUrl);
  115.         sb.append(", driverClass=").append(this.driverClass);
  116.         sb.append(", username=").append(this.username);
  117.         sb.append(", password=**********");
  118.         sb.append(", schema=").append(super.getSchema());
  119.         sb.append("]");
  120.         return sb.toString();
  121.     }

  122. }