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  package org.dbunit.dataset.filter;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.dbunit.database.AmbiguousTableNameException;
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.DataSetUtils;
29  import org.dbunit.dataset.IDataSet;
30  import org.dbunit.dataset.ITableIterator;
31  import org.dbunit.dataset.ITableMetaData;
32  import org.dbunit.dataset.NoSuchTableException;
33  import org.dbunit.dataset.OrderedTableNameMap;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /**
38   * This filter expose a specified table sequence and can be used to reorder
39   * tables in a dataset. This implementation does not support duplicate table names.
40   * Thus you cannot specify the same table name more than once in this filter
41   * and the filtered dataset must not contains duplicate table names. This is
42   * the default filter used by the {@link org.dbunit.dataset.FilteredDataSet}.
43   *
44   * @author Manuel Laflamme
45   * @author Last changed by: $Author: gommma $
46   * @version $Revision: 1049 $ $Date: 2009-09-27 15:59:22 -0500 (Sun, 27 Sep 2009) $
47   * @since Mar 7, 2003
48   */
49  public class SequenceTableFilter implements ITableFilter
50  {
51  
52      /**
53       * Logger for this class
54       */
55      private static final Logger logger = LoggerFactory.getLogger(SequenceTableFilter.class);
56  
57      private final OrderedTableNameMap _tableNameMap;
58  
59      /**
60       * Creates a new SequenceTableFilter with specified table names sequence.
61       * @throws AmbiguousTableNameException If the given array contains ambiguous names
62       */
63      public SequenceTableFilter(String[] tableNames) 
64      throws AmbiguousTableNameException
65      {
66          this(tableNames, false);
67      }
68  
69      /**
70       * Creates a new SequenceTableFilter with specified table names sequence.
71       * @param tableNames
72       * @param caseSensitiveTableNames
73       * @throws AmbiguousTableNameException If the given array contains ambiguous names
74       * @since 2.4.2
75       */
76      public SequenceTableFilter(String[] tableNames, boolean caseSensitiveTableNames) 
77      throws AmbiguousTableNameException
78      {
79          // Gather all tables in the OrderedTableNameMap which also makes the duplicate check
80          _tableNameMap = new OrderedTableNameMap(caseSensitiveTableNames);
81          for (int i = 0; i < tableNames.length; i++)
82          {
83              _tableNameMap.add(tableNames[i], null);
84          }
85      }
86  
87      ////////////////////////////////////////////////////////////////////////////
88      // ITableFilter interface
89  
90      public boolean accept(String tableName) throws DataSetException
91      {
92          logger.debug("accept(tableName={}) - start", tableName);
93  
94          return _tableNameMap.containsTable(tableName);
95      }
96  
97      public String[] getTableNames(IDataSet dataSet) throws DataSetException
98      {
99          logger.debug("getTableNames(dataSet={}) - start", dataSet);
100 
101         List nameList = new ArrayList();
102         String[] tableNames = _tableNameMap.getTableNames();
103         for (int i = 0; i < tableNames.length; i++)
104         {
105             try
106             {
107                 // Use the table name from the filtered dataset. This ensure
108                 // that table names are having the same case (lower/upper) from
109                 // getTableNames() and getTables() methods.
110                 ITableMetaData metaData = dataSet.getTableMetaData(tableNames[i]);
111                 nameList.add(metaData.getTableName());
112             }
113             catch (NoSuchTableException e)
114             {
115                 logger.debug("Table '{}' not found in filtered dataset {}", tableNames[i], dataSet);
116                 // Skip this table name because the filtered dataset does not
117                 // contains it.
118             }
119         }
120 
121         return (String[])nameList.toArray(new String[0]);
122     }
123 
124     public ITableIterator iterator(IDataSet dataSet, boolean reversed)
125             throws DataSetException
126     {
127     	if(logger.isDebugEnabled())
128     		logger.debug("iterator(dataSet={}, reversed={}) - start", dataSet, String.valueOf(reversed));
129 
130         String[] tableNames = getTableNames(dataSet);
131         return new SequenceTableIterator(reversed ?
132                 DataSetUtils.reverseStringArray(tableNames) : tableNames, dataSet);
133     }
134     
135     public String toString()
136     {
137         StringBuffer sb = new StringBuffer();
138         sb.append(getClass().getName()).append("[");
139         sb.append("_tableNameMap=").append(_tableNameMap);
140         sb.append("]");
141         return sb.toString();
142     }
143 }
144