1 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 ; 28 import java.math.BigDecimal ; 29 import java.sql.Date ; 30 31 37 public class ReplacementTableTest extends AbstractTableTest 38 { 39 public ReplacementTableTest(String s) 40 { 41 super(s); 42 } 43 44 protected ITable createTable() throws Exception 45 { 46 return createDataSet().getTable("TEST_TABLE"); 47 } 48 49 private IDataSet createDataSet() throws Exception 50 { 51 return new ReplacementDataSet( 52 new FlatXmlDataSet(new File ("src/xml/flatXmlTableTest.xml"))); 53 } 54 55 public void testGetMissingValue() throws Exception 56 { 57 } 59 60 public void testObjectReplacement() throws Exception 61 { 62 String tableName = "TABLE_NAME"; 63 BigDecimal trueObject = new BigDecimal (1); 64 BigDecimal falseObject = new BigDecimal (0); 65 Date now = new Date (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 Object [] actualRow = new Object [] { 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 Object [] expectedRow = new Object [] { 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 118 { 119 String 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 Object [] actualRow = new Object [] { 134 "substring", 135 "substring_", 136 "_substring_", 137 "_substring", 138 "substringsubstring substring", 139 "this is a string", 140 new Long (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 Object [] expectedRow = new Object [] { 151 "replacement", 152 "replacement_", 153 "_replacement_", 154 "_replacement", 155 "replacementreplacement replacement", 156 "this is a string", 157 new Long (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 168 { 169 String 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_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 Object [] actualRow = new Object [] { 197 "${substring}", 198 "${substring}_", 199 "_${substring}_", 200 "_${substring}", 201 "${substring}${substring} ${substring}", 202 "this is a string", 203 new Long (0), 204 null, 205 "substring", 206 "substring_", 207 "_substring_", 208 "_substring", 209 "substringsubstring substring", 210 "_${substring_", 211 "_$substring}_", 212 "_substring}_", 213 "}", 214 "${", 215 "${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 Object [] expectedRow = new Object [] { 228 "replacement", 229 "replacement_", 230 "_replacement_", 231 "_replacement", 232 "replacementreplacement replacement", 233 "this is a string", 234 new Long (0), 235 null, 236 "substring", 237 "substring_", 238 "_substring_", 239 "_substring", 240 "substringsubstring substring", 241 "_${substring_", 242 "_$substring}_", 243 "_substring}_", 244 "}", 245 "${", 246 "${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 258 { 259 String 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_SUBSTRING5", DataType.CHAR), 279 new Column("BAD_SUBSTRING1", DataType.CHAR), 280 new Column("BAD_SUBSTRING2", DataType.CHAR), 281 }; 282 283 Object [] actualRow = new Object [] { 285 "!substring!", 286 "!substring!_", 287 "_!substring!_", 288 "_!substring!", 289 "!substring!!substring! !substring!", 290 "this is a string", 291 new Long (0), 292 null, 293 "substring", 294 "substring_", 295 "_substring_", 296 "_substring", 297 "substringsubstring substring", 298 "_!substring_", 299 "_substring!_", 300 "!", 301 "!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 Object [] expectedRow = new Object [] { 314 "replacement", 315 "replacement_", 316 "_replacement_", 317 "_replacement", 318 "replacementreplacement replacement", 319 "this is a string", 320 new Long (0), 321 null, 322 "substring", 323 "substring_", 324 "_substring_", 325 "_substring", 326 "substringsubstring substring", 327 "_!substring_", 328 "_substring!_", 329 "!", 330 "!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 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 e) 351 { 352 } 353 354 try 355 { 356 replacementTable.addReplacementSubstring("substring", null); 357 fail("Should not be here!"); 358 } 359 catch (NullPointerException e) 360 { 361 } 362 } 363 364 } 365 | Popular Tags |