1 /***
2 Copyright (C) 2005 The Java Community
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 2 of the License, or (at your option) any later
7 version.
8
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 Place, Suite 330, Boston, MA 02111-1307 USA.
16 */
17 package org.bejug.javacareers.jobs.common;
18
19 import java.sql.SQLException;
20
21 import org.dbunit.Assertion;
22 import org.dbunit.DatabaseUnitException;
23 import org.dbunit.database.IDatabaseConnection;
24 import org.dbunit.dataset.DataSetException;
25 import org.dbunit.dataset.DefaultDataSet;
26 import org.dbunit.dataset.DefaultTable;
27 import org.dbunit.dataset.IDataSet;
28 import org.dbunit.dataset.ITable;
29 import org.dbunit.dataset.ReplacementDataSet;
30 import org.dbunit.dataset.SortedTable;
31 import org.dbunit.dataset.filter.DefaultColumnFilter;
32 import org.dbunit.operation.DatabaseOperation;
33
34 /***
35 *
36 * @author Sven Schauwvliege (Last modified by $Author: shally $)
37 * @version $Revision: 1.8 $ - $Date: 2005/12/08 14:53:46 $
38 */
39
40
41 public abstract class AbstractSpringContextDBUnitTests extends
42 AbstractSpringContextTests{
43
44
45
46 /***
47 * the databaseconnection to use by DBUnit.
48 * Set through field-injection.
49 */
50 protected IDatabaseConnection iConnection = null;
51
52 /***
53 * the dataset to use by DBUnit based on the tables needed for this test.
54 * set by the init method.
55 */
56 private IDataSet testDataSet = null;
57
58
59 /***
60 * the dataset to use by DBUnit.
61 * Set through field-injection.
62 */
63 protected IDataSet dataSet = null;
64
65
66 /***
67 * @return the wanted databaseconnection to use by DBUnit.
68 */
69 private IDatabaseConnection getConnection() {
70 return iConnection;
71 }
72
73 /***
74 * @return the wanted dataset to use by DBUnit.
75 */
76 protected IDataSet getDataSet() {
77 return dataSet;
78 }
79
80 /***
81 * default constructor.
82 * Is necessary for the field-injection in the testcases.
83 * The flag populateProtectedVariables should be set to true to support the
84 * field-injection.
85 */
86 public AbstractSpringContextDBUnitTests() {
87 super();
88 }
89
90 /***
91 * sub classes should implement the init method with the tablenames needed for the test.
92 * {@inheritDoc}
93 */
94 public abstract void onSetUpInTransaction() throws Exception;
95
96 /***
97 * method to setup database with dbunit.
98 * @param tableNames the tablenames of the needed tables.
99 */
100 protected void init(String[] tableNames) {
101 ITable[] tables =new DefaultTable[tableNames.length];
102 for (int i = 0; i < tableNames.length; i++) {
103 ITable table = null;
104 try {
105 table = getDataSet().getTable(tableNames[i]);
106 } catch (DataSetException e1) {
107 assertTrue("table not found " + tableNames[i], false);
108 e1.printStackTrace();
109 }
110 tables[i] = table;
111 }
112
113 ReplacementDataSet dataset = new ReplacementDataSet(new DefaultDataSet(tables));
114 dataset.addReplacementObject("[NULL]", null);
115
116
117 try {
118 DatabaseOperation.DELETE.execute(getConnection(), getDataSet());
119 DatabaseOperation.INSERT.execute(getConnection(), dataset);
120 } catch (DatabaseUnitException e) {
121 e.printStackTrace();
122 assertTrue("error in database setup :" + e, false);
123 } catch (SQLException e) {
124 e.printStackTrace();
125 assertTrue("error in database setup :" + e, false);
126 }
127 testDataSet = dataset;
128
129
130
131
132
133
134
135
136 }
137
138
139 /***
140 * method to test if the db is as expected.
141 * @param tableNames names of the tables used in this test
142 * @param ignore the columns to ignore
143 */
144 public void assertDBAsExpected( String[] tableNames, String[] ignore) {
145
146 IDataSet actualDataSet = null;
147 try {
148 actualDataSet = getConnection().createDataSet(tableNames);
149 } catch (SQLException e1) {
150 e1.printStackTrace();
151 assertTrue("SQLException", false);
152 }
153
154 IDataSet expectedDataSet = getTestDataSet();
155
156 for (int i = 0; i < tableNames.length; i++) {
157
158 try {
159 ITable expected = expectedDataSet.getTable(tableNames[i]);
160 ITable actual = actualDataSet.getTable(tableNames[i]);
161
162
163 ITable filteredExpected = DefaultColumnFilter.excludedColumnsTable(
164 expected, ignore);
165
166 ITable filteredActual = DefaultColumnFilter.excludedColumnsTable(
167 actual, ignore);
168
169
170 filteredExpected = new SortedTable(filteredExpected);
171 filteredActual = new SortedTable(filteredActual,filteredExpected.getTableMetaData());
172
173
174 Assertion.assertEquals(filteredExpected, filteredActual);
175 } catch (DataSetException e) {
176 e.printStackTrace();
177 assertTrue("DataSetException", false);
178 } catch (DatabaseUnitException e) {
179 e.printStackTrace();
180 assertTrue("DatabaseUnitException", false);
181 }
182 }
183 }
184
185 /***
186 * deletes al tables.
187 * @param tableNames of the tables to be deleted
188 * @throws Exception if an error
189 */
190 public void emptyTable(String[] tableNames) throws Exception{
191 for (int i = 0; i < tableNames.length; i++) {
192 IDataSet dataset = new DefaultDataSet(new DefaultTable(tableNames[i]));
193 DatabaseOperation.DELETE_ALL.execute(getConnection(), dataset );
194 }
195 }
196
197 /***
198 * @return Returns the actualDataSet.
199 */
200 public IDataSet getTestDataSet() {
201 return testDataSet;
202 }
203
204
205 }
206
207
208 /***
209 * $Log: AbstractSpringContextDBUnitTests.java,v $
210 * Revision 1.8 2005/12/08 14:53:46 shally
211 * Opkuis voor checkstyle.
212 *
213 * Revision 1.7 2005/08/17 09:12:02 schauwvliege
214 * Checkstyle
215 *
216 * Revision 1.6 2005/08/12 14:15:20 ge0ffrey
217 * cache vs transactions in tests
218 *
219 * Revision 1.5 2005/07/06 12:10:11 schauwvliege
220 * added person/contact and location to model
221 *
222 * Revision 1.4 2005/07/05 15:02:54 schauwvliege
223 * more information in the Junit log
224 *
225 * Revision 1.2 2005/06/09 08:19:03 bejug_cc
226 * Fix initial import
227 *
228 * Revision 1.2 2005/05/25 11:06:53 ssc
229 * added DBUnit tests and fixed some errors
230 *
231 * Revision 1.1 2005/05/18 15:55:48 ssc
232 * Extracted DBUit test to new abstract testclass to increase testing speed for non DBUnit tests
233 *
234 **/