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;
22  
23  import java.math.BigDecimal;
24  import java.sql.Date;
25  
26  import junit.framework.Assert;
27  
28  import org.dbunit.Assertion;
29  import org.dbunit.dataset.datatype.DataType;
30  import org.dbunit.dataset.xml.FlatXmlDataSet;
31  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
32  import org.dbunit.testutil.TestUtils;
33  
34  /**
35   *
36   * @author Manuel Laflamme
37   * @since Mar 17, 2003
38   * @version $Revision$
39   */
40  public class ReplacementTableTest extends AbstractTableTest
41  {
42      public ReplacementTableTest(String s)
43      {
44          super(s);
45      }
46  
47      protected ITable createTable() throws Exception
48      {
49          return createDataSet().getTable("TEST_TABLE");
50      }
51  
52      private IDataSet createDataSet() throws Exception
53      {
54          FlatXmlDataSet fds = new FlatXmlDataSetBuilder().build(TestUtils.getFile("xml/flatXmlTableTest.xml"));
55          return new ReplacementDataSet(fds);
56      }
57  
58      public void testGetMissingValue() throws Exception
59      {
60          // TODO test something usefull
61      }
62  
63      public void testObjectReplacement() throws Exception
64      {
65          String tableName = "TABLE_NAME";
66          BigDecimal trueObject = new BigDecimal((double)1);
67          BigDecimal falseObject = new BigDecimal((double)0);
68          Date now = new Date(System.currentTimeMillis());
69  
70          Column[] columns = new Column[] {
71              new Column("BOOLEAN_TRUE", DataType.BOOLEAN),
72              new Column("BOOLEAN_FALSE", DataType.BOOLEAN),
73              new Column("STRING_TRUE", DataType.CHAR),
74              new Column("STRING_FALSE", DataType.CHAR),
75              new Column("STRING_VALUE", DataType.CHAR),
76              new Column("DATE_VALUE", DataType.DATE),
77              new Column("NULL_TO_STRING_VALUE", DataType.CHAR),
78              new Column("STRING_TO_NULL_VALUE", DataType.CHAR),
79          };
80  
81          // Setup actual table
82          Object[] actualRow = new Object[] {
83              Boolean.TRUE,
84              Boolean.FALSE,
85              Boolean.TRUE.toString(),
86              Boolean.FALSE.toString(),
87              "value",
88              "now",
89              null,
90              "null",
91          };
92  
93          DefaultTable originalTable = new DefaultTable(tableName, columns);
94          originalTable.addRow(actualRow);
95          ReplacementTable actualTable = new ReplacementTable(originalTable);
96          actualTable.addReplacementObject(Boolean.TRUE, trueObject);
97          actualTable.addReplacementObject(Boolean.FALSE, falseObject);
98          actualTable.addReplacementObject("now", now);
99          actualTable.addReplacementObject("null", null);
100         actualTable.addReplacementObject(null, "nullreplacement");
101 
102         // Setup expected table
103         Object[] expectedRow = new Object[] {
104             trueObject,
105             falseObject,
106             Boolean.TRUE.toString(),
107             Boolean.FALSE.toString(),
108             "value",
109             now,
110             "nullreplacement",
111             null,
112         };
113 
114         DefaultTable expectedTable = new DefaultTable(tableName, columns);
115         expectedTable.addRow(expectedRow);
116 
117         Assertion.assertEquals(expectedTable, actualTable);
118     }
119 
120     public void testSubstringReplacement() throws Exception
121     {
122         String tableName = "TABLE_NAME";
123 
124         Column[] columns = new Column[] {
125             new Column("ONLY_SUBSTRING", DataType.CHAR),
126             new Column("START_SUBSTRING", DataType.CHAR),
127             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
128             new Column("END_SUBSTRING", DataType.CHAR),
129             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
130             new Column("NO_SUBSTRING", DataType.CHAR),
131             new Column("NOT_A_STRING", DataType.NUMERIC),
132             new Column("NULL_VALUE", DataType.CHAR),
133         };
134 
135         // Setup actual table
136         Object[] actualRow = new Object[] {
137             "substring",
138             "substring_",
139             "_substring_",
140             "_substring",
141             "substringsubstring substring",
142             "this is a string",
143             new Long(0),
144             null,
145         };
146 
147         DefaultTable originalTable = new DefaultTable(tableName, columns);
148         originalTable.addRow(actualRow);
149         ReplacementTable actualTable = new ReplacementTable(originalTable);
150         actualTable.addReplacementSubstring("substring", "replacement");
151 
152         // Setup expected table
153         Object[] expectedRow = new Object[] {
154             "replacement",
155             "replacement_",
156             "_replacement_",
157             "_replacement",
158             "replacementreplacement replacement",
159             "this is a string",
160             new Long(0),
161             null,
162         };
163 
164         DefaultTable expectedTable = new DefaultTable(tableName, columns);
165         expectedTable.addRow(expectedRow);
166 
167         Assertion.assertEquals(expectedTable, actualTable);
168     }
169 
170     /**
171      * Tests that replacement will fail properly when strict replacement fails.
172      */
173     public void testStrictReplacement() throws Exception
174     {
175         String tableName = "TABLE_NAME";
176         String replacedColumnName = "REPLACED_COLUMN";
177         String notReplacedColumnName = "NOT_REPLACED_COLUMN";
178 
179         String replacedValue = "replacement";
180         String notReplacedValue = "badstring";
181         String notReplacedDelimitedValue = "${" + notReplacedValue + "}";
182         
183         Column[] columns = new Column[] {
184             new Column(replacedColumnName, DataType.CHAR),
185             new Column(notReplacedColumnName, DataType.CHAR),
186         };
187 
188         // Setup actual table
189         Object[] actualRow = new Object[] {
190             "${substring}",
191             notReplacedDelimitedValue,
192         };
193 
194         DefaultTable originalTable = new DefaultTable(tableName, columns);
195         originalTable.addRow(actualRow);
196         ReplacementTable actualTable = new ReplacementTable(originalTable);
197         actualTable.addReplacementSubstring("substring", replacedValue);
198         actualTable.setSubstringDelimiters("${", "}");
199 
200         // Setup expected table
201         Object[] expectedRow = new Object[] {
202             replacedValue,
203             notReplacedDelimitedValue,
204         };
205 
206         DefaultTable expectedTable = new DefaultTable(tableName, columns);
207         expectedTable.addRow(expectedRow);
208 
209         Assertion.assertEquals(expectedTable, actualTable);
210 
211         String foundReplaced = (String) actualTable.getValue(0, replacedColumnName);
212         Assert.assertEquals(replacedValue, foundReplaced);
213         
214         // we should get back the non-replaced value with the delimiters in it
215         String foundNotReplaced = (String) actualTable.getValue(0, notReplacedColumnName);
216         Assert.assertEquals(notReplacedDelimitedValue, foundNotReplaced);
217         
218         // prior to this, it was just testing that it hooks up properly.
219         // now try some tests with the strict replacement set
220         actualTable.setStrictReplacement(true);
221 
222         // this should still succeed
223         foundReplaced = (String) actualTable.getValue(0, replacedColumnName);
224         Assert.assertEquals(replacedValue, foundReplaced);
225 
226         // this should fail
227         boolean failed;
228         try
229         {
230             actualTable.getValue(0, notReplacedColumnName);
231             failed = false;
232         }
233         catch (DataSetException dse)
234         {
235             failed = true;
236         }
237         Assert.assertTrue("Expecting a DataSetException", failed);
238         
239         // try again after adding the badstring as a replacement
240         String replacedValue2 = "replacement2";
241         actualTable.addReplacementSubstring(notReplacedValue, replacedValue2);
242         foundReplaced = (String) actualTable.getValue(0, notReplacedColumnName);
243         Assert.assertEquals(replacedValue2, foundReplaced);
244     }
245     
246     public void testDelimitedSubstringReplacement() throws Exception
247     {
248         String tableName = "TABLE_NAME";
249 
250         Column[] columns = new Column[] {
251             new Column("ONLY_SUBSTRING", DataType.CHAR),
252             new Column("START_SUBSTRING", DataType.CHAR),
253             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
254             new Column("END_SUBSTRING", DataType.CHAR),
255             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
256             new Column("NO_SUBSTRING", DataType.CHAR),
257             new Column("NOT_A_STRING", DataType.NUMERIC),
258             new Column("NULL_VALUE", DataType.CHAR),
259             new Column("ONLY_NONDELIMITED_SUBSTRING", DataType.CHAR),
260             new Column("START_NONDELIMITED_SUBSTRING", DataType.CHAR),
261             new Column("MIDDLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
262             new Column("END_NONDELIMITED_SUBSTRING", DataType.CHAR),
263             new Column("MULTIPLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
264             new Column("BAD_DELIMITED_SUBSTRING1", DataType.CHAR),
265             new Column("BAD_DELIMITED_SUBSTRING2", DataType.CHAR),
266             new Column("BAD_DELIMITED_SUBSTRING3", DataType.CHAR),
267 //            new Column("BAD_DELIMITED_SUBSTRING4", DataType.CHAR),
268             new Column("BAD_DELIMITED_SUBSTRING5", DataType.CHAR),
269             new Column("BAD_DELIMITED_SUBSTRING6", DataType.CHAR),
270             new Column("BAD_SUBSTRING1", DataType.CHAR),
271             new Column("BAD_SUBSTRING2", DataType.CHAR),
272         };
273 
274         // Setup actual table
275         Object[] actualRow = new Object[] {
276             "${substring}",
277             "${substring}_",
278             "_${substring}_",
279             "_${substring}",
280             "${substring}${substring} ${substring}",
281             "this is a string",
282             new Long(0),
283             null,
284             "substring",
285             "substring_",
286             "_substring_",
287             "_substring",
288             "substringsubstring substring",
289             "_${substring_",
290             "_$substring}_",
291             "_substring}_",
292             "}",
293             "${",
294 //            "${substring${substring} ${substring}",  - Should we support this???
295             "${substringsubstring}${}${}${substring}${}_",
296             "${}",
297         };
298 
299         DefaultTable originalTable = new DefaultTable(tableName, columns);
300         originalTable.addRow(actualRow);
301         ReplacementTable actualTable = new ReplacementTable(originalTable);
302         actualTable.addReplacementSubstring("substring", "replacement");
303         actualTable.setSubstringDelimiters("${", "}");
304 
305         // Setup expected table
306         Object[] expectedRow = new Object[] {
307             "replacement",
308             "replacement_",
309             "_replacement_",
310             "_replacement",
311             "replacementreplacement replacement",
312             "this is a string",
313             new Long(0),
314             null,
315             "substring",
316             "substring_",
317             "_substring_",
318             "_substring",
319             "substringsubstring substring",
320             "_${substring_",
321             "_$substring}_",
322             "_substring}_",
323             "}",
324             "${",
325 //            "${substringreplacement replacement",
326             "${substringsubstring}${}${}replacement${}_",
327             "${}",
328         };
329 
330         DefaultTable expectedTable = new DefaultTable(tableName, columns);
331         expectedTable.addRow(expectedRow);
332 
333         Assertion.assertEquals(expectedTable, actualTable);
334     }
335 
336     public void testDelimitedSubstringReplacementWithIdenticalDelimiters() throws Exception
337     {
338         String tableName = "TABLE_NAME";
339 
340         Column[] columns = new Column[] {
341             new Column("ONLY_SUBSTRING", DataType.CHAR),
342             new Column("START_SUBSTRING", DataType.CHAR),
343             new Column("MIDDLE_SUBSTRING", DataType.CHAR),
344             new Column("END_SUBSTRING", DataType.CHAR),
345             new Column("MULTIPLE_SUBSTRING", DataType.CHAR),
346             new Column("NO_SUBSTRING", DataType.CHAR),
347             new Column("NOT_A_STRING", DataType.NUMERIC),
348             new Column("NULL_VALUE", DataType.CHAR),
349             new Column("ONLY_NONDELIMITED_SUBSTRING", DataType.CHAR),
350             new Column("START_NONDELIMITED_SUBSTRING", DataType.CHAR),
351             new Column("MIDDLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
352             new Column("END_NONDELIMITED_SUBSTRING", DataType.CHAR),
353             new Column("MULTIPLE_NONDELIMITED_SUBSTRING", DataType.CHAR),
354             new Column("BAD_DELIMITED_SUBSTRING1", DataType.CHAR),
355             new Column("BAD_DELIMITED_SUBSTRING2", DataType.CHAR),
356 //            new Column("BAD_DELIMITED_SUBSTRING4", DataType.CHAR),
357             new Column("BAD_DELIMITED_SUBSTRING5", DataType.CHAR),
358             new Column("BAD_SUBSTRING1", DataType.CHAR),
359             new Column("BAD_SUBSTRING2", DataType.CHAR),
360         };
361 
362         // Setup actual table
363         Object[] actualRow = new Object[] {
364             "!substring!",
365             "!substring!_",
366             "_!substring!_",
367             "_!substring!",
368             "!substring!!substring! !substring!",
369             "this is a string",
370             new Long(0),
371             null,
372             "substring",
373             "substring_",
374             "_substring_",
375             "_substring",
376             "substringsubstring substring",
377             "_!substring_",
378             "_substring!_",
379             "!",
380 //            "!substring!substring! !substring!",  - Should we support this???
381             "!substringsubstring!!!!!!substring!!!_",
382             "!!",
383         };
384 
385         DefaultTable originalTable = new DefaultTable(tableName, columns);
386         originalTable.addRow(actualRow);
387         ReplacementTable actualTable = new ReplacementTable(originalTable);
388         actualTable.addReplacementSubstring("substring", "replacement");
389         actualTable.setSubstringDelimiters("!", "!");
390 
391         // Setup expected table
392         Object[] expectedRow = new Object[] {
393             "replacement",
394             "replacement_",
395             "_replacement_",
396             "_replacement",
397             "replacementreplacement replacement",
398             "this is a string",
399             new Long(0),
400             null,
401             "substring",
402             "substring_",
403             "_substring_",
404             "_substring",
405             "substringsubstring substring",
406             "_!substring_",
407             "_substring!_",
408             "!",
409 //            "!substringreplacement replacement",
410             "!substringsubstring!!!!!replacement!!_",
411             "!!",
412         };
413 
414         DefaultTable expectedTable = new DefaultTable(tableName, columns);
415         expectedTable.addRow(expectedRow);
416 
417         Assertion.assertEquals(expectedTable, actualTable);
418     }
419 
420     public void testAddNullReplacementSubstring() throws Exception
421     {
422         ReplacementTable replacementTable =
423                 new ReplacementTable(new DefaultTable("TABLE"));
424         try
425         {
426             replacementTable.addReplacementSubstring(null, "replacement");
427             fail("Should not be here!");
428         }
429         catch (NullPointerException e)
430         {
431         }
432 
433         try
434         {
435             replacementTable.addReplacementSubstring("substring", null);
436             fail("Should not be here!");
437         }
438         catch (NullPointerException e)
439         {
440         }
441     }
442 
443 }