001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.ant;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.DatabaseUnitException;
028: import org.dbunit.database.IDatabaseConnection;
029: import org.dbunit.dataset.IDataSet;
030: import org.dbunit.ext.mssql.InsertIdentityOperation;
031: import org.dbunit.operation.DatabaseOperation;
032: import org.dbunit.operation.TransactionOperation;
033:
034: import java.io.File;
035: import java.sql.SQLException;
036:
037: /**
038: * The <code>Operation</code> class is the step that defines which
039: * operation will be performed in the execution of the <code>DbUnitTask</code>
040: * task.
041: *
042: * @author Timothy Ruppert
043: * @author Ben Cox
044: * @version $Revision: 554 $
045: * @since Jun 10, 2002
046: * @see org.dbunit.ant.DbUnitTaskStep
047: */
048: public class Operation extends AbstractStep {
049:
050: /**
051: * Logger for this class
052: */
053: private static final Logger logger = LoggerFactory
054: .getLogger(Operation.class);
055:
056: private static final String DEFAULT_FORMAT = FORMAT_FLAT;
057:
058: protected String _type = "CLEAN_INSERT";
059: private String _format;
060: private File _src;
061: private boolean _transaction = false;
062: private DatabaseOperation _operation;
063: private boolean _forwardOperation = true;
064:
065: public String getType() {
066: logger.debug("getType() - start");
067:
068: return _type;
069: }
070:
071: public File getSrc() {
072: logger.debug("getSrc() - start");
073:
074: return _src;
075: }
076:
077: public DatabaseOperation getDbOperation() {
078: logger.debug("getDbOperation() - start");
079:
080: return _operation;
081: }
082:
083: public String getFormat() {
084: logger.debug("getFormat() - start");
085:
086: return _format != null ? _format : DEFAULT_FORMAT;
087: }
088:
089: public boolean isTransaction() {
090: logger.debug("isTransaction() - start");
091:
092: return _transaction;
093: }
094:
095: public void setType(String type) {
096: logger.debug("setType(type=" + type + ") - start");
097:
098: if ("UPDATE".equals(type)) {
099: _operation = DatabaseOperation.UPDATE;
100: _forwardOperation = true;
101: } else if ("INSERT".equals(type)) {
102: _operation = DatabaseOperation.INSERT;
103: _forwardOperation = true;
104: } else if ("REFRESH".equals(type)) {
105: _operation = DatabaseOperation.REFRESH;
106: _forwardOperation = true;
107: } else if ("DELETE".equals(type)) {
108: _operation = DatabaseOperation.DELETE;
109: _forwardOperation = false;
110: } else if ("DELETE_ALL".equals(type)) {
111: _operation = DatabaseOperation.DELETE_ALL;
112: _forwardOperation = false;
113: } else if ("CLEAN_INSERT".equals(type)) {
114: _operation = DatabaseOperation.CLEAN_INSERT;
115: _forwardOperation = false;
116: } else if ("NONE".equals(type)) {
117: _operation = DatabaseOperation.NONE;
118: _forwardOperation = true;
119: } else if ("MSSQL_CLEAN_INSERT".equals(type)) {
120: _operation = InsertIdentityOperation.CLEAN_INSERT;
121: _forwardOperation = false;
122: } else if ("MSSQL_INSERT".equals(type)) {
123: _operation = InsertIdentityOperation.INSERT;
124: _forwardOperation = true;
125: } else if ("MSSQL_REFRESH".equals(type)) {
126: _operation = InsertIdentityOperation.REFRESH;
127: _forwardOperation = true;
128: } else {
129: throw new IllegalArgumentException(
130: "Type must be one of: UPDATE, INSERT,"
131: + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, "
132: + " or MSSQL_REFRESH but was: " + type);
133: }
134: _type = type;
135: }
136:
137: public void setSrc(File src) {
138: logger.debug("setSrc(src=" + src + ") - start");
139:
140: _src = src;
141: }
142:
143: public void setFormat(String format) {
144: logger.debug("setFormat(format=" + format + ") - start");
145:
146: if (format.equalsIgnoreCase(FORMAT_FLAT)
147: || format.equalsIgnoreCase(FORMAT_XML)
148: || format.equalsIgnoreCase(FORMAT_CSV)) {
149: _format = format;
150: } else {
151: throw new IllegalArgumentException(
152: "Type must be either 'flat'(default), 'xml' or 'csv' but was: "
153: + format);
154: }
155: }
156:
157: public void setTransaction(boolean transaction) {
158: logger.debug("setTransaction(transaction=" + transaction
159: + ") - start");
160:
161: _transaction = transaction;
162: }
163:
164: public void execute(IDatabaseConnection connection)
165: throws DatabaseUnitException {
166: logger.debug("execute(connection=" + connection + ") - start");
167:
168: if (_operation == null) {
169: throw new DatabaseUnitException(
170: "Operation.execute(): setType(String) must be called before execute()!");
171: }
172:
173: if (_operation == DatabaseOperation.NONE) {
174: return;
175: }
176:
177: try {
178: DatabaseOperation operation = (_transaction ? new TransactionOperation(
179: _operation)
180: : _operation);
181: IDataSet dataset = getSrcDataSet(getSrc(), getFormat(),
182: _forwardOperation);
183: operation.execute(connection, dataset);
184: } catch (SQLException e) {
185: logger.error("execute()", e);
186:
187: throw new DatabaseUnitException(e);
188: }
189: }
190:
191: public String getLogMessage() {
192: logger.debug("getLogMessage() - start");
193:
194: return "Executing operation: " + _type
195: + "\n on file: "
196: + ((_src == null) ? null : _src.getAbsolutePath())
197: + "\n with format: " + _format;
198: }
199:
200: public String toString() {
201: logger.debug("toString() - start");
202:
203: StringBuffer result = new StringBuffer();
204: result.append("Operation: ");
205: result.append(" type=" + _type);
206: result.append(", format=" + _format);
207: result.append(", src=" + _src == null ? null : _src
208: .getAbsolutePath());
209: result.append(", operation = " + _operation);
210:
211: return result.toString();
212: }
213: }
|