KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > DatabaseTableMetaDataTest


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.database;
23
24 import org.dbunit.AbstractDatabaseTest;
25 import org.dbunit.dataset.*;
26 import org.dbunit.dataset.datatype.DataType;
27
28 /**
29  * @author Manuel Laflamme
30  * @version $Revision: 1.20 $
31  * @since Mar 14, 2002
32  */

33 public class DatabaseTableMetaDataTest extends AbstractDatabaseTest
34 {
35     public DatabaseTableMetaDataTest(String JavaDoc s)
36     {
37         super(s);
38     }
39
40     protected IDataSet createDataSet() throws Exception JavaDoc
41     {
42         return _connection.createDataSet();
43     }
44
45     public void testGetPrimaryKeys() throws Exception JavaDoc
46     {
47         String JavaDoc tableName = "PK_TABLE";
48 // String[] expected = {"PK0"};
49
String JavaDoc[] expected = {"PK0", "PK1", "PK2"};
50
51         ITableMetaData metaData = createDataSet().getTableMetaData(tableName);
52         Column[] columns = metaData.getPrimaryKeys();
53
54         assertEquals("pk count", expected.length, columns.length);
55         for (int i = 0; i < columns.length; i++)
56         {
57             Column column = columns[i];
58             assertEquals("name", expected[i], column.getColumnName());
59         }
60     }
61
62     public void testGetNoPrimaryKeys() throws Exception JavaDoc
63     {
64         String JavaDoc tableName = "TEST_TABLE";
65
66         ITableMetaData metaData = createDataSet().getTableMetaData(tableName);
67         Column[] columns = metaData.getPrimaryKeys();
68
69         assertEquals("pk count", 0, columns.length);
70     }
71
72     public void testGetNoColumns() throws Exception JavaDoc
73     {
74         String JavaDoc tableName = "UNKNOWN_TABLE";
75
76         ITableMetaData metaData = new DatabaseTableMetaData(tableName,
77                 getConnection());
78         try
79         {
80             metaData.getColumns();
81             fail("Should not be here!");
82         }
83         catch (NoColumnsFoundException e)
84         {
85         }
86
87         // try a second times to ensure error is consistent
88
try
89         {
90             metaData.getColumns();
91             fail("Should not be here!");
92         }
93         catch (NoColumnsFoundException e)
94         {
95         }
96     }
97
98     public void testColumnIsNullable() throws Exception JavaDoc
99     {
100         String JavaDoc tableName = "PK_TABLE";
101         String JavaDoc[] notNullable = {"PK0", "PK1", "PK2"};
102         String JavaDoc[] nullable = {"NORMAL0", "NORMAL1"};
103
104         ITableMetaData metaData = createDataSet().getTableMetaData(tableName);
105         Column[] columns = metaData.getColumns();
106
107         assertEquals("column count", nullable.length + notNullable.length,
108                 columns.length);
109
110         // not nullable
111
for (int i = 0; i < notNullable.length; i++)
112         {
113             Column column = DataSetUtils.getColumn(notNullable[i], columns);
114             assertEquals(notNullable[i], Column.NO_NULLS, column.getNullable());
115         }
116
117         // nullable
118
for (int i = 0; i < nullable.length; i++)
119         {
120             Column column = DataSetUtils.getColumn(nullable[i], columns);
121             assertEquals(nullable[i], Column.NULLABLE, column.getNullable());
122         }
123     }
124
125 // public void testUnsupportedColumnDataType() throws Exception
126
// {
127
// fail("Mock this test!");
128
// String tableName = "EMPTY_MULTITYPE_TABLE";
129
// String[] expectedNames = {
130
// "VARCHAR_COL",
131
// "NUMERIC_COL",
132
// "TIMESTAMP_COL",
133
// };
134
//
135
// ITableMetaData metaData = createDataSet().getTableMetaData(tableName);
136
// Column[] columns = metaData.getColumns();
137
//
138
// assertEquals("column count", expectedNames.length, columns.length);
139
//
140
// for (int i = 0; i < columns.length; i++)
141
// {
142
// Column column = columns[i];
143
// assertEquals("name", expectedNames[i], column.getColumnName());
144
// }
145
// }
146

147     public void testColumnDataType() throws Exception JavaDoc
148     {
149         String JavaDoc tableName = "EMPTY_MULTITYPE_TABLE";
150         String JavaDoc[] expectedNames = {
151             "VARCHAR_COL",
152             "NUMERIC_COL",
153             "TIMESTAMP_COL",
154             "VARBINARY_COL",
155         };
156         DataType[] expectedTypes = {
157             DataType.VARCHAR,
158             DataType.NUMERIC,
159             DataType.TIMESTAMP,
160             DataType.VARBINARY,
161         };
162
163         ITableMetaData metaData = createDataSet().getTableMetaData(tableName);
164         Column[] columns = metaData.getColumns();
165
166         assertEquals("expected columns", expectedNames.length, expectedTypes.length);
167         assertEquals("column count", expectedNames.length, columns.length);
168
169         for (int i = 0; i < columns.length; i++)
170         {
171             Column column = columns[i];
172             assertEquals("name", expectedNames[i], column.getColumnName());
173             assertEquals("datatype", expectedTypes[i], column.getDataType());
174         }
175     }
176 }
177
178
179
180
181
182
183
184
Popular Tags