DbConfig.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2008, 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.ant;

  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.Properties;
  25. import java.util.Set;

  26. import org.apache.tools.ant.ProjectComponent;
  27. import org.apache.tools.ant.taskdefs.Property;
  28. import org.dbunit.DatabaseUnitException;
  29. import org.dbunit.database.DatabaseConfig;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;

  32. /**
  33.  * The database configuration for the ant task.
  34.  *
  35.  * @author gommma (gommma AT users.sourceforge.net)
  36.  * @author Last changed by: $Author$
  37.  * @version $Revision$ $Date$
  38.  * @since 2.4.0
  39.  */
  40. public class DbConfig extends ProjectComponent
  41. {
  42.     /**
  43.      * Logger for this class
  44.      */
  45.     private static final Logger logger = LoggerFactory.getLogger(AbstractStep.class);

  46.     private Set properties = new HashSet();
  47.     private Set features = new HashSet();
  48.    
  49.     public DbConfig()
  50.     {
  51.     }

  52.     public void addProperty(Property property)
  53.     {
  54.         logger.trace("addProperty(property={}) - start)", property);
  55.        
  56.         this.properties.add(property);
  57.     }

  58.     public void addFeature(Feature feature)
  59.     {
  60.         logger.trace("addFeature(feature={}) - start)", feature);
  61.        
  62.         this.features.add(feature);
  63.     }

  64.     /**
  65.      * Copies the parameters set in this configuration via ant into the given
  66.      * {@link DatabaseConfig} that is used by the dbunit connection.
  67.      * @param config The configuration object to be initialized/updated
  68.      * @throws DatabaseUnitException
  69.      */
  70.     public void copyTo(DatabaseConfig config) throws DatabaseUnitException
  71.     {
  72.         Properties javaProps = new Properties();
  73.        
  74.         for (Iterator iterator = this.features.iterator(); iterator.hasNext();) {
  75.             Feature feature = (Feature)iterator.next();
  76.            
  77.             String propName = feature.getName();
  78.             String propValue = String.valueOf(feature.isValue());
  79.            
  80.             logger.debug("Setting property {}", feature);
  81.             javaProps.setProperty(propName, propValue);
  82.         }
  83.        
  84.         // Copy the properties into java.util.Properties
  85.         for (Iterator iterator = this.properties.iterator(); iterator.hasNext();) {
  86.             Property prop = (Property) iterator.next();
  87.            
  88.             String propName = prop.getName();
  89.             String propValue = prop.getValue();

  90.             if(propName==null)
  91.                 throw new NullPointerException("The propName must not be null");
  92.            
  93.             if(propValue==null)
  94.                 throw new NullPointerException("The propValue must not be null");

  95.             logger.debug("Setting property {}", prop);
  96.             javaProps.setProperty(propName, propValue);
  97.         }
  98.        
  99.         config.setPropertiesByString(javaProps);
  100.     }

  101.     /**
  102.      * @author gommma (gommma AT users.sourceforge.net)
  103.      * @author Last changed by: $Author$
  104.      * @version $Revision$ $Date$
  105.      * @since 2.4.0
  106.      */
  107.     public static class Feature
  108.     {
  109.         private String name;
  110.         private boolean value;
  111.        
  112.         public String getName() {
  113.             return name;
  114.         }
  115.         public void setName(String name) {
  116.             this.name = name;
  117.         }
  118.         public boolean isValue() {
  119.             return value;
  120.         }
  121.         public void setValue(boolean value) {
  122.             this.value = value;
  123.         }
  124.     }
  125. }