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.Arrays;
25  import java.util.Collection;
26  
27  import org.dbunit.dataset.DataSetUtils;
28  import org.dbunit.dataset.DefaultDataSet;
29  import org.dbunit.dataset.DefaultTable;
30  import org.dbunit.dataset.IDataSet;
31  import org.dbunit.dataset.ITable;
32  import org.dbunit.dataset.LowerCaseDataSet;
33  
34  /**
35   * @author Manuel Laflamme
36   * @since Mar 18, 2003
37   * @version $Revision$
38   */
39  public class ExcludeTableFilterTest extends AbstractTableFilterTest
40  {
41      static final String MATCHING_NAME = "aBcDe";
42      static final String[] MATCHING_PATTERNS =
43              IncludeTableFilterTest.MATCHING_PATTERNS;
44      static final String[] NONMATCHING_PATTERNS =
45              IncludeTableFilterTest.NONMATCHING_PATTERNS;
46  
47      public ExcludeTableFilterTest(String s)
48      {
49          super(s);
50      }
51  
52      public void testAccept() throws Exception
53      {
54          String[] validNames = getExpectedNames();
55          ExcludeTableFilter filter = new ExcludeTableFilter();
56          filter.excludeTable(getExtraTableName());
57  
58          for (int i = 0; i < validNames.length; i++)
59          {
60              String validName = validNames[i];
61              assertEquals(validName, true, filter.accept(validName));
62          }
63      }
64  
65      public void testAcceptWithAddTableITable() throws Exception
66      {
67          String[] validNames = getExpectedNames();
68          ExcludeTableFilter filter = new ExcludeTableFilter();
69          ITable itable = new DefaultTable(getExtraTableName());
70          filter.addTable(itable);
71  
72          for (int i = 0; i < validNames.length; i++)
73          {
74              String validName = validNames[i];
75              assertEquals(validName, true, filter.accept(validName));
76          }
77      }
78  
79      public void testAcceptWithAddTableCollection() throws Exception
80      {
81          String[] validNames = getExpectedNames();
82          ExcludeTableFilter filter = new ExcludeTableFilter();
83  
84          Collection<ITable> collection = new ArrayList<ITable>();
85          ITable itable = new DefaultTable(getExtraTableName());
86          collection.add(itable);
87  
88          filter.addTables(collection);
89  
90          for (int i = 0; i < validNames.length; i++)
91          {
92              String validName = validNames[i];
93              assertEquals(validName, true, filter.accept(validName));
94          }
95      }
96  
97      public void testAcceptWithAddTableDataSet() throws Exception
98      {
99          String[] validNames = getExpectedNames();
100         ExcludeTableFilter filter = new ExcludeTableFilter();
101 
102         filter.addTables(new DefaultDataSet(new DefaultTable(getExtraTableName())));
103 
104         for (int i = 0; i < validNames.length; i++)
105         {
106             String validName = validNames[i];
107             assertEquals(validName, true, filter.accept(validName));
108         }
109     }
110 
111     public void testIsCaseInsensitiveValidName() throws Exception
112     {
113         String[] validNames = getExpectedNames();
114         ExcludeTableFilter filter = new ExcludeTableFilter();
115         filter.excludeTable(getExtraTableName());
116 
117         for (int i = 0; i < validNames.length; i++)
118         {
119             String validName = validNames[i];
120             assertEquals(validName, true, filter.accept(validName));
121         }
122     }
123 
124     public void testIsValidNameAndInvalid() throws Exception
125     {
126         String[] invalidNames = new String[] {
127             "INVALID_TABLE",
128             "UNKNOWN_TABLE",
129         };
130         ITableFilter filter = new ExcludeTableFilter(invalidNames);
131 
132         for (int i = 0; i < invalidNames.length; i++)
133         {
134             String invalidName = invalidNames[i];
135             assertEquals(invalidName, false, filter.accept(invalidName));
136         }
137     }
138 
139     public void testGetTableNames() throws Exception
140     {
141         String[] expectedNames = getExpectedNames();
142         ExcludeTableFilter filter = new ExcludeTableFilter();
143         filter.excludeTable(getExtraTableName());
144 
145         IDataSet dataSet = createDataSet();
146         assertTrue("dataset names count",
147                 dataSet.getTableNames().length > expectedNames.length);
148 
149         String[] actualNames = filter.getTableNames(dataSet);
150         assertEquals("name count", expectedNames.length, actualNames.length);
151         assertEquals("names",
152                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
153     }
154 
155     public void testGetTableNamesAndTableNotInDecoratedDataSet() throws Exception
156     {
157         String[] expectedNames = getExpectedNames();
158         ExcludeTableFilter filter = new ExcludeTableFilter();
159         filter.excludeTable(getExtraTableName());
160         filter.excludeTable("UNKNOWN_TABLE");
161 
162         IDataSet dataSet = createDataSet();
163         assertTrue("dataset names count",
164                 dataSet.getTableNames().length > expectedNames.length);
165 
166         String[] actualNames = filter.getTableNames(dataSet);
167         assertEquals("name count", expectedNames.length, actualNames.length);
168         assertEquals("names",
169                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
170     }
171 
172     public void testGetCaseInsensitiveTableNames() throws Exception
173     {
174         ExcludeTableFilter filter = new ExcludeTableFilter();
175         filter.excludeTable(getExtraTableName());
176 
177         String[] expectedNames = getExpectedLowerNames();
178         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
179         assertTrue("dataset names count",
180                 dataSet.getTableNames().length > expectedNames.length);
181 
182         String[] actualNames = filter.getTableNames(dataSet);
183         assertEquals("name count", expectedNames.length, actualNames.length);
184         assertEquals("names",
185                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
186     }
187 
188     public void testGetReverseTableNames() throws Exception
189     {
190         // Cannot test!
191     }
192 
193     public void testIterator() throws Exception
194     {
195         String[] expectedNames = getExpectedNames();
196         ExcludeTableFilter filter = new ExcludeTableFilter();
197         filter.excludeTable(getExtraTableName());
198 
199         IDataSet dataSet = createDataSet();
200         assertTrue("dataset names count",
201                 dataSet.getTableNames().length > expectedNames.length);
202 
203         ITable[] actualTables = DataSetUtils.getTables(
204                 filter.iterator(dataSet, false));
205         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
206         assertEquals("table count", expectedNames.length, actualTables.length);
207         assertEquals("table names",
208                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
209     }
210 
211     public void testCaseInsensitiveIterator() throws Exception
212     {
213         ExcludeTableFilter filter = new ExcludeTableFilter();
214         filter.excludeTable(getExtraTableName());
215 
216         String[] expectedNames = getExpectedLowerNames();
217         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
218         assertTrue("dataset names count",
219                 dataSet.getTableNames().length > expectedNames.length);
220 
221         ITable[] actualTables = DataSetUtils.getTables(
222                 filter.iterator(dataSet, false));
223         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
224         assertEquals("table count", expectedNames.length, actualTables.length);
225         assertEquals("table names",
226                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
227     }
228 
229     public void testReverseIterator() throws Exception
230     {
231         // Cannot test!
232     }
233 
234     public void testIteratorAndTableNotInDecoratedDataSet() throws Exception
235     {
236         String[] expectedNames = getExpectedNames();
237         ExcludeTableFilter filter = new ExcludeTableFilter();
238         filter.excludeTable(getExtraTableName());
239         filter.excludeTable("UNKNOWN_TABLE");
240 
241         IDataSet dataSet = createDataSet();
242         assertTrue("dataset names count",
243                 dataSet.getTableNames().length > expectedNames.length);
244 
245         ITable[] actualTables = DataSetUtils.getTables(
246                 filter.iterator(dataSet, false));
247         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
248         assertEquals("table count", expectedNames.length, actualTables.length);
249         assertEquals("table names",
250                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
251     }
252 
253     ////////////////////////////////////////////////////////////////////////////
254 
255     public void testIsValidNameWithPatterns() throws Exception
256     {
257         String validName = MATCHING_NAME;
258 
259         String[] patterns = NONMATCHING_PATTERNS;
260         for (int i = 0; i < patterns.length; i++)
261         {
262             String pattern = patterns[i];
263             ExcludeTableFilter filter = new ExcludeTableFilter();
264             filter.excludeTable(pattern);
265             assertEquals(pattern, true, filter.accept(validName));
266         }
267     }
268 
269     public void testIsValidNameInvalidWithPatterns() throws Exception
270     {
271         String validName = MATCHING_NAME;
272 
273         String[] patterns = MATCHING_PATTERNS;
274         for (int i = 0; i < patterns.length; i++)
275         {
276             String pattern = patterns[i];
277             ExcludeTableFilter filter = new ExcludeTableFilter();
278             filter.excludeTable(pattern);
279             assertEquals(pattern, false, filter.accept(validName));
280         }
281     }
282 
283     public void testGetTableNamesWithPatterns() throws Exception
284     {
285         String nonMatchingName = "toto titi tata";
286         String[] expectedNames = new String[] {nonMatchingName};
287         IDataSet dataSet = new DefaultDataSet(new ITable[] {
288             new DefaultTable(MATCHING_NAME),
289             new DefaultTable(nonMatchingName),
290         });
291         assertTrue("dataset names count",
292                 dataSet.getTableNames().length > expectedNames.length);
293 
294         String[] patterns = MATCHING_PATTERNS;
295         for (int i = 0; i < patterns.length; i++)
296         {
297             String pattern = patterns[i];
298             ExcludeTableFilter filter = new ExcludeTableFilter();
299             filter.excludeTable(pattern);
300 
301             // this pattern match everything, so ensure everything filtered
302             if (pattern.equals("*"))
303             {
304                 String[] actualNames = filter.getTableNames(dataSet);
305                 assertEquals("name count - " + pattern,
306                         0, actualNames.length);
307             }
308             else
309             {
310                 String[] actualNames = filter.getTableNames(dataSet);
311                 assertEquals("name count - " + pattern,
312                         expectedNames.length, actualNames.length);
313                 assertEquals("names - " + pattern,
314                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
315             }
316         }
317     }
318 
319     public void testGetTableNamesWithNonMatchingPatterns() throws Exception
320     {
321         String[] expectedNames = new String[] {MATCHING_NAME};
322         IDataSet dataSet = new DefaultDataSet(new ITable[] {
323             new DefaultTable(MATCHING_NAME),
324         });
325 
326         String[] patterns = NONMATCHING_PATTERNS;
327         for (int i = 0; i < patterns.length; i++)
328         {
329             String pattern = patterns[i];
330             ExcludeTableFilter filter = new ExcludeTableFilter();
331             filter.excludeTable(pattern);
332 
333             String[] actualNames = filter.getTableNames(dataSet);
334             assertEquals("name count - " + pattern,
335                     expectedNames.length, actualNames.length);
336             assertEquals("names - " + pattern,
337                     Arrays.asList(expectedNames), Arrays.asList(actualNames));
338         }
339     }
340 
341     public void testGetTablesWithPatterns() throws Exception
342     {
343         String nonMatchingName = "toto titi tata";
344         String[] expectedNames = new String[] {nonMatchingName};
345         IDataSet dataSet = new DefaultDataSet(new ITable[] {
346             new DefaultTable(MATCHING_NAME),
347             new DefaultTable(nonMatchingName),
348         });
349         assertTrue("dataset names count",
350                 dataSet.getTableNames().length > expectedNames.length);
351 
352         String[] patterns = MATCHING_PATTERNS;
353         for (int i = 0; i < patterns.length; i++)
354         {
355             String pattern = patterns[i];
356             ExcludeTableFilter filter = new ExcludeTableFilter();
357             filter.excludeTable(pattern);
358 
359             // this pattern match everything, so ensure everything is filtered
360             if (pattern.equals("*"))
361             {
362                 ITable[] actualTables = DataSetUtils.getTables(
363                         filter.iterator(dataSet, false));
364                 String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
365                 assertEquals("table count - " + pattern,
366                         0, actualNames.length);
367             }
368             else
369             {
370                 ITable[] actualTables = DataSetUtils.getTables(
371                         filter.iterator(dataSet, false));
372                 String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
373                 assertEquals("table count - " + pattern,
374                         expectedNames.length, actualTables.length);
375                 assertEquals("table names - " + pattern,
376                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
377             }
378         }
379     }
380 
381     public void testGetTablesWithNonMatchingPatterns() throws Exception
382     {
383         String[] expectedNames = new String[] {MATCHING_NAME};
384         IDataSet dataSet = new DefaultDataSet(new ITable[] {
385             new DefaultTable(MATCHING_NAME),
386         });
387         assertTrue("dataset names count",
388                 dataSet.getTableNames().length > 0);
389 
390         String[] patterns = NONMATCHING_PATTERNS;
391         for (int i = 0; i < patterns.length; i++)
392         {
393             String pattern = patterns[i];
394             ExcludeTableFilter filter = new ExcludeTableFilter();
395             filter.excludeTable(pattern);
396 
397             ITable[] actualTables = DataSetUtils.getTables(
398                     filter.iterator(dataSet, false));
399             String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
400             assertEquals("table count - " + pattern,
401                     expectedNames.length, actualTables.length);
402             assertEquals("table names - " + pattern,
403                     Arrays.asList(expectedNames), Arrays.asList(actualNames));
404         }
405     }
406 
407 }