KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
38 import java.io.FileReader JavaDoc;
39 import java.io.Reader JavaDoc;
40
41 /**
42  * @author Manuel Laflamme
43  * @version $Revision: 1.23 $
44  * @since Feb 19, 2002
45  */

46 public class UpdateOperationTest extends AbstractDatabaseTest
47 {
48     public UpdateOperationTest(String JavaDoc s)
49     {
50         super(s);
51     }
52
53     ////////////////////////////////////////////////////////////////////////////
54
//
55

56     protected IDataSet getDataSet() throws Exception JavaDoc
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 JavaDoc("src/xml/blobInsertTest.xml")),
65                     dataSet);
66         }
67
68         if (environment.support(TestFeature.CLOB))
69         {
70             dataSet = new CompositeDataSet(
71                     new FlatXmlDataSet(new File JavaDoc("src/xml/clobInsertTest.xml")),
72                     dataSet);
73         }
74
75         return dataSet;
76     }
77
78     ////////////////////////////////////////////////////////////////////////////
79
//
80

81     public void testMockExecute() throws Exception JavaDoc
82     {
83         String JavaDoc schemaName = "schema";
84         String JavaDoc tableName = "table";
85         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c4", "c1"};
97         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
98                 tableName, columns, primaryKeys));
99         table.addRow(new Object JavaDoc[]{"toto", "1234", "false", "0"});
100         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), null, "0"});
101         IDataSet dataSet = new DefaultDataSet(table);
102
103         // setup mock objects
104
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         // execute operation
121
new UpdateOperation().execute(connection, dataSet);
122
123         statement.verify();
124         factory.verify();
125         connection.verify();
126     }
127
128     public void testExecuteWithEscapedName() throws Exception JavaDoc
129     {
130         String JavaDoc schemaName = "schema";
131         String JavaDoc tableName = "table";
132         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c4", "c1"};
144         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
145                 tableName, columns, primaryKeys));
146         table.addRow(new Object JavaDoc[]{"toto", "1234", "false", "0"});
147         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), null, "0"});
148         IDataSet dataSet = new DefaultDataSet(table);
149
150         // setup mock objects
151
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         // execute operation
168
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 JavaDoc
178     {
179         String JavaDoc schemaName = "schema";
180         String JavaDoc tableName = "table";
181         String JavaDoc[] 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 JavaDoc[] primaryKeys = {"c4", "c1"};
195         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
196                 tableName, columns, primaryKeys));
197         table.addRow(new Object JavaDoc[]{"toto", "1234", "false", "0"});
198         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), null, "0"});
199         IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table});
200
201         // setup mock objects
202
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         // execute operation
219
new UpdateOperation().execute(connection, dataSet);
220
221         statement.verify();
222         factory.verify();
223         connection.verify();
224     }
225
226     public void testExecuteWithEmptyTable() throws Exception JavaDoc
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         // setup mock objects
234
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         // execute operation
243
new UpdateOperation().execute(connection, dataSet);
244
245         factory.verify();
246         connection.verify();
247     }
248
249     public void testUpdateClob() throws Exception JavaDoc
250     {
251         // execute this test only if the target database support CLOB
252
DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
253         if (environment.support(TestFeature.CLOB))
254         {
255             String JavaDoc tableName = "CLOB_TABLE";
256
257             {
258                 IDataSet beforeDataSet = new FlatXmlDataSet(
259                         new File JavaDoc("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 JavaDoc("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 JavaDoc
279     {
280         // execute this test only if the target database support BLOB
281
DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
282         if (environment.support(TestFeature.BLOB))
283         {
284             String JavaDoc tableName = "BLOB_TABLE";
285
286             {
287                 IDataSet beforeDataSet = new FlatXmlDataSet(
288                         new File JavaDoc("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 // System.out.println("****** BEFORE *******");
295
// FlatXmlDataSet.write(_connection.createDataSet(), System.out);
296
}
297
298             IDataSet afterDataSet = new FlatXmlDataSet(
299                     new File JavaDoc("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 // System.out.println("****** AFTER *******");
308
// FlatXmlDataSet.write(_connection.createDataSet(), System.out);
309
}
310         }
311     }
312
313     public void testExecute() throws Exception JavaDoc
314     {
315         Reader JavaDoc in = new FileReader JavaDoc(
316                 new File JavaDoc("src/xml/updateOperationTest.xml"));
317         IDataSet dataSet = new XmlDataSet(in);
318
319         testExecute(dataSet);
320
321     }
322
323     public void testExecuteCaseInsensitive() throws Exception JavaDoc
324     {
325         Reader JavaDoc in = new FileReader JavaDoc(
326                 new File JavaDoc("src/xml/updateOperationTest.xml"));
327         IDataSet dataSet = new XmlDataSet(in);
328
329         testExecute(new LowerCaseDataSet(dataSet));
330     }
331
332     public void testExecuteForwardOnly() throws Exception JavaDoc
333     {
334         Reader JavaDoc in = new FileReader JavaDoc(
335                 new File JavaDoc("src/xml/updateOperationTest.xml"));
336         IDataSet dataSet = new XmlDataSet(in);
337
338         testExecute(new ForwardOnlyDataSet(dataSet));
339     }
340
341     public void testExecuteAndNoPrimaryKeys() throws Exception JavaDoc
342     {
343         String JavaDoc tableName = "test_table";
344
345         Reader JavaDoc reader = new FileReader JavaDoc("src/xml/updateOperationNoPKTest.xml");
346         IDataSet dataSet = new FlatXmlDataSet(reader);
347
348         // verify table before
349
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         // verify table after
362
assertEquals("row count before", 6, _connection.getRowCount(tableName));
363     }
364
365     private void testExecute(IDataSet dataSet) throws Exception JavaDoc
366     {
367         String JavaDoc tableName = "PK_TABLE";
368         String JavaDoc[] columnNames = {"PK0", "PK1", "PK2", "NORMAL0", "NORMAL1"};
369         int modifiedRow = 1;
370
371         // verify table before
372
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             // verify modified row
382
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             // all other row must be equals than before update
396
else
397             {
398                 for (int j = 0; j < columnNames.length; j++)
399                 {
400                     String JavaDoc name = columnNames[j];
401                     Object JavaDoc valueAfter = tableAfter.getValue(i, name);
402                     Object JavaDoc 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