DataSourceDatabaseTester.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 org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import javax.sql.DataSource;

  25. import org.dbunit.database.DatabaseConnection;
  26. import org.dbunit.database.IDatabaseConnection;

  27. /**
  28.  * DatabaseTester that uses a {@link DataSource} to create connections.
  29.  *
  30.  * @author Andres Almiray (aalmiray@users.sourceforge.net)
  31.  * @author Felipe Leme (dbunit@felipeal.net)
  32.  * @author Last changed by: $Author$
  33.  * @version $Revision$ $Date$
  34.  * @since 2.2.0
  35.  */
  36. public class DataSourceDatabaseTester extends AbstractDatabaseTester
  37. {

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

  42.     private DataSource dataSource;

  43.     /**
  44.      * Creates a new DataSourceDatabaseTester with the specified DataSource.
  45.      *
  46.      * @param dataSource the DataSource to pull connections from
  47.      */
  48.     public DataSourceDatabaseTester( DataSource dataSource )
  49.     {
  50.         super();

  51.         if (dataSource == null) {
  52.             throw new NullPointerException(
  53.                     "The parameter 'dataSource' must not be null");
  54.         }
  55.         this.dataSource = dataSource;
  56.     }

  57.     /**
  58.      * Creates a new DataSourceDatabaseTester with the specified DataSource and schema name.
  59.      * @param dataSource the DataSource to pull connections from
  60.      * @param schema The schema name to be used for new dbunit connections
  61.      * @since 2.4.5
  62.      */
  63.     public DataSourceDatabaseTester(DataSource dataSource, String schema)
  64.     {
  65.         super(schema);
  66.        
  67.         if (dataSource == null) {
  68.             throw new NullPointerException(
  69.                     "The parameter 'dataSource' must not be null");
  70.         }
  71.         this.dataSource = dataSource;
  72.     }

  73.     public IDatabaseConnection getConnection() throws Exception
  74.     {
  75.         logger.debug("getConnection() - start");

  76.         assertTrue( "DataSource is not set", dataSource!=null );
  77.         return new DatabaseConnection( dataSource.getConnection(), getSchema() );
  78.     }
  79. }