View Javadoc
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  package org.dbunit.ant;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.dbunit.Assertion;
27  import org.dbunit.DatabaseUnitException;
28  import org.dbunit.database.IDatabaseConnection;
29  import org.dbunit.dataset.IDataSet;
30  import org.dbunit.dataset.ITable;
31  import org.dbunit.dataset.NoSuchTableException;
32  import org.dbunit.dataset.SortedTable;
33  import org.dbunit.dataset.ITableMetaData;
34  import org.dbunit.dataset.filter.DefaultColumnFilter;
35  
36  import java.io.File;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * The <code>Compare</code> class is the step that compare the content of the
42   * database against the specified dataset.
43   *
44   * @author Manuel Laflamme
45   * @version $Revision$
46   * @since Apr 3, 2004
47   * @see DbUnitTaskStep
48   */
49  public class Compare extends AbstractStep
50  {
51  
52      /**
53       * Logger for this class
54       */
55      private static final Logger logger = LoggerFactory.getLogger(Compare.class);
56  
57      private static final String DEFAULT_FORMAT = FORMAT_FLAT;
58  
59      private String _format;
60      private File _src;
61      private List _tables = new ArrayList();
62      private boolean _sort = false;
63  
64      public File getSrc()
65      {
66          return _src;
67      }
68  
69      public void setSrc(File src)
70      {
71          logger.debug("setSrc(src={}) - start", src);
72  
73          _src = src;
74      }
75  
76      public void setSort(boolean sort)
77      {
78          logger.debug("setSort(sort={}) - start", String.valueOf(sort));
79  
80          _sort = sort;
81      }
82  
83      public String getFormat()
84      {
85          return _format != null ? _format : DEFAULT_FORMAT;
86      }
87  
88      public void setFormat(String format)
89      {
90          logger.debug("setFormat(format={}) - start", format);
91  
92          // Check if the given format is accepted
93          checkDataFormat(format);
94          // If we get here the given format is a valid data format
95          _format = format;
96      }
97  
98      public List getTables()
99      {
100         return _tables;
101     }
102 
103     public void addTable(Table table)
104     {
105         logger.debug("addTable(table={}) - start", table);
106 
107         _tables.add(table);
108     }
109 
110     public void addQuery(Query query)
111     {
112         logger.debug("addQuery(query={}) - start", query);
113 
114         _tables.add(query);
115     }
116 
117     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
118     {
119         logger.debug("execute(connection={}) - start", connection);
120 
121         IDataSet expectedDataset = getSrcDataSet(_src, getFormat(), false);
122         IDataSet actualDataset = getDatabaseDataSet(connection, _tables);
123 
124         String[] tableNames = null;
125         if (_tables.size() == 0)
126         {
127             // No tables specified, assume must compare all tables from
128             // expected dataset
129             tableNames = expectedDataset.getTableNames();
130         }
131         else
132         {
133             tableNames = actualDataset.getTableNames();
134         }
135 
136         for (int i = 0; i < tableNames.length; i++)
137         {
138             String tableName = tableNames[i];
139             ITable expectedTable;
140 			try {
141 				expectedTable = expectedDataset.getTable(tableName);
142 			} catch (NoSuchTableException e) {
143 				throw new DatabaseUnitException("Did not find table in source file '" + 
144 						_src + "' using format '" + getFormat() + "'", e);
145 			}
146             ITableMetaData expectedMetaData = expectedTable.getTableMetaData();
147 
148             ITable actualTable;
149 			try {
150 				actualTable = actualDataset.getTable(tableName);
151 			} catch (NoSuchTableException e) {
152 				throw new DatabaseUnitException("Did not find table in actual dataset '" + 
153 						actualDataset + "' via db connection '" + connection + "'", e);
154 			}
155             // Only compare columns present in expected table. Extra columns
156             // are filtered out from actual database table.
157             actualTable = DefaultColumnFilter.includedColumnsTable(
158                     actualTable, expectedMetaData.getColumns());
159 
160             if (_sort)
161             {
162                 expectedTable = new SortedTable(expectedTable);
163                 actualTable = new SortedTable(actualTable);
164             }
165             Assertion.assertEquals(expectedTable, actualTable);
166         }
167     }
168 
169     public String getLogMessage()
170     {
171         return "Executing compare: "
172                 + "\n          from file: " + ((_src == null) ? null : _src.getAbsolutePath())
173                 + "\n          with format: " + _format;
174     }
175 
176     public String toString()
177     {
178         StringBuffer result = new StringBuffer();
179         result.append("Compare: ");
180         result.append(" src=");
181         result.append((_src == null ? "null" : _src.getAbsolutePath()));
182         result.append(", format= ");
183         result.append(_format);
184         result.append(", tables= ");
185         result.append(_tables);
186 
187         return result.toString();
188     }
189 }
190