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