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