View Javadoc
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  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Properties;
26  import java.util.Set;
27  
28  import org.apache.tools.ant.ProjectComponent;
29  import org.apache.tools.ant.taskdefs.Property;
30  import org.dbunit.DatabaseUnitException;
31  import org.dbunit.database.DatabaseConfig;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * The database configuration for the ant task.
37   * 
38   * @author gommma (gommma AT users.sourceforge.net)
39   * @author Last changed by: $Author$
40   * @version $Revision$ $Date$
41   * @since 2.4.0
42   */
43  public class DbConfig extends ProjectComponent
44  {
45      /**
46       * Logger for this class
47       */
48      private static final Logger logger = LoggerFactory.getLogger(AbstractStep.class);
49  
50      private Set properties = new HashSet();
51      private Set features = new HashSet();
52      
53      public DbConfig()
54      {
55      }
56  
57      public void addProperty(Property property)
58      {
59          logger.trace("addProperty(property={}) - start)", property);
60          
61          this.properties.add(property);
62      }
63  
64      public void addFeature(Feature feature)
65      {
66          logger.trace("addFeature(feature={}) - start)", feature);
67          
68          this.features.add(feature);
69      }
70  
71      /**
72       * Copies the parameters set in this configuration via ant into the given
73       * {@link DatabaseConfig} that is used by the dbunit connection.
74       * @param config The configuration object to be initialized/updated
75       * @throws DatabaseUnitException 
76       */
77      public void copyTo(DatabaseConfig config) throws DatabaseUnitException 
78      {
79          Properties javaProps = new Properties();
80          
81          for (Iterator iterator = this.features.iterator(); iterator.hasNext();) {
82              Feature feature = (Feature)iterator.next();
83              
84              String propName = feature.getName();
85              String propValue = String.valueOf(feature.isValue());
86              
87              logger.debug("Setting property {}", feature);
88              javaProps.setProperty(propName, propValue);
89          }
90          
91          // Copy the properties into java.util.Properties
92          for (Iterator iterator = this.properties.iterator(); iterator.hasNext();) {
93              Property prop = (Property) iterator.next();
94              
95              String propName = prop.getName();
96              String propValue = prop.getValue();
97  
98              if(propName==null)
99                  throw new NullPointerException("The propName must not be null");
100             
101             if(propValue==null)
102                 throw new NullPointerException("The propValue must not be null");
103 
104             logger.debug("Setting property {}", prop);
105             javaProps.setProperty(propName, propValue);
106         }
107         
108         config.setPropertiesByString(javaProps);
109     }
110 
111     /**
112      * @author gommma (gommma AT users.sourceforge.net)
113      * @author Last changed by: $Author$
114      * @version $Revision$ $Date$
115      * @since 2.4.0
116      */
117     public static class Feature
118     {
119         private String name;
120         private boolean value;
121         
122         public String getName() {
123             return name;
124         }
125         public void setName(String name) {
126             this.name = name;
127         }
128         public boolean isValue() {
129             return value;
130         }
131         public void setValue(boolean value) {
132             this.value = value;
133         }
134     }
135 }