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.Assertion;
028: import org.dbunit.DatabaseUnitException;
029: import org.dbunit.database.IDatabaseConnection;
030: import org.dbunit.dataset.IDataSet;
031: import org.dbunit.dataset.ITable;
032: import org.dbunit.dataset.SortedTable;
033: import org.dbunit.dataset.ITableMetaData;
034: import org.dbunit.dataset.filter.DefaultColumnFilter;
035:
036: import java.io.File;
037: import java.util.ArrayList;
038: import java.util.List;
039:
040: /**
041: * The <code>Compare</code> class is the step that compare the content of the
042: * database against the specified dataset.
043: *
044: * @author Manuel Laflamme
045: * @version $Revision: 554 $
046: * @since Apr 3, 2004
047: * @see DbUnitTaskStep
048: */
049: public class Compare extends AbstractStep {
050:
051: /**
052: * Logger for this class
053: */
054: private static final Logger logger = LoggerFactory
055: .getLogger(Compare.class);
056:
057: private static final String DEFAULT_FORMAT = FORMAT_FLAT;
058:
059: private String _format;
060: private File _src;
061: private List _tables = new ArrayList();
062: private boolean _sort = false;
063:
064: public File getSrc() {
065: logger.debug("getSrc() - start");
066:
067: return _src;
068: }
069:
070: public void setSrc(File src) {
071: logger.debug("setSrc(src=" + src + ") - start");
072:
073: _src = src;
074: }
075:
076: public void setSort(boolean sort) {
077: logger.debug("setSort(sort=" + sort + ") - start");
078:
079: _sort = sort;
080: }
081:
082: public String getFormat() {
083: logger.debug("getFormat() - start");
084:
085: return _format != null ? _format : DEFAULT_FORMAT;
086: }
087:
088: public void setFormat(String format) {
089: logger.debug("setFormat(format=" + format + ") - start");
090:
091: if (format.equalsIgnoreCase(FORMAT_FLAT)
092: || format.equalsIgnoreCase(FORMAT_XML)
093: || format.equalsIgnoreCase(FORMAT_CSV)) {
094: _format = format;
095: } else {
096: throw new IllegalArgumentException(
097: "Type must be either 'flat'(default) csv or 'xml' but was: "
098: + format);
099: }
100: }
101:
102: public List getTables() {
103: logger.debug("getTables() - start");
104:
105: return _tables;
106: }
107:
108: public void addTable(Table table) {
109: logger.debug("addTable(table=" + table + ") - start");
110:
111: _tables.add(table);
112: }
113:
114: public void addQuery(Query query) {
115: logger.debug("addQuery(query=" + query + ") - start");
116:
117: _tables.add(query);
118: }
119:
120: public void execute(IDatabaseConnection connection)
121: throws DatabaseUnitException {
122: logger.debug("execute(connection=" + connection + ") - start");
123:
124: IDataSet expectedDataset = getSrcDataSet(_src, getFormat(),
125: false);
126: IDataSet actualDataset = getDatabaseDataSet(connection,
127: _tables, false);
128:
129: String[] tableNames = null;
130: if (_tables.size() == 0) {
131: // No tables specified, assume must compare all tables from
132: // expected dataset
133: tableNames = expectedDataset.getTableNames();
134: } else {
135: tableNames = actualDataset.getTableNames();
136: }
137:
138: for (int i = 0; i < tableNames.length; i++) {
139: String tableName = tableNames[i];
140: ITable expectedTable = expectedDataset.getTable(tableName);
141: ITableMetaData expectedMetaData = expectedTable
142: .getTableMetaData();
143:
144: // Only compare columns present in expected table. Extra columns
145: // are filtered out from actual database table.
146: ITable actualTable = actualDataset.getTable(tableName);
147: actualTable = DefaultColumnFilter.includedColumnsTable(
148: actualTable, expectedMetaData.getColumns());
149:
150: if (_sort) {
151: expectedTable = new SortedTable(expectedTable);
152: actualTable = new SortedTable(actualTable);
153: }
154: Assertion.assertEquals(expectedTable, actualTable);
155: }
156: }
157:
158: public String getLogMessage() {
159: logger.debug("getLogMessage() - start");
160:
161: return "Executing compare: " + "\n from file: "
162: + ((_src == null) ? null : _src.getAbsolutePath())
163: + "\n with format: " + _format;
164: }
165:
166: public String toString() {
167: logger.debug("toString() - start");
168:
169: StringBuffer result = new StringBuffer();
170: result.append("Compare: ");
171: result.append(" src=" + _src.getAbsolutePath());
172: result.append(", format= " + _format);
173: result.append(", tables= " + _tables);
174:
175: return result.toString();
176: }
177: }
|