KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > FilteredDataSet


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.dbunit.dataset.filter.ITableFilter;
25 import org.dbunit.dataset.filter.SequenceTableFilter;
26
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  * @version $Revision: 1.18 $
37  * @since Feb 22, 2002
38  */

39 public class FilteredDataSet extends AbstractDataSet
40 {
41     private final IDataSet _dataSet;
42     private final ITableFilter _filter;
43
44     /**
45      * Creates a FilteredDataSet that decorates the specified dataset and
46      * exposes only the specified tables using {@link SequenceTableFilter} as
47      * filtering startegy.
48      */

49     public FilteredDataSet(String JavaDoc[] tableNames, IDataSet dataSet)
50     {
51         _filter = new SequenceTableFilter(tableNames);
52         _dataSet = dataSet;
53     }
54
55     /**
56      * Creates a FilteredDataSet that decorates the specified dataset and
57      * exposes only the tables allowed by the specified filter.
58      *
59      * @param dataSet the filtered dataset
60      * @param filter the filtering strategy
61      */

62     public FilteredDataSet(ITableFilter filter, IDataSet dataSet)
63     {
64         _dataSet = dataSet;
65         _filter = filter;
66     }
67
68     ////////////////////////////////////////////////////////////////////////////
69
// AbstractDataSet class
70

71     protected ITableIterator createIterator(boolean reversed)
72             throws DataSetException
73     {
74         return _filter.iterator(_dataSet, reversed);
75     }
76
77     ////////////////////////////////////////////////////////////////////////////
78
// IDataSet interface
79

80     public String JavaDoc[] getTableNames() throws DataSetException
81     {
82         return _filter.getTableNames(_dataSet);
83     }
84
85     public ITableMetaData getTableMetaData(String JavaDoc tableName)
86             throws DataSetException
87     {
88         if (!_filter.accept(tableName))
89         {
90             throw new NoSuchTableException(tableName);
91         }
92
93         return _dataSet.getTableMetaData(tableName);
94     }
95
96     public ITable getTable(String JavaDoc tableName) throws DataSetException
97     {
98         if (!_filter.accept(tableName))
99         {
100             throw new NoSuchTableException(tableName);
101         }
102
103         return _dataSet.getTable(tableName);
104     }
105 }
106
107
108
109
110
111
112
113
Popular Tags