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