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 java.io.File;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import java.util.ArrayList;
032: import java.util.Arrays;
033: import java.util.List;
034:
035: import org.apache.tools.ant.Project;
036: import org.dbunit.DatabaseUnitException;
037: import org.dbunit.database.IDatabaseConnection;
038: import org.dbunit.dataset.IDataSet;
039: import org.dbunit.dataset.csv.CsvDataSetWriter;
040: import org.dbunit.dataset.xml.FlatDtdDataSet;
041: import org.dbunit.dataset.xml.FlatXmlWriter;
042: import org.dbunit.dataset.xml.XmlDataSet;
043:
044: /**
045: * The <code>Export</code> class is the step that facilitates exporting
046: * the contents of the database and/or it's corresponding dtd to a file.
047: * The export can be performed on a full dataset or a partial one if
048: * specific table names are identified.
049: *
050: * @author Timothy Ruppert
051: * @author Ben Cox
052: * @version $Revision: 554 $
053: * @since Jun 10, 2002
054: * @see DbUnitTaskStep
055: */
056: public class Export extends AbstractStep {
057:
058: /**
059: * Logger for this class
060: */
061: private static final Logger logger = LoggerFactory
062: .getLogger(Export.class);
063:
064: private File _dest;
065: private String _format = FORMAT_FLAT;
066: private String _doctype = null;
067: private List _tables = new ArrayList();
068:
069: public Export() {
070: }
071:
072: private String getAbsolutePath(File filename) {
073: logger.debug("getAbsolutePath(filename=" + filename
074: + ") - start");
075:
076: return filename != null ? filename.getAbsolutePath() : "null";
077: }
078:
079: public File getDest() {
080: logger.debug("getDest() - start");
081:
082: return _dest;
083: }
084:
085: public String getFormat() {
086: logger.debug("getFormat() - start");
087:
088: return _format;
089: }
090:
091: public List getTables() {
092: logger.debug("getTables() - start");
093:
094: return _tables;
095: }
096:
097: public void setDest(File dest) {
098: logger.debug("setDest(dest=" + dest + ") - start");
099:
100: _dest = dest;
101: }
102:
103: public void setFormat(String format) {
104: logger.debug("setFormat(format=" + format + ") - start");
105:
106: if (format.equalsIgnoreCase(FORMAT_FLAT)
107: || format.equalsIgnoreCase(FORMAT_XML)
108: || format.equalsIgnoreCase(FORMAT_DTD)
109: || format.equalsIgnoreCase(FORMAT_CSV)) {
110: _format = format;
111: } else {
112: throw new IllegalArgumentException(
113: "Type must be one of: 'flat'(default), 'xml', or 'dtd' but was: "
114: + format);
115: }
116: }
117:
118: public void addTable(Table table) {
119: logger.debug("addTable(table=" + table + ") - start");
120:
121: _tables.add(table);
122: }
123:
124: public void addQuery(Query query) {
125: logger.debug("addQuery(query=" + query + ") - start");
126:
127: _tables.add(query);
128: }
129:
130: public void addQuerySet(QuerySet querySet) {
131: logger.debug("addQuerySet(querySet=" + querySet + ") - start");
132:
133: _tables.add(querySet);
134: }
135:
136: public String getDoctype() {
137: logger.debug("getDoctype() - start");
138:
139: return _doctype;
140: }
141:
142: public void setDoctype(String doctype) {
143: logger.debug("setDoctype(doctype=" + doctype + ") - start");
144:
145: _doctype = doctype;
146: }
147:
148: public void execute(IDatabaseConnection connection)
149: throws DatabaseUnitException {
150: logger.debug("execute(connection=" + connection + ") - start");
151:
152: try {
153: if (_dest == null) {
154: throw new DatabaseUnitException(
155: "'_dest' is a required attribute of the <export> step.");
156: }
157:
158: IDataSet dataset = getDatabaseDataSet(connection, _tables,
159: false);
160:
161: log("dataset tables: "
162: + Arrays.asList(dataset.getTableNames()),
163: Project.MSG_VERBOSE);
164:
165: // Write the dataset
166: if (_format.equals(FORMAT_CSV)) {
167: CsvDataSetWriter.write(dataset, _dest);
168: } else {
169: OutputStream out = new FileOutputStream(_dest);
170: try {
171: if (_format.equalsIgnoreCase(FORMAT_FLAT)) {
172: FlatXmlWriter writer = new FlatXmlWriter(out);
173: writer.setDocType(_doctype);
174: writer.write(dataset);
175: } else if (_format.equalsIgnoreCase(FORMAT_XML)) {
176: XmlDataSet.write(dataset, out);
177: } else if (_format.equalsIgnoreCase(FORMAT_DTD)) {
178: FlatDtdDataSet.write(dataset, out);
179: }
180:
181: } finally {
182: out.close();
183: }
184: }
185:
186: } catch (IOException e) {
187: logger.error("execute()", e);
188:
189: throw new DatabaseUnitException(e);
190: }
191: }
192:
193: public String getLogMessage() {
194: logger.debug("getLogMessage() - start");
195:
196: return "Executing export: " + "\n in format: " + _format
197: + " to datafile: " + getAbsolutePath(_dest);
198: }
199:
200: public String toString() {
201: logger.debug("toString() - start");
202:
203: StringBuffer result = new StringBuffer();
204: result.append("Export: ");
205: result.append(" dest=" + getAbsolutePath(_dest));
206: result.append(", format= " + _format);
207: result.append(", doctype= " + _doctype);
208: result.append(", tables= " + _tables);
209:
210: return result.toString();
211: }
212: }
|