KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.sql.SQLException JavaDoc;
41
42 /**
43  * @author Manuel Laflamme
44  * @version $Revision: 1.27 $
45  * @since Feb 19, 2002
46  */

47 public class InsertOperationTest extends AbstractDatabaseTest
48 {
49     public InsertOperationTest(String JavaDoc s)
50     {
51         super(s);
52     }
53
54     public void testMockExecute() throws Exception JavaDoc
55     {
56         String JavaDoc schemaName = "schema";
57         String JavaDoc tableName = "table";
58         String JavaDoc[] expected = {
59             "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
60             "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
61         };
62
63         // setup table
64
Column[] columns = new Column[]{
65             new Column("c1", DataType.VARCHAR),
66             new Column("c2", DataType.NUMERIC),
67             new Column("c3", DataType.BOOLEAN),
68         };
69         DefaultTable table = new DefaultTable(tableName, columns);
70         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
71         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
72         IDataSet dataSet = new DefaultDataSet(table);
73
74         // setup mock objects
75
MockBatchStatement statement = new MockBatchStatement();
76         statement.addExpectedBatchStrings(expected);
77         statement.setExpectedExecuteBatchCalls(1);
78         statement.setExpectedClearBatchCalls(1);
79         statement.setExpectedCloseCalls(1);
80
81         MockStatementFactory factory = new MockStatementFactory();
82         factory.setExpectedCreatePreparedStatementCalls(1);
83         factory.setupStatement(statement);
84
85         MockDatabaseConnection connection = new MockDatabaseConnection();
86         connection.setupDataSet(dataSet);
87         connection.setupSchema(schemaName);
88         connection.setupStatementFactory(factory);
89         connection.setExpectedCloseCalls(0);
90
91         // execute operation
92
new InsertOperation().execute(connection, dataSet);
93
94         statement.verify();
95         factory.verify();
96         connection.verify();
97     }
98
99     public void testExecuteUnknownColumn() throws Exception JavaDoc
100     {
101         String JavaDoc tableName = "table";
102
103         // setup table
104
Column[] columns = new Column[]{
105             new Column("column", DataType.VARCHAR),
106             new Column("unknown", DataType.VARCHAR),
107         };
108         DefaultTable table = new DefaultTable(tableName, columns);
109         table.addRow();
110         table.setValue(0, columns[0].getColumnName(), null);
111         table.setValue(0, columns[0].getColumnName(), "value");
112         IDataSet insertDataset = new DefaultDataSet(table);
113
114         IDataSet databaseDataSet = new DefaultDataSet(
115                 new DefaultTable(tableName, new Column[]{
116                     new Column("column", DataType.VARCHAR),
117                 }));
118
119         // setup mock objects
120
MockBatchStatement statement = new MockBatchStatement();
121         statement.setExpectedExecuteBatchCalls(0);
122         statement.setExpectedClearBatchCalls(0);
123         statement.setExpectedCloseCalls(0);
124
125         MockStatementFactory factory = new MockStatementFactory();
126         factory.setExpectedCreatePreparedStatementCalls(0);
127         factory.setupStatement(statement);
128
129         MockDatabaseConnection connection = new MockDatabaseConnection();
130         connection.setupDataSet(databaseDataSet);
131         connection.setupStatementFactory(factory);
132         connection.setExpectedCloseCalls(0);
133
134         // execute operation
135
try
136         {
137             new InsertOperation().execute(connection, insertDataset);
138             fail("Should not be here!");
139         }
140         catch (NoSuchColumnException e)
141         {
142
143         }
144
145         statement.verify();
146         factory.verify();
147         connection.verify();
148     }
149
150     public void testExecuteIgnoreNone() throws Exception JavaDoc
151     {
152         String JavaDoc schemaName = "schema";
153         String JavaDoc tableName = "table";
154         String JavaDoc[] expected = {
155             "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
156             "insert into schema.table (c2, c3) values (123.45, 'true')",
157             "insert into schema.table (c1, c2, c3) values ('qwerty1', 1, 'true')",
158             "insert into schema.table (c1, c2, c3) values ('qwerty2', 2, 'false')",
159             "insert into schema.table (c3) values ('false')",
160         };
161
162         // setup table
163
Column[] columns = new Column[]{
164             new Column("c1", DataType.VARCHAR),
165             new Column("c2", DataType.NUMERIC),
166             new Column("c3", DataType.BOOLEAN),
167         };
168         DefaultTable table = new DefaultTable(tableName, columns);
169         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
170         table.addRow(new Object JavaDoc[]{ITable.NO_VALUE, new Double JavaDoc("123.45"), "true"});
171         table.addRow(new Object JavaDoc[]{"qwerty1", "1", Boolean.TRUE});
172         table.addRow(new Object JavaDoc[]{"qwerty2", "2", Boolean.FALSE});
173         table.addRow(new Object JavaDoc[]{ITable.NO_VALUE, ITable.NO_VALUE, Boolean.FALSE});
174         IDataSet dataSet = new DefaultDataSet(table);
175
176         // setup mock objects
177
MockBatchStatement statement = new MockBatchStatement();
178         statement.addExpectedBatchStrings(expected);
179         statement.setExpectedExecuteBatchCalls(4);
180         statement.setExpectedClearBatchCalls(4);
181         statement.setExpectedCloseCalls(4);
182
183         MockStatementFactory factory = new MockStatementFactory();
184         factory.setExpectedCreatePreparedStatementCalls(4);
185         factory.setupStatement(statement);
186
187         MockDatabaseConnection connection = new MockDatabaseConnection();
188         connection.setupDataSet(dataSet);
189         connection.setupSchema(schemaName);
190         connection.setupStatementFactory(factory);
191         connection.setExpectedCloseCalls(0);
192
193         // execute operation
194
new InsertOperation().execute(connection, dataSet);
195
196         statement.verify();
197         factory.verify();
198         connection.verify();
199     }
200
201 // public void testExecuteNullAsNone() throws Exception
202
// {
203
// String schemaName = "schema";
204
// String tableName = "table";
205
// String[] expected = {
206
// "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
207
// "insert into schema.table (c2, c3) values (123.45, 'true')",
208
// "insert into schema.table (c1, c2, c3) values ('qwerty1', 1, 'true')",
209
// "insert into schema.table (c1, c2, c3) values ('qwerty2', 2, 'false')",
210
// "insert into schema.table (c3) values ('false')",
211
// };
212
//
213
// // setup table
214
// List valueList = new ArrayList();
215
// valueList.add(new Object[]{"toto", "1234", Boolean.FALSE});
216
// valueList.add(new Object[]{null, new Double("123.45"), "true"});
217
// valueList.add(new Object[]{"qwerty1", "1", Boolean.TRUE});
218
// valueList.add(new Object[]{"qwerty2", "2", Boolean.FALSE});
219
// valueList.add(new Object[]{null, null, Boolean.FALSE});
220
// Column[] columns = new Column[]{
221
// new Column("c1", DataType.VARCHAR),
222
// new Column("c2", DataType.NUMERIC),
223
// new Column("c3", DataType.BOOLEAN),
224
// };
225
// DefaultTable table = new DefaultTable(tableName, columns, valueList);
226
// IDataSet dataSet = new DefaultDataSet(table);
227
//
228
// // setup mock objects
229
// MockBatchStatement statement = new MockBatchStatement();
230
// statement.addExpectedBatchStrings(expected);
231
// statement.setExpectedExecuteBatchCalls(4);
232
// statement.setExpectedClearBatchCalls(4);
233
// statement.setExpectedCloseCalls(4);
234
//
235
// MockStatementFactory factory = new MockStatementFactory();
236
// factory.setExpectedCreatePreparedStatementCalls(4);
237
// factory.setupStatement(statement);
238
//
239
// MockDatabaseConnection connection = new MockDatabaseConnection();
240
// connection.setupDataSet(dataSet);
241
// connection.setupSchema(schemaName);
242
// connection.setupStatementFactory(factory);
243
// connection.setExpectedCloseCalls(0);
244
// DatabaseConfig config = connection.getConfig();
245
// config.setFeature(DatabaseConfig.FEATURE_NULL_AS_NONE, true);
246
//
247
// // execute operation
248
// new InsertOperation().execute(connection, dataSet);
249
//
250
// statement.verify();
251
// factory.verify();
252
// connection.verify();
253
// }
254

255     public void testExecuteWithEscapedNames() throws Exception JavaDoc
256     {
257         String JavaDoc schemaName = "schema";
258         String JavaDoc tableName = "table";
259         String JavaDoc[] expected = {
260             "insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('toto', 1234, 'false')",
261             "insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('qwerty', 123.45, 'true')",
262         };
263
264         // setup table
265
Column[] columns = new Column[]{
266             new Column("c1", DataType.VARCHAR),
267             new Column("c2", DataType.NUMERIC),
268             new Column("c3", DataType.BOOLEAN),
269         };
270         DefaultTable table = new DefaultTable(tableName, columns);
271         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
272         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
273         IDataSet dataSet = new DefaultDataSet(table);
274
275         // setup mock objects
276
MockBatchStatement statement = new MockBatchStatement();
277         statement.addExpectedBatchStrings(expected);
278         statement.setExpectedExecuteBatchCalls(1);
279         statement.setExpectedClearBatchCalls(1);
280         statement.setExpectedCloseCalls(1);
281
282         MockStatementFactory factory = new MockStatementFactory();
283         factory.setExpectedCreatePreparedStatementCalls(1);
284         factory.setupStatement(statement);
285
286         MockDatabaseConnection connection = new MockDatabaseConnection();
287         connection.setupDataSet(dataSet);
288         connection.setupSchema(schemaName);
289         connection.setupStatementFactory(factory);
290         connection.setExpectedCloseCalls(0);
291
292         // execute operation
293
connection.getConfig().setProperty(
294                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "'?'");
295         new InsertOperation().execute(connection, dataSet);
296
297         statement.verify();
298         factory.verify();
299         connection.verify();
300     }
301
302     public void testExecuteWithDuplicateTables() throws Exception JavaDoc
303     {
304         String JavaDoc schemaName = "schema";
305         String JavaDoc tableName = "table";
306         String JavaDoc[] expected = {
307             "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
308             "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
309             "insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
310             "insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
311         };
312
313         // setup table
314
Column[] columns = new Column[]{
315             new Column("c1", DataType.VARCHAR),
316             new Column("c2", DataType.NUMERIC),
317             new Column("c3", DataType.BOOLEAN),
318         };
319         DefaultTable table = new DefaultTable(tableName, columns);
320         table.addRow(new Object JavaDoc[]{"toto", "1234", Boolean.FALSE});
321         table.addRow(new Object JavaDoc[]{"qwerty", new Double JavaDoc("123.45"), "true"});
322         IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table});
323
324         // setup mock objects
325
MockBatchStatement statement = new MockBatchStatement();
326         statement.addExpectedBatchStrings(expected);
327         statement.setExpectedExecuteBatchCalls(2);
328         statement.setExpectedClearBatchCalls(2);
329         statement.setExpectedCloseCalls(2);
330
331         MockStatementFactory factory = new MockStatementFactory();
332         factory.setExpectedCreatePreparedStatementCalls(2);
333         factory.setupStatement(statement);
334
335         MockDatabaseConnection connection = new MockDatabaseConnection();
336         connection.setupDataSet(new DefaultDataSet(table));
337         connection.setupSchema(schemaName);
338         connection.setupStatementFactory(factory);
339         connection.setExpectedCloseCalls(0);
340
341         // execute operation
342
new InsertOperation().execute(connection, dataSet);
343
344         statement.verify();
345         factory.verify();
346         connection.verify();
347     }
348
349     public void testExecuteWithEmptyTable() throws Exception JavaDoc
350     {
351         Column[] columns = {new Column("c1", DataType.VARCHAR)};
352         ITable table = new DefaultTable(new DefaultTableMetaData(
353                 "name", columns, columns));
354         IDataSet dataSet = new DefaultDataSet(table);
355
356         // setup mock objects
357
MockStatementFactory factory = new MockStatementFactory();
358         factory.setExpectedCreatePreparedStatementCalls(0);
359
360         MockDatabaseConnection connection = new MockDatabaseConnection();
361         connection.setupDataSet(dataSet);
362         connection.setupStatementFactory(factory);
363         connection.setExpectedCloseCalls(0);
364
365         // execute operation
366
new InsertOperation().execute(connection, dataSet);
367
368         factory.verify();
369         connection.verify();
370     }
371
372     public void testInsertClob() throws Exception JavaDoc
373     {
374         // execute this test only if the target database support CLOB
375
DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
376         if (environment.support(TestFeature.CLOB))
377         {
378             String JavaDoc tableName = "CLOB_TABLE";
379
380             Reader JavaDoc in = new FileReader JavaDoc(new File JavaDoc("src/xml/clobInsertTest.xml"));
381             IDataSet xmlDataSet = new FlatXmlDataSet(in);
382
383             assertEquals("count before", 0, _connection.getRowCount(tableName));
384
385             DatabaseOperation.INSERT.execute(_connection, xmlDataSet);
386
387             ITable tableAfter = _connection.createDataSet().getTable(tableName);
388             assertEquals("count after", 3, tableAfter.getRowCount());
389             Assertion.assertEquals(xmlDataSet.getTable(tableName), tableAfter);
390         }
391     }
392
393     public void testInsertBlob() throws Exception JavaDoc
394     {
395         // execute this test only if the target database support BLOB
396
DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
397         if (environment.support(TestFeature.BLOB))
398         {
399             String JavaDoc tableName = "BLOB_TABLE";
400
401             Reader JavaDoc in = new FileReader JavaDoc(new File JavaDoc("src/xml/blobInsertTest.xml"));
402             IDataSet xmlDataSet = new FlatXmlDataSet(in);
403
404             assertEquals("count before", 0, _connection.getRowCount(tableName));
405
406             DatabaseOperation.INSERT.execute(_connection, xmlDataSet);
407
408             ITable tableAfter = _connection.createDataSet().getTable(tableName);
409             assertEquals("count after", 1, tableAfter.getRowCount());
410             Assertion.assertEquals(xmlDataSet.getTable(tableName), tableAfter);
411         }
412     }
413
414     public void testMissingColumns() throws Exception JavaDoc
415     {
416         Reader JavaDoc in = new FileReader JavaDoc("src/xml/missingColumnTest.xml");
417         IDataSet xmlDataSet = new XmlDataSet(in);
418
419         ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
420         DatabaseOperation.INSERT.execute(_connection, xmlDataSet);
421         ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
422
423         // verify tables before
424
for (int i = 0; i < tablesBefore.length; i++)
425         {
426             ITable table = tablesBefore[i];
427             String JavaDoc tableName = table.getTableMetaData().getTableName();
428             if (tableName.startsWith("EMPTY"))
429             {
430                 assertEquals(tableName + " before", 0, table.getRowCount());
431             }
432         }
433
434         // verify tables after
435
for (int i = 0; i < tablesAfter.length; i++)
436         {
437             ITable databaseTable = tablesAfter[i];
438             String JavaDoc tableName = databaseTable.getTableMetaData().getTableName();
439
440             if (tableName.startsWith("EMPTY"))
441             {
442                 Column[] columns = databaseTable.getTableMetaData().getColumns();
443                 ITable xmlTable = xmlDataSet.getTable(tableName);
444
445                 // verify row count
446
assertEquals("row count", xmlTable.getRowCount(),
447                         databaseTable.getRowCount());
448
449                 // for each table row
450
for (int j = 0; j < databaseTable.getRowCount(); j++)
451                 {
452                     // verify first column values
453
Object JavaDoc expected = xmlTable.getValue(j, columns[0].getColumnName());
454                     Object JavaDoc actual = databaseTable.getValue(j, columns[0].getColumnName());
455
456                     assertEquals(tableName + "." + columns[0].getColumnName(),
457                             expected, actual);
458
459                     // all remaining columns should be null except mssql server timestamp column which is of type binary.
460
for (int k = 1; k < columns.length; k++)
461                     {
462                         String JavaDoc columnName = columns[k].getColumnName();
463                         assertEquals(tableName + "." + columnName,
464                                 null, databaseTable.getValue(j, columnName));
465                     }
466                 }
467             }
468         }
469
470     }
471
472     public void testExecute() throws Exception JavaDoc
473     {
474         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertOperationTest.xml");
475         IDataSet dataSet = new XmlDataSet(in);
476
477         testExecute(dataSet);
478     }
479
480     public void testExecuteCaseInsensitive() throws Exception JavaDoc
481     {
482         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertOperationTest.xml");
483         IDataSet dataSet = new XmlDataSet(in);
484
485         testExecute(new LowerCaseDataSet(dataSet));
486     }
487
488     public void testExecuteForwardOnly() throws Exception JavaDoc
489     {
490         Reader JavaDoc in = new FileReader JavaDoc("src/xml/insertOperationTest.xml");
491         IDataSet dataSet = new XmlDataSet(in);
492
493         testExecute(new ForwardOnlyDataSet(dataSet));
494     }
495
496     private void testExecute(IDataSet dataSet) throws Exception JavaDoc, SQLException JavaDoc
497     {
498         ITable[] tablesBefore = DataSetUtils.getTables(_connection.createDataSet());
499         DatabaseOperation.INSERT.execute(_connection, dataSet);
500         ITable[] tablesAfter = DataSetUtils.getTables(_connection.createDataSet());
501
502         assertEquals("table count", tablesBefore.length, tablesAfter.length);
503         for (int i = 0; i < tablesBefore.length; i++)
504         {
505             ITable table = tablesBefore[i];
506             String JavaDoc name = table.getTableMetaData().getTableName();
507
508
509             if (name.startsWith("EMPTY"))
510             {
511                 assertEquals(name + "before", 0, table.getRowCount());
512             }
513         }
514
515         for (int i = 0; i < tablesAfter.length; i++)
516         {
517             ITable table = tablesAfter[i];
518             String JavaDoc name = table.getTableMetaData().getTableName();
519
520             if (name.startsWith("EMPTY"))
521             {
522                 if (dataSet instanceof ForwardOnlyDataSet)
523                 {
524                     assertTrue(name, table.getRowCount() > 0);
525                 }
526                 else
527                 {
528                     SortedTable expectedTable = new SortedTable(
529                             dataSet.getTable(name), table.getTableMetaData());
530                     SortedTable actualTable = new SortedTable(table);
531                     Assertion.assertEquals(expectedTable, actualTable);
532                 }
533             }
534         }
535     }
536 }
537
538
539
540
541
542
543
544
545
546
547
Popular Tags