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