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