JndiDatabaseTester.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 java.util.Properties;

  25. import javax.naming.Context;
  26. import javax.naming.InitialContext;
  27. import javax.naming.NamingException;
  28. import javax.sql.DataSource;

  29. import org.dbunit.database.DatabaseConnection;
  30. import org.dbunit.database.IDatabaseConnection;

  31. /**
  32.  * DatabaseTester that pulls a DataSource from a JNDI location.
  33.  *
  34.  * @author Andres Almiray (aalmiray@users.sourceforge.net)
  35.  * @author Last changed by: $Author$
  36.  * @version $Revision$ $Date$
  37.  * @since 2.2.0
  38.  */
  39. public class JndiDatabaseTester extends AbstractDatabaseTester
  40. {

  41.     /**
  42.      * Logger for this class
  43.      */
  44.     private static final Logger logger = LoggerFactory.getLogger(JndiDatabaseTester.class);

  45.     private DataSource dataSource;
  46.     private Properties environment;
  47.     private boolean initialized = false;
  48.     private String lookupName;

  49.     /**
  50.      * Creates a JndiDatabaseTester with default JNDI properties.
  51.      *
  52.      * @param lookupName the name of the resource in the JNDI context
  53.      */
  54.     public JndiDatabaseTester(String lookupName)
  55.     {
  56.         this(null, lookupName);
  57.     }

  58.     /**
  59.      * Creates a JndiDatabaseTester with specific JNDI properties.
  60.      *
  61.      * @param environment A Properties object with JNDI properties. Can be null
  62.      * @param lookupName the name of the resource in the JNDI context
  63.      */
  64.     public JndiDatabaseTester(Properties environment, String lookupName)
  65.     {
  66.         this(environment, lookupName, null);
  67.     }

  68.     /**
  69.      * Creates a JndiDatabaseTester with specific JNDI properties.
  70.      *
  71.      * @param environment A Properties object with JNDI properties. Can be <code>null</code>
  72.      * @param lookupName the name of the resource in the JNDI context
  73.      * @param schema The schema name to be used for new dbunit connections. Can be <code>null</code>
  74.      * @since 2.4.5
  75.      */
  76.     public JndiDatabaseTester(Properties environment, String lookupName, String schema)
  77.     {
  78.         super(schema);

  79.         if (lookupName == null) {
  80.             throw new NullPointerException(
  81.                     "The parameter 'lookupName' must not be null");
  82.         }
  83.         this.lookupName = lookupName;
  84.         this.environment = environment;
  85.     }

  86.     public IDatabaseConnection getConnection() throws Exception
  87.     {
  88.         logger.trace("getConnection() - start");

  89.         if( !initialized ){
  90.             initialize();
  91.         }

  92.         return new DatabaseConnection( dataSource.getConnection(), getSchema() );
  93.     }

  94.     /**
  95.      * Verifies the configured properties and locates the Datasource through
  96.      * JNDI.<br>
  97.      * This method is called by {@link getConnection} if the tester has not been
  98.      * initialized yet.
  99.      */
  100.     private void initialize() throws NamingException
  101.     {
  102.         logger.trace("initialize() - start");

  103.         Context context = new InitialContext( environment );
  104.         assertNotNullNorEmpty( "lookupName", lookupName );
  105.         Object obj = context.lookup( lookupName );
  106.         assertTrue( "JNDI object with [" + lookupName + "] not found", obj!=null );
  107.         assertTrue( "Object [" + obj + "] at JNDI location [" + lookupName
  108.                 + "] is not of type [" + DataSource.class.getName() + "]", obj instanceof DataSource );
  109.         dataSource = (DataSource) obj;
  110.         assertTrue( "DataSource is not set", dataSource!=null );
  111.         initialized = true;
  112.     }

  113.     public String toString()
  114.     {
  115.         final StringBuilder sb = new StringBuilder();
  116.         sb.append(getClass().getName()).append("[");
  117.         sb.append("lookupName=").append(this.lookupName);
  118.         sb.append(", environment=").append(this.environment);
  119.         sb.append(", initialized=").append(this.initialized);
  120.         sb.append(", dataSource=").append(this.dataSource);
  121.         sb.append(", schema=").append(super.getSchema());
  122.         sb.append("]");
  123.         return sb.toString();
  124.     }

  125. }