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  
22  package org.dbunit.dataset;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import org.dbunit.database.AmbiguousTableNameException;
28  import org.dbunit.dataset.filter.ITableFilter;
29  import org.dbunit.dataset.filter.SequenceTableFilter;
30  
31  /**
32   * Decorates a dataset and exposes only some tables from it. Can be used with
33   * different filtering strategies.
34   *
35   * @see ITableFilter
36   * @see SequenceTableFilter
37   * @see org.dbunit.dataset.filter.DefaultTableFilter
38   *
39   * @author Manuel Laflamme
40   * @version $Revision$
41   * @since Feb 22, 2002
42   */
43  public class FilteredDataSet extends AbstractDataSet
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory.getLogger(FilteredDataSet.class);
50  
51      private final IDataSet _dataSet;
52      private final ITableFilter _filter;
53  
54      /**
55       * Creates a FilteredDataSet that decorates the specified dataset and
56       * exposes only the specified tables using {@link SequenceTableFilter} as
57       * filtering strategy.
58       * @throws AmbiguousTableNameException If the given tableNames array contains ambiguous names
59       */
60      public FilteredDataSet(String[] tableNames, IDataSet dataSet) 
61      throws AmbiguousTableNameException
62      {
63          _filter = new SequenceTableFilter(tableNames, dataSet.isCaseSensitiveTableNames());
64          _dataSet = dataSet;
65      }
66  
67      /**
68       * Creates a FilteredDataSet that decorates the specified dataset and
69       * exposes only the tables allowed by the specified filter.
70       *
71       * @param dataSet the filtered dataset
72       * @param filter the filtering strategy
73       */
74      public FilteredDataSet(ITableFilter filter, IDataSet dataSet)
75      {
76          _dataSet = dataSet;
77          _filter = filter;
78      }
79  
80      ////////////////////////////////////////////////////////////////////////////
81      // AbstractDataSet class
82  
83      protected ITableIterator createIterator(boolean reversed)
84              throws DataSetException
85      {
86      	if(logger.isDebugEnabled())
87      		logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
88      	
89          return _filter.iterator(_dataSet, reversed);
90      }
91  
92      ////////////////////////////////////////////////////////////////////////////
93      // IDataSet interface
94  
95      public String[] getTableNames() throws DataSetException
96      {
97          return _filter.getTableNames(_dataSet);
98      }
99  
100     public ITableMetaData getTableMetaData(String tableName)
101             throws DataSetException
102     {
103         if (!_filter.accept(tableName))
104         {
105             throw new NoSuchTableException(tableName);
106         }
107 
108         return _dataSet.getTableMetaData(tableName);
109     }
110 
111     public ITable getTable(String tableName) throws DataSetException
112     {
113         logger.debug("getTable(tableName={}) - start", tableName);
114 
115         if (!_filter.accept(tableName))
116         {
117             throw new NoSuchTableException(tableName);
118         }
119 
120         return _dataSet.getTable(tableName);
121     }
122 }
123 
124 
125 
126 
127 
128 
129