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.AbstractDataSetTest; 30 import org.dbunit.dataset.DataSetUtils; 31 import org.dbunit.dataset.DefaultDataSet; 32 import org.dbunit.dataset.DefaultTable; 33 import org.dbunit.dataset.EmptyTableDataSet; 34 import org.dbunit.dataset.IDataSet; 35 import org.dbunit.dataset.ITable; 36 import org.dbunit.dataset.LowerCaseDataSet; 37 38 45 public class DeleteAllOperationTest extends AbstractDatabaseTest 46 { 47 public DeleteAllOperationTest(String s) 48 { 49 super(s); 50 } 51 52 protected void setUp() throws Exception 53 { 54 super.setUp(); 55 56 DatabaseOperation.CLEAN_INSERT.execute(_connection, 57 getEnvironment().getInitDataSet()); 58 } 59 60 protected DatabaseOperation getDeleteAllOperation() 61 { 62 return new DeleteAllOperation(); 63 } 64 65 protected String getExpectedStament(String tableName) 66 { 67 return "delete from " + tableName; 68 } 69 70 public void testMockExecute() throws Exception 71 { 72 String schemaName = "schema"; 73 String tableName = "table"; 74 String expected = getExpectedStament(schemaName + "." + tableName); 75 76 IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName)); 77 78 MockBatchStatement statement = new MockBatchStatement(); 80 statement.addExpectedBatchString(expected); 81 statement.setExpectedExecuteBatchCalls(1); 82 statement.setExpectedClearBatchCalls(1); 83 statement.setExpectedCloseCalls(1); 84 85 MockStatementFactory factory = new MockStatementFactory(); 86 factory.setExpectedCreateStatementCalls(1); 87 factory.setupStatement(statement); 88 89 MockDatabaseConnection connection = new MockDatabaseConnection(); 90 connection.setupDataSet(dataSet); 91 connection.setupSchema(schemaName); 92 connection.setupStatementFactory(factory); 93 connection.setExpectedCloseCalls(0); 94 95 getDeleteAllOperation().execute(connection, dataSet); 97 98 statement.verify(); 99 factory.verify(); 100 connection.verify(); 101 } 102 103 public void testExecuteWithEscapedNames() throws Exception 104 { 105 String schemaName = "schema"; 106 String tableName = "table"; 107 String expected = getExpectedStament("'" + schemaName + "'.'" + tableName +"'"); 108 109 IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName)); 110 111 MockBatchStatement statement = new MockBatchStatement(); 113 statement.addExpectedBatchString(expected); 114 statement.setExpectedExecuteBatchCalls(1); 115 statement.setExpectedClearBatchCalls(1); 116 statement.setExpectedCloseCalls(1); 117 118 MockStatementFactory factory = new MockStatementFactory(); 119 factory.setExpectedCreateStatementCalls(1); 120 factory.setupStatement(statement); 121 122 MockDatabaseConnection connection = new MockDatabaseConnection(); 123 connection.setupDataSet(dataSet); 124 connection.setupSchema(schemaName); 125 connection.setupStatementFactory(factory); 126 connection.setExpectedCloseCalls(0); 127 128 connection.getConfig().setProperty( 130 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "'?'"); 131 getDeleteAllOperation().execute(connection, dataSet); 132 133 statement.verify(); 134 factory.verify(); 135 connection.verify(); 136 } 137 138 public void testExecuteWithDuplicateTables() throws Exception 139 { 140 String schemaName = "schema"; 141 String tableName1 = "table1"; 142 String tableName2 = "table2"; 143 String expected1 = getExpectedStament(schemaName + "." + tableName1); 144 String expected2 = getExpectedStament(schemaName + "." + tableName2); 145 146 ITable table1 = new DefaultTable(tableName1); 147 ITable table2 = new DefaultTable(tableName2); 148 IDataSet dataSet = new DefaultDataSet( 149 new ITable[] {table1, table2, table1}); 150 151 MockBatchStatement statement = new MockBatchStatement(); 153 statement.addExpectedBatchString(expected2); 154 statement.addExpectedBatchString(expected1); 155 statement.setExpectedExecuteBatchCalls(1); 156 statement.setExpectedClearBatchCalls(1); 157 statement.setExpectedCloseCalls(1); 158 159 MockStatementFactory factory = new MockStatementFactory(); 160 factory.setExpectedCreateStatementCalls(1); 161 factory.setupStatement(statement); 162 163 MockDatabaseConnection connection = new MockDatabaseConnection(); 164 connection.setupDataSet(new DefaultDataSet(table1, table2)); 165 connection.setupSchema(schemaName); 166 connection.setupStatementFactory(factory); 167 connection.setExpectedCloseCalls(0); 168 169 getDeleteAllOperation().execute(connection, dataSet); 171 172 statement.verify(); 173 factory.verify(); 174 connection.verify(); 175 } 176 177 public void testExecute() throws Exception 178 { 179 IDataSet databaseDataSet = _connection.createDataSet(); 180 IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables( 181 databaseDataSet); 182 183 testExecute(dataSet); 184 } 185 186 public void testExecuteEmpty() throws Exception 187 { 188 IDataSet databaseDataSet = _connection.createDataSet(); 189 IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables( 190 databaseDataSet); 191 192 testExecute(new EmptyTableDataSet(dataSet)); 193 } 194 195 public void testExecuteCaseInsentive() throws Exception 196 { 197 IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables( 198 _connection.createDataSet()); 199 200 testExecute(new LowerCaseDataSet(dataSet)); 201 } 202 203 209 private void testExecute(IDataSet dataSet) throws Exception 210 { 211 ITable[] tablesBefore = DataSetUtils.getTables(AbstractDataSetTest.removeExtraTestTables(_connection.createDataSet())); 213 getDeleteAllOperation().execute(_connection, dataSet); 214 ITable[] tablesAfter = DataSetUtils.getTables(AbstractDataSetTest.removeExtraTestTables(_connection.createDataSet())); 215 216 assertTrue("table count > 0", tablesBefore.length > 0); 217 assertEquals("table count", tablesBefore.length, tablesAfter.length); 218 for (int i = 0; i < tablesBefore.length; i++) 219 { 220 ITable table = tablesBefore[i]; 221 String name = table.getTableMetaData().getTableName(); 222 223 if (!name.toUpperCase().startsWith("EMPTY")) 224 { 225 assertTrue(name + " before", table.getRowCount() > 0); 226 } 227 } 228 229 for (int i = 0; i < tablesAfter.length; i++) 230 { 231 ITable table = tablesAfter[i]; 232 String name = table.getTableMetaData().getTableName(); 233 assertEquals(name + " after " + i, 0, table.getRowCount()); 234 } 235 } 236 237 public void testExecuteWithEmptyDataset() throws Exception 238 { 239 getDeleteAllOperation().execute( 240 _connection, new DefaultDataSet(new ITable[0])); 241 } 242 } 243 244 245 246 247 248 249 | Popular Tags |