KickJava   Java API By Example, From Geeks To Geeks.

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


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.Assertion;
25 import org.dbunit.DatabaseUnitException;
26 import org.dbunit.database.IDatabaseConnection;
27 import org.dbunit.dataset.CompositeTable;
28 import org.dbunit.dataset.IDataSet;
29 import org.dbunit.dataset.ITable;
30 import org.dbunit.dataset.SortedTable;
31 import org.dbunit.dataset.ITableMetaData;
32 import org.dbunit.dataset.FilteredTableMetaData;
33 import org.dbunit.dataset.filter.DefaultColumnFilter;
34
35 import java.io.File JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * The <code>Compare</code> class is the step that compare the content of the
41  * database against the specified dataset.
42  *
43  * @author Manuel Laflamme
44  * @version $Revision: 1.2 $
45  * @since Apr 3, 2004
46  * @see DbUnitTaskStep
47  */

48 public class Compare extends AbstractStep
49 {
50     private static final String JavaDoc DEFAULT_FORMAT = FORMAT_FLAT;
51
52     private String JavaDoc _format;
53     private File JavaDoc _src;
54     private List JavaDoc _tables = new ArrayList JavaDoc();
55     private boolean _sort = false;
56
57     public File JavaDoc getSrc()
58     {
59         return _src;
60     }
61
62     public void setSrc(File JavaDoc src)
63     {
64         _src = src;
65     }
66
67     public void setSort(boolean sort)
68     {
69         _sort = sort;
70     }
71
72     public String JavaDoc getFormat()
73     {
74         return _format != null ? _format : DEFAULT_FORMAT;
75     }
76
77     public void setFormat(String JavaDoc format)
78     {
79         if (format.equalsIgnoreCase(FORMAT_FLAT)
80                 || format.equalsIgnoreCase(FORMAT_XML)
81                 || format.equalsIgnoreCase(FORMAT_CSV)
82         )
83         {
84             _format = format;
85         }
86         else
87         {
88             throw new IllegalArgumentException JavaDoc("Type must be either 'flat'(default) csv or 'xml' but was: " + format);
89         }
90     }
91
92     public List JavaDoc getTables()
93     {
94         return _tables;
95     }
96
97     public void addTable(Table table)
98     {
99         _tables.add(table);
100     }
101
102     public void addQuery(Query query)
103     {
104         _tables.add(query);
105     }
106
107     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
108     {
109         IDataSet expectedDataset = getSrcDataSet(_src, getFormat(), false);
110         IDataSet actualDataset = getDatabaseDataSet(connection, _tables, false);
111
112         String JavaDoc[] tableNames = null;
113         if (_tables.size() == 0)
114         {
115             // No tables specified, assume must compare all tables from
116
// expected dataset
117
tableNames = expectedDataset.getTableNames();
118         }
119         else
120         {
121             tableNames = actualDataset.getTableNames();
122         }
123
124         for (int i = 0; i < tableNames.length; i++)
125         {
126             String JavaDoc tableName = tableNames[i];
127             ITable expectedTable = expectedDataset.getTable(tableName);
128             ITableMetaData expectedMetaData = expectedTable.getTableMetaData();
129
130             // Only compare columns present in expected table. Extra columns
131
// are filtered out from actual database table.
132
ITable actualTable = actualDataset.getTable(tableName);
133             actualTable = DefaultColumnFilter.includedColumnsTable(
134                     actualTable, expectedMetaData.getColumns());
135
136             if (_sort)
137             {
138                 expectedTable = new SortedTable(expectedTable);
139                 actualTable = new SortedTable(actualTable);
140             }
141             Assertion.assertEquals(expectedTable, actualTable);
142         }
143     }
144
145     public String JavaDoc getLogMessage()
146     {
147         return "Executing compare: "
148                 + "\n from file: " + ((_src == null) ? null : _src.getAbsolutePath())
149                 + "\n with format: " + _format;
150     }
151
152     public String JavaDoc toString()
153     {
154         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
155         result.append("Compare: ");
156         result.append(" SRC=" + _src.getAbsolutePath());
157         result.append(", format= " + _format);
158         result.append(", tables= " + _tables);
159
160         return result.toString();
161     }
162 }
163
164
Popular Tags