1 21 22 package org.dbunit.operation; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.database.DatabaseConfig; 26 import org.dbunit.database.MockDatabaseConnection; 27 import org.dbunit.database.statement.MockBatchStatement; 28 import org.dbunit.database.statement.MockStatementFactory; 29 import org.dbunit.dataset.Column; 30 import org.dbunit.dataset.DefaultDataSet; 31 import org.dbunit.dataset.DefaultTable; 32 import org.dbunit.dataset.DefaultTableMetaData; 33 import org.dbunit.dataset.IDataSet; 34 import org.dbunit.dataset.ITable; 35 import org.dbunit.dataset.ITableMetaData; 36 import org.dbunit.dataset.LowerCaseDataSet; 37 import org.dbunit.dataset.NoPrimaryKeyException; 38 import org.dbunit.dataset.datatype.DataType; 39 import org.dbunit.dataset.xml.XmlDataSet; 40 41 import java.io.File ; 42 import java.io.FileReader ; 43 import java.io.Reader ; 44 45 50 public class DeleteOperationTest extends AbstractDatabaseTest 51 { 52 public DeleteOperationTest(String s) 53 { 54 super(s); 55 } 56 57 public void testMockExecute() throws Exception 58 { 59 String schemaName = "schema"; 60 String tableName1 = "table1"; 61 String tableName2 = "table2"; 62 String [] expected = { 63 "delete from schema.table2 where c2 = 1234 and c1 = 'toto'", 64 "delete from schema.table2 where c2 = 123.45 and c1 = 'qwerty'", 65 "delete from schema.table1 where c2 = 1234 and c1 = 'toto'", 66 "delete from schema.table1 where c2 = 123.45 and c1 = 'qwerty'", 67 }; 68 69 Column[] columns = new Column[]{ 70 new Column("c1", DataType.VARCHAR), 71 new Column("c2", DataType.NUMERIC), 72 new Column("c3", DataType.BOOLEAN), 73 }; 74 String [] primaryKeys = {"c2", "c1"}; 75 76 DefaultTable table1 = new DefaultTable(new DefaultTableMetaData( 77 tableName1, columns, primaryKeys)); 78 table1.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 79 table1.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 80 DefaultTable table2 = new DefaultTable(new DefaultTableMetaData( 81 tableName2, columns, primaryKeys)); 82 table2.addTableRows(table1); 83 IDataSet dataSet = new DefaultDataSet(table1, table2); 84 85 MockBatchStatement statement = new MockBatchStatement(); 87 statement.addExpectedBatchStrings(expected); 88 statement.setExpectedExecuteBatchCalls(2); 89 statement.setExpectedClearBatchCalls(2); 90 statement.setExpectedCloseCalls(2); 91 92 MockStatementFactory factory = new MockStatementFactory(); 93 factory.setExpectedCreatePreparedStatementCalls(2); 94 factory.setupStatement(statement); 95 96 MockDatabaseConnection connection = new MockDatabaseConnection(); 97 connection.setupDataSet(dataSet); 98 connection.setupSchema(schemaName); 99 connection.setupStatementFactory(factory); 100 connection.setExpectedCloseCalls(0); 101 102 new DeleteOperation().execute(connection, dataSet); 104 105 statement.verify(); 106 factory.verify(); 107 connection.verify(); 108 } 109 110 public void testExecuteWithEscapedNames() throws Exception 111 { 112 String schemaName = "schema"; 113 String tableName = "table"; 114 String [] expected = { 115 "delete from [schema].[table] where [c2] = 123.45 and [c1] = 'qwerty'", 116 "delete from [schema].[table] where [c2] = 1234 and [c1] = 'toto'", 117 }; 118 119 Column[] columns = new Column[]{ 120 new Column("c1", DataType.VARCHAR), 121 new Column("c2", DataType.NUMERIC), 122 new Column("c3", DataType.BOOLEAN), 123 }; 124 String [] primaryKeys = {"c2", "c1"}; 125 126 DefaultTable table = new DefaultTable(new DefaultTableMetaData( 127 tableName, columns, primaryKeys)); 128 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 129 table.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 130 IDataSet dataSet = new DefaultDataSet(table); 131 132 MockBatchStatement statement = new MockBatchStatement(); 134 statement.addExpectedBatchStrings(expected); 135 statement.setExpectedExecuteBatchCalls(1); 136 statement.setExpectedClearBatchCalls(1); 137 statement.setExpectedCloseCalls(1); 138 139 MockStatementFactory factory = new MockStatementFactory(); 140 factory.setExpectedCreatePreparedStatementCalls(1); 141 factory.setupStatement(statement); 142 143 MockDatabaseConnection connection = new MockDatabaseConnection(); 144 connection.setupDataSet(dataSet); 145 connection.setupSchema(schemaName); 146 connection.setupStatementFactory(factory); 147 connection.setExpectedCloseCalls(0); 148 149 connection.getConfig().setProperty( 151 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "[?]"); 152 new DeleteOperation().execute(connection, dataSet); 153 154 statement.verify(); 155 factory.verify(); 156 connection.verify(); 157 } 158 159 public void testExecuteWithDuplicateTables() throws Exception 160 { 161 String schemaName = "schema"; 162 String tableName = "table"; 163 String [] expected = { 164 "delete from schema.table where c2 = 123.45 and c1 = 'qwerty'", 165 "delete from schema.table where c2 = 1234 and c1 = 'toto'", 166 "delete from schema.table where c2 = 123.45 and c1 = 'qwerty'", 167 "delete from schema.table where c2 = 1234 and c1 = 'toto'", 168 }; 169 170 Column[] columns = new Column[]{ 171 new Column("c1", DataType.VARCHAR), 172 new Column("c2", DataType.NUMERIC), 173 new Column("c3", DataType.BOOLEAN), 174 }; 175 String [] primaryKeys = {"c2", "c1"}; 176 177 DefaultTable table = new DefaultTable(new DefaultTableMetaData( 178 tableName, columns, primaryKeys)); 179 table.addRow(new Object []{"toto", "1234", Boolean.FALSE}); 180 table.addRow(new Object []{"qwerty", new Double ("123.45"), "true"}); 181 IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table}); 182 183 MockBatchStatement statement = new MockBatchStatement(); 185 statement.addExpectedBatchStrings(expected); 186 statement.setExpectedExecuteBatchCalls(2); 187 statement.setExpectedClearBatchCalls(2); 188 statement.setExpectedCloseCalls(2); 189 190 MockStatementFactory factory = new MockStatementFactory(); 191 factory.setExpectedCreatePreparedStatementCalls(2); 192 factory.setupStatement(statement); 193 194 MockDatabaseConnection connection = new MockDatabaseConnection(); 195 connection.setupDataSet(new DefaultDataSet(table)); 196 connection.setupSchema(schemaName); 197 connection.setupStatementFactory(factory); 198 connection.setExpectedCloseCalls(0); 199 200 new DeleteOperation().execute(connection, dataSet); 202 203 statement.verify(); 204 factory.verify(); 205 connection.verify(); 206 } 207 208 public void testExecuteWithEmptyTable() throws Exception 209 { 210 Column[] columns = {new Column("c1", DataType.VARCHAR)}; 211 ITable table = new DefaultTable(new DefaultTableMetaData( 212 "name", columns, columns)); 213 IDataSet dataSet = new DefaultDataSet(table); 214 215 MockStatementFactory factory = new MockStatementFactory(); 217 factory.setExpectedCreatePreparedStatementCalls(0); 218 219 MockDatabaseConnection connection = new MockDatabaseConnection(); 220 connection.setupDataSet(dataSet); 221 connection.setupStatementFactory(factory); 222 connection.setExpectedCloseCalls(0); 223 224 new DeleteOperation().execute(connection, dataSet); 226 227 factory.verify(); 228 connection.verify(); 229 } 230 231 public void testExecuteAndNoPrimaryKey() throws Exception 232 { 233 IDataSet dataSet = _connection.createDataSet(); 234 ITableMetaData metaData = dataSet.getTableMetaData("TEST_TABLE"); 235 try 236 { 237 new DeleteOperation().getOperationData( 238 metaData, null, _connection); 239 fail("Should throw a NoPrimaryKeyException"); 240 } 241 catch (NoPrimaryKeyException e) 242 { 243 } 244 } 245 246 public void testExecute() throws Exception 247 { 248 Reader in = new FileReader ( 249 new File ("src/xml/deleteOperationTest.xml")); 250 IDataSet dataSet = new XmlDataSet(in); 251 252 testExecute(dataSet); 253 254 } 255 256 public void testExecuteCaseInsensitive() throws Exception 257 { 258 Reader in = new FileReader ( 259 new File ("src/xml/deleteOperationTest.xml")); 260 IDataSet dataSet = new XmlDataSet(in); 261 262 testExecute(new LowerCaseDataSet(dataSet)); 263 } 264 265 private void testExecute(IDataSet dataSet) throws Exception 266 { 267 String tableName = "PK_TABLE"; 268 String columnName = "PK0"; 269 270 ITable tableBefore = createOrderedTable(tableName, columnName); 272 assertEquals("row count before", 3, tableBefore.getRowCount()); 273 assertEquals("before", "0", tableBefore.getValue(0, columnName).toString()); 274 assertEquals("before", "1", tableBefore.getValue(1, columnName).toString()); 275 assertEquals("before", "2", tableBefore.getValue(2, columnName).toString()); 276 277 DatabaseOperation.DELETE.execute(_connection, dataSet); 278 279 ITable tableAfter = createOrderedTable(tableName, columnName); 280 assertEquals("row count after", 2, tableAfter.getRowCount()); 281 assertEquals("after", "0", tableAfter.getValue(0, columnName).toString()); 282 assertEquals("after", "2", tableAfter.getValue(1, columnName).toString()); 283 } 284 } 285 286 287 288 289 290 291 292 293 | Popular Tags |