View Javadoc
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.AbstractDatabaseIT;
25  import org.dbunit.dataset.*;
26  import org.dbunit.dataset.xml.XmlDataSet;
27  import org.dbunit.testutil.TestUtils;
28  
29  import java.io.FileReader;
30  import java.io.Reader;
31  
32  /**
33   * @author Manuel Laflamme
34   * @version $Revision$
35   * @since May 7, 2002
36   */
37  public class AbstractBatchOperationIT extends AbstractDatabaseIT
38  {
39      public AbstractBatchOperationIT(String s)
40      {
41          super(s);
42      }
43  
44      public void testGetOperationMetaDataAndMissingColumns() throws Exception
45      {
46          Reader in = TestUtils.getFileReader("xml/missingColumnTest.xml");
47          IDataSet xmlDataSet = new XmlDataSet(in);
48  
49          ITable[] xmlTables = DataSetUtils.getTables(xmlDataSet);
50          for (int i = 0; i < xmlTables.length; i++)
51          {
52              ITable xmlTable = xmlTables[i];
53              ITableMetaData xmlMetaData = xmlTable.getTableMetaData();
54              String tableName = xmlMetaData.getTableName();
55  
56              ITable databaseTable = _connection.createDataSet().getTable(tableName);
57              ITableMetaData databaseMetaData = databaseTable.getTableMetaData();
58  
59              // ensure xml table is missing some columns present in database table
60              assertTrue(tableName + " missing columns", xmlMetaData.getColumns().length <
61                      databaseMetaData.getColumns().length);
62  
63              ITableMetaData resultMetaData =
64                      AbstractBatchOperation.getOperationMetaData(_connection, xmlMetaData);
65  
66              // result metadata must contains database columns matching the xml columns
67              Column[] resultColumns = resultMetaData.getColumns();
68              assertEquals("result columns count", xmlMetaData.getColumns().length,
69                      resultColumns.length);
70              for (int j = 0; j < resultColumns.length; j++)
71              {
72                  Column resultColumn = resultColumns[j];
73                  Column databaseColumn = Columns.getColumn(resultColumn.getColumnName(), databaseMetaData.getColumns());
74                  Column xmlColumn = xmlMetaData.getColumns()[j];
75  
76                  assertEquals("column name", convertString(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
95      {
96          String tableName = "PK_TABLE";
97          Reader in = TestUtils.getFileReader("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