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.database;
23  
24  import org.dbunit.DatabaseEnvironment;
25  import org.dbunit.dataset.AbstractDataSetTest;
26  import org.dbunit.dataset.Column;
27  import org.dbunit.dataset.DataSetException;
28  import org.dbunit.dataset.DefaultTableMetaData;
29  import org.dbunit.dataset.IDataSet;
30  import org.dbunit.dataset.ITableMetaData;
31  import org.dbunit.dataset.NoSuchTableException;
32  import org.dbunit.dataset.datatype.DataType;
33  import org.dbunit.dataset.filter.DefaultColumnFilter;
34  import org.dbunit.dataset.filter.ITableFilterSimple;
35  import org.dbunit.util.QualifiedTableName;
36  
37  
38  /**
39   * @author Manuel Laflamme
40   * @version $Revision$
41   * @since Feb 18, 2002
42   */
43  public class DatabaseDataSetIT extends AbstractDataSetTest
44  {
45      private IDatabaseConnection _connection;
46  
47      public DatabaseDataSetIT(String s)
48      {
49          super(s);
50      }
51  
52      ////////////////////////////////////////////////////////////////////////////
53      // TestCase class
54  
55      protected void setUp() throws Exception
56      {
57          super.setUp();
58  
59          _connection = DatabaseEnvironment.getInstance().getConnection();
60      }
61  
62      protected void tearDown() throws Exception
63      {
64          super.tearDown();
65  
66          _connection = null;
67      }
68  
69      ////////////////////////////////////////////////////////////////////////////
70      // AbstractDataSetTest class
71  
72  
73      protected String convertString(String str) throws Exception
74      {
75          return DatabaseEnvironment.getInstance().convertString(str);
76      }
77  
78      protected IDataSet createDataSet() throws Exception
79      {
80          return _connection.createDataSet();
81      }
82  
83      protected String[] getExpectedNames() throws Exception
84      {
85          return _connection.createDataSet().getTableNames();
86      }
87  
88      protected IDataSet createDuplicateDataSet() throws Exception
89      {
90          throw new UnsupportedOperationException();
91      }
92      
93      protected IDataSet createMultipleCaseDuplicateDataSet() throws Exception 
94      {
95          throw new UnsupportedOperationException();
96      }
97      
98  
99      ////////////////////////////////////////////////////////////////////////////
100     // Test methods
101 
102     public void testGetSelectStatement() throws Exception
103     {
104         String schemaName = "schema";
105         String tableName = "table";
106         Column[] columns = new Column[]{
107             new Column("c1", DataType.UNKNOWN),
108             new Column("c2", DataType.UNKNOWN),
109             new Column("c3", DataType.UNKNOWN),
110         };
111         String expected = "select c1, c2, c3 from schema.table";
112 
113         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
114         String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null);
115         assertEquals("select statement", expected, sql);
116     }
117 
118     public void testGetSelectStatementWithEscapedNames() throws Exception
119     {
120         String schemaName = "schema";
121         String tableName = "table";
122         Column[] columns = new Column[]{
123             new Column("c1", DataType.UNKNOWN),
124             new Column("c2", DataType.UNKNOWN),
125             new Column("c3", DataType.UNKNOWN),
126         };
127         String expected = "select 'c1', 'c2', 'c3' from 'schema'.'table'";
128 
129         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns);
130         String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, "'?'");
131         assertEquals("select statement", expected, sql);
132     }
133     
134     public void testGetSelectStatementWithEscapedNamesAndOrderBy() throws Exception
135     {
136         String schemaName = "schema";
137         String tableName = "table";
138         Column[] columns = new Column[]{
139             new Column("c1", DataType.UNKNOWN),
140             new Column("c2", DataType.UNKNOWN),
141             new Column("c3", DataType.UNKNOWN),
142         };
143         String expected = "select 'c1', 'c2', 'c3' from 'schema'.'table' order by 'c1', 'c2'";
144         
145         String[] primaryKeys = { "c1", "c2" };
146 
147         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns, primaryKeys);
148         String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, "'?'");
149         assertEquals("select statement", expected, sql);
150     }
151 
152     public void testGetSelectStatementWithPrimaryKeys() throws Exception
153     {
154         String schemaName = "schema";
155         String tableName = "table";
156         Column[] columns = new Column[]{
157             new Column("c1", DataType.UNKNOWN),
158             new Column("c2", DataType.UNKNOWN),
159             new Column("c3", DataType.UNKNOWN),
160         };
161         String expected = "select c1, c2, c3 from schema.table order by c1, c2, c3";
162 
163         ITableMetaData metaData = new DefaultTableMetaData(tableName, columns, columns);
164         String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null);
165         assertEquals("select statement", expected, sql);
166     }
167 
168     public void testGetQualifiedTableNames() throws Exception
169     {
170         String[] expectedNames = getExpectedNames();
171 
172         IDatabaseConnection connection = new DatabaseConnection(
173                 _connection.getConnection(), _connection.getSchema());
174         connection.getConfig().setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
175 
176         IDataSet dataSet = connection.createDataSet();
177         String[] actualNames = dataSet.getTableNames();
178 
179         assertEquals("name count", expectedNames.length, actualNames.length);
180         for (int i = 0; i < actualNames.length; i++)
181         {
182             String expected = new QualifiedTableName(
183             		expectedNames[i], _connection.getSchema()).getQualifiedName();
184             String actual = actualNames[i];
185             assertEquals("name", expected, actual);
186         }
187     }
188 
189     public void testGetColumnsAndQualifiedNamesEnabled() throws Exception
190     {
191         String tableName = new QualifiedTableName(
192                 "TEST_TABLE", _connection.getSchema()).getQualifiedName();
193         String[] expected = {"COLUMN0", "COLUMN1", "COLUMN2", "COLUMN3"};
194 
195         IDatabaseConnection connection = new DatabaseConnection(
196                 _connection.getConnection(), _connection.getSchema());
197         connection.getConfig().setFeature(
198                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
199 
200         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
201         Column[] columns = metaData.getColumns();
202 
203         assertEquals("column count", expected.length, columns.length);
204         for (int i = 0; i < columns.length; i++)
205         {
206             assertEquals("column name", convertString(expected[i]), columns[i].getColumnName());
207         }
208     }
209 
210     public void testGetPrimaryKeysAndQualifiedNamesEnabled() throws Exception
211     {
212         String tableName = new QualifiedTableName(
213                 "PK_TABLE", _connection.getSchema()).getQualifiedName();
214         String[] expected = {"PK0", "PK1", "PK2"};
215 
216         IDatabaseConnection connection = new DatabaseConnection(
217                 _connection.getConnection(), _connection.getSchema());
218         connection.getConfig().setFeature(
219                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
220 
221         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
222         Column[] columns = metaData.getPrimaryKeys();
223 
224         assertEquals("column count", expected.length, columns.length);
225         for (int i = 0; i < columns.length; i++)
226         {
227             assertEquals("column name", convertString(expected[i]), columns[i].getColumnName());
228         }
229     }
230 
231     public void testGetPrimaryKeysWithColumnFilters() throws Exception
232     {
233       
234       // TODO (felipeal): I don't know if PK_TABLE is a standard JDBC name or if
235       // it's HSQLDB specific. Anyway, now that HSQLDB's schema is set on property,
236       // we cannot add it as prefix here....
237       String tableName = "PK_TABLE";
238 //        String tableName = DataSetUtils.getQualifiedName(
239 //                _connection.getSchema(), "PK_TABLE");
240        
241         String[] expected = {"PK0", "PK2"};
242 
243         DefaultColumnFilter filter = new DefaultColumnFilter();
244         filter.includeColumn("PK0");
245         filter.includeColumn("PK2");
246 
247         IDatabaseConnection connection = new DatabaseConnection(
248                 _connection.getConnection(), _connection.getSchema());
249         connection.getConfig().setProperty(
250                 DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, filter);
251 
252         ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName);
253         Column[] columns = metaData.getPrimaryKeys();
254 
255         assertEquals("column count", expected.length, columns.length);
256         for (int i = 0; i < columns.length; i++)
257         {
258             assertEquals("column name", convertString(expected[i]), columns[i].getColumnName());
259         }
260     }
261 
262 //    public void testGetTableNamesAndCaseSensitive() throws Exception
263 //    {
264 //        DatabaseMetaData metaData = _connection.getConnection().getMetaData();
265 //        metaData.
266 //    }
267 
268     public void testCreateDuplicateDataSet() throws Exception 
269     {
270         // Cannot test! Unsupported feature.
271     }
272 
273     public void testCreateMultipleCaseDuplicateDataSet() throws Exception 
274     {
275         // Cannot test! Unsupported feature.
276     }
277 
278     public void testGetTableThatIsFiltered() throws Exception
279     {
280         final String existingTableToFilter = convertString("TEST_TABLE");
281         ITableFilterSimple tableFilter = new ITableFilterSimple(){
282             public boolean accept(String tableName) throws DataSetException {
283                 if(tableName.equals(existingTableToFilter))
284                     return false;
285                 return true;
286             }
287         };
288         IDataSet dataSet = new DatabaseDataSet(_connection, false, tableFilter);
289         try
290         {
291             dataSet.getTable(existingTableToFilter);
292             fail("Should not be able to retrieve table from dataset that has not been loaded - expected an exception");
293         }
294         catch(NoSuchTableException expected)
295         {
296             assertEquals(existingTableToFilter, expected.getMessage());
297         }
298     }
299 
300 }
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312