1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.Assertion; 26 import org.dbunit.DatabaseEnvironment; 27 import org.dbunit.TestFeature; 28 import org.dbunit.database.DatabaseConfig; 29 import org.dbunit.database.MockDatabaseConnection; 30 import org.dbunit.database.statement.MockBatchStatement; 31 import org.dbunit.database.statement.MockStatementFactory; 32 import org.dbunit.dataset.*; 33 import org.dbunit.dataset.datatype.DataType; 34 import org.dbunit.dataset.xml.FlatXmlDataSet; 35 import org.dbunit.dataset.xml.XmlDataSet; 36 37 import java.io.File ; 38 import java.io.FileReader ; 39 import java.io.Reader ; 40 import java.sql.SQLException ; 41 42 47 public class InsertOperationTest extends AbstractDatabaseTest 48 { 49 public InsertOperationTest(String s) 50 { 51 super(s); 52 } 53 54 public void testMockExecute() throws Exception 55 { 56 String schemaName = "schema"; 57 String tableName = "table"; 58 String [] expected = { 59 "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')", 60 "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')", 61 }; 62 63 Column[] columns = new Column[]{ 65 new Column("c1", DataType.VARCHAR), 66 new Column("c2", DataType.NUMERIC), 67 new Column("c3", DataType.BOOLEAN), 68 }; 69 DefaultTable table = new DefaultTable(tableName, columns); 70 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 71 table.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 72 IDataSet dataSet = new DefaultDataSet(table); 73 74 MockBatchStatement statement = new MockBatchStatement(); 76 statement.addExpectedBatchStrings(expected); 77 statement.setExpectedExecuteBatchCalls(1); 78 statement.setExpectedClearBatchCalls(1); 79 statement.setExpectedCloseCalls(1); 80 81 MockStatementFactory factory = new MockStatementFactory(); 82 factory.setExpectedCreatePreparedStatementCalls(1); 83 factory.setupStatement(statement); 84 85 MockDatabaseConnection connection = new MockDatabaseConnection(); 86 connection.setupDataSet(dataSet); 87 connection.setupSchema(schemaName); 88 connection.setupStatementFactory(factory); 89 connection.setExpectedCloseCalls(0); 90 91 new InsertOperation().execute(connection, dataSet); 93 94 statement.verify(); 95 factory.verify(); 96 connection.verify(); 97 } 98 99 public void testExecuteUnknownColumn() throws Exception 100 { 101 String tableName = "table"; 102 103 Column[] columns = new Column[]{ 105 new Column("column", DataType.VARCHAR), 106 new Column("unknown", DataType.VARCHAR), 107 }; 108 DefaultTable table = new DefaultTable(tableName, columns); 109 table.addRow(); 110 table.setValue(0, columns[0].getColumnName(), null); 111 table.setValue(0, columns[0].getColumnName(), "value"); 112 IDataSet insertDataset = new DefaultDataSet(table); 113 114 IDataSet databaseDataSet = new DefaultDataSet( 115 new DefaultTable(tableName, new Column[]{ 116 new Column("column", DataType.VARCHAR), 117 })); 118 119 MockBatchStatement statement = new MockBatchStatement(); 121 statement.setExpectedExecuteBatchCalls(0); 122 statement.setExpectedClearBatchCalls(0); 123 statement.setExpectedCloseCalls(0); 124 125 MockStatementFactory factory = new MockStatementFactory(); 126 factory.setExpectedCreatePreparedStatementCalls(0); 127 factory.setupStatement(statement); 128 129 MockDatabaseConnection connection = new MockDatabaseConnection(); 130 connection.setupDataSet(databaseDataSet); 131 connection.setupStatementFactory(factory); 132 connection.setExpectedCloseCalls(0); 133 134 try 136 { 137 new InsertOperation().execute(connection, insertDataset); 138 fail("Should not be here!"); 139 } 140 catch (NoSuchColumnException e) 141 { 142 143 } 144 145 statement.verify(); 146 factory.verify(); 147 connection.verify(); 148 } 149 150 public void testExecuteIgnoreNone() throws Exception 151 { 152 String schemaName = "schema"; 153 String tableName = "table"; 154 String [] expected = { 155 "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')", 156 "insert into schema.table (c2, c3) values (123.45, 'true')", 157 "insert into schema.table (c1, c2, c3) values ('qwerty1', 1, 'true')", 158 "insert into schema.table (c1, c2, c3) values ('qwerty2', 2, 'false')", 159 "insert into schema.table (c3) values ('false')", 160 }; 161 162 Column[] columns = new Column[]{ 164 new Column("c1", DataType.VARCHAR), 165 new Column("c2", DataType.NUMERIC), 166 new Column("c3", DataType.BOOLEAN), 167 }; 168 DefaultTable table = new DefaultTable(tableName, columns); 169 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 170 table.addRow(new Object []{ITable.NO_VALUE, new Double ("123.45"), "true"}); 171 table.addRow(new Object []{"qwerty1", "1", Boolean.TRUE}); 172 table.addRow(new Object []{"qwerty2", "2", Boolean.FALSE}); 173 table.addRow(new Object []{ITable.NO_VALUE, ITable.NO_VALUE, Boolean.FALSE}); 174 IDataSet dataSet = new DefaultDataSet(table); 175 176 MockBatchStatement statement = new MockBatchStatement(); 178 statement.addExpectedBatchStrings(expected); 179 statement.setExpectedExecuteBatchCalls(4); 180 statement.setExpectedClearBatchCalls(4); 181 statement.setExpectedCloseCalls(4); 182 183 MockStatementFactory factory = new MockStatementFactory(); 184 factory.setExpectedCreatePreparedStatementCalls(4); 185 factory.setupStatement(statement); 186 187 MockDatabaseConnection connection = new MockDatabaseConnection(); 188 connection.setupDataSet(dataSet); 189 connection.setupSchema(schemaName); 190 connection.setupStatementFactory(factory); 191 connection.setExpectedCloseCalls(0); 192 193 new InsertOperation().execute(connection, dataSet); 195 196 statement.verify(); 197 factory.verify(); 198 connection.verify(); 199 } 200 201 255 public void testExecuteWithEscapedNames() throws Exception 256 { 257 String schemaName = "schema"; 258 String tableName = "table"; 259 String [] expected = { 260 "insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('toto', 1234, 'false')", 261 "insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('qwerty', 123.45, 'true')", 262 }; 263 264 Column[] columns = new Column[]{ 266 new Column("c1", DataType.VARCHAR), 267 new Column("c2", DataType.NUMERIC), 268 new Column("c3", DataType.BOOLEAN), 269 }; 270 DefaultTable table = new DefaultTable(tableName, columns); 271 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 272 table.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 273 IDataSet dataSet = new DefaultDataSet(table); 274 275 MockBatchStatement statement = new MockBatchStatement(); 277 statement.addExpectedBatchStrings(expected); 278 statement.setExpectedExecuteBatchCalls(1); 279 statement.setExpectedClearBatchCalls(1); 280 statement.setExpectedCloseCalls(1); 281 282 MockStatementFactory factory = new MockStatementFactory(); 283 factory.setExpectedCreatePreparedStatementCalls(1); 284 factory.setupStatement(statement); 285 286 MockDatabaseConnection connection = new MockDatabaseConnection(); 287 connection.setupDataSet(dataSet); 288 connection.setupSchema(schemaName); 289 connection.setupStatementFactory(factory); 290 connection.setExpectedCloseCalls(0); 291 292 connection.getConfig().setProperty( 294 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "'?'"); 295 new InsertOperation().execute(connection, dataSet); 296 297 statement.verify(); 298 factory.verify(); 299 connection.verify(); 300 } 301 302 public void testExecuteWithDuplicateTables() throws Exception 303 { 304 String schemaName = "schema"; 305 String tableName = "table"; 306 String [] expected = { 307 "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')", 308 "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')", 309 "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')", 310 "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')", 311 }; 312 313 Column[] columns = new Column[]{ 315 new Column("c1", DataType.VARCHAR), 316 new Column("c2", DataType.NUMERIC), 317 new Column("c3", DataType.BOOLEAN), 318 }; 319 DefaultTable table = new DefaultTable(tableName, columns); 320 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 321 table.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 322 IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table}); 323 324 MockBatchStatement statement = new MockBatchStatement(); 326 statement.addExpectedBatchStrings(expected); 327 statement.setExpectedExecuteBatchCalls(2); 328 statement.setExpectedClearBatchCalls(2); 329 statement.setExpectedCloseCalls(2); 330 331 MockStatementFactory factory = new MockStatementFactory(); 332 factory.setExpectedCreatePreparedStatementCalls(2); 333 factory.setupStatement(statement); 334 335 MockDatabaseConnection connection = new MockDatabaseConnection(); 336 connection.setupDataSet(new DefaultDataSet(table)); 337 connection.setupSchema(schemaName); 338 connection.setupStatementFactory(factory); 339 connection.setExpectedCloseCalls(0); 340 341 new InsertOperation().execute(connection, dataSet); 343 344 statement.verify(); 345 factory.verify(); 346 connection.verify(); 347 } 348 349 public void testExecuteWithEmptyTable() throws Exception 350 { 351 Column[] columns = {new Column("c1", DataType.VARCHAR)}; 352 ITable table = new DefaultTable(new DefaultTableMetaData( 353 "name", columns, columns)); 354 IDataSet dataSet = new DefaultDataSet(table); 355 356 MockStatementFactory factory = new MockStatementFactory(); 358 factory.setExpectedCreatePreparedStatementCalls(0); 359 360 MockDatabaseConnection connection = new MockDatabaseConnection(); 361 connection.setupDataSet(dataSet); 362 connection.setupStatementFactory(factory); 363 connection.setExpectedCloseCalls(0); 364 365 new InsertOperation().execute(connection, dataSet); 367 368 factory.verify(); 369 connection.verify(); 370 } 371 372 public void testInsertClob() throws Exception 373 { 374 DatabaseEnvironment environment = DatabaseEnvironment.getInstance(); 376 if (environment.support(TestFeature.CLOB)) 377 { 378 String tableName = "CLOB_TABLE"; 379 380 Reader in = new FileReader (new File ("src/xml/clobInsertTest.xml")); 381 IDataSet xmlDataSet = new FlatXmlDataSet(in); 382 383 assertEquals("count before", 0, _connection.getRowCount(tableName)); 384 385 DatabaseOperation.INSERT.execute(_connection, xmlDataSet); 386 387 ITable tableAfter = _connection.createDataSet().getTable(tableName); 388 assertEquals("count after", 3, tableAfter.getRowCount()); 389 Assertion.assertEquals(xmlDataSet.getTable(tableName), tableAfter); 390 } 391 } 392 393 public void testInsertBlob() throws Exception 394 { 395 DatabaseEnvironment environment = DatabaseEnvironment.getInstance(); 397 if (environment.support(TestFeature.BLOB)) 398 { 399 String tableName = "BLOB_TABLE"; 400 401 Reader in = new FileReader (new File ("src/xml/blobInsertTest.xml")); 402 IDataSet xmlDataSet = new FlatXmlDataSet(in); 403 404 assertEquals("count before", 0, _connection.getRowCount(tableName)); 405 406 DatabaseOperation.INSERT.execute(_connection, xmlDataSet); 407 408 ITable tableAfter = _connection.createDataSet().getTable(tableName); 409 assertEquals("count after", 1, tableAfter.getRowCount()); 410 Assertion.assertEquals(xmlDataSet.getTable(tableName), tableAfter); 411 } 412 } 413 414 public void testMissingColumns() throws Exception 415 { 416 Reader in = new FileReader ("src/xml/missingColumnTest.xml"); 417 IDataSet xmlDataSet = new XmlDataSet(in); 418 419 ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet()); 420 DatabaseOperation.INSERT.execute(_connection, xmlDataSet); 421 ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet()); 422 423 for (int i = 0; i < tablesBefore.length; i++) 425 { 426 ITable table = tablesBefore[i]; 427 String tableName = table.getTableMetaData().getTableName(); 428 if (tableName.startsWith("EMPTY")) 429 { 430 assertEquals(tableName + " before", 0, table.getRowCount()); 431 } 432 } 433 434 for (int i = 0; i < tablesAfter.length; i++) 436 { 437 ITable databaseTable = tablesAfter[i]; 438 String tableName = databaseTable.getTableMetaData().getTableName(); 439 440 if (tableName.startsWith("EMPTY")) 441 { 442 Column[] columns = databaseTable.getTableMetaData().getColumns(); 443 ITable xmlTable = xmlDataSet.getTable(tableName); 444 445 assertEquals("row count", xmlTable.getRowCount(), 447 databaseTable.getRowCount()); 448 449 for (int j = 0; j < databaseTable.getRowCount(); j++) 451 { 452 Object expected = xmlTable.getValue(j, columns[0].getColumnName()); 454 Object actual = databaseTable.getValue(j, columns[0].getColumnName()); 455 456 assertEquals(tableName + "." + columns[0].getColumnName(), 457 expected, actual); 458 459 for (int k = 1; k < columns.length; k++) 461 { 462 String columnName = columns[k].getColumnName(); 463 assertEquals(tableName + "." + columnName, 464 null, databaseTable.getValue(j, columnName)); 465 } 466 } 467 } 468 } 469 470 } 471 472 public void testExecute() throws Exception 473 { 474 Reader in = new FileReader ("src/xml/insertOperationTest.xml"); 475 IDataSet dataSet = new XmlDataSet(in); 476 477 testExecute(dataSet); 478 } 479 480 public void testExecuteCaseInsensitive() throws Exception 481 { 482 Reader in = new FileReader ("src/xml/insertOperationTest.xml"); 483 IDataSet dataSet = new XmlDataSet(in); 484 485 testExecute(new LowerCaseDataSet(dataSet)); 486 } 487 488 public void testExecuteForwardOnly() throws Exception 489 { 490 Reader in = new FileReader ("src/xml/insertOperationTest.xml"); 491 IDataSet dataSet = new XmlDataSet(in); 492 493 testExecute(new ForwardOnlyDataSet(dataSet)); 494 } 495 496 private void testExecute(IDataSet dataSet) throws Exception , SQLException 497 { 498 ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet()); 499 DatabaseOperation.INSERT.execute(_connection, dataSet); 500 ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet()); 501 502 assertEquals("table count", tablesBefore.length, tablesAfter.length); 503 for (int i = 0; i < tablesBefore.length; i++) 504 { 505 ITable table = tablesBefore[i]; 506 String name = table.getTableMetaData().getTableName(); 507 508 509 if (name.startsWith("EMPTY")) 510 { 511 assertEquals(name + "before", 0, table.getRowCount()); 512 } 513 } 514 515 for (int i = 0; i < tablesAfter.length; i++) 516 { 517 ITable table = tablesAfter[i]; 518 String name = table.getTableMetaData().getTableName(); 519 520 if (name.startsWith("EMPTY")) 521 { 522 if (dataSet instanceof ForwardOnlyDataSet) 523 { 524 assertTrue(name, table.getRowCount() > 0); 525 } 526 else 527 { 528 SortedTable expectedTable = new SortedTable( 529 dataSet.getTable(name), table.getTableMetaData()); 530 SortedTable actualTable = new SortedTable(table); 531 Assertion.assertEquals(expectedTable, actualTable); 532 } 533 } 534 } 535 } 536 } 537 538 539 540 541 542 543 544 545 546 547 | Popular Tags |