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 org.dbunit.database.AmbiguousTableNameException;
24  import org.dbunit.dataset.DataSetException;
25  import org.dbunit.dataset.IDataSet;
26  import org.dbunit.dataset.ITable;
27  import org.dbunit.dataset.ITableIterator;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.util.Collection;
32  
33  
34  /**
35   * This filter exposes only allowed tables from the filtered dataset. This
36   * implementation do not modify the original table sequence from the filtered
37   * dataset and support duplicate table names.
38   *
39   * @author Manuel Laflamme
40   * @since Mar 7, 2003
41   * @version $Revision$
42   */
43  public class IncludeTableFilter extends AbstractTableFilter implements ITableFilter
44  {
45  
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger = LoggerFactory.getLogger(IncludeTableFilter.class);
50  
51      private final PatternMatcher _patternMatcher = new PatternMatcher();
52  
53      /**
54       * Create a new empty IncludeTableFilter. Use {@link #includeTable} to allow
55       * access to some tables.
56       */
57      public IncludeTableFilter()
58      {
59      }
60  
61      /**
62       * Create a new IncludeTableFilter which allow access to specified tables.
63       */
64      public IncludeTableFilter(String[] tableNames)
65      {
66          for (int i = 0; i < tableNames.length; i++)
67          {
68              String tableName = tableNames[i];
69              includeTable(tableName);
70          }
71      }
72  
73      /**
74       * Add a new accepted table name pattern.
75       * The following wildcard characters are supported:
76       * '*' matches zero or more characters,
77       * '?' matches one character.
78       */
79      public void includeTable(String patternName)
80      {
81          logger.debug("includeTable(patternName={} - start", patternName);
82  
83          _patternMatcher.addPattern(patternName);
84      }
85  
86      public boolean isEmpty()
87      {
88          logger.debug("isEmpty() - start");
89  
90          return _patternMatcher.isEmpty();
91      }
92  
93      ////////////////////////////////////////////////////////////////////////////
94      // ITableFilter interface
95  
96      public boolean isValidName(String tableName)
97      {
98          logger.debug("isValidName(tableName={}) - start", tableName);
99  
100         return _patternMatcher.accept(tableName);
101     }
102 
103     public void addTable(ITable table) throws AmbiguousTableNameException {
104         logger.debug("addTable() - start");
105         includeTable(table.getTableMetaData().getTableName());
106     }
107 
108     public void addTables(Collection<ITable> tables) throws AmbiguousTableNameException {
109         logger.debug("addTables(Collection) - start");
110         for(ITable table: tables) {
111             addTable(table);
112         }
113     }
114 
115     public void addTables(IDataSet dataSet) throws DataSetException {
116         logger.debug("addTables(IDataSet) - start");
117         ITableIterator iterator = dataSet.iterator();
118         while(iterator.next())
119             addTable(iterator.getTable());
120     }
121 }