KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ant > Operation


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.dataset.IDataSet;
27 import org.dbunit.ext.mssql.InsertIdentityOperation;
28 import org.dbunit.operation.DatabaseOperation;
29
30 import java.io.File JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 /**
34  * The <code>Operation</code> class is the step that defines which
35  * operation will be performed in the execution of the <code>DbUnitTask</code>
36  * task.
37  *
38  * @author Timothy Ruppert
39  * @author Ben Cox
40  * @version $Revision: 1.13 $
41  * @since Jun 10, 2002
42  * @see org.dbunit.ant.DbUnitTaskStep
43  */

44 public class Operation extends AbstractStep
45 {
46     private static final String JavaDoc DEFAULT_FORMAT = FORMAT_FLAT;
47
48     protected String JavaDoc _type = "CLEAN_INSERT";
49     private String JavaDoc _format;
50     private File JavaDoc _src;
51     private DatabaseOperation _operation;
52     private boolean _forwardOperation = true;
53
54     public String JavaDoc getType()
55     {
56         return _type;
57     }
58
59     public File JavaDoc getSrc()
60     {
61         return _src;
62     }
63
64     public DatabaseOperation getDbOperation()
65     {
66         return _operation;
67     }
68
69     public String JavaDoc getFormat()
70     {
71         return _format != null ? _format : DEFAULT_FORMAT;
72     }
73
74     public void setType(String JavaDoc type)
75     {
76         if ("UPDATE".equals(type))
77         {
78             _operation = DatabaseOperation.UPDATE;
79             _forwardOperation = true;
80         }
81         else if ("INSERT".equals(type))
82         {
83             _operation = DatabaseOperation.INSERT;
84             _forwardOperation = true;
85         }
86         else if ("REFRESH".equals(type))
87         {
88             _operation = DatabaseOperation.REFRESH;
89             _forwardOperation = true;
90         }
91         else if ("DELETE".equals(type))
92         {
93             _operation = DatabaseOperation.DELETE;
94             _forwardOperation = false;
95         }
96         else if ("DELETE_ALL".equals(type))
97         {
98             _operation = DatabaseOperation.DELETE_ALL;
99             _forwardOperation = false;
100         }
101         else if ("CLEAN_INSERT".equals(type))
102         {
103             _operation = DatabaseOperation.CLEAN_INSERT;
104             _forwardOperation = false;
105         }
106         else if ("NONE".equals(type))
107         {
108             _operation = DatabaseOperation.NONE;
109             _forwardOperation = true;
110         }
111         else if ("MSSQL_CLEAN_INSERT".equals(type))
112         {
113             _operation = InsertIdentityOperation.CLEAN_INSERT;
114             _forwardOperation = false;
115         }
116         else if ("MSSQL_INSERT".equals(type))
117         {
118             _operation = InsertIdentityOperation.INSERT;
119             _forwardOperation = true;
120         }
121         else if ("MSSQL_REFRESH".equals(type))
122         {
123             _operation = InsertIdentityOperation.REFRESH;
124             _forwardOperation = true;
125         }
126         else
127         {
128             throw new IllegalArgumentException JavaDoc("Type must be one of: UPDATE, INSERT,"
129                     + " REFRESH, DELETE, DELETE_ALL, CLEAN_INSERT, MSSQL_INSERT, "
130                     + " or MSSQL_REFRESH but was: " + type);
131         }
132         _type = type;
133     }
134
135     public void setSrc(File JavaDoc src)
136     {
137         _src = src;
138     }
139
140     public void setFormat(String JavaDoc format)
141     {
142         if (format.equalsIgnoreCase(FORMAT_FLAT)
143                 || format.equalsIgnoreCase(FORMAT_XML)
144                 || format.equalsIgnoreCase(FORMAT_CSV)
145         )
146         {
147             _format = format;
148         }
149         else
150         {
151             throw new IllegalArgumentException JavaDoc("Type must be either 'flat'(default), 'xml' or 'csv' but was: " + format);
152         }
153     }
154
155     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
156     {
157         if (_operation == null)
158         {
159             throw new DatabaseUnitException("Operation.execute(): setType(String) must be called before execute()!");
160         }
161
162         if (_operation == DatabaseOperation.NONE)
163         {
164             return;
165         }
166
167         try
168         {
169             IDataSet dataset = getSrcDataSet(getSrc(),
170                     getFormat(), _forwardOperation);
171             _operation.execute(connection, dataset);
172         }
173         catch (SQLException JavaDoc e)
174         {
175             throw new DatabaseUnitException(e);
176         }
177     }
178
179     public String JavaDoc getLogMessage()
180     {
181         return "Executing operation: " + _type
182                 + "\n on file: " + ((_src == null) ? null : _src.getAbsolutePath())
183                 + "\n with format: " + _format;
184     }
185
186
187     public String JavaDoc toString()
188     {
189         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
190         result.append("Operation: ");
191         result.append(" type=" + _type);
192         result.append(", format=" + _format);
193         result.append(", SRC=" + _src == null ? null : _src.getAbsolutePath());
194         result.append(", operation = " + _operation);
195
196         return result.toString();
197     }
198 }
199
200
Popular Tags