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  package org.dbunit.database;
22  
23  import java.sql.Connection;
24  import java.util.Arrays;
25  
26  import org.dbunit.DdlExecutor;
27  import org.dbunit.H2Environment;
28  import org.dbunit.HypersonicEnvironment;
29  import org.dbunit.dataset.FilteredDataSet;
30  import org.dbunit.dataset.IDataSet;
31  import org.dbunit.dataset.filter.ITableFilter;
32  import org.dbunit.testutil.TestUtils;
33  
34  import junit.framework.TestCase;
35  
36  /**
37   * @author Manuel Laflamme
38   * @since May 8, 2004
39   * @version $Revision$
40   */
41  public class DatabaseSequenceFilterTest extends TestCase
42  {
43      Connection _jdbcConnection;
44  
45      public DatabaseSequenceFilterTest(final String s)
46      {
47          super(s);
48      }
49  
50      @Override
51      protected void setUp() throws Exception
52      {
53          super.setUp();
54  
55          _jdbcConnection = HypersonicEnvironment.createJdbcConnection("tempdb");
56      }
57  
58      @Override
59      protected void tearDown() throws Exception
60      {
61          super.tearDown();
62  
63          HypersonicEnvironment.shutdown(_jdbcConnection);
64          _jdbcConnection.close();
65  
66          HypersonicEnvironment.deleteFiles("tempdb");
67      }
68  
69      public void testGetTableNames() throws Exception
70      {
71          final String[] expectedNoFilter =
72                  {"A", "B", "C", "D", "E", "F", "G", "H",};
73          final String[] expectedFiltered =
74                  {"D", "A", "F", "C", "G", "E", "H", "B",};
75  
76          DdlExecutor.executeDdlFile(
77                  TestUtils.getFile("sql/hypersonic_fk.sql"), _jdbcConnection);
78          final IDatabaseConnection connection =
79                  new DatabaseConnection(_jdbcConnection);
80  
81          final IDataSet databaseDataset = connection.createDataSet();
82          final String[] actualNoFilter = databaseDataset.getTableNames();
83          assertEquals("no filter", Arrays.asList(expectedNoFilter),
84                  Arrays.asList(actualNoFilter));
85  
86          final ITableFilter filter = new DatabaseSequenceFilter(connection);
87          final IDataSet filteredDataSet =
88                  new FilteredDataSet(filter, databaseDataset);
89          final String[] actualFiltered = filteredDataSet.getTableNames();
90          assertEquals("filtered", Arrays.asList(expectedFiltered),
91                  Arrays.asList(actualFiltered));
92      }
93  
94      public void testGetTableNamesCyclic() throws Exception
95      {
96          final String[] expectedNoFilter = {"A", "B", "C", "D", "E",};
97  
98          DdlExecutor.executeDdlFile(
99                  TestUtils.getFile("sql/hypersonic_cyclic.sql"),
100                 _jdbcConnection);
101         final IDatabaseConnection connection =
102                 new DatabaseConnection(_jdbcConnection);
103 
104         final IDataSet databaseDataset = connection.createDataSet();
105         final String[] actualNoFilter = databaseDataset.getTableNames();
106         assertEquals("no filter", Arrays.asList(expectedNoFilter),
107                 Arrays.asList(actualNoFilter));
108 
109         boolean gotCyclicTablesDependencyException = false;
110 
111         try
112         {
113             final ITableFilter filter = new DatabaseSequenceFilter(connection);
114             final IDataSet filteredDataSet =
115                     new FilteredDataSet(filter, databaseDataset);
116             filteredDataSet.getTableNames();
117             fail("Should not be here!");
118         } catch (final CyclicTablesDependencyException expected)
119         {
120             gotCyclicTablesDependencyException = true;
121         }
122         assertTrue("Expected CyclicTablesDependencyException was not raised",
123                 gotCyclicTablesDependencyException);
124     }
125 
126     public void testCaseSensitiveTableNames() throws Exception
127     {
128         final String[] expectedNoFilter =
129                 {"MixedCaseTable", "UPPER_CASE_TABLE"};
130         final String[] expectedFiltered =
131                 {"MixedCaseTable", "UPPER_CASE_TABLE"};
132 
133         DdlExecutor.executeDdlFile(
134                 TestUtils.getFile("sql/hypersonic_case_sensitive_test.sql"),
135                 _jdbcConnection);
136         final IDatabaseConnection connection =
137                 new DatabaseConnection(_jdbcConnection);
138 
139         connection.getConfig().setProperty(
140                 DatabaseConfig.FEATURE_CASE_SENSITIVE_TABLE_NAMES,
141                 Boolean.TRUE);
142 
143         final IDataSet databaseDataset = connection.createDataSet();
144         final String[] actualNoFilter = databaseDataset.getTableNames();
145         assertEquals("no filter", Arrays.asList(expectedNoFilter),
146                 Arrays.asList(actualNoFilter));
147 
148         final ITableFilter filter = new DatabaseSequenceFilter(connection);
149         final IDataSet filteredDataSet =
150                 new FilteredDataSet(filter, databaseDataset);
151         final String[] actualFiltered = filteredDataSet.getTableNames();
152         assertEquals("filtered", Arrays.asList(expectedFiltered),
153                 Arrays.asList(actualFiltered));
154     }
155 
156     /**
157      * Note that this test uses the H2 database because we could not find out
158      * how to create 2 separate schemas in the hsqldb in memory DB.
159      *
160      * @throws Exception
161      */
162     public void testMultiSchemaFks() throws Exception
163     {
164         final Connection jdbcConnection =
165                 H2Environment.createJdbcConnection("test");
166         DdlExecutor.executeDdlFile(
167                 TestUtils.getFile("sql/h2_multischema_fk_test.sql"),
168                 jdbcConnection);
169         final IDatabaseConnection connection =
170                 new DatabaseConnection(jdbcConnection);
171         connection.getConfig().setProperty(
172                 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, Boolean.TRUE);
173 
174         final IDataSet databaseDataset = connection.createDataSet();
175         final ITableFilter filter = new DatabaseSequenceFilter(connection);
176         final IDataSet filteredDataSet =
177                 new FilteredDataSet(filter, databaseDataset);
178 
179         final String[] actualNoFilter = databaseDataset.getTableNames();
180         assertEquals(2, actualNoFilter.length);
181         assertEquals("A.FOO", actualNoFilter[0]);
182         assertEquals("B.BAR", actualNoFilter[1]);
183 
184         final String[] actualFiltered = filteredDataSet.getTableNames();
185         assertEquals(2, actualFiltered.length);
186         assertEquals("A.FOO", actualFiltered[0]);
187         assertEquals("B.BAR", actualFiltered[1]);
188     }
189 }