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.IDataSet;
30  import org.dbunit.dataset.ITable;
31  import org.dbunit.dataset.LowerCaseDataSet;
32  
33  /**
34   * @author Manuel Laflamme
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since 2.2.0
38   */
39  public class SequenceTableFilterTest extends AbstractTableFilterTest
40  {
41  
42      public SequenceTableFilterTest(String s)
43      {
44          super(s);
45      }
46  
47      public void testAccept() throws Exception
48      {
49          String[] validNames = getExpectedNames();
50          ITableFilter filter = new SequenceTableFilter(validNames);
51  
52          for (int i = 0; i < validNames.length; i++)
53          {
54              String validName = validNames[i];
55              assertEquals(validName, true, filter.accept(validName));
56          }
57      }
58  
59      public void testIsCaseInsensitiveValidName() throws Exception
60      {
61          String[] validNames = getExpectedNames();
62          ITableFilter filter = new SequenceTableFilter(validNames);
63  
64          for (int i = 0; i < validNames.length; i++)
65          {
66              String validName = validNames[i];
67              assertEquals(validName, true, filter.accept(validName));
68          }
69      }
70  
71      public void testIsValidNameAndInvalid() throws Exception
72      {
73          String[] invalidNames = new String[] {
74              "INVALID_TABLE",
75              "UNKNOWN_TABLE",
76          };
77          String[] validNames = getExpectedNames();
78          ITableFilter filter = new SequenceTableFilter(validNames);
79  
80          for (int i = 0; i < invalidNames.length; i++)
81          {
82              String invalidName = invalidNames[i];
83              assertEquals(invalidName, false, filter.accept(invalidName));
84          }
85      }
86  
87      public void testGetTableNames() throws Exception
88      {
89          String[] expectedNames = getExpectedNames();
90          ITableFilter filter = new SequenceTableFilter(expectedNames);
91  
92          IDataSet dataSet = createDataSet();
93          assertTrue("dataset names count",
94                  dataSet.getTableNames().length > expectedNames.length);
95  
96          String[] 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
103     {
104         String[] expectedNames = getExpectedNames();
105 
106         List filterNameList = new ArrayList(Arrays.asList(expectedNames));
107         filterNameList.add("UNKNOWN_TABLE");
108         String[] filterNames = (String[])filterNameList.toArray(new String[0]);
109         ITableFilter filter = new SequenceTableFilter(filterNames);
110 
111         IDataSet dataSet = createDataSet();
112         assertTrue("dataset names count",
113                 dataSet.getTableNames().length > expectedNames.length);
114 
115         String[] 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 testGetCaseInsensitiveTableNames() throws Exception
122     {
123         String[] filterNames = getExpectedNames();
124         ITableFilter filter = new SequenceTableFilter(filterNames);
125 
126         String[] expectedNames = getExpectedLowerNames();
127         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
128         assertTrue("dataset names count",
129                 dataSet.getTableNames().length > expectedNames.length);
130 
131         String[] actualNames = filter.getTableNames(dataSet);
132         assertEquals("name count", expectedNames.length, actualNames.length);
133         assertEquals("names",
134                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
135     }
136 
137     public void testGetReverseTableNames() throws Exception
138     {
139         String[] expectedNames = DataSetUtils.reverseStringArray(getExpectedNames());
140         ITableFilter filter = new SequenceTableFilter(expectedNames);
141 
142         IDataSet dataSet = createDataSet();
143         assertTrue("dataset names count",
144                 dataSet.getTableNames().length > expectedNames.length);
145 
146         String[] actualNames = filter.getTableNames(dataSet);
147         assertEquals("name count", expectedNames.length, actualNames.length);
148         assertEquals("names",
149                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
150     }
151 
152     public void testIterator() throws Exception
153     {
154         String[] expectedNames = getExpectedNames();
155         ITableFilter filter = new SequenceTableFilter(expectedNames);
156 
157         IDataSet dataSet = createDataSet();
158         assertTrue("dataset names count",
159                 dataSet.getTableNames().length > expectedNames.length);
160 
161         ITable[] actualTables = DataSetUtils.getTables(
162                 filter.iterator(dataSet, false));
163         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
164         assertEquals("table count", expectedNames.length, actualTables.length);
165         assertEquals("table names",
166                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
167     }
168 
169     public void testCaseInsensitiveIterator() throws Exception
170     {
171         ITableFilter filter = new SequenceTableFilter(getExpectedNames());
172         String[] lowerNames = getExpectedLowerNames();
173 
174         IDataSet dataSet = new LowerCaseDataSet(createDataSet());
175         assertTrue("dataset names count",
176                 dataSet.getTableNames().length > lowerNames.length);
177 
178         ITable[] actualTables = DataSetUtils.getTables(
179                 filter.iterator(dataSet, false));
180         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
181         assertEquals("table count", lowerNames.length, actualTables.length);
182         assertEquals("table names",
183                 Arrays.asList(lowerNames), Arrays.asList(actualNames));
184     }
185 
186     public void testReverseIterator() throws Exception
187     {
188         String[] filterNames = getExpectedNames();
189         String[] expectedNames = DataSetUtils.reverseStringArray(filterNames);
190         ITableFilter filter = new SequenceTableFilter(filterNames);
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, true));
198         String[] 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 testIteratorAndTableNotInDecoratedDataSet() throws Exception
205     {
206         String[] expectedNames = getExpectedNames();
207 
208         List filterNameList = new ArrayList(Arrays.asList(expectedNames));
209         filterNameList.add("UNKNOWN_TABLE");
210         String[] filterNames = (String[])filterNameList.toArray(new String[0]);
211         ITableFilter filter = new SequenceTableFilter(filterNames);
212 
213         IDataSet dataSet = createDataSet();
214         assertTrue("dataset names count",
215                 dataSet.getTableNames().length > expectedNames.length);
216 
217         ITable[] actualTables = DataSetUtils.getTables(
218                 filter.iterator(dataSet, false));
219         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
220         assertEquals("table count", expectedNames.length, actualTables.length);
221         assertEquals("table names",
222                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
223     }
224 
225     ////////////////////////////////////////////////////////////////////////////
226 
227     public void testIteratorWithDifferentSequence() throws Exception
228     {
229         String[] expectedNames = DataSetUtils.reverseStringArray(getExpectedNames());
230         ITableFilter filter = new SequenceTableFilter(expectedNames);
231 
232         IDataSet dataSet = createDataSet();
233         assertTrue("dataset names count",
234                 dataSet.getTableNames().length > expectedNames.length);
235 
236         ITable[] actualTables = DataSetUtils.getTables(
237                 filter.iterator(dataSet, false));
238         String[] actualNames = new DefaultDataSet(actualTables).getTableNames();
239         assertEquals("table count", expectedNames.length, actualTables.length);
240         assertEquals("table names",
241                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
242     }
243 
244 }