KickJava   Java API By Example, From Geeks To Geeks.

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


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.DatabaseEnvironment;
25 import org.dbunit.dataset.*;
26 import org.dbunit.dataset.filter.DefaultColumnFilter;
27 import org.dbunit.dataset.datatype.DataType;
28
29 /**
30  * @author Manuel Laflamme
31  * @version $Revision: 1.25 $
32  * @since Feb 18, 2002
33  */

34 public class DatabaseDataSetTest extends AbstractDataSetTest
35 {
36     private IDatabaseConnection _connection;
37
38     public DatabaseDataSetTest(String JavaDoc s)
39     {
40         super(s);
41     }
42
43     ////////////////////////////////////////////////////////////////////////////
44
// TestCase class
45

46     protected void setUp() throws Exception JavaDoc
47     {
48         super.setUp();
49
50         _connection = DatabaseEnvironment.getInstance().getConnection();
51     }
52
53     protected void tearDown() throws Exception JavaDoc
54     {
55         super.tearDown();
56
57         _connection = null;
58     }
59
60     ////////////////////////////////////////////////////////////////////////////
61
// AbstractDataSetTest class
62

63     protected IDataSet createDataSet() throws Exception JavaDoc
64     {
65         return _connection.createDataSet();
66     }
67
68     protected String JavaDoc[] getExpectedNames() throws Exception JavaDoc
69     {
70         return _connection.createDataSet().getTableNames();
71     }
72
73     protected IDataSet createDuplicateDataSet() throws Exception JavaDoc
74     {
75         throw new UnsupportedOperationException JavaDoc();
76     }
77
78     ////////////////////////////////////////////////////////////////////////////
79
// Test methods
80

81     public void testGetSelectStatement() throws Exception JavaDoc
82     {
83         String JavaDoc schemaName = "schema";
84         String JavaDoc tableName = "table";
85         Column[] columns = new Column[]{
86             new Column("c1", DataType.UNKNOWN),
87             new Column("c2", DataType.UNKNOWN),
88             new Column("c3", DataType.UNKNOWN),
89         };
90         String JavaDoc expected = "select c1, c2, c3 from schema.table";
91
92         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
93         String JavaDoc sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null);
94         assertEquals("select statement", expected, sql);
95     }
96
97     public void testGetSelectStatementWithEscapedNames() throws Exception JavaDoc
98     {
99         String JavaDoc schemaName = "schema";
100         String JavaDoc tableName = "table";
101         Column[] columns = new Column[]{
102             new Column("c1", DataType.UNKNOWN),
103             new Column("c2", DataType.UNKNOWN),
104             new Column("c3", DataType.UNKNOWN),
105         };
106         String JavaDoc expected = "select 'c1', 'c2', 'c3' from 'schema'.'table'";
107
108         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
109         String JavaDoc sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, "'?'");
110         assertEquals("select statement", expected, sql);
111     }
112
113     public void testGetSelectStatementWithPrimaryKeys() throws Exception JavaDoc
114     {
115         String JavaDoc schemaName = "schema";
116         String JavaDoc tableName = "table";
117         Column[] columns = new Column[]{
118             new Column("c1", DataType.UNKNOWN),
119             new Column("c2", DataType.UNKNOWN),
120             new Column("c3", DataType.UNKNOWN),
121         };
122         String JavaDoc expected = "select c1, c2, c3 from schema.table order by c1, c2, c3";
123
124         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns, columns);
125         String JavaDoc sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null);
126         assertEquals("select statement", expected, sql);
127     }
128
129     public void testGetQualifiedTableNames() throws Exception JavaDoc
130     {
131         String JavaDoc[] expectedNames = getExpectedNames();
132
133         IDatabaseConnection connection = new DatabaseConnection(
134                 _connection.getConnection(), _connection.getSchema());
135         connection.getConfig().setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
136
137         IDataSet dataSet = connection.createDataSet();
138         String JavaDoc[] actualNames = dataSet.getTableNames();
139
140         assertEquals("name count", expectedNames.length, actualNames.length);
141         for (int i = 0; i < actualNames.length; i++)
142         {
143             String JavaDoc expected = DataSetUtils.getQualifiedName(
144                     _connection.getSchema(), expectedNames[i]);
145             String JavaDoc actual = actualNames[i];
146             assertEquals("name", expected, actual);
147         }
148     }
149
150     public void testGetColumnsAndQualifiedNamesEnabled() throws Exception JavaDoc
151     {
152         String JavaDoc tableName = DataSetUtils.getQualifiedName(
153                 _connection.getSchema(), "TEST_TABLE");
154         String JavaDoc[] expected = {"COLUMN0", "COLUMN1", "COLUMN2", "COLUMN3"};
155
156         IDatabaseConnection connection = new DatabaseConnection(
157                 _connection.getConnection(), _connection.getSchema());
158         connection.getConfig().setFeature(
159                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
160
161         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
162         Column[] columns = metaData.getColumns();
163
164         assertEquals("column count", expected.length, columns.length);
165         for (int i = 0; i < columns.length; i++)
166         {
167             assertEquals("column name", expected[i], columns[i].getColumnName());
168         }
169     }
170
171     public void testGetPrimaryKeysAndQualifiedNamesEnabled() throws Exception JavaDoc
172     {
173         String JavaDoc tableName = DataSetUtils.getQualifiedName(
174                 _connection.getSchema(), "PK_TABLE");
175         String JavaDoc[] expected = {"PK0", "PK1", "PK2"};
176
177         IDatabaseConnection connection = new DatabaseConnection(
178                 _connection.getConnection(), _connection.getSchema());
179         connection.getConfig().setFeature(
180                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
181
182         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
183         Column[] columns = metaData.getPrimaryKeys();
184
185         assertEquals("column count", expected.length, columns.length);
186         for (int i = 0; i < columns.length; i++)
187         {
188             assertEquals("column name", expected[i], columns[i].getColumnName());
189         }
190     }
191
192     public void testGetPrimaryKeysWithColumnFilters() throws Exception JavaDoc
193     {
194         String JavaDoc tableName = DataSetUtils.getQualifiedName(
195                 _connection.getSchema(), "PK_TABLE");
196         String JavaDoc[] expected = {"PK0", "PK2"};
197
198         DefaultColumnFilter filter = new DefaultColumnFilter();
199         filter.includeColumn("PK0");
200         filter.includeColumn("PK2");
201
202         IDatabaseConnection connection = new DatabaseConnection(
203                 _connection.getConnection(), _connection.getSchema());
204         connection.getConfig().setProperty(
205                 DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, filter);
206
207         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
208         Column[] columns = metaData.getPrimaryKeys();
209
210         assertEquals("column count", expected.length, columns.length);
211         for (int i = 0; i < columns.length; i++)
212         {
213             assertEquals("column name", expected[i], columns[i].getColumnName());
214         }
215     }
216
217 // public void testGetTableNamesAndCaseSensitive() throws Exception
218
// {
219
// DatabaseMetaData metaData = _connection.getConnection().getMetaData();
220
// metaData.
221
// }
222

223     public void testGetDuplicateTable() throws Exception JavaDoc
224     {
225         // Cannot test! Unsupported feature.
226
}
227
228     public void testGetDuplicateTableMetaData() throws Exception JavaDoc
229     {
230         // Cannot test! Unsupported feature.
231
}
232
233     public void testGetDuplicateTableNames() throws Exception JavaDoc
234     {
235         // Cannot test! Unsupported feature.
236
}
237
238     public void testGetDuplicateTables() throws Exception JavaDoc
239     {
240         // Cannot test! Unsupported feature.
241
}
242
243     public void testGetCaseInsensitiveDuplicateTable() throws Exception JavaDoc
244     {
245         // Cannot test! Unsupported feature.
246
}
247
248     public void testGetCaseInsensitiveDuplicateTableMetaData() throws Exception JavaDoc
249     {
250         // Cannot test! Unsupported feature.
251
}
252
253     public void testReverseIteratorAndDuplicateTable() throws Exception JavaDoc
254     {
255         // Cannot test! Unsupported feature.
256
}
257
258     public void testIteratorAndDuplicateTable() throws Exception JavaDoc
259     {
260         // Cannot test! Unsupported feature.
261
}
262 }
263
264
265
266
267
268
269
270
271
272
273
274
Popular Tags