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 41 46 public class UpdateOperationTest extends AbstractDatabaseTest 47 { 48 public UpdateOperationTest(String s) 49 { 50 super(s); 51 } 52 53 56 protected IDataSet getDataSet() throws Exception 57 { 58 IDataSet dataSet = super.getDataSet(); 59 60 DatabaseEnvironment environment = DatabaseEnvironment.getInstance(); 61 if (environment.support(TestFeature.BLOB)) 62 { 63 dataSet = new CompositeDataSet( 64 new FlatXmlDataSet(new File ("src/xml/blobInsertTest.xml")), 65 dataSet); 66 } 67 68 if (environment.support(TestFeature.CLOB)) 69 { 70 dataSet = new CompositeDataSet( 71 new FlatXmlDataSet(new File ("src/xml/clobInsertTest.xml")), 72 dataSet); 73 } 74 75 return dataSet; 76 } 77 78 81 public void testMockExecute() throws Exception 82 { 83 String schemaName = "schema"; 84 String tableName = "table"; 85 String [] expected = { 86 "update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'", 87 "update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'", 88 }; 89 90 Column[] columns = new Column[]{ 91 new Column("c1", DataType.VARCHAR), 92 new Column("c2", DataType.NUMERIC), 93 new Column("c3", DataType.VARCHAR), 94 new Column("c4", DataType.NUMERIC), 95 }; 96 String [] primaryKeys = {"c4", "c1"}; 97 DefaultTable table = new DefaultTable(new DefaultTableMetaData( 98 tableName, columns, primaryKeys)); 99 table.addRow(new Object []{"toto", "1234", "false", "0"}); 100 table.addRow(new Object []{"qwerty", new Double ("123.45"), null, "0"}); 101 IDataSet dataSet = new DefaultDataSet(table); 102 103 MockBatchStatement statement = new MockBatchStatement(); 105 statement.addExpectedBatchStrings(expected); 106 statement.setExpectedExecuteBatchCalls(1); 107 statement.setExpectedClearBatchCalls(1); 108 statement.setExpectedCloseCalls(1); 109 110 MockStatementFactory factory = new MockStatementFactory(); 111 factory.setExpectedCreatePreparedStatementCalls(1); 112 factory.setupStatement(statement); 113 114 MockDatabaseConnection connection = new MockDatabaseConnection(); 115 connection.setupDataSet(dataSet); 116 connection.setupSchema(schemaName); 117 connection.setupStatementFactory(factory); 118 connection.setExpectedCloseCalls(0); 119 120 new UpdateOperation().execute(connection, dataSet); 122 123 statement.verify(); 124 factory.verify(); 125 connection.verify(); 126 } 127 128 public void testExecuteWithEscapedName() throws Exception 129 { 130 String schemaName = "schema"; 131 String tableName = "table"; 132 String [] expected = { 133 "update [schema].[table] set [c2] = 1234, [c3] = 'false' where [c4] = 0 and [c1] = 'toto'", 134 "update [schema].[table] set [c2] = 123.45, [c3] = NULL where [c4] = 0 and [c1] = 'qwerty'", 135 }; 136 137 Column[] columns = new Column[]{ 138 new Column("c1", DataType.VARCHAR), 139 new Column("c2", DataType.NUMERIC), 140 new Column("c3", DataType.VARCHAR), 141 new Column("c4", DataType.NUMERIC), 142 }; 143 String [] primaryKeys = {"c4", "c1"}; 144 DefaultTable table = new DefaultTable(new DefaultTableMetaData( 145 tableName, columns, primaryKeys)); 146 table.addRow(new Object []{"toto", "1234", "false", "0"}); 147 table.addRow(new Object []{"qwerty", new Double ("123.45"), null, "0"}); 148 IDataSet dataSet = new DefaultDataSet(table); 149 150 MockBatchStatement statement = new MockBatchStatement(); 152 statement.addExpectedBatchStrings(expected); 153 statement.setExpectedExecuteBatchCalls(1); 154 statement.setExpectedClearBatchCalls(1); 155 statement.setExpectedCloseCalls(1); 156 157 MockStatementFactory factory = new MockStatementFactory(); 158 factory.setExpectedCreatePreparedStatementCalls(1); 159 factory.setupStatement(statement); 160 161 MockDatabaseConnection connection = new MockDatabaseConnection(); 162 connection.setupDataSet(dataSet); 163 connection.setupSchema(schemaName); 164 connection.setupStatementFactory(factory); 165 connection.setExpectedCloseCalls(0); 166 167 connection.getConfig().setProperty( 169 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "[?]"); 170 new UpdateOperation().execute(connection, dataSet); 171 172 statement.verify(); 173 factory.verify(); 174 connection.verify(); 175 } 176 177 public void testExecuteWithDuplicateTables() throws Exception 178 { 179 String schemaName = "schema"; 180 String tableName = "table"; 181 String [] expected = { 182 "update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'", 183 "update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'", 184 "update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'", 185 "update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'", 186 }; 187 188 Column[] columns = new Column[]{ 189 new Column("c1", DataType.VARCHAR), 190 new Column("c2", DataType.NUMERIC), 191 new Column("c3", DataType.VARCHAR), 192 new Column("c4", DataType.NUMERIC), 193 }; 194 String [] primaryKeys = {"c4", "c1"}; 195 DefaultTable table = new DefaultTable(new DefaultTableMetaData( 196 tableName, columns, primaryKeys)); 197 table.addRow(new Object []{"toto", "1234", "false", "0"}); 198 table.addRow(new Object []{"qwerty", new Double ("123.45"), null, "0"}); 199 IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table}); 200 201 MockBatchStatement statement = new MockBatchStatement(); 203 statement.addExpectedBatchStrings(expected); 204 statement.setExpectedExecuteBatchCalls(2); 205 statement.setExpectedClearBatchCalls(2); 206 statement.setExpectedCloseCalls(2); 207 208 MockStatementFactory factory = new MockStatementFactory(); 209 factory.setExpectedCreatePreparedStatementCalls(2); 210 factory.setupStatement(statement); 211 212 MockDatabaseConnection connection = new MockDatabaseConnection(); 213 connection.setupDataSet(new DefaultDataSet(table)); 214 connection.setupSchema(schemaName); 215 connection.setupStatementFactory(factory); 216 connection.setExpectedCloseCalls(0); 217 218 new UpdateOperation().execute(connection, dataSet); 220 221 statement.verify(); 222 factory.verify(); 223 connection.verify(); 224 } 225 226 public void testExecuteWithEmptyTable() throws Exception 227 { 228 Column[] columns = {new Column("c1", DataType.VARCHAR)}; 229 ITable table = new DefaultTable(new DefaultTableMetaData( 230 "name", columns, columns)); 231 IDataSet dataSet = new DefaultDataSet(table); 232 233 MockStatementFactory factory = new MockStatementFactory(); 235 factory.setExpectedCreatePreparedStatementCalls(0); 236 237 MockDatabaseConnection connection = new MockDatabaseConnection(); 238 connection.setupDataSet(dataSet); 239 connection.setupStatementFactory(factory); 240 connection.setExpectedCloseCalls(0); 241 242 new UpdateOperation().execute(connection, dataSet); 244 245 factory.verify(); 246 connection.verify(); 247 } 248 249 public void testUpdateClob() throws Exception 250 { 251 DatabaseEnvironment environment = DatabaseEnvironment.getInstance(); 253 if (environment.support(TestFeature.CLOB)) 254 { 255 String tableName = "CLOB_TABLE"; 256 257 { 258 IDataSet beforeDataSet = new FlatXmlDataSet( 259 new File ("src/xml/clobInsertTest.xml")); 260 261 ITable tableBefore = _connection.createDataSet().getTable(tableName); 262 assertEquals("count before", 3, _connection.getRowCount(tableName)); 263 Assertion.assertEquals(beforeDataSet.getTable(tableName), tableBefore); 264 } 265 266 IDataSet afterDataSet = new FlatXmlDataSet( 267 new File ("src/xml/clobUpdateTest.xml")); 268 DatabaseOperation.REFRESH.execute(_connection, afterDataSet); 269 270 { 271 ITable tableAfter = _connection.createDataSet().getTable(tableName); 272 assertEquals("count after", 4, tableAfter.getRowCount()); 273 Assertion.assertEquals(afterDataSet.getTable(tableName), tableAfter); 274 } 275 } 276 } 277 278 public void testUpdateBlob() throws Exception 279 { 280 DatabaseEnvironment environment = DatabaseEnvironment.getInstance(); 282 if (environment.support(TestFeature.BLOB)) 283 { 284 String tableName = "BLOB_TABLE"; 285 286 { 287 IDataSet beforeDataSet = new FlatXmlDataSet( 288 new File ("src/xml/blobInsertTest.xml")); 289 290 ITable tableBefore = _connection.createDataSet().getTable(tableName); 291 assertEquals("count before", 1, _connection.getRowCount(tableName)); 292 Assertion.assertEquals(beforeDataSet.getTable(tableName), tableBefore); 293 294 } 297 298 IDataSet afterDataSet = new FlatXmlDataSet( 299 new File ("src/xml/blobUpdateTest.xml")); 300 DatabaseOperation.REFRESH.execute(_connection, afterDataSet); 301 302 { 303 ITable tableAfter = _connection.createDataSet().getTable(tableName); 304 assertEquals("count after", 2, tableAfter.getRowCount()); 305 Assertion.assertEquals(afterDataSet.getTable(tableName), tableAfter); 306 307 } 310 } 311 } 312 313 public void testExecute() throws Exception 314 { 315 Reader in = new FileReader ( 316 new File ("src/xml/updateOperationTest.xml")); 317 IDataSet dataSet = new XmlDataSet(in); 318 319 testExecute(dataSet); 320 321 } 322 323 public void testExecuteCaseInsensitive() throws Exception 324 { 325 Reader in = new FileReader ( 326 new File ("src/xml/updateOperationTest.xml")); 327 IDataSet dataSet = new XmlDataSet(in); 328 329 testExecute(new LowerCaseDataSet(dataSet)); 330 } 331 332 public void testExecuteForwardOnly() throws Exception 333 { 334 Reader in = new FileReader ( 335 new File ("src/xml/updateOperationTest.xml")); 336 IDataSet dataSet = new XmlDataSet(in); 337 338 testExecute(new ForwardOnlyDataSet(dataSet)); 339 } 340 341 public void testExecuteAndNoPrimaryKeys() throws Exception 342 { 343 String tableName = "test_table"; 344 345 Reader reader = new FileReader ("src/xml/updateOperationNoPKTest.xml"); 346 IDataSet dataSet = new FlatXmlDataSet(reader); 347 348 assertEquals("row count before", 6, _connection.getRowCount(tableName)); 350 351 try 352 { 353 DatabaseOperation.REFRESH.execute(_connection, dataSet); 354 fail("Should not be here!"); 355 } 356 catch (NoPrimaryKeyException e) 357 { 358 359 } 360 361 assertEquals("row count before", 6, _connection.getRowCount(tableName)); 363 } 364 365 private void testExecute(IDataSet dataSet) throws Exception 366 { 367 String tableName = "PK_TABLE"; 368 String [] columnNames = {"PK0", "PK1", "PK2", "NORMAL0", "NORMAL1"}; 369 int modifiedRow = 1; 370 371 ITable tableBefore = createOrderedTable(tableName, columnNames[0]); 373 assertEquals("row count before", 3, tableBefore.getRowCount()); 374 375 DatabaseOperation.UPDATE.execute(_connection, dataSet); 376 377 ITable tableAfter = createOrderedTable(tableName, columnNames[0]); 378 assertEquals("row count after", 3, tableAfter.getRowCount()); 379 for (int i = 0; i < tableAfter.getRowCount(); i++) 380 { 381 if (i == modifiedRow) 383 { 384 assertEquals("PK0", "1", 385 tableAfter.getValue(i, "PK0").toString()); 386 assertEquals("PK1", "1", 387 tableAfter.getValue(i, "PK1").toString()); 388 assertEquals("PK2", "1", 389 tableAfter.getValue(i, "PK2").toString()); 390 assertEquals("NORMAL0", "toto", 391 tableAfter.getValue(i, "NORMAL0").toString()); 392 assertEquals("NORMAL1", "qwerty", 393 tableAfter.getValue(i, "NORMAL1").toString()); 394 } 395 else 397 { 398 for (int j = 0; j < columnNames.length; j++) 399 { 400 String name = columnNames[j]; 401 Object valueAfter = tableAfter.getValue(i, name); 402 Object valueBefore = tableBefore.getValue(i, name); 403 assertEquals("c=" + name + ",r=" + j, valueBefore, valueAfter); 404 } 405 } 406 } 407 } 408 409 410 } 411 412 413 414 415 416 417 418 419 | Popular Tags |