FilteredDataSet.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.dataset;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.database.AmbiguousTableNameException;
  25. import org.dbunit.dataset.filter.ITableFilter;
  26. import org.dbunit.dataset.filter.SequenceTableFilter;

  27. /**
  28.  * Decorates a dataset and exposes only some tables from it. Can be used with
  29.  * different filtering strategies.
  30.  *
  31.  * @see ITableFilter
  32.  * @see SequenceTableFilter
  33.  * @see org.dbunit.dataset.filter.DefaultTableFilter
  34.  *
  35.  * @author Manuel Laflamme
  36.  * @author Last changed by: Luke Cann
  37.  * @version $Revision$
  38.  * @since Feb 22, 2002
  39.  */
  40. public class FilteredDataSet extends AbstractDataSet
  41. {

  42.     /**
  43.      * Logger for this class
  44.      */
  45.     private static final Logger logger = LoggerFactory.getLogger(FilteredDataSet.class);

  46.     private final IDataSet _dataSet;
  47.     private final ITableFilter _filter;

  48.     /**
  49.      * Creates a FilteredDataSet that decorates the specified dataset and
  50.      * exposes only the specified tables using {@link SequenceTableFilter} as
  51.      * filtering strategy.
  52.      * @throws AmbiguousTableNameException If the given tableNames array contains ambiguous names
  53.      */
  54.     public FilteredDataSet(String[] tableNames, IDataSet dataSet)
  55.     throws AmbiguousTableNameException
  56.     {
  57.         super(dataSet.isCaseSensitiveTableNames());
  58.         _filter = new SequenceTableFilter(tableNames, dataSet.isCaseSensitiveTableNames());
  59.         _dataSet = dataSet;
  60.     }

  61.     /**
  62.      * Creates a FilteredDataSet that decorates the specified dataset and
  63.      * exposes only the tables allowed by the specified filter.
  64.      *
  65.      * @param dataSet the filtered dataset
  66.      * @param filter the filtering strategy
  67.      */
  68.     public FilteredDataSet(ITableFilter filter, IDataSet dataSet)
  69.     {
  70.         super(dataSet.isCaseSensitiveTableNames());
  71.         _dataSet = dataSet;
  72.         _filter = filter;
  73.     }

  74.     ////////////////////////////////////////////////////////////////////////////
  75.     // AbstractDataSet class

  76.     protected ITableIterator createIterator(boolean reversed)
  77.             throws DataSetException
  78.     {
  79.         if(logger.isDebugEnabled())
  80.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
  81.        
  82.         return _filter.iterator(_dataSet, reversed);
  83.     }

  84.     ////////////////////////////////////////////////////////////////////////////
  85.     // IDataSet interface

  86.     public String[] getTableNames() throws DataSetException
  87.     {
  88.         return _filter.getTableNames(_dataSet);
  89.     }

  90.     public ITableMetaData getTableMetaData(String tableName)
  91.             throws DataSetException
  92.     {
  93.         if (!_filter.accept(tableName))
  94.         {
  95.             throw new NoSuchTableException(tableName);
  96.         }

  97.         return _dataSet.getTableMetaData(tableName);
  98.     }

  99.     public ITable getTable(String tableName) throws DataSetException
  100.     {
  101.         logger.debug("getTable(tableName={}) - start", tableName);

  102.         if (!_filter.accept(tableName))
  103.         {
  104.             throw new NoSuchTableException(tableName);
  105.         }

  106.         return _dataSet.getTable(tableName);
  107.     }
  108. }