001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.filter.ITableFilter;
028: import org.dbunit.dataset.filter.SequenceTableFilter;
029:
030: /**
031: * Decorates a dataset and exposes only some tables from it. Can be used with
032: * different filtering strategies.
033: *
034: * @see ITableFilter
035: * @see SequenceTableFilter
036: * @see org.dbunit.dataset.filter.DefaultTableFilter
037: *
038: * @author Manuel Laflamme
039: * @version $Revision: 554 $
040: * @since Feb 22, 2002
041: */
042: public class FilteredDataSet extends AbstractDataSet {
043:
044: /**
045: * Logger for this class
046: */
047: private static final Logger logger = LoggerFactory
048: .getLogger(FilteredDataSet.class);
049:
050: private final IDataSet _dataSet;
051: private final ITableFilter _filter;
052:
053: /**
054: * Creates a FilteredDataSet that decorates the specified dataset and
055: * exposes only the specified tables using {@link SequenceTableFilter} as
056: * filtering startegy.
057: */
058: public FilteredDataSet(String[] tableNames, IDataSet dataSet) {
059: _filter = new SequenceTableFilter(tableNames);
060: _dataSet = dataSet;
061: }
062:
063: /**
064: * Creates a FilteredDataSet that decorates the specified dataset and
065: * exposes only the tables allowed by the specified filter.
066: *
067: * @param dataSet the filtered dataset
068: * @param filter the filtering strategy
069: */
070: public FilteredDataSet(ITableFilter filter, IDataSet dataSet) {
071: _dataSet = dataSet;
072: _filter = filter;
073: }
074:
075: ////////////////////////////////////////////////////////////////////////////
076: // AbstractDataSet class
077:
078: protected ITableIterator createIterator(boolean reversed)
079: throws DataSetException {
080: logger.debug("createIterator(reversed=" + reversed
081: + ") - start");
082:
083: return _filter.iterator(_dataSet, reversed);
084: }
085:
086: ////////////////////////////////////////////////////////////////////////////
087: // IDataSet interface
088:
089: public String[] getTableNames() throws DataSetException {
090: logger.debug("getTableNames() - start");
091:
092: return _filter.getTableNames(_dataSet);
093: }
094:
095: public ITableMetaData getTableMetaData(String tableName)
096: throws DataSetException {
097: logger.debug("getTableMetaData(tableName=" + tableName
098: + ") - start");
099:
100: if (!_filter.accept(tableName)) {
101: throw new NoSuchTableException(tableName);
102: }
103:
104: return _dataSet.getTableMetaData(tableName);
105: }
106:
107: public ITable getTable(String tableName) throws DataSetException {
108: logger.debug("getTable(tableName=" + tableName + ") - start");
109:
110: if (!_filter.accept(tableName)) {
111: throw new NoSuchTableException(tableName);
112: }
113:
114: return _dataSet.getTable(tableName);
115: }
116: }
|