KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

262     public void testIteratorWithDifferentSequence() throws Exception JavaDoc
263     {
264         String JavaDoc[] expectedNames = DataSetUtils.reverseStringArray(getExpectedNames());
265         ITableFilter filter = new SequenceTableFilter(expectedNames);
266
267         IDataSet dataSet = createDataSet();
268         assertTrue("dataset names count",
269                 dataSet.getTableNames().length > expectedNames.length);
270
271         ITable[] actualTables = DataSetUtils.getTables(
272                 filter.iterator(dataSet, false));
273         String JavaDoc[] actualNames = new DefaultDataSet(actualTables).getTableNames();
274         assertEquals("table count", expectedNames.length, actualTables.length);
275         assertEquals("table names",
276                 Arrays.asList(expectedNames), Arrays.asList(actualNames));
277     }
278
279 }
280
Popular Tags