KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
42 import java.io.FileReader JavaDoc;
43 import java.io.Reader JavaDoc;
44
45 /**
46  * @author Manuel Laflamme
47  * @version $Revision: 1.20 $
48  * @since Feb 19, 2002
49  */

50 public class DeleteOperationTest extends AbstractDatabaseTest
51 {
52     public DeleteOperationTest(String JavaDoc s)
53     {
54         super(s);
55     }
56
57     public void testMockExecute() throws Exception JavaDoc
58     {
59         String JavaDoc schemaName = "schema";
60         String JavaDoc tableName1 = "table1";
61         String JavaDoc tableName2 = "table2";
62         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c2", "c1"};
75
76         DefaultTable table1 = new DefaultTable(new DefaultTableMetaData(
77                 tableName1, columns, primaryKeys));
78         table1.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
79         table1.addRow(new Object JavaDoc[]{"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         // setup mock objects
86
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         // execute operation
103
new DeleteOperation().execute(connection, dataSet);
104
105         statement.verify();
106         factory.verify();
107         connection.verify();
108     }
109
110     public void testExecuteWithEscapedNames() throws Exception JavaDoc
111     {
112         String JavaDoc schemaName = "schema";
113         String JavaDoc tableName = "table";
114         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c2", "c1"};
125
126         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
127                 tableName, columns, primaryKeys));
128         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
129         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
130         IDataSet dataSet = new DefaultDataSet(table);
131
132         // setup mock objects
133
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         // execute operation
150
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 JavaDoc
160     {
161         String JavaDoc schemaName = "schema";
162         String JavaDoc tableName = "table";
163         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c2", "c1"};
176
177         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
178                 tableName, columns, primaryKeys));
179         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
180         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
181         IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table});
182
183         // setup mock objects
184
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         // execute operation
201
new DeleteOperation().execute(connection, dataSet);
202
203         statement.verify();
204         factory.verify();
205         connection.verify();
206     }
207
208     public void testExecuteWithEmptyTable() throws Exception JavaDoc
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         // setup mock objects
216
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         // execute operation
225
new DeleteOperation().execute(connection, dataSet);
226
227         factory.verify();
228         connection.verify();
229     }
230
231     public void testExecuteAndNoPrimaryKey() throws Exception JavaDoc
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 JavaDoc
247     {
248         Reader JavaDoc in = new FileReader JavaDoc(
249                 new File JavaDoc("src/xml/deleteOperationTest.xml"));
250         IDataSet dataSet = new XmlDataSet(in);
251
252         testExecute(dataSet);
253
254     }
255
256     public void testExecuteCaseInsensitive() throws Exception JavaDoc
257     {
258         Reader JavaDoc in = new FileReader JavaDoc(
259                 new File JavaDoc("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 JavaDoc
266     {
267         String JavaDoc tableName = "PK_TABLE";
268         String JavaDoc columnName = "PK0";
269
270         // verify table before
271
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