KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > filter > SequenceTableFilter


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
23 import org.dbunit.database.AmbiguousTableNameException;
24 import org.dbunit.dataset.*;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * This filter expose a specified table sequence and can be used to reorder
31  * tables in a dataset. This implementation do not support duplicate table names.
32  * Thus you cannot specify the same table name more than once in this filter
33  * and the filtered dataset must not contains duplicate table names. This is
34  * the default filter used by the {@link org.dbunit.dataset.FilteredDataSet}.
35  *
36  * @author Manuel Laflamme
37  * @since Mar 7, 2003
38  * @version $Revision: 1.6 $
39  */

40 public class SequenceTableFilter implements ITableFilter
41 {
42     private final String JavaDoc[] _tableNames;
43
44     /**
45      * Creates a new SequenceTableFilter with specified table names sequence.
46      */

47     public SequenceTableFilter(String JavaDoc[] tableNames)
48     {
49         _tableNames = tableNames;
50     }
51
52     private boolean accept(String JavaDoc tableName, String JavaDoc[] tableNames,
53             boolean verifyDuplicate) throws AmbiguousTableNameException
54     {
55         boolean found = false;
56         for (int i = 0; i < tableNames.length; i++)
57         {
58             if (tableName.equalsIgnoreCase(tableNames[i]))
59             {
60                 if (!verifyDuplicate)
61                 {
62                     return true;
63                 }
64
65                 if (found)
66                 {
67                     throw new AmbiguousTableNameException(tableName);
68                 }
69                 found = true;
70             }
71         }
72
73         return found;
74     }
75
76     ////////////////////////////////////////////////////////////////////////////
77
// ITableFilter interface
78

79     public boolean accept(String JavaDoc tableName) throws DataSetException
80     {
81         return accept(tableName, _tableNames, true);
82     }
83
84     public String JavaDoc[] getTableNames(IDataSet dataSet) throws DataSetException
85     {
86         List JavaDoc nameList = new ArrayList JavaDoc();
87         for (int i = 0; i < _tableNames.length; i++)
88         {
89             try
90             {
91                 // Use the table name from the filtered dataset. This ensure
92
// that table names are having the same case (lower/upper) from
93
// getTableNames() and getTables() methods.
94
ITableMetaData metaData = dataSet.getTableMetaData(_tableNames[i]);
95                 nameList.add(metaData.getTableName());
96             }
97             catch (NoSuchTableException e)
98             {
99                 // Skip this table name because the filtered dataset does not
100
// contains it.
101
}
102         }
103
104         return (String JavaDoc[])nameList.toArray(new String JavaDoc[0]);
105     }
106
107     public ITableIterator iterator(IDataSet dataSet, boolean reversed)
108             throws DataSetException
109     {
110         String JavaDoc[] tableNames = getTableNames(dataSet);
111         return new SequenceTableIterator(reversed ?
112                 DataSetUtils.reverseStringArray(tableNames) : tableNames, dataSet);
113     }
114 }
115
116
Popular Tags