KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
26
27 /**
28  * @author Manuel Laflamme
29  * @since Mar 18, 2003
30  * @version $Revision: 1.6 $
31  */

32 public class ExcludeTableFilterTest extends AbstractTableFilterTest
33 {
34     static final String JavaDoc MATCHING_NAME = "aBcDe";
35     static final String JavaDoc[] MATCHING_PATTERNS =
36             IncludeTableFilterTest.MATCHING_PATTERNS;
37     static final String JavaDoc[] NONMATCHING_PATTERNS =
38             IncludeTableFilterTest.NONMATCHING_PATTERNS;
39
40     public ExcludeTableFilterTest(String JavaDoc s)
41     {
42         super(s);
43     }
44
45     public void testAccept() throws Exception JavaDoc
46     {
47         String JavaDoc[] validNames = getExpectedNames();
48         ExcludeTableFilter filter = new ExcludeTableFilter();
49         filter.excludeTable(getExtraTableName());
50
51         for (int i = 0; i < validNames.length; i++)
52         {
53             String JavaDoc validName = validNames[i];
54             assertEquals(validName, true, filter.accept(validName));
55         }
56     }
57
58     public void testIsCaseInsensitiveValidName() throws Exception JavaDoc
59     {
60         String JavaDoc[] validNames = getExpectedNames();
61         ExcludeTableFilter filter = new ExcludeTableFilter();
62         filter.excludeTable(getExtraTableName());
63
64         for (int i = 0; i < validNames.length; i++)
65         {
66             String JavaDoc validName = validNames[i];
67             assertEquals(validName, true, filter.accept(validName));
68         }
69     }
70
71     public void testIsValidNameAndInvalid() throws Exception JavaDoc
72     {
73         String JavaDoc[] invalidNames = new String JavaDoc[] {
74             "INVALID_TABLE",
75             "UNKNOWN_TABLE",
76         };
77         ITableFilter filter = new ExcludeTableFilter(invalidNames);
78
79         for (int i = 0; i < invalidNames.length; i++)
80         {
81             String JavaDoc invalidName = invalidNames[i];
82             assertEquals(invalidName, false, filter.accept(invalidName));
83         }
84     }
85
86     public void testGetTableNames() throws Exception JavaDoc
87     {
88         String JavaDoc[] expectedNames = getExpectedNames();
89         ExcludeTableFilter filter = new ExcludeTableFilter();
90         filter.excludeTable(getExtraTableName());
91
92         IDataSet dataSet = createDataSet();
93         assertTrue("dataset names count",
94                 dataSet.getTableNames().length > expectedNames.length);
95
96         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
97         assertEquals("name count", expectedNames.length, actualNames.length);
98         assertEquals("names",
99                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
100     }
101
102     public void testGetTableNamesAndTableNotInDecoratedDataSet() throws Exception JavaDoc
103     {
104         String JavaDoc[] expectedNames = getExpectedNames();
105         ExcludeTableFilter filter = new ExcludeTableFilter();
106         filter.excludeTable(getExtraTableName());
107         filter.excludeTable("UNKNOWN_TABLE");
108
109         IDataSet dataSet = createDataSet();
110         assertTrue("dataset names count",
111                 dataSet.getTableNames().length > expectedNames.length);
112
113         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
114         assertEquals("name count", expectedNames.length, actualNames.length);
115         assertEquals("names",
116                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
117     }
118
119     public void testGetDuplicateTableNames() throws Exception JavaDoc
120     {
121         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
122         ExcludeTableFilter filter = new ExcludeTableFilter();
123         filter.excludeTable(getExtraTableName());
124
125         IDataSet dataSet = createDuplicateDataSet();
126         assertTrue("dataset names count",
127                 dataSet.getTableNames().length > expectedNames.length);
128
129         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
130         assertEquals("name count", expectedNames.length, actualNames.length);
131         assertEquals("names",
132                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
133     }
134
135     public void testGetCaseInsensitiveTableNames() throws Exception JavaDoc
136     {
137         ExcludeTableFilter filter = new ExcludeTableFilter();
138         filter.excludeTable(getExtraTableName());
139
140         String JavaDoc[] expectedNames = getExpectedLowerNames();
141         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
142         assertTrue("dataset names count",
143                 dataSet.getTableNames().length > expectedNames.length);
144
145         String JavaDoc[] actualNames = filter.getTableNames(dataSet);
146         assertEquals("name count", expectedNames.length, actualNames.length);
147         assertEquals("names",
148                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
149     }
150
151     public void testGetReverseTableNames() throws Exception JavaDoc
152     {
153         // Cannot test!
154
}
155
156     public void testIterator() throws Exception JavaDoc
157     {
158         String JavaDoc[] expectedNames = getExpectedNames();
159         ExcludeTableFilter filter = new ExcludeTableFilter();
160         filter.excludeTable(getExtraTableName());
161
162         IDataSet dataSet = createDataSet();
163         assertTrue("dataset names count",
164                 dataSet.getTableNames().length > expectedNames.length);
165
166         ITable[] actualTables = DataSetUtils.getTables(
167                 filter.iterator(dataSet, false));
168         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
169         assertEquals("table count", expectedNames.length, actualTables.length);
170         assertEquals("table names",
171                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
172     }
173
174     public void testIteratorWithDuplicateTables() throws Exception JavaDoc
175     {
176         String JavaDoc[] expectedNames = getExpectedDuplicateNames();
177         ExcludeTableFilter filter = new ExcludeTableFilter();
178         filter.excludeTable(getExtraTableName());
179
180         IDataSet dataSet = createDuplicateDataSet();
181         assertTrue("dataset names count",
182                 dataSet.getTableNames().length > expectedNames.length);
183
184         ITable[] actualTables = DataSetUtils.getTables(
185                 filter.iterator(dataSet, false));
186         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
187         assertEquals("table count", expectedNames.length, actualTables.length);
188         assertEquals("table names",
189                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
190     }
191
192     public void testCaseInsensitiveIterator() throws Exception JavaDoc
193     {
194         ExcludeTableFilter filter = new ExcludeTableFilter();
195         filter.excludeTable(getExtraTableName());
196
197         String JavaDoc[] expectedNames = getExpectedLowerNames();
198         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
199         assertTrue("dataset names count",
200                 dataSet.getTableNames().length > expectedNames.length);
201
202         ITable[] actualTables = DataSetUtils.getTables(
203                 filter.iterator(dataSet, false));
204         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
205         assertEquals("table count", expectedNames.length, actualTables.length);
206         assertEquals("table names",
207                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
208     }
209
210     public void testReverseIterator() throws Exception JavaDoc
211     {
212         // Cannot test!
213
}
214
215     public void testIteratorAndTableNotInDecoratedDataSet() throws Exception JavaDoc
216     {
217         String JavaDoc[] expectedNames = getExpectedNames();
218         ExcludeTableFilter filter = new ExcludeTableFilter();
219         filter.excludeTable(getExtraTableName());
220         filter.excludeTable("UNKNOWN_TABLE");
221
222         IDataSet dataSet = createDataSet();
223         assertTrue("dataset names count",
224                 dataSet.getTableNames().length > expectedNames.length);
225
226         ITable[] actualTables = DataSetUtils.getTables(
227                 filter.iterator(dataSet, false));
228         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
229         assertEquals("table count", expectedNames.length, actualTables.length);
230         assertEquals("table names",
231                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
232     }
233
234     ////////////////////////////////////////////////////////////////////////////
235

236     public void testIsValidNameWithPatterns() throws Exception JavaDoc
237     {
238         String JavaDoc validName = MATCHING_NAME;
239
240         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
241         for (int i = 0; i < patterns.length; i++)
242         {
243             String JavaDoc pattern = patterns[i];
244             ExcludeTableFilter filter = new ExcludeTableFilter();
245             filter.excludeTable(pattern);
246             assertEquals(pattern, true, filter.accept(validName));
247         }
248     }
249
250     public void testIsValidNameInvalidWithPatterns() throws Exception JavaDoc
251     {
252         String JavaDoc validName = MATCHING_NAME;
253
254         String JavaDoc[] patterns = MATCHING_PATTERNS;
255         for (int i = 0; i < patterns.length; i++)
256         {
257             String JavaDoc pattern = patterns[i];
258             ExcludeTableFilter filter = new ExcludeTableFilter();
259             filter.excludeTable(pattern);
260             assertEquals(pattern, false, filter.accept(validName));
261         }
262     }
263
264     public void testGetTableNamesWithPatterns() throws Exception JavaDoc
265     {
266         String JavaDoc nonMatchingName = "toto titi tata";
267         String JavaDoc[] expectedNames = new String JavaDoc[] {nonMatchingName};
268         IDataSet dataSet = new DefaultDataSet(new ITable[] {
269             new DefaultTable(MATCHING_NAME),
270             new DefaultTable(nonMatchingName),
271         });
272         assertTrue("dataset names count",
273                 dataSet.getTableNames().length > expectedNames.length);
274
275         String JavaDoc[] patterns = MATCHING_PATTERNS;
276         for (int i = 0; i < patterns.length; i++)
277         {
278             String JavaDoc pattern = patterns[i];
279             ExcludeTableFilter filter = new ExcludeTableFilter();
280             filter.excludeTable(pattern);
281
282             // this pattern match everything, so ensure everything filtered
283
if (pattern.equals("*"))
284             {
285                 String JavaDoc[] actualNames = filter.getTableNames(dataSet);
286                 assertEquals("name count - " + pattern,
287                         0, actualNames.length);
288             }
289             else
290             {
291                 String JavaDoc[] actualNames = filter.getTableNames(dataSet);
292                 assertEquals("name count - " + pattern,
293                         expectedNames.length, actualNames.length);
294                 assertEquals("names - " + pattern,
295                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
296             }
297         }
298     }
299
300     public void testGetTableNamesWithNonMatchingPatterns() throws Exception JavaDoc
301     {
302         String JavaDoc[] expectedNames = new String JavaDoc[] {MATCHING_NAME};
303         IDataSet dataSet = new DefaultDataSet(new ITable[] {
304             new DefaultTable(MATCHING_NAME),
305         });
306
307         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
308         for (int i = 0; i < patterns.length; i++)
309         {
310             String JavaDoc pattern = patterns[i];
311             ExcludeTableFilter filter = new ExcludeTableFilter();
312             filter.excludeTable(pattern);
313
314             String JavaDoc[] actualNames = filter.getTableNames(dataSet);
315             assertEquals("name count - " + pattern,
316                     expectedNames.length, actualNames.length);
317             assertEquals("names - " + pattern,
318                     Arrays.asList(expectedNames), Arrays.asList(actualNames));
319         }
320     }
321
322     public void testGetTablesWithPatterns() throws Exception JavaDoc
323     {
324         String JavaDoc nonMatchingName = "toto titi tata";
325         String JavaDoc[] expectedNames = new String JavaDoc[] {nonMatchingName};
326         IDataSet dataSet = new DefaultDataSet(new ITable[] {
327             new DefaultTable(MATCHING_NAME),
328             new DefaultTable(nonMatchingName),
329         });
330         assertTrue("dataset names count",
331                 dataSet.getTableNames().length > expectedNames.length);
332
333         String JavaDoc[] patterns = MATCHING_PATTERNS;
334         for (int i = 0; i < patterns.length; i++)
335         {
336             String JavaDoc pattern = patterns[i];
337             ExcludeTableFilter filter = new ExcludeTableFilter();
338             filter.excludeTable(pattern);
339
340             // this pattern match everything, so ensure everything is filtered
341
if (pattern.equals("*"))
342             {
343                 ITable[] actualTables = DataSetUtils.getTables(
344                         filter.iterator(dataSet, false));
345                 String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
346                 assertEquals("table count - " + pattern,
347                         0, actualNames.length);
348             }
349             else
350             {
351                 ITable[] actualTables = DataSetUtils.getTables(
352                         filter.iterator(dataSet, false));
353                 String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
354                 assertEquals("table count - " + pattern,
355                         expectedNames.length, actualTables.length);
356                 assertEquals("table names - " + pattern,
357                         Arrays.asList(expectedNames), Arrays.asList(actualNames));
358             }
359         }
360     }
361
362     public void testGetTablesWithNonMatchingPatterns() throws Exception JavaDoc
363     {
364         String JavaDoc[] expectedNames = new String JavaDoc[] {MATCHING_NAME};
365         IDataSet dataSet = new DefaultDataSet(new ITable[] {
366             new DefaultTable(MATCHING_NAME),
367         });
368         assertTrue("dataset names count",
369                 dataSet.getTableNames().length > 0);
370
371         String JavaDoc[] patterns = NONMATCHING_PATTERNS;
372         for (int i = 0; i < patterns.length; i++)
373         {
374             String JavaDoc pattern = patterns[i];
375             ExcludeTableFilter filter = new ExcludeTableFilter();
376             filter.excludeTable(pattern);
377
378             ITable[] actualTables = DataSetUtils.getTables(
379                     filter.iterator(dataSet, false));
380             String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
381             assertEquals("table count - " + pattern,
382                     expectedNames.length, actualTables.length);
383             assertEquals("table names - " + pattern,
384                     Arrays.asList(expectedNames), Arrays.asList(actualNames));
385         }
386     }
387
388 }
389
Popular Tags