SequenceTableFilter.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.filter;

  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.dbunit.database.AmbiguousTableNameException;
  25. import org.dbunit.dataset.DataSetException;
  26. import org.dbunit.dataset.DataSetUtils;
  27. import org.dbunit.dataset.IDataSet;
  28. import org.dbunit.dataset.ITableIterator;
  29. import org.dbunit.dataset.ITableMetaData;
  30. import org.dbunit.dataset.NoSuchTableException;
  31. import org.dbunit.dataset.OrderedTableNameMap;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;

  34. /**
  35.  * This filter expose a specified table sequence and can be used to reorder
  36.  * tables in a dataset. This implementation does not support duplicate table names.
  37.  * Thus you cannot specify the same table name more than once in this filter
  38.  * and the filtered dataset must not contains duplicate table names. This is
  39.  * the default filter used by the {@link org.dbunit.dataset.FilteredDataSet}.
  40.  *
  41.  * @author Manuel Laflamme
  42.  * @author Last changed by: $Author$
  43.  * @version $Revision$ $Date$
  44.  * @since Mar 7, 2003
  45.  */
  46. public class SequenceTableFilter implements ITableFilter
  47. {

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

  52.     private final OrderedTableNameMap _tableNameMap;

  53.     /**
  54.      * Creates a new SequenceTableFilter with specified table names sequence.
  55.      * @throws AmbiguousTableNameException If the given array contains ambiguous names
  56.      */
  57.     public SequenceTableFilter(String[] tableNames)
  58.     throws AmbiguousTableNameException
  59.     {
  60.         this(tableNames, false);
  61.     }

  62.     /**
  63.      * Creates a new SequenceTableFilter with specified table names sequence.
  64.      * @param tableNames
  65.      * @param caseSensitiveTableNames
  66.      * @throws AmbiguousTableNameException If the given array contains ambiguous names
  67.      * @since 2.4.2
  68.      */
  69.     public SequenceTableFilter(String[] tableNames, boolean caseSensitiveTableNames)
  70.     throws AmbiguousTableNameException
  71.     {
  72.         // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
  73.         _tableNameMap = new OrderedTableNameMap(caseSensitiveTableNames);
  74.         for (int i = 0; i < tableNames.length; i++)
  75.         {
  76.             _tableNameMap.add(tableNames[i], null);
  77.         }
  78.     }

  79.     ////////////////////////////////////////////////////////////////////////////
  80.     // ITableFilter interface

  81.     public boolean accept(String tableName) throws DataSetException
  82.     {
  83.         logger.debug("accept(tableName={}) - start", tableName);

  84.         return _tableNameMap.containsTable(tableName);
  85.     }

  86.     public String[] getTableNames(IDataSet dataSet) throws DataSetException
  87.     {
  88.         logger.debug("getTableNames(dataSet={}) - start", dataSet);

  89.         List nameList = new ArrayList();
  90.         String[] tableNames = _tableNameMap.getTableNames();
  91.         for (int i = 0; i < tableNames.length; i++)
  92.         {
  93.             try
  94.             {
  95.                 // Use the table name from the filtered dataset. This ensure
  96.                 // that table names are having the same case (lower/upper) from
  97.                 // getTableNames() and getTables() methods.
  98.                 ITableMetaData metaData = dataSet.getTableMetaData(tableNames[i]);
  99.                 nameList.add(metaData.getTableName());
  100.             }
  101.             catch (NoSuchTableException e)
  102.             {
  103.                 logger.debug("Table '{}' not found in filtered dataset {}", tableNames[i], dataSet);
  104.                 // Skip this table name because the filtered dataset does not
  105.                 // contains it.
  106.             }
  107.         }

  108.         return (String[])nameList.toArray(new String[0]);
  109.     }

  110.     public ITableIterator iterator(IDataSet dataSet, boolean reversed)
  111.             throws DataSetException
  112.     {
  113.         if(logger.isDebugEnabled())
  114.             logger.debug("iterator(dataSet={}, reversed={}) - start", dataSet, String.valueOf(reversed));

  115.         String[] tableNames = getTableNames(dataSet);
  116.         return new SequenceTableIterator(reversed ?
  117.                 DataSetUtils.reverseStringArray(tableNames) : tableNames, dataSet);
  118.     }
  119.    
  120.     public String toString()
  121.     {
  122.         final StringBuilder sb = new StringBuilder();
  123.         sb.append(getClass().getName()).append("[");
  124.         sb.append("_tableNameMap=").append(_tableNameMap);
  125.         sb.append("]");
  126.         return sb.toString();
  127.     }
  128. }