KickJava   Java API By Example, From Geeks To Geeks.

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


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.dataset.*;
26 import org.dbunit.dataset.xml.XmlDataSet;
27
28 import java.io.FileReader JavaDoc;
29 import java.io.Reader JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.7 $
34  * @since May 7, 2002
35  */

36 public class AbstractBatchOperationTest extends AbstractDatabaseTest
37 {
38     public AbstractBatchOperationTest(String JavaDoc s)
39     {
40         super(s);
41     }
42
43     public void testGetOperationMetaDataAndMissingColumns() throws Exception JavaDoc
44     {
45         Reader JavaDoc in = new FileReader JavaDoc("src/xml/missingColumnTest.xml");
46         IDataSet xmlDataSet = new XmlDataSet(in);
47
48         ITable[] xmlTables = DataSetUtils.getTables(xmlDataSet);
49         for (int i = 0; i < xmlTables.length; i++)
50         {
51             ITable xmlTable = xmlTables[i];
52             ITableMetaData xmlMetaData = xmlTable.getTableMetaData();
53             String JavaDoc tableName = xmlMetaData.getTableName();
54
55             ITable databaseTable = _connection.createDataSet().getTable(tableName);
56             ITableMetaData databaseMetaData = databaseTable.getTableMetaData();
57
58             // ensure xml table is missing some columns present in database table
59
assertTrue(tableName + " missing columns", xmlMetaData.getColumns().length <
60                     databaseMetaData.getColumns().length);
61
62             ITableMetaData resultMetaData =
63                     AbstractBatchOperation.getOperationMetaData(_connection, xmlMetaData);
64
65             // result metadata must contains database columns matching the xml columns
66
Column[] resultColumns = resultMetaData.getColumns();
67             assertEquals("result columns count", xmlMetaData.getColumns().length,
68                     resultColumns.length);
69             for (int j = 0; j < resultColumns.length; j++)
70             {
71                 Column resultColumn = resultColumns[j];
72                 Column databaseColumn = DataSetUtils.getColumn(
73                         resultColumn.getColumnName(), databaseMetaData.getColumns());
74                 Column xmlColumn = xmlMetaData.getColumns()[j];
75
76                 assertEquals("column name", xmlColumn.getColumnName(),
77                         resultColumn.getColumnName());
78                 assertSame("column instance", resultColumn, databaseColumn);
79             }
80
81             // result metadata must contains database primary keys
82
Column[] resultPrimaryKeys = resultMetaData.getPrimaryKeys();
83             assertEquals("key count", databaseMetaData.getPrimaryKeys().length,
84                     resultPrimaryKeys.length);
85             for (int j = 0; j < resultPrimaryKeys.length; j++)
86             {
87                 Column resultPrimaryKey = resultPrimaryKeys[j];
88                 Column databasePrimaryKey = databaseMetaData.getPrimaryKeys()[j];
89                 assertSame("key instance", databasePrimaryKey, resultPrimaryKey);
90             }
91         }
92     }
93
94     public void testGetOperationMetaDataAndUnknownColumns() throws Exception JavaDoc
95     {
96         String JavaDoc tableName = "PK_TABLE";
97         Reader JavaDoc in = new FileReader JavaDoc("src/xml/unknownColumnTest.xml");
98         IDataSet xmlDataSet = new XmlDataSet(in);
99
100         ITable xmlTable = xmlDataSet.getTable(tableName);
101
102         try
103         {
104             AbstractBatchOperation.getOperationMetaData(_connection,
105                     xmlTable.getTableMetaData());
106             fail("Should throw a NoSuchColumnException");
107         }
108         catch (NoSuchColumnException e)
109         {
110         }
111     }
112
113 }
114
115
116
Popular Tags