KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > filter > IncludeTableFilterTest


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.dataset.*;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * @author Manuel Laflamme
31  * @since Mar 11, 2003
32  * @version $Revision: 1.6 $
33  */

34 public class IncludeTableFilterTest extends AbstractTableFilterTest
35 {
36     static final String JavaDoc MATCHING_NAME = "aBcDe";
37     static final String JavaDoc[] MATCHING_PATTERNS = {
38         "?bcde",
39         "?bc*",
40         "*",
41         "a?cde",
42         "abcd?",
43         "*e",
44         "a*",
45         "a*e",
46         "a*d*e",
47         "a**e",
48         "abcde*",
49         "*abcde",
50         "?????",
51     };
52     static final String JavaDoc[] NONMATCHING_PATTERNS = {
53         "?abcde",
54         "abcde?",
55         "*f*",
56         "??????",
57         "????",
58     };
59
60
61     public IncludeTableFilterTest(String JavaDoc s)
62     {
63         super(s);
64     }
65
66     public void testAccept() throws Exception JavaDoc
67     {
68         String JavaDoc[] validNames = getExpectedNames();
69         ITableFilter filter = new IncludeTableFilter(validNames);
70
71         for (int i = 0; i < validNames.length; i++)
72         {
73             String JavaDoc validName = validNames[i];
74             assertEquals(validName, true, filter.accept(validName));
75         }
76     }
77
78     public void testIsCaseInsensitiveValidName() throws Exception JavaDoc
79     {
80         String JavaDoc[] validNames = getExpectedNames();
81         ITableFilter filter = new IncludeTableFilter(validNames);
82
83         for (int i = 0; i < validNames.length; i++)
84         {
85             String JavaDoc validName = validNames[i];
86             assertEquals(validName, true, filter.accept(validName));
87         }
88     }
89
90     public void testIsValidNameAndInvalid() throws Exception JavaDoc
91     {
92         String JavaDoc[] invalidNames = new String JavaDoc[] {
93             "INVALID_TABLE",
94             "UNKNOWN_TABLE",
95         };
96         String JavaDoc[] validNames = getExpectedNames();
97         ITableFilter filter = new IncludeTableFilter(validNames);
98
99         for (int i = 0; i < invalidNames.length; i++)
100         {
101             String JavaDoc invalidName = invalidNames[i];
102             assertEquals(invalidName, false, filter.accept(invalidName));
103         }
104     }
105
106     public void testGetTableNames() throws Exception JavaDoc
107     {
108         String JavaDoc[] expectedNames = getExpectedNames();
109         ITableFilter filter = new IncludeTableFilter(expectedNames);
110
111         IDataSet dataSet = createDataSet();
112         assertTrue("dataset names count",
113                 dataSet.getTableNames().length > expectedNames.length);
114
115         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
116         assertEquals("name count", expectedNames.length, actualNames.length);
117         assertEquals("names",
118                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
119     }
120
121     public void testGetTableNamesAndTableNotInDecoratedDataSet() throws Exception JavaDoc
122     {
123         String JavaDoc[] expectedNames = getExpectedNames();
124
125         List JavaDoc filterNameList = new ArrayList JavaDoc(Arrays.asList(expectedNames));
126         filterNameList.add("UNKNOWN_TABLE");
127         String JavaDoc[] filterNames = (String JavaDoc[])filterNameList.toArray(new String JavaDoc[0]);
128         ITableFilter filter = new IncludeTableFilter(filterNames);
129
130         IDataSet dataSet = createDataSet();
131         assertTrue("dataset names count",
132                 dataSet.getTableNames().length > expectedNames.length);
133
134         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
135         assertEquals("name count", expectedNames.length, actualNames.length);
136         assertEquals("names",
137                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
138     }
139
140     public void testGetDuplicateTableNames() throws Exception JavaDoc
141     {
142         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
143         ITableFilter filter = new IncludeTableFilter(expectedNames);
144
145         IDataSet dataSet = createDuplicateDataSet();
146         assertTrue("dataset names count",
147                 dataSet.getTableNames().length > expectedNames.length);
148
149         String JavaDoc[] 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 testGetCaseInsensitiveTableNames() throws Exception JavaDoc
156     {
157         String JavaDoc[] filterNames = getExpectedNames();
158         ITableFilter filter = new IncludeTableFilter(filterNames);
159
160         String JavaDoc[] expectedNames = getExpectedLowerNames();
161         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
162         assertTrue("dataset names count",
163                 dataSet.getTableNames().length > expectedNames.length);
164
165         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
166         assertEquals("name count", expectedNames.length, actualNames.length);
167         assertEquals("names",
168                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
169     }
170
171     public void testGetReverseTableNames() throws Exception JavaDoc
172     {
173         String JavaDoc[] expectedNames = getExpectedNames();
174         String JavaDoc[] filterNames = DataSetUtils.reverseStringArray(expectedNames);
175         ITableFilter filter = new IncludeTableFilter(filterNames);
176
177         IDataSet dataSet = createDataSet();
178         assertTrue("dataset names count",
179                 dataSet.getTableNames().length > expectedNames.length);
180
181         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
182         assertEquals("name count", expectedNames.length, actualNames.length);
183         assertEquals("names",
184                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
185     }
186
187     public void testIterator() throws Exception JavaDoc
188     {
189         String JavaDoc[] expectedNames = getExpectedNames();
190         ITableFilter filter = new IncludeTableFilter(expectedNames);
191
192         IDataSet dataSet = createDataSet();
193         assertTrue("dataset names count",
194                 dataSet.getTableNames().length > expectedNames.length);
195
196         ITable[] actualTables = DataSetUtils.getTables(
197                 filter.iterator(dataSet, false));
198         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
199         assertEquals("table count", expectedNames.length, actualTables.length);
200         assertEquals("table names",
201                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
202     }
203
204     public void testIteratorWithDuplicateTables() throws Exception JavaDoc
205     {
206         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
207         ITableFilter filter = new IncludeTableFilter(expectedNames);
208
209         IDataSet dataSet = createDuplicateDataSet();
210         assertTrue("dataset names count",
211                 dataSet.getTableNames().length > expectedNames.length);
212
213         ITable[] actualTables = DataSetUtils.getTables(
214                 filter.iterator(dataSet, false));
215         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
216         assertEquals("table count", expectedNames.length, actualTables.length);
217         assertEquals("table names",
218                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
219     }
220
221     public void testCaseInsensitiveIterator() throws Exception JavaDoc
222     {
223         ITableFilter filter = new IncludeTableFilter(getExpectedNames());
224         String JavaDoc[] lowerNames = getExpectedLowerNames();
225
226         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
227         assertTrue("dataset names count",
228                 dataSet.getTableNames().length > lowerNames.length);
229
230         ITable[] actualTables = DataSetUtils.getTables(
231                 filter.iterator(dataSet, false));
232         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
233         assertEquals("table count", lowerNames.length, actualTables.length);
234         assertEquals("table names",
235                 Arrays.asList(lowerNames), Arrays.asList(actualNames));
236     }
237
238     public void testReverseIterator() throws Exception JavaDoc
239     {
240         String JavaDoc[] filterNames = getExpectedNames();
241         String JavaDoc[] expectedNames = DataSetUtils.reverseStringArray(filterNames);
242         ITableFilter filter = new IncludeTableFilter(filterNames);
243
244         IDataSet dataSet = createDataSet();
245         assertTrue("dataset names count",
246                 dataSet.getTableNames().length > expectedNames.length);
247
248         ITable[] actualTables = DataSetUtils.getTables(
249                 filter.iterator(dataSet, true));
250         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
251         assertEquals("table count", expectedNames.length, actualTables.length);
252         assertEquals("table names",
253                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
254     }
255
256     public void testIteratorAndTableNotInDecoratedDataSet() throws Exception JavaDoc
257     {
258         String JavaDoc[] expectedNames = getExpectedNames();
259
260         List JavaDoc filterNameList = new ArrayList JavaDoc(Arrays.asList(expectedNames));
261         filterNameList.add("UNKNOWN_TABLE");
262         String JavaDoc[] filterNames = (String JavaDoc[])filterNameList.toArray(new String JavaDoc[0]);
263         ITableFilter filter = new IncludeTableFilter(filterNames);
264
265         IDataSet dataSet = createDataSet();
266         assertTrue("dataset names count",
267                 dataSet.getTableNames().length > expectedNames.length);
268
269         ITable[] actualTables = DataSetUtils.getTables(
270                 filter.iterator(dataSet, false));
271         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
272         assertEquals("table count", expectedNames.length, actualTables.length);
273         assertEquals("table names",
274                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
275     }
276
277     ////////////////////////////////////////////////////////////////////////////
278

279     public void testIsValidNameWithPatterns() throws Exception JavaDoc
280     {
281         String JavaDoc validName = MATCHING_NAME;
282
283         String JavaDoc[] patterns = MATCHING_PATTERNS;
284         for (int i = 0; i < patterns.length; i++)
285         {
286             String JavaDoc pattern = patterns[i];
287             IncludeTableFilter filter = new IncludeTableFilter();
288             filter.includeTable(pattern);
289             assertEquals(pattern, true, filter.accept(validName));
290         }
291     }
292
293     public void testIsValidNameInvalidWithPatterns() throws Exception JavaDoc
294     {
295         String JavaDoc validName = MATCHING_NAME;
296
297         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
298         for (int i = 0; i < patterns.length; i++)
299         {
300             String JavaDoc pattern = patterns[i];
301             IncludeTableFilter filter = new IncludeTableFilter();
302             filter.includeTable(pattern);
303             assertEquals(pattern, false, filter.accept(validName));
304         }
305     }
306
307     public void testGetTableNamesWithPatterns() throws Exception JavaDoc
308     {
309         String JavaDoc[] expectedNames = new String JavaDoc[] {MATCHING_NAME};
310         IDataSet dataSet = new DefaultDataSet(new ITable[] {
311             new DefaultTable(MATCHING_NAME),
312             new DefaultTable("toto"),
313             new DefaultTable("1234"),
314             new DefaultTable("fedcba"),
315         });
316         assertTrue("dataset names count",
317                 dataSet.getTableNames().length > expectedNames.length);
318
319         String JavaDoc[] patterns = MATCHING_PATTERNS;
320         for (int i = 0; i < patterns.length; i++)
321         {
322             String JavaDoc pattern = patterns[i];
323             IncludeTableFilter filter = new IncludeTableFilter();
324             filter.includeTable(pattern);
325
326             // this pattern match everything, so ensure nothing is filtered
327
if (pattern.equals("*"))
328             {
329                 String JavaDoc[] actualNames = filter.getTableNames(dataSet);
330                 assertEquals("name count - " + pattern,
331                         dataSet.getTableNames().length, actualNames.length);
332                 assertEquals("names - " + pattern,
333                         Arrays.asList(dataSet.getTableNames()),
334                         Arrays.asList(actualNames));
335             }
336             else
337             {
338                 String JavaDoc[] actualNames = filter.getTableNames(dataSet);
339                 assertEquals("name count - " + pattern,
340                         expectedNames.length, actualNames.length);
341                 assertEquals("names - " + pattern,
342                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
343             }
344         }
345     }
346
347     public void testGetTableNamesWithNonMatchingPatterns() throws Exception JavaDoc
348     {
349         IDataSet dataSet = new DefaultDataSet(new ITable[] {
350             new DefaultTable(MATCHING_NAME),
351         });
352         assertTrue("dataset names count",
353                 dataSet.getTableNames().length > 0);
354
355         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
356         for (int i = 0; i < patterns.length; i++)
357         {
358             String JavaDoc pattern = patterns[i];
359             IncludeTableFilter filter = new IncludeTableFilter();
360             filter.includeTable(pattern);
361
362             String JavaDoc[] actualNames = filter.getTableNames(dataSet);
363             assertEquals("name count - " + pattern, 0, actualNames.length);
364         }
365     }
366
367     public void testGetTablesWithPatterns() throws Exception JavaDoc
368     {
369         String JavaDoc[] expectedNames = new String JavaDoc[] {MATCHING_NAME};
370         IDataSet dataSet = new DefaultDataSet(new ITable[] {
371             new DefaultTable(MATCHING_NAME),
372             new DefaultTable("toto"),
373             new DefaultTable("1234"),
374             new DefaultTable("fedcba"),
375         });
376         assertTrue("dataset names count",
377                 dataSet.getTableNames().length > expectedNames.length);
378
379         String JavaDoc[] patterns = MATCHING_PATTERNS;
380         for (int i = 0; i < patterns.length; i++)
381         {
382             String JavaDoc pattern = patterns[i];
383             IncludeTableFilter filter = new IncludeTableFilter();
384             filter.includeTable(pattern);
385
386             // this pattern match everything, so ensure nothing is filtered
387
if (pattern.equals("*"))
388             {
389                 ITable[] actualTables = DataSetUtils.getTables(
390                         filter.iterator(dataSet, false));
391                 String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
392                 assertEquals("table count - " + pattern,
393                         dataSet.getTableNames().length, actualNames.length);
394                 assertEquals("table names - " + pattern,
395                         Arrays.asList(dataSet.getTableNames()),
396                         Arrays.asList(actualNames));
397             }
398             else
399             {
400                 ITable[] actualTables = DataSetUtils.getTables(
401                         filter.iterator(dataSet, false));
402                 String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
403                 assertEquals("table count - " + pattern,
404                         expectedNames.length, actualTables.length);
405                 assertEquals("table names - " + pattern,
406                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
407             }
408         }
409     }
410
411     public void testGetTablesWithNonMatchingPatterns() throws Exception JavaDoc
412     {
413         IDataSet dataSet = new DefaultDataSet(new ITable[] {
414             new DefaultTable(MATCHING_NAME),
415         });
416         assertTrue("dataset names count",
417                 dataSet.getTableNames().length > 0);
418
419         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
420         for (int i = 0; i < patterns.length; i++)
421         {
422             String JavaDoc pattern = patterns[i];
423             IncludeTableFilter filter = new IncludeTableFilter();
424             filter.includeTable(pattern);
425
426             ITable[] actualTables = DataSetUtils.getTables(
427                     filter.iterator(dataSet, false));
428             assertEquals("table count - " + pattern, 0, actualTables.length);
429         }
430     }
431
432 }
433
Popular Tags