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