KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > operation > DeleteAllOperationTest


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

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 /**
39  * @author Manuel Laflamme
40  * @author Eric Pugh
41  * TODO Refactor all the references to AbstractDataSetTest.removeExtraTestTables() to something better.
42  * @version $Revision: 1.17 $
43  * @since Feb 18, 2002
44  */

45 public class DeleteAllOperationTest extends AbstractDatabaseTest
46 {
47     public DeleteAllOperationTest(String JavaDoc s)
48     {
49         super(s);
50     }
51
52     protected void setUp() throws Exception JavaDoc
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 JavaDoc getExpectedStament(String JavaDoc tableName)
66     {
67         return "delete from " + tableName;
68     }
69
70     public void testMockExecute() throws Exception JavaDoc
71     {
72         String JavaDoc schemaName = "schema";
73         String JavaDoc tableName = "table";
74         String JavaDoc expected = getExpectedStament(schemaName + "." + tableName);
75
76         IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName));
77
78         // setup mock objects
79
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         // execute operation
96
getDeleteAllOperation().execute(connection, dataSet);
97
98         statement.verify();
99         factory.verify();
100         connection.verify();
101     }
102
103     public void testExecuteWithEscapedNames() throws Exception JavaDoc
104     {
105         String JavaDoc schemaName = "schema";
106         String JavaDoc tableName = "table";
107         String JavaDoc expected = getExpectedStament("'" + schemaName + "'.'" + tableName +"'");
108
109         IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName));
110
111         // setup mock objects
112
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         // execute operation
129
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 JavaDoc
139     {
140         String JavaDoc schemaName = "schema";
141         String JavaDoc tableName1 = "table1";
142         String JavaDoc tableName2 = "table2";
143         String JavaDoc expected1 = getExpectedStament(schemaName + "." + tableName1);
144         String JavaDoc 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         // setup mock objects
152
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         // execute operation
170
getDeleteAllOperation().execute(connection, dataSet);
171
172         statement.verify();
173         factory.verify();
174         connection.verify();
175     }
176
177     public void testExecute() throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
196     {
197         IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables(
198                 _connection.createDataSet());
199
200         testExecute(new LowerCaseDataSet(dataSet));
201     }
202
203     /* The AbstractDataSetTest.removeExtraTestTables() is required when you
204     run on something besides hypersone (like mssql or oracle) to deal with
205     the extra tables that may not have data.
206
207     Need something like getDefaultTables or something that is totally cross dbms.
208     */

209     private void testExecute(IDataSet dataSet) throws Exception JavaDoc
210     {
211         //dataSet = dataSet);
212
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 JavaDoc 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 JavaDoc name = table.getTableMetaData().getTableName();
233             assertEquals(name + " after " + i, 0, table.getRowCount());
234         }
235     }
236
237     public void testExecuteWithEmptyDataset() throws Exception JavaDoc
238     {
239         getDeleteAllOperation().execute(
240                 _connection, new DefaultDataSet(new ITable[0]));
241     }
242 }
243
244
245
246
247
248
249
Popular Tags