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.IDataSet;
25  import org.dbunit.dataset.ITable;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import org.dbunit.dataset.DataSetException;
30  
31  import java.util.Collection;
32  
33  /**
34   * This filter exposes only tables matching include patterns and not matching
35   * exclude patterns. This implementation do not modify the original table
36   * sequence from the filtered dataset and support duplicate table names.
37   *
38   * @author Manuel Laflamme
39   * @since Apr 17, 2004
40   * @version $Revision$
41   */
42  public class DefaultTableFilter extends AbstractTableFilter implements ITableFilter
43  {
44  
45      /**
46       * Logger for this class
47       */
48      private static final Logger logger = LoggerFactory.getLogger(DefaultTableFilter.class);
49  
50      private final IncludeTableFilter _includeFilter = new IncludeTableFilter();
51      private final ExcludeTableFilter _excludeFilter = new ExcludeTableFilter();
52  
53      /**
54       * Add a new accepted table name pattern.
55       * The following wildcard characters are supported:
56       * '*' matches zero or more characters,
57       * '?' matches one character.
58       */
59      public void includeTable(String patternName)
60      {
61          logger.debug("includeTable(patternName=" + patternName + ") - start");
62  
63          _includeFilter.includeTable(patternName);
64      }
65  
66      /**
67       * Add a new refused table pattern name.
68       * The following wildcard characters are supported:
69       * '*' matches zero or more characters,
70       * '?' matches one character.
71       */
72      public void excludeTable(String patternName)
73      {
74          logger.debug("excludeTable(patternName=" + patternName + ") - start");
75  
76          _excludeFilter.excludeTable(patternName);
77      }
78  
79      ////////////////////////////////////////////////////////////////////////////
80      // AbstractTableFilter interface
81  
82      public boolean isValidName(String tableName) throws DataSetException
83      {
84          logger.debug("isValidName(tableName=" + tableName + ") - start");
85  
86          if (_includeFilter.isEmpty() || _includeFilter.accept(tableName))
87          {
88              return _excludeFilter.accept(tableName);
89          }
90          return false;
91      }
92  
93      /**
94       * Not supported.  Throws AmbiguousTableNameException when called.  It just
95       * does not make sense to add a table since this class holds both
96       * IncludeTableFilter and ExcludeTableFilter sets.  So to which set would the
97       * table be stored into?  The only way out is to throw an exception.
98       * @param table
99       * @throws AmbiguousTableNameException
100      */
101     public void addTable(ITable table) throws UnsupportedOperationException {
102         logger.debug("addTable() - start");
103         throw new UnsupportedOperationException();
104     }
105 
106     /**
107      * Not supported.  Throws AmbiguousTableNameException when called.  It just
108      * does not make sense to add tables since this class holds both
109      * IncludeTableFilter and ExcludeTableFilter sets.  So to which set would the
110      * tables be stored into?  The only way out is to throw an exception.
111      * @param tables
112      * @throws AmbiguousTableNameException
113      */
114     public void addTables(Collection<ITable> tables) throws UnsupportedOperationException {
115         logger.debug("addTables(Collection) - start");
116         throw new UnsupportedOperationException();
117     }
118 
119     /**
120      * Not supported.  Throws AmbiguousTableNameException when called.  It just
121      * does not make sense to add tables from a dataset since this class holds both
122      * IncludeTableFilter and ExcludeTableFilter sets.  So to which set would the
123      * tables be stored into?  The only way out is to throw an exception.
124      * @param dataSet
125      * @throws AmbiguousTableNameException
126      */
127     public void addTables(IDataSet dataSet) throws UnsupportedOperationException {
128         logger.debug("addTables(IDataSet) - start");
129         throw new UnsupportedOperationException();
130     }
131 }