Compare.java

  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. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.Assertion;
  25. import org.dbunit.DatabaseUnitException;
  26. import org.dbunit.database.IDatabaseConnection;
  27. import org.dbunit.dataset.IDataSet;
  28. import org.dbunit.dataset.ITable;
  29. import org.dbunit.dataset.NoSuchTableException;
  30. import org.dbunit.dataset.SortedTable;
  31. import org.dbunit.dataset.ITableMetaData;
  32. import org.dbunit.dataset.filter.DefaultColumnFilter;

  33. import java.io.File;
  34. import java.util.ArrayList;
  35. import java.util.List;

  36. /**
  37.  * The <code>Compare</code> class is the step that compare the content of the
  38.  * database against the specified dataset.
  39.  *
  40.  * @author Manuel Laflamme
  41.  * @version $Revision$
  42.  * @since Apr 3, 2004
  43.  * @see DbUnitTaskStep
  44.  */
  45. public class Compare extends AbstractStep
  46. {

  47.     /**
  48.      * Logger for this class
  49.      */
  50.     private static final Logger logger = LoggerFactory.getLogger(Compare.class);

  51.     private static final String DEFAULT_FORMAT = FORMAT_FLAT;

  52.     private String _format;
  53.     private File _src;
  54.     private List _tables = new ArrayList();
  55.     private boolean _sort = false;

  56.     public File getSrc()
  57.     {
  58.         return _src;
  59.     }

  60.     public void setSrc(File src)
  61.     {
  62.         logger.debug("setSrc(src={}) - start", src);

  63.         _src = src;
  64.     }

  65.     public void setSort(boolean sort)
  66.     {
  67.         logger.debug("setSort(sort={}) - start", String.valueOf(sort));

  68.         _sort = sort;
  69.     }

  70.     public String getFormat()
  71.     {
  72.         return _format != null ? _format : DEFAULT_FORMAT;
  73.     }

  74.     public void setFormat(String format)
  75.     {
  76.         logger.debug("setFormat(format={}) - start", format);

  77.         // Check if the given format is accepted
  78.         checkDataFormat(format);
  79.         // If we get here the given format is a valid data format
  80.         _format = format;
  81.     }

  82.     public List getTables()
  83.     {
  84.         return _tables;
  85.     }

  86.     public void addTable(Table table)
  87.     {
  88.         logger.debug("addTable(table={}) - start", table);

  89.         _tables.add(table);
  90.     }

  91.     public void addQuery(Query query)
  92.     {
  93.         logger.debug("addQuery(query={}) - start", query);

  94.         _tables.add(query);
  95.     }

  96.     public void execute(IDatabaseConnection connection) throws DatabaseUnitException
  97.     {
  98.         logger.debug("execute(connection={}) - start", connection);

  99.         IDataSet expectedDataset = getSrcDataSet(_src, getFormat(), false);
  100.         IDataSet actualDataset = getDatabaseDataSet(connection, _tables);

  101.         String[] tableNames = null;
  102.         if (_tables.size() == 0)
  103.         {
  104.             // No tables specified, assume must compare all tables from
  105.             // expected dataset
  106.             tableNames = expectedDataset.getTableNames();
  107.         }
  108.         else
  109.         {
  110.             tableNames = actualDataset.getTableNames();
  111.         }

  112.         for (int i = 0; i < tableNames.length; i++)
  113.         {
  114.             String tableName = tableNames[i];
  115.             ITable expectedTable;
  116.             try {
  117.                 expectedTable = expectedDataset.getTable(tableName);
  118.             } catch (NoSuchTableException e) {
  119.                 throw new DatabaseUnitException("Did not find table in source file '" +
  120.                         _src + "' using format '" + getFormat() + "'", e);
  121.             }
  122.             ITableMetaData expectedMetaData = expectedTable.getTableMetaData();

  123.             ITable actualTable;
  124.             try {
  125.                 actualTable = actualDataset.getTable(tableName);
  126.             } catch (NoSuchTableException e) {
  127.                 throw new DatabaseUnitException("Did not find table in actual dataset '" +
  128.                         actualDataset + "' via db connection '" + connection + "'", e);
  129.             }
  130.             // Only compare columns present in expected table. Extra columns
  131.             // are filtered out from actual database table.
  132.             actualTable = DefaultColumnFilter.includedColumnsTable(
  133.                     actualTable, expectedMetaData.getColumns());

  134.             if (_sort)
  135.             {
  136.                 expectedTable = new SortedTable(expectedTable);
  137.                 actualTable = new SortedTable(actualTable);
  138.             }
  139.             Assertion.assertEquals(expectedTable, actualTable);
  140.         }
  141.     }

  142.     public String getLogMessage()
  143.     {
  144.         return "Executing compare: "
  145.                 + "\n          from file: " + ((_src == null) ? null : _src.getAbsolutePath())
  146.                 + "\n          with format: " + _format;
  147.     }

  148.     public String toString()
  149.     {
  150.         StringBuilder result = new StringBuilder();
  151.         result.append("Compare: ");
  152.         result.append(" src=");
  153.         result.append((_src == null ? "null" : _src.getAbsolutePath()));
  154.         result.append(", format= ");
  155.         result.append(_format);
  156.         result.append(", tables= ");
  157.         result.append(_tables);

  158.         return result.toString();
  159.     }
  160. }