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