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