View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, 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.search;
22  
23  import java.io.File;
24  import java.sql.Connection;
25  import java.util.HashSet;
26  import java.util.TreeSet;
27  
28  import junit.framework.TestCase;
29  import junitx.framework.ArrayAssert;
30  
31  import org.dbunit.DdlExecutor;
32  import org.dbunit.HypersonicEnvironment;
33  import org.dbunit.database.DatabaseConnection;
34  import org.dbunit.database.IDatabaseConnection;
35  import org.dbunit.database.PrimaryKeyFilter.PkTableMap;
36  import org.dbunit.dataset.IDataSet;
37  import org.dbunit.dataset.NoSuchTableException;
38  import org.dbunit.testutil.TestUtils;
39  import org.dbunit.util.search.SearchException;
40  
41  /**
42   * @author Felipe Leme (dbunit@felipeal.net)
43   * @author Last changed by: $Author$
44   * @version $Revision$ $Date$
45   * @since Aug 28, 2005
46   */
47  public class TablesDependencyHelperTest extends TestCase {
48  
49    
50      private Connection jdbcConnection;
51    
52      private IDatabaseConnection connection;
53    
54      protected void setUp( String sqlFile ) throws Exception {
55      	this.setUp(new String[]{sqlFile});
56      }
57  
58      protected void setUp( String[] sqlFileList ) throws Exception {
59          this.jdbcConnection = HypersonicEnvironment.createJdbcConnection("mem:tempdb");
60          for (int i = 0; i < sqlFileList.length; i++) {
61          	File sql = TestUtils.getFile("sql/" + sqlFileList[i]);
62              DdlExecutor.executeDdlFile(sql, this.jdbcConnection);
63  		}
64          this.connection = new DatabaseConnection(jdbcConnection);
65      }
66  
67      protected void tearDown() throws Exception {
68          HypersonicEnvironment.shutdown(this.jdbcConnection);
69          this.jdbcConnection.close();
70  //      HypersonicEnvironment.deleteFiles( "tempdb" );
71      }   
72  
73  
74      public void testGetDependentTablesFromOneTable() throws Exception {    
75          setUp( ImportNodesFilterSearchCallbackTest.SQL_FILE );    
76          String[][] allInput = ImportNodesFilterSearchCallbackTest.SINGLE_INPUT;
77          String[][] allExpectedOutput = ImportNodesFilterSearchCallbackTest.SINGLE_OUTPUT;
78          for (int i = 0; i < allInput.length; i++) {
79              String[] input = allInput[i];
80              String[] expectedOutput = allExpectedOutput[i];
81              String[] actualOutput = TablesDependencyHelper.getDependentTables( this.connection, input[0]);
82              ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutput );
83          }           
84      }
85  
86      public void testGetDependentTablesFromOneTable_RootTableDoesNotExist() throws Exception {    
87          setUp( ImportNodesFilterSearchCallbackTest.SQL_FILE );    
88  
89          try
90          {
91              TablesDependencyHelper.getDependentTables( this.connection, "XXXXXX_TABLE_NON_EXISTING");
92              fail("Should not be able to get the dependent tables for a non existing input table");
93          }
94          catch(SearchException expected)
95          {
96              Throwable cause = expected.getCause();
97              assertTrue(cause instanceof NoSuchTableException);
98              String expectedMessage = "The table 'XXXXXX_TABLE_NON_EXISTING' does not exist in schema 'null'";
99              assertEquals(expectedMessage, cause.getMessage());
100         }
101     }
102 
103     public void testGetDependentTablesFromManyTables() throws Exception {    
104         setUp( ImportNodesFilterSearchCallbackTest.SQL_FILE );    
105         String[][] allInput = ImportNodesFilterSearchCallbackTest.COMPOUND_INPUT;
106         String[][] allExpectedOutput = ImportNodesFilterSearchCallbackTest.COMPOUND_OUTPUT;
107         for (int i = 0; i < allInput.length; i++) {
108             String[] input = allInput[i];
109             String[] expectedOutput = allExpectedOutput[i];
110             String[] actualOutput = TablesDependencyHelper.getDependentTables( this.connection, input);
111             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutput );
112         }           
113     }
114 
115     public void testGetAllDependentTablesFromOneTable() throws Exception {    
116         setUp( ImportAndExportKeysSearchCallbackOwnFileTest.SQL_FILE );    
117         String[][] allInput = ImportAndExportKeysSearchCallbackOwnFileTest.SINGLE_INPUT;
118         String[][] allExpectedOutput = ImportAndExportKeysSearchCallbackOwnFileTest.SINGLE_OUTPUT;
119         for (int i = 0; i < allInput.length; i++) {
120             String[] input = allInput[i];
121             String[] expectedOutput = allExpectedOutput[i];
122             String[] actualOutput = TablesDependencyHelper.getAllDependentTables( this.connection, input[0]);
123             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutput );
124         }           
125     }
126 
127     public void testGetAllDependentTablesFromManyTables() throws Exception {    
128         setUp( ImportAndExportKeysSearchCallbackOwnFileTest.SQL_FILE );    
129         String[][] allInput = ImportAndExportKeysSearchCallbackOwnFileTest.COMPOUND_INPUT;
130         String[][] allExpectedOutput = ImportAndExportKeysSearchCallbackOwnFileTest.COMPOUND_OUTPUT;
131         for (int i = 0; i < allInput.length; i++) {
132             String[] input = allInput[i];
133             String[] expectedOutput = allExpectedOutput[i];
134             String[] actualOutput = TablesDependencyHelper.getAllDependentTables( this.connection, input);
135             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutput );
136         }           
137     }
138 
139     public void testGetAllDatasetFromOneTable() throws Exception {    
140         setUp( ImportAndExportKeysSearchCallbackOwnFileTest.SQL_FILE );    
141         String[][] allInput = ImportAndExportKeysSearchCallbackOwnFileTest.SINGLE_INPUT;
142         String[][] allExpectedOutput = ImportAndExportKeysSearchCallbackOwnFileTest.SINGLE_OUTPUT;
143         for (int i = 0; i < allInput.length; i++) {
144             String[] input = allInput[i];
145             String[] expectedOutput = allExpectedOutput[i];
146             IDataSet actualOutput = TablesDependencyHelper.getAllDataset( this.connection, input[0], new HashSet());
147             String[] actualOutputTables = actualOutput.getTableNames();
148             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutputTables );
149         }           
150     }
151 
152     public void testGetAllDatasetFromOneTable_SeparateSchema() throws Exception {
153         setUp( new String[] {
154         		"hypersonic_switch_schema.sql", 
155         		ImportAndExportKeysSearchCallbackOwnFileTest.SQL_FILE
156         		} );
157         
158         String[][] allInputWithSchema = ImportAndExportKeysSearchCallbackOwnFileTest.getSingleInputWithSchema("TEST_SCHEMA");
159         String[][] allExpectedOutput = ImportAndExportKeysSearchCallbackOwnFileTest.SINGLE_OUTPUT;
160         for (int i = 0; i < allInputWithSchema.length; i++) {
161             String[] input = allInputWithSchema[i];
162             String[] expectedOutput = allExpectedOutput[i];
163             IDataSet actualOutput = TablesDependencyHelper.getAllDataset( this.connection, input[0], new HashSet());
164             String[] actualOutputTables = actualOutput.getTableNames();
165             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutputTables );
166         }
167     }
168 
169     /**
170      * Ensure the order is not lost on the way because of the conversion between Map and Array
171      * @throws Exception
172      */
173     public void testGetDatasetFromManyTables() throws Exception 
174     {    
175         setUp( ImportNodesFilterSearchCallbackTest.SQL_FILE );    
176         String[][] allInput = ImportNodesFilterSearchCallbackTest.COMPOUND_INPUT;
177         String[][] allExpectedOutput = ImportNodesFilterSearchCallbackTest.COMPOUND_OUTPUT;
178         for (int i = 0; i < allInput.length; i++) {
179             String[] input = allInput[i];
180             PkTableMap inputMap = new PkTableMap();
181             for (int j = 0; j < input.length; j++) {
182                 inputMap.put(input[j], new TreeSet());
183             }
184 
185             String[] expectedOutput = allExpectedOutput[i];
186             IDataSet actualOutput = TablesDependencyHelper.getDataset( this.connection, inputMap);
187             String[] actualOutputArray = actualOutput.getTableNames();
188             ArrayAssert.assertEquals( "output didn't match for i=" + i, expectedOutput, actualOutputArray );
189         }           
190     }
191 
192 
193     // TODO ImportAndExportKeysSearchCallbackOwnFileTest
194 
195 }