View Javadoc
1   package org.codehaus.mojo.dbunit;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   * 
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   * 
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25  */
26  
27  import java.sql.Connection;
28  import java.sql.Driver;
29  import java.sql.SQLException;
30  import java.util.Properties;
31  
32  import org.apache.maven.plugin.AbstractMojo;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.settings.Server;
36  import org.apache.maven.settings.Settings;
37  import org.dbunit.database.DatabaseConfig;
38  import org.dbunit.database.DatabaseConnection;
39  import org.dbunit.database.ForwardOnlyResultSetTableFactory;
40  import org.dbunit.database.IDatabaseConnection;
41  import org.dbunit.database.IMetadataHandler;
42  import org.dbunit.dataset.datatype.IDataTypeFactory;
43  
44  /**
45   * Common configurations for all DBUnit operations
46   * @author <a href="mailto:dantran@gmail.com">Dan Tran</a>
47   * @author <a href="mailto:topping@codehaus.org">Brian Topping</a>
48   * @version $Id$
49   * @requiresDependencyResolution compile
50   */
51  public abstract class AbstractDbUnitMojo
52      extends AbstractMojo
53  {
54  
55      /**
56       * The class name of the JDBC driver to be used.
57       * 
58       * @parameter expression="${driver}" 
59       * @required
60       */
61      protected String driver;
62  
63      /**
64       * Database username.  If not given, it will be looked up through 
65       * settings.xml's server with ${settingsKey} as key
66       * @parameter expression="${username}" 
67       */
68      protected String username;
69  
70      /**
71       * Database password. If not given, it will be looked up through settings.xml's 
72       * server with ${settingsKey} as key
73       * @parameter expression="${password}" 
74       */
75      protected String password;
76  
77      /**
78       * The JDBC URL for the database to access, e.g. jdbc:db2:SAMPLE.
79       * 
80       * @parameter
81       * @required expression="${url}" 
82       */
83      protected String url;
84  
85      /**
86       * The schema name that tables can be found under.
87       * 
88       * @parameter expression="${schema}" 
89       */
90      protected String schema;
91  
92      /**
93       * Set the DataType factory to add support for non-standard database vendor data types.
94       * 
95       * @parameter expression="${dataTypeFactoryName}" default-value="org.dbunit.dataset.datatype.DefaultDataTypeFactory"
96       */
97      protected String dataTypeFactoryName = "org.dbunit.dataset.datatype.DefaultDataTypeFactory";
98  
99      /**
100      * Enable or disable usage of JDBC batched statement by DbUnit
101      * @parameter expression="${supportBatchStatement}" default-value="false"
102      */
103     protected boolean supportBatchStatement;
104 
105     /**
106      * Enable or disable multiple schemas support by prefixing table names with the schema name.
107      * 
108      * @parameter expression="${useQualifiedTableNames}" default-value="false"
109      */
110     protected boolean useQualifiedTableNames;
111 
112     /**
113      * Enable or disable the warning message displayed when DbUnit encounter an unsupported data type.
114      * @parameter expression="${datatypeWarning}" default-value="false"
115      */
116     protected boolean datatypeWarning;
117 
118     /**
119      * escapePattern
120      * 
121      * @parameter expression="${escapePattern}" 
122      */
123     protected String escapePattern;
124 
125     /**
126      * skipOracleRecycleBinTables
127      * 
128      * @parameter expression="${escapePattern}" default-value="false"
129      * @since 1.0-beta-2
130      */
131     protected boolean skipOracleRecycleBinTables;
132     
133     /**
134      * Skip the execution when true, very handy when using together with maven.test.skip.
135      * 
136      * @parameter expression="${skip}" default-value="false"
137      */
138     protected boolean skip;
139     
140     /**
141      * Access to hidding username/password
142      * @parameter expression="${settings}"
143      * @readonly
144      */
145     private Settings settings;
146 
147     /**
148      * Server's id in settings.xml to look up username and password.
149      * Default to ${url} if not given.
150      * @parameter expression="${settingsKey}" 
151      */
152     private String settingsKey;
153 
154     /**
155      * Class name of metadata handler.
156      * @parameter expression="${metadataHandlerName}" default-value="org.dbunit.database.DefaultMetadataHandler"
157      * @since 1.0-beta-3
158      */
159     protected String metadataHandlerName;
160 
161     /**
162      * Be case sensitive when handling tables.
163      * @see http://www.dbunit.org/properties.html#casesensitivetablenames
164      * 
165      * @parameter default-value="false"
166      */
167     private boolean caseSensitiveTableNames;
168 
169 
170     ////////////////////////////////////////////////////////////////////
171 
172 
173     public void execute()
174         throws MojoExecutionException, MojoFailureException
175     {
176         loadUserInfoFromSettings();
177     }
178 
179     IDatabaseConnection createConnection()
180         throws Exception
181     {
182 
183         // Instantiate JDBC driver
184         Class dc = Class.forName( driver );
185         Driver driverInstance = (Driver) dc.newInstance();
186         Properties info = new Properties();
187         info.put( "user", username );
188 
189         if ( password != null )
190         {
191             info.put( "password", password );
192         }
193 
194         Connection conn = driverInstance.connect( url, info );
195 
196         if ( conn == null )
197         {
198             // Driver doesn't understand the URL
199             throw new SQLException( "No suitable Driver for " + url );
200         }
201         conn.setAutoCommit( true );
202 
203         IDatabaseConnection connection = new DatabaseConnection( conn, schema );
204         DatabaseConfig config = connection.getConfig();
205         config.setFeature( DatabaseConfig.FEATURE_BATCHED_STATEMENTS, supportBatchStatement );
206         config.setFeature( DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, useQualifiedTableNames );
207         config.setFeature( DatabaseConfig.FEATURE_DATATYPE_WARNING, datatypeWarning );
208         config.setFeature( DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES, this.skipOracleRecycleBinTables );
209         config.setFeature( DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES, caseSensitiveTableNames );
210         
211         config.setProperty( DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern );
212         config.setProperty( DatabaseConfig.PROPERTY_RESULTSET_TABLE_FACTORY, new ForwardOnlyResultSetTableFactory() );
213 
214         // Setup data type factory
215         IDataTypeFactory dataTypeFactory = (IDataTypeFactory) Class.forName( dataTypeFactoryName ).newInstance();
216         config.setProperty( DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory );
217 
218         // Setup metadata handler
219         IMetadataHandler metadataHandler = (IMetadataHandler) Class.forName( metadataHandlerName ).newInstance();
220         config.setProperty( DatabaseConfig.PROPERTY_METADATA_HANDLER, metadataHandler );
221 
222         return connection;
223     }
224 
225     /**
226      * Load username password from settings if user has not set them in JVM properties
227      */
228     private void loadUserInfoFromSettings()
229         throws MojoExecutionException
230     {
231         if ( this.settingsKey == null )
232         {
233             this.settingsKey = url;
234         }
235 
236         if ( ( username == null || password == null ) && ( settings != null ) )
237         {
238             Server server = this.settings.getServer( this.settingsKey );
239 
240             if ( server != null )
241             {
242                 if ( username == null )
243                 {
244                     username = server.getUsername();
245                 }
246 
247                 if ( password == null )
248                 {
249                     password = server.getPassword();
250                 }
251             }
252         }
253 
254         if ( username == null )
255         {
256             //allow emtpy username
257             username =  "" ;
258         }
259 
260         if ( password == null )
261         {
262             //allow emtpy password
263             password = "" ;
264         }
265     }
266 
267 
268 }