KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > ReplacementTableTest


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;
22
23 import org.dbunit.Assertion;
24 import org.dbunit.dataset.datatype.DataType;
25 import org.dbunit.dataset.xml.FlatXmlDataSet;
26
27 import java.io.File JavaDoc;
28 import java.math.BigDecimal JavaDoc;
29 import java.sql.Date JavaDoc;
30
31 /**
32  *
33  * @author Manuel Laflamme
34  * @since Mar 17, 2003
35  * @version $Revision: 1.7 $
36  */

37 public class ReplacementTableTest extends AbstractTableTest
38 {
39     public ReplacementTableTest(String JavaDoc s)
40     {
41         super(s);
42     }
43
44     protected ITable createTable() throws Exception JavaDoc
45     {
46         return createDataSet().getTable("TEST_TABLE");
47     }
48
49     private IDataSet createDataSet() throws Exception JavaDoc
50     {
51         return new ReplacementDataSet(
52                 new FlatXmlDataSet(new File JavaDoc("src/xml/flatXmlTableTest.xml")));
53     }
54
55     public void testGetMissingValue() throws Exception JavaDoc
56     {
57         // TODO test something usefull
58
}
59
60     public void testObjectReplacement() throws Exception JavaDoc
61     {
62         String JavaDoc tableName = "TABLE_NAME";
63         BigDecimal JavaDoc trueObject = new BigDecimal JavaDoc(1);
64         BigDecimal JavaDoc falseObject = new BigDecimal JavaDoc(0);
65         Date JavaDoc now = new Date JavaDoc(System.currentTimeMillis());
66
67         Column[] columns = new Column[] {
68             new Column("BOOLEAN_TRUE", DataType.BOOLEAN),
69             new Column("BOOLEAN_FALSE", DataType.BOOLEAN),
70             new Column("STRING_TRUE", DataType.CHAR),
71             new Column("STRING_FALSE", DataType.CHAR),
72             new Column("STRING_VALUE", DataType.CHAR),
73             new Column("DATE_VALUE", DataType.DATE),
74             new Column("NULL_TO_STRING_VALUE", DataType.CHAR),
75             new Column("STRING_TO_NULL_VALUE", DataType.CHAR),
76         };
77
78         // Setup actual table
79
Object JavaDoc[] actualRow = new Object JavaDoc[] {
80             Boolean.TRUE,
81             Boolean.FALSE,
82             Boolean.TRUE.toString(),
83             Boolean.FALSE.toString(),
84             "value",
85             "now",
86             null,
87             "null",
88         };
89
90         DefaultTable originalTable = new DefaultTable(tableName, columns);
91         originalTable.addRow(actualRow);
92         ReplacementTable actualTable = new ReplacementTable(originalTable);
93         actualTable.addReplacementObject(Boolean.TRUE, trueObject);
94         actualTable.addReplacementObject(Boolean.FALSE, falseObject);
95         actualTable.addReplacementObject("now", now);
96         actualTable.addReplacementObject("null", null);
97         actualTable.addReplacementObject(null, "nullreplacement");
98
99         // Setup expected table
100
Object JavaDoc[] expectedRow = new Object JavaDoc[] {
101             trueObject,
102             falseObject,
103             Boolean.TRUE.toString(),
104             Boolean.FALSE.toString(),
105             "value",
106             now,
107             "nullreplacement",
108             null,
109         };
110
111         DefaultTable expectedTable = new DefaultTable(tableName, columns);
112         expectedTable.addRow(expectedRow);
113
114         Assertion.assertEquals(expectedTable, actualTable);
115     }
116
117     public void testSubstringReplacement() throws Exception JavaDoc
118     {
119         String JavaDoc tableName = "TABLE_NAME";
120
121         Column[] columns = new Column[] {
122             new Column("ONLY_SUBSTRING", DataType.CHAR),
123             new Column("START_SUBSTRING", DataType.CHAR),
124             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
125             new Column("END_SUBSTRING", DataType.CHAR),
126             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
127             new Column("NO_SUBSTRING", DataType.CHAR),
128             new Column("NOT_A_STRING", DataType.NUMERIC),
129             new Column("NULL_VALUE", DataType.CHAR),
130         };
131
132         // Setup actual table
133
Object JavaDoc[] actualRow = new Object JavaDoc[] {
134             "substring",
135             "substring_",
136             "_substring_",
137             "_substring",
138             "substringsubstring substring",
139             "this is a string",
140             new Long JavaDoc(0),
141             null,
142         };
143
144         DefaultTable originalTable = new DefaultTable(tableName, columns);
145         originalTable.addRow(actualRow);
146         ReplacementTable actualTable = new ReplacementTable(originalTable);
147         actualTable.addReplacementSubstring("substring", "replacement");
148
149         // Setup expected table
150
Object JavaDoc[] expectedRow = new Object JavaDoc[] {
151             "replacement",
152             "replacement_",
153             "_replacement_",
154             "_replacement",
155             "replacementreplacement replacement",
156             "this is a string",
157             new Long JavaDoc(0),
158             null,
159         };
160
161         DefaultTable expectedTable = new DefaultTable(tableName, columns);
162         expectedTable.addRow(expectedRow);
163
164         Assertion.assertEquals(expectedTable, actualTable);
165     }
166
167     public void testDelimitedSubstringReplacement() throws Exception JavaDoc
168     {
169         String JavaDoc tableName = "TABLE_NAME";
170
171         Column[] columns = new Column[] {
172             new Column("ONLY_SUBSTRING", DataType.CHAR),
173             new Column("START_SUBSTRING", DataType.CHAR),
174             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
175             new Column("END_SUBSTRING", DataType.CHAR),
176             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
177             new Column("NO_SUBSTRING", DataType.CHAR),
178             new Column("NOT_A_STRING", DataType.NUMERIC),
179             new Column("NULL_VALUE", DataType.CHAR),
180             new Column("ONLY_NONDELIMITED_SUBSTRING", DataType.CHAR),
181             new Column("START_NONDELIMITED_SUBSTRING", DataType.CHAR),
182             new Column("MIDDLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
183             new Column("END_NONDELIMITED_SUBSTRING", DataType.CHAR),
184             new Column("MULTIPLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
185             new Column("BAD_DELIMITED_SUBSTRING1", DataType.CHAR),
186             new Column("BAD_DELIMITED_SUBSTRING2", DataType.CHAR),
187             new Column("BAD_DELIMITED_SUBSTRING3", DataType.CHAR),
188 // new Column("BAD_DELIMITED_SUBSTRING4", DataType.CHAR),
189
new Column("BAD_DELIMITED_SUBSTRING5", DataType.CHAR),
190             new Column("BAD_DELIMITED_SUBSTRING6", DataType.CHAR),
191             new Column("BAD_SUBSTRING1", DataType.CHAR),
192             new Column("BAD_SUBSTRING2", DataType.CHAR),
193         };
194
195         // Setup actual table
196
Object JavaDoc[] actualRow = new Object JavaDoc[] {
197             "${substring}",
198             "${substring}_",
199             "_${substring}_",
200             "_${substring}",
201             "${substring}${substring} ${substring}",
202             "this is a string",
203             new Long JavaDoc(0),
204             null,
205             "substring",
206             "substring_",
207             "_substring_",
208             "_substring",
209             "substringsubstring substring",
210             "_${substring_",
211             "_$substring}_",
212             "_substring}_",
213             "}",
214             "${",
215 // "${substring${substring} ${substring}", - Should we support this???
216
"${substringsubstring}${}${}${substring}${}_",
217             "${}",
218         };
219
220         DefaultTable originalTable = new DefaultTable(tableName, columns);
221         originalTable.addRow(actualRow);
222         ReplacementTable actualTable = new ReplacementTable(originalTable);
223         actualTable.addReplacementSubstring("substring", "replacement");
224         actualTable.setSubstringDelimiters("${", "}");
225
226         // Setup expected table
227
Object JavaDoc[] expectedRow = new Object JavaDoc[] {
228             "replacement",
229             "replacement_",
230             "_replacement_",
231             "_replacement",
232             "replacementreplacement replacement",
233             "this is a string",
234             new Long JavaDoc(0),
235             null,
236             "substring",
237             "substring_",
238             "_substring_",
239             "_substring",
240             "substringsubstring substring",
241             "_${substring_",
242             "_$substring}_",
243             "_substring}_",
244             "}",
245             "${",
246 // "${substringreplacement replacement",
247
"${substringsubstring}${}${}replacement${}_",
248             "${}",
249         };
250
251         DefaultTable expectedTable = new DefaultTable(tableName, columns);
252         expectedTable.addRow(expectedRow);
253
254         Assertion.assertEquals(expectedTable, actualTable);
255     }
256
257     public void testDelimitedSubstringReplacementWithIdenticalDelimiters() throws Exception JavaDoc
258     {
259         String JavaDoc tableName = "TABLE_NAME";
260
261         Column[] columns = new Column[] {
262             new Column("ONLY_SUBSTRING", DataType.CHAR),
263             new Column("START_SUBSTRING", DataType.CHAR),
264             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
265             new Column("END_SUBSTRING", DataType.CHAR),
266             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
267             new Column("NO_SUBSTRING", DataType.CHAR),
268             new Column("NOT_A_STRING", DataType.NUMERIC),
269             new Column("NULL_VALUE", DataType.CHAR),
270             new Column("ONLY_NONDELIMITED_SUBSTRING", DataType.CHAR),
271             new Column("START_NONDELIMITED_SUBSTRING", DataType.CHAR),
272             new Column("MIDDLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
273             new Column("END_NONDELIMITED_SUBSTRING", DataType.CHAR),
274             new Column("MULTIPLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
275             new Column("BAD_DELIMITED_SUBSTRING1", DataType.CHAR),
276             new Column("BAD_DELIMITED_SUBSTRING2", DataType.CHAR),
277 // new Column("BAD_DELIMITED_SUBSTRING4", DataType.CHAR),
278
new Column("BAD_DELIMITED_SUBSTRING5", DataType.CHAR),
279             new Column("BAD_SUBSTRING1", DataType.CHAR),
280             new Column("BAD_SUBSTRING2", DataType.CHAR),
281         };
282
283         // Setup actual table
284
Object JavaDoc[] actualRow = new Object JavaDoc[] {
285             "!substring!",
286             "!substring!_",
287             "_!substring!_",
288             "_!substring!",
289             "!substring!!substring! !substring!",
290             "this is a string",
291             new Long JavaDoc(0),
292             null,
293             "substring",
294             "substring_",
295             "_substring_",
296             "_substring",
297             "substringsubstring substring",
298             "_!substring_",
299             "_substring!_",
300             "!",
301 // "!substring!substring! !substring!", - Should we support this???
302
"!substringsubstring!!!!!!substring!!!_",
303             "!!",
304         };
305
306         DefaultTable originalTable = new DefaultTable(tableName, columns);
307         originalTable.addRow(actualRow);
308         ReplacementTable actualTable = new ReplacementTable(originalTable);
309         actualTable.addReplacementSubstring("substring", "replacement");
310         actualTable.setSubstringDelimiters("!", "!");
311
312         // Setup expected table
313
Object JavaDoc[] expectedRow = new Object JavaDoc[] {
314             "replacement",
315             "replacement_",
316             "_replacement_",
317             "_replacement",
318             "replacementreplacement replacement",
319             "this is a string",
320             new Long JavaDoc(0),
321             null,
322             "substring",
323             "substring_",
324             "_substring_",
325             "_substring",
326             "substringsubstring substring",
327             "_!substring_",
328             "_substring!_",
329             "!",
330 // "!substringreplacement replacement",
331
"!substringsubstring!!!!!replacement!!_",
332             "!!",
333         };
334
335         DefaultTable expectedTable = new DefaultTable(tableName, columns);
336         expectedTable.addRow(expectedRow);
337
338         Assertion.assertEquals(expectedTable, actualTable);
339     }
340
341     public void testAddNullReplacementSubstring() throws Exception JavaDoc
342     {
343         ReplacementTable replacementTable =
344                 new ReplacementTable(new DefaultTable("TABLE"));
345         try
346         {
347             replacementTable.addReplacementSubstring(null, "replacement");
348             fail("Should not be here!");
349         }
350         catch (NullPointerException JavaDoc e)
351         {
352         }
353
354         try
355         {
356             replacementTable.addReplacementSubstring("substring", null);
357             fail("Should not be here!");
358         }
359         catch (NullPointerException JavaDoc e)
360         {
361         }
362     }
363
364 }
365
Popular Tags