View Javadoc
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  
22  package org.dbunit.ant;
23  
24  import org.dbunit.DatabaseUnitException;
25  import org.dbunit.database.IDatabaseConnection;
26  import org.dbunit.database.DatabaseSequenceFilter;
27  import org.dbunit.dataset.IDataSet;
28  import org.dbunit.dataset.ReplacementDataSet;
29  import org.dbunit.dataset.FilteredDataSet;
30  import org.dbunit.ext.mssql.InsertIdentityOperation;
31  import org.dbunit.operation.DatabaseOperation;
32  import org.dbunit.operation.TransactionOperation;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import java.io.File;
37  import java.sql.SQLException;
38  
39  /**
40   * The <code>Operation</code> class is the step that defines which
41   * operation will be performed in the execution of the <code>DbUnitTask</code>
42   * task.
43   *
44   * @author Timothy Ruppert
45   * @author Ben Cox
46   * @version $Revision$
47   * @since Jun 10, 2002
48   * @see org.dbunit.ant.DbUnitTaskStep
49   */
50  public class Operation extends AbstractStep
51  {
52  
53      /**
54       * Logger for this class
55       */
56      private static final Logger logger = LoggerFactory.getLogger(Operation.class);
57  
58      private static final String DEFAULT_FORMAT = FORMAT_FLAT;
59  
60      protected String _type = "CLEAN_INSERT";
61      private String _format;
62      private File _src;
63      private boolean _transaction = false;
64      private DatabaseOperation _operation;
65      private boolean _forwardOperation = true;
66      private String _nullToken;
67  
68      public File getSrc()
69      {
70          return _src;
71      }
72  
73      public void setSrc(File src)
74      {
75          _src = src;
76      }
77  
78      public String getFormat()
79      {
80          return _format != null ? _format : DEFAULT_FORMAT;
81      }
82  
83      public void setFormat(String format)
84      {
85          logger.debug("setFormat(format={}) - start", format);
86  
87          // Check if the given format is accepted
88          checkDataFormat(format);
89          // If we get here the given format is a valid data format
90          _format = format;
91      }
92  
93      public boolean isTransaction()
94      {
95          return _transaction;
96      }
97  
98      public void setTransaction(boolean transaction)
99      {
100         _transaction = transaction;
101     }
102 
103     public String getNullToken() 
104     {
105         return _nullToken;
106     }
107 
108     public void setNullToken(final String nullToken) 
109     {
110         this._nullToken = nullToken;
111     }
112 
113     public DatabaseOperation getDbOperation()
114     {
115         return _operation;
116     }
117 
118     public String getType()
119     {
120         return _type;
121     }
122 
123     public void setType(String type) 
124     {
125         logger.debug("setType(type={}) - start", type);
126 
127         if ("UPDATE".equals(type)) {
128             _operation = DatabaseOperation.UPDATE;
129             _forwardOperation = true;
130         } else if ("INSERT".equals(type)) {
131             _operation = DatabaseOperation.INSERT;
132             _forwardOperation = true;
133         } else if ("REFRESH".equals(type)) {
134             _operation = DatabaseOperation.REFRESH;
135             _forwardOperation = true;
136         } else if ("DELETE".equals(type)) {
137             _operation = DatabaseOperation.DELETE;
138             _forwardOperation = false;
139         } else if ("DELETE_ALL".equals(type)) {
140             _operation = DatabaseOperation.DELETE_ALL;
141             _forwardOperation = false;
142         } else if ("CLEAN_INSERT".equals(type)) {
143             _operation = DatabaseOperation.CLEAN_INSERT;
144             _forwardOperation = false;
145         } else if ("NONE".equals(type)) {
146             _operation = DatabaseOperation.NONE;
147             _forwardOperation = true;
148         } else if ("MSSQL_CLEAN_INSERT".equals(type)) {
149             _operation = InsertIdentityOperation.CLEAN_INSERT;
150             _forwardOperation = false;
151         } else if ("MSSQL_INSERT".equals(type)) {
152             _operation = InsertIdentityOperation.INSERT;
153             _forwardOperation = true;
154         } else if ("MSSQL_REFRESH".equals(type)) {
155             _operation = InsertIdentityOperation.REFRESH;
156             _forwardOperation = true;
157         } else {
158             throw new IllegalArgumentException("Type must be one of: UPDATE, INSERT,"
159                     + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, "
160                     + " or MSSQL_REFRESH but was: " + type);
161         }
162         _type = type;
163     }
164 
165     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
166     {
167         logger.debug("execute(connection={}) - start", connection);
168         if (_operation == null)
169         {
170             throw new DatabaseUnitException("Operation.execute(): setType(String) must be called before execute()!");
171         }
172 
173         if (_operation == DatabaseOperation.NONE)
174         {
175             return;
176         }
177 
178         try {
179             DatabaseOperation operation = (_transaction ? new TransactionOperation(_operation) : _operation);
180             // TODO This is not very nice and the design should be reviewed but it works for now (gommma)
181             boolean useForwardOnly = _forwardOperation && ! isOrdered();
182             IDataSet dataset = getSrcDataSet(getSrc(), getFormat(), useForwardOnly);
183             if (_nullToken != null) {
184                 dataset = new ReplacementDataSet(dataset);
185                 ((ReplacementDataSet)dataset).addReplacementObject(_nullToken, null);
186             }
187             if(isOrdered()) 
188             {
189                 DatabaseSequenceFilter databaseSequenceFilter = new DatabaseSequenceFilter(connection);
190                 dataset = new FilteredDataSet(databaseSequenceFilter, dataset);
191             }
192             operation.execute(connection, dataset);
193         }
194         catch (SQLException e)
195         {
196             throw new DatabaseUnitException(e);
197         }
198     }
199 
200     public String getLogMessage()
201     {
202         return "Executing operation: " + _type
203                 + "\n          on   file: " + ((_src == null) ? null : _src.getAbsolutePath())
204                 + "\n          with format: " + _format;
205     }
206 
207 
208     public String toString()
209     {
210         StringBuffer result = new StringBuffer();
211         result.append("Operation: ");
212         result.append(" type=").append(_type);
213         result.append(", format=").append(_format);
214         result.append(", src=").append(_src == null ? "null" : _src.getAbsolutePath());
215         result.append(", operation=").append(_operation);
216         result.append(", nullToken=").append(_nullToken);
217         result.append(", ordered=").append(super.isOrdered());
218         return result.toString();
219     }
220 }