View Javadoc

1   package net.sourceforge.mavenplugins.dbunit;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2002 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Maven" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Maven", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   *
56   * ====================================================================
57   */
58  
59  import org.apache.commons.lang.StringUtils;
60  import org.dbunit.DatabaseUnitException;
61  import org.dbunit.database.DatabaseSequenceFilter;
62  import org.dbunit.database.IDatabaseConnection;
63  import org.dbunit.dataset.DataSetException;
64  import org.dbunit.dataset.FilteredDataSet;
65  import org.dbunit.dataset.IDataSet;
66  import org.dbunit.dataset.filter.ITableFilter;
67  import org.dbunit.ext.mssql.InsertIdentityOperation;
68  import org.dbunit.operation.DatabaseOperation;
69  
70  import java.io.IOException;
71  import java.sql.SQLException;
72  
73  public class DataSetTool extends DbUnitTool
74  {
75      /** the type of data set being processed */
76      private String dataSetFormat;
77      /** the operation to perform on the data set */
78      private String operation;
79      /** dbunit database operation based on the string */
80      private DatabaseOperation dbOperation;
81      /** list of comma separated table names to export */
82      private String exportTables;
83      /** export psuedo operation */
84      private static final String EXPORT = "EXPORT";
85      /** format for excel files */
86      private static final String EXCEL = "EXCEL";
87  
88      /**
89       * @return the format of the data set.
90       */
91      public String getDataSetFormat()
92      {
93          return dataSetFormat;
94      }
95  
96      /**
97       * TODO better document this
98       * Set the format of the dataset, e.g. EXCEL
99       * @param name the dbunit dataset format
100      */
101     public void setDataSetFormat(String name)
102     {
103         dataSetFormat = name;
104     }
105 
106     /**
107      * The operation to perform on a dataset, e.g.
108      * <code>INSERT</code>, <code>CLEAN_INSERT</code>, <code>DELETE</code> or
109      * <code>EXPORT</code>
110      */
111     public String getOperation()
112     {
113         return operation;
114     }
115 
116     /**
117      * @return the operation to perform on a dataset
118      * @see #getOperation()
119      */
120     public void setOperation(String name)
121     {
122         operation = name;
123         if (operation == null) operation = "INSERT";
124         if (operation.equalsIgnoreCase("CLEAN_INSERT")) dbOperation = DatabaseOperation.CLEAN_INSERT;
125         if (operation.equalsIgnoreCase("DELETE")) dbOperation = DatabaseOperation.DELETE;
126         if (operation.equalsIgnoreCase("INSERT")) dbOperation = DatabaseOperation.INSERT;
127         if (operation.equalsIgnoreCase("MSSQL_CLEAN_INSERT")) dbOperation = InsertIdentityOperation.CLEAN_INSERT;
128     }
129 
130     /**
131      * @return a comma separated list of table names
132      */
133     public String getExportTables()
134     {
135         return exportTables;
136     }
137 
138     /**
139      * set the list of tables to be exported
140      * @param tables a comma separated list of table names
141      */
142     public void setExportTables(String tables)
143     {
144         exportTables = tables;
145     }
146 
147     /**
148      * @return the dataset to operate on from the given connection
149      */
150     private IDataSet getDataSet(IDatabaseConnection connection) throws SQLException
151     {
152     	IDataSet dataSet = null;
153         if (getExportTables() == null)
154         {
155         	dataSet = connection.createDataSet();
156         }
157         else
158         {
159 	        String[] tables = StringUtils.split(getExportTables(), ",");
160         	dataSet = connection.createDataSet(tables);
161         }
162         try
163         {
164 	        ITableFilter filter = new DatabaseSequenceFilter(connection);
165 	        return new FilteredDataSet(filter, dataSet);
166         }
167         catch (DataSetException e)
168         {
169         		throw new SQLException(e.getMessage());
170         }
171     }
172 
173     /**
174      * Process a dataset using a DbUnit operation
175      */
176     public void process() throws ClassNotFoundException, SQLException, IOException, DataSetException, DatabaseUnitException
177     {
178         if (getOperation().equalsIgnoreCase(EXPORT))
179         {
180             IDatabaseConnection connection= getConnection();
181             writeDataSetToFile(getFileName(), getDataSetFormat(), getDataSet(connection));
182             connection.close();
183         }
184         else if (dbOperation != null)
185         {
186             // TODO create a dataset of the right type
187             IDataSet dataset = getDataSetFromFile(getFileName(), getDataSetFormat());
188             dbOperation.execute(getConnection(), dataset);
189         }
190         else
191         {
192             throw new IllegalArgumentException("Invalid value for maven.dbunit.importType");
193         	}
194     }
195     
196 }