Revision e32470ff
Added by Andreas Kohlbecker over 6 years ago
cdmlib-test/src/main/java/eu/etaxonomy/cdm/database/DataBaseTablePrinter.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2009 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.database; |
|
11 |
|
|
12 |
import java.io.ByteArrayOutputStream; |
|
13 |
import java.io.File; |
|
14 |
import java.io.FileNotFoundException; |
|
15 |
import java.io.FileOutputStream; |
|
16 |
import java.io.IOException; |
|
17 |
import java.io.OutputStream; |
|
18 |
import java.io.OutputStreamWriter; |
|
19 |
import java.sql.SQLException; |
|
20 |
|
|
21 |
import javax.sql.DataSource; |
|
22 |
import javax.xml.transform.Result; |
|
23 |
import javax.xml.transform.Source; |
|
24 |
import javax.xml.transform.Transformer; |
|
25 |
import javax.xml.transform.TransformerException; |
|
26 |
import javax.xml.transform.TransformerFactory; |
|
27 |
import javax.xml.transform.stream.StreamResult; |
|
28 |
|
|
29 |
import org.apache.log4j.Logger; |
|
30 |
import org.dbunit.database.DatabaseConfig; |
|
31 |
import org.dbunit.database.DatabaseConnection; |
|
32 |
import org.dbunit.database.DatabaseDataSet; |
|
33 |
import org.dbunit.database.IDatabaseConnection; |
|
34 |
import org.dbunit.dataset.IDataSet; |
|
35 |
import org.dbunit.dataset.ITableIterator; |
|
36 |
import org.dbunit.dataset.filter.ExcludeTableFilter; |
|
37 |
import org.dbunit.dataset.filter.ITableFilterSimple; |
|
38 |
import org.dbunit.dataset.xml.FlatDtdDataSet; |
|
39 |
import org.dbunit.dataset.xml.FlatXmlDataSet; |
|
40 |
import org.dbunit.dataset.xml.FlatXmlWriter; |
|
41 |
import org.dbunit.ext.h2.H2DataTypeFactory; |
|
42 |
import org.springframework.beans.factory.annotation.Autowired; |
|
43 |
import org.springframework.stereotype.Component; |
|
44 |
|
|
45 |
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest; |
|
46 |
import eu.etaxonomy.cdm.test.unitils.FlatFullXmlWriter; |
|
47 |
|
|
48 |
/** |
|
49 |
* |
|
50 |
* <h2>Creating create DbUnit dataset files</h2> |
|
51 |
* In order to create DbUnit datasets for integration tests it is highly recommended method to use the |
|
52 |
* {@link #writeDbUnitDataSetFile(String[])} method. |
|
53 |
* |
|
54 |
*/ |
|
55 |
@Component |
|
56 |
public class DataBaseTablePrinter { |
|
57 |
|
|
58 |
protected static final Logger logger = Logger.getLogger(DataBaseTablePrinter.class); |
|
59 |
|
|
60 |
@Autowired |
|
61 |
protected DataSource dataSource; |
|
62 |
|
|
63 |
public DataBaseTablePrinter(){ |
|
64 |
} |
|
65 |
|
|
66 |
protected IDatabaseConnection getConnection() { |
|
67 |
IDatabaseConnection connection = null; |
|
68 |
try { |
|
69 |
/// FIXME must use unitils.properties: database.schemaNames |
|
70 |
connection = new DatabaseConnection(dataSource.getConnection(), "PUBLIC"); |
|
71 |
|
|
72 |
DatabaseConfig config = connection.getConfig(); |
|
73 |
|
|
74 |
// FIXME must use unitils.properties: org.unitils.core.dbsupport.DbSupport.implClassName |
|
75 |
// & database.dialect to find configured DataTypeFactory |
|
76 |
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, |
|
77 |
new H2DataTypeFactory()); |
|
78 |
} catch (Exception e) { |
|
79 |
logger.error(e); |
|
80 |
} |
|
81 |
return connection; |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* Prints the data set to an output stream, using the |
|
86 |
* {@link FlatXmlDataSet}. |
|
87 |
* <p> |
|
88 |
* <h2>NOTE: for compatibility with unitils 3.x you may |
|
89 |
* want to use the {@link #printDataSetWithNull(OutputStream)} |
|
90 |
* method instead.</h2> |
|
91 |
* <p> |
|
92 |
* Remember, if you've just called save() or |
|
93 |
* update(), the data isn't written to the database until the |
|
94 |
* transaction is committed, and that isn't until after the |
|
95 |
* method exits. Consequently, if you want to test writing to |
|
96 |
* the database, either use the {@literal @ExpectedDataSet} |
|
97 |
* annotation (that executes after the test is run), or use |
|
98 |
* {@link CdmTransactionalIntegrationTest}. |
|
99 |
* |
|
100 |
* @param out The OutputStream to write to. |
|
101 |
* @see FlatFullXmlWriter |
|
102 |
*/ |
|
103 |
public void printDataSet(OutputStream out) { |
|
104 |
IDatabaseConnection connection = null; |
|
105 |
|
|
106 |
try { |
|
107 |
connection = getConnection(); |
|
108 |
IDataSet actualDataSet = connection.createDataSet(); |
|
109 |
FlatXmlDataSet.write(actualDataSet, out); |
|
110 |
} catch (Exception e) { |
|
111 |
logger.error(e); |
|
112 |
} finally { |
|
113 |
try { |
|
114 |
if (connection != null){ |
|
115 |
connection.close(); |
|
116 |
} |
|
117 |
} catch (SQLException sqle) { |
|
118 |
logger.error(sqle); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
/** |
|
124 |
* Prints the data set to an output stream, using the |
|
125 |
* {@link FlatFullXmlWriter}. |
|
126 |
* which is a variant of the {@link org.dbunit.dataset.xml.FlatXmlWriter}. It |
|
127 |
* inserts '[null]' place holders for null values instead of skipping them. |
|
128 |
* This was necessary to make this xml database export compatible to the |
|
129 |
* {@link MultiSchemaXmlDataSetReader} which is used in Unitils since version 3.x |
|
130 |
* <p> |
|
131 |
* @param out out The OutputStream to write to. |
|
132 |
* @param includeTableNames |
|
133 |
*/ |
|
134 |
public void printDataSetWithNull(OutputStream out, String[] includeTableNames) { |
|
135 |
printDataSetWithNull(out, null, null, includeTableNames); |
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* Prints the data set to an output stream, using the |
|
140 |
* {@link FlatFullXmlWriter}. |
|
141 |
* which is a variant of the {@link org.dbunit.dataset.xml.FlatXmlWriter}. It |
|
142 |
* inserts '[null]' place holders for null values instead of skipping them. |
|
143 |
* This was necessary to make this xml database export compatible to the |
|
144 |
* {@link MultiSchemaXmlDataSetReader} which is used in Unitils since version 3.x |
|
145 |
* <p> |
|
146 |
* Remember, if you've just called save() or |
|
147 |
* update(), the data isn't written to the database until the |
|
148 |
* transaction is committed, and that isn't until after the |
|
149 |
* method exits. Consequently, if you want to test writing to |
|
150 |
* the database, either use the {@literal @ExpectedDataSet} |
|
151 |
* annotation (that executes after the test is run), or use |
|
152 |
* {@link CdmTransactionalIntegrationTest}. |
|
153 |
* |
|
154 |
* @param out The OutputStream to write to. |
|
155 |
* @see FlatFullXmlWriter |
|
156 |
*/ |
|
157 |
public void printDataSetWithNull(OutputStream out) { |
|
158 |
printDataSetWithNull(out, null, null, null); |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* @param out |
|
163 |
* @param excludeTermLoadingTables |
|
164 |
* @param excludeFilter the tables to be <em>excluded</em> |
|
165 |
*/ |
|
166 |
public void printDataSetWithNull(OutputStream out, Boolean excludeTermLoadingTables, |
|
167 |
ITableFilterSimple excludeFilterOrig, String[] includeTableNames) { |
|
168 |
|
|
169 |
ITableFilterSimple excludeFilter = excludeFilterOrig; |
|
170 |
if(excludeTermLoadingTables != null && excludeTermLoadingTables.equals(true)){ |
|
171 |
ExcludeTableFilter excludeTableFilter = new ExcludeTableFilter(); |
|
172 |
|
|
173 |
for(String tname : CdmIntegrationTest.termLoadingTables){ |
|
174 |
excludeTableFilter.excludeTable(tname); |
|
175 |
} |
|
176 |
excludeFilter = excludeTableFilter; |
|
177 |
} |
|
178 |
|
|
179 |
if( excludeFilter != null && includeTableNames != null){ |
|
180 |
throw new RuntimeException("Ambiguous parameters: excludeFilter can not be used together with includeTableNames or excludeTermLoadingTable."); |
|
181 |
} |
|
182 |
|
|
183 |
IDatabaseConnection connection = null; |
|
184 |
try { |
|
185 |
connection = getConnection(); |
|
186 |
IDataSet dataSet; |
|
187 |
if (includeTableNames != null) { |
|
188 |
dataSet = connection.createDataSet(includeTableNames); |
|
189 |
} else { |
|
190 |
if (excludeFilter == null){ |
|
191 |
excludeFilter = new ExcludeTableFilter(); |
|
192 |
} |
|
193 |
dataSet = new DatabaseDataSet(connection, false, excludeFilter); |
|
194 |
} |
|
195 |
FlatFullXmlWriter writer = new FlatFullXmlWriter(out); |
|
196 |
writer.write(dataSet); |
|
197 |
} catch (Exception e) { |
|
198 |
logger.error("Error on writing dataset:", e); |
|
199 |
} finally { |
|
200 |
try { |
|
201 |
if (connection != null){ |
|
202 |
connection.close(); |
|
203 |
} |
|
204 |
} catch (SQLException sqle) { |
|
205 |
logger.error(sqle); |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 |
/** |
|
211 |
* |
|
212 |
* @param out |
|
213 |
* @param formatString can be null, otherwise a format string like eg. "< %1$s />" see also {@link String#format(String, Object...)} |
|
214 |
*/ |
|
215 |
public void printTableNames(OutputStream out, String formatString) { |
|
216 |
IDatabaseConnection connection = null; |
|
217 |
OutputStreamWriter writer = new OutputStreamWriter(out); |
|
218 |
|
|
219 |
try { |
|
220 |
connection = getConnection(); |
|
221 |
IDataSet actualDataSet = connection.createDataSet(); |
|
222 |
ITableIterator tableIterator = actualDataSet.iterator(); |
|
223 |
String tableName = null; |
|
224 |
while(tableIterator.next()){ |
|
225 |
tableName = tableIterator.getTable().getTableMetaData().getTableName(); |
|
226 |
if(formatString != null){ |
|
227 |
tableName = String.format(formatString, tableName); |
|
228 |
} |
|
229 |
writer.append(tableName).append("\n"); |
|
230 |
} |
|
231 |
} catch (Exception e) { |
|
232 |
logger.error(e); |
|
233 |
} finally { |
|
234 |
try { |
|
235 |
writer.close(); |
|
236 |
} catch (IOException ioe) { |
|
237 |
logger.error(ioe); |
|
238 |
} |
|
239 |
try { |
|
240 |
if (connection != null){ |
|
241 |
connection.close(); |
|
242 |
} |
|
243 |
} catch (SQLException sqle) { |
|
244 |
logger.error(sqle); |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
250 |
* Prints the named tables to an output stream, using dbunit's |
|
251 |
* {@link org.dbunit.dataset.xml.FlatXmlDataSet}. |
|
252 |
* <p> |
|
253 |
* <h2>NOTE: for compatibility with unitils 3.x you may |
|
254 |
* want to use the {@link #printDataSetWithNull(OutputStream)} |
|
255 |
* method instead.</h2> |
|
256 |
* |
|
257 |
* <p> |
|
258 |
* Remember, if you've just called save() or |
|
259 |
* update(), the data isn't written to the database until the |
|
260 |
* transaction is committed, and that isn't until after the |
|
261 |
* method exits. Consequently, if you want to test writing to |
|
262 |
* the database, either use the {@literal @ExpectedDataSet} |
|
263 |
* annotation (that executes after the test is run), or use |
|
264 |
* {@link CdmTransactionalIntegrationTest}. |
|
265 |
* |
|
266 |
* @see {@link #printDataSet(OutputStream)} |
|
267 |
* @see FlatFullXmlWriter |
|
268 |
* |
|
269 |
* @param out |
|
270 |
* the OutputStream to write the XML to |
|
271 |
* @param includeTableNames |
|
272 |
* the names of tables to print (should be in upper case letters) |
|
273 |
*/ |
|
274 |
public void printDataSet(OutputStream out, String[] includeTableNames) { |
|
275 |
IDatabaseConnection connection = null; |
|
276 |
|
|
277 |
if(includeTableNames == null){ |
|
278 |
return; |
|
279 |
} |
|
280 |
|
|
281 |
try { |
|
282 |
connection = getConnection(); |
|
283 |
IDataSet actualDataSet = connection.createDataSet(includeTableNames); |
|
284 |
FlatXmlDataSet.write(actualDataSet, out); |
|
285 |
|
|
286 |
} catch (Exception e) { |
|
287 |
logger.error(e); |
|
288 |
} finally { |
|
289 |
try { |
|
290 |
if (connection != null){ |
|
291 |
connection.close(); |
|
292 |
} |
|
293 |
} catch (SQLException sqle) { |
|
294 |
logger.error(sqle); |
|
295 |
} |
|
296 |
} |
|
297 |
} |
|
298 |
|
|
299 |
/** |
|
300 |
* Prints the named tables to an output stream, using dbunit's |
|
301 |
* {@link org.dbunit.dataset.xml.FlatXmlWriter}. |
|
302 |
* <p> |
|
303 |
* <h2>NOTE: for compatibility with unitils 3.x you may |
|
304 |
* want to use the {@link #printDataSetWithNull(OutputStream)} |
|
305 |
* method instead.</h2> |
|
306 |
* |
|
307 |
* <p> |
|
308 |
* Remember, if you've just called save() or |
|
309 |
* update(), the data isn't written to the database until the |
|
310 |
* transaction is committed, and that isn't until after the |
|
311 |
* method exits. Consequently, if you want to test writing to |
|
312 |
* the database, either use the {@literal @ExpectedDataSet} |
|
313 |
* annotation (that executes after the test is run), or use |
|
314 |
* {@link CdmTransactionalIntegrationTest}. |
|
315 |
* |
|
316 |
* @see {@link #printDataSet(OutputStream)} |
|
317 |
* @see FlatFullXmlWriter |
|
318 |
* @param out |
|
319 |
* @param filter |
|
320 |
*/ |
|
321 |
public void printDataSet(OutputStream out, ITableFilterSimple filter) { |
|
322 |
if (filter == null){ |
|
323 |
filter = new ExcludeTableFilter(); |
|
324 |
} |
|
325 |
|
|
326 |
IDatabaseConnection connection = null; |
|
327 |
|
|
328 |
try { |
|
329 |
connection = getConnection(); |
|
330 |
// FlatXmlDataSet.write(actualDataSet, out); |
|
331 |
|
|
332 |
IDataSet dataSet = new DatabaseDataSet(connection, false, filter); |
|
333 |
|
|
334 |
FlatXmlWriter writer = new FlatXmlWriter(out); |
|
335 |
writer.write(dataSet); |
|
336 |
|
|
337 |
} catch (Exception e) { |
|
338 |
logger.error(e); |
|
339 |
} finally { |
|
340 |
try { |
|
341 |
if (connection != null){ |
|
342 |
connection.close(); |
|
343 |
} |
|
344 |
} catch (SQLException sqle) { |
|
345 |
logger.error(sqle); |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
|
|
350 |
|
|
351 |
/** |
|
352 |
* Prints a dtd to an output stream, using dbunit's |
|
353 |
* {@link org.dbunit.dataset.xml.FlatDtdDataSet}. |
|
354 |
* |
|
355 |
* @param out The OutputStream to write to. |
|
356 |
* @see org.dbunit.dataset.xml.FlatDtdDataSet |
|
357 |
*/ |
|
358 |
public void printDtd(OutputStream out) { |
|
359 |
IDatabaseConnection connection = null; |
|
360 |
|
|
361 |
try { |
|
362 |
connection = getConnection(); |
|
363 |
IDataSet actualDataSet = connection.createDataSet(); |
|
364 |
FlatDtdDataSet.write(actualDataSet, out); |
|
365 |
} catch (Exception e) { |
|
366 |
logger.error(e); |
|
367 |
} finally { |
|
368 |
try { |
|
369 |
if (connection != null){ |
|
370 |
connection.close(); |
|
371 |
} |
|
372 |
} catch (SQLException sqle) { |
|
373 |
logger.error(sqle); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
|
|
378 |
/** |
|
379 |
* <b>WARNING</b> read this doc before using this method. |
|
380 |
* <p> |
|
381 |
* This is the recommended method to create DbUnit data set for integration tests. |
|
382 |
* This method will create the DbUnit data set file for the specific test class |
|
383 |
* it has been called from. This happens in full compliance with the DbUnit conventions, |
|
384 |
* so that the test class will immediately be able to use the |
|
385 |
* newly created file during the next run. |
|
386 |
* This also means that using <code>writeDbUnitDataSetFile()</code> |
|
387 |
* in a test class <b>will overwrite the existing data set file</b>. This is not |
|
388 |
* considered to be harmful since we expect that any development always is |
|
389 |
* backed up by a VCS like git, svn and therefore recovery of overwritten |
|
390 |
* data source files should not cause any problems. |
|
391 |
* <p> |
|
392 |
* Writes a DbUnit {@link org.unitils.dbunit.annotation.DataSet} file from the current data base connection using the |
|
393 |
* {@link FlatFullXmlWriter} which is a variant of the |
|
394 |
* {@link org.dbunit.dataset.xml.FlatXmlWriter}. It |
|
395 |
* inserts <code>'[null]'</code> place holders for null values instead of skipping them. |
|
396 |
* |
|
397 |
*/ |
|
398 |
public void writeDbUnitDataSetFile(String[] includeTableNames, Class<?> testClass) throws FileNotFoundException { |
|
399 |
writeDbUnitDataSetFile(includeTableNames, testClass, null); |
|
400 |
} |
|
401 |
|
|
402 |
/** |
|
403 |
* |
|
404 |
* Extension of method mentioned in "see also" where you can specify an appendix for the |
|
405 |
* generated DbUnit {@link org.unitils.dbunit.annotation.DataSet} file. |
|
406 |
* |
|
407 |
* @param methodName the appendix of the generated DbUnit dataset file |
|
408 |
* @see {@link #writeDbUnitDataSetFile(String[], Class)} |
|
409 |
*/ |
|
410 |
public void writeDbUnitDataSetFile(String[] includeTableNames, Class<?> testClass, String methodName) throws FileNotFoundException { |
|
411 |
|
|
412 |
String pathname = "src" + File.separator + "test" + File.separator + "resources" + File.separator + testClass.getName().replace(".", File.separator); |
|
413 |
if(methodName!=null){ |
|
414 |
pathname += "-"+methodName; |
|
415 |
} |
|
416 |
pathname += ".xml"; |
|
417 |
File file = new File(pathname); |
|
418 |
|
|
419 |
if (file.exists()){ |
|
420 |
logger.warn("** OVERWRITING DbUnit dataset file " + file.getAbsolutePath()); |
|
421 |
} else { |
|
422 |
logger.warn("Writing new DbUnit dataset file to " + file.getAbsolutePath()); |
|
423 |
} |
|
424 |
|
|
425 |
printDataSetWithNull( |
|
426 |
new FileOutputStream(file), |
|
427 |
false, |
|
428 |
null, |
|
429 |
includeTableNames |
|
430 |
); |
|
431 |
} |
|
432 |
|
|
433 |
/** |
|
434 |
* Transforms a javax.xml.transform.Source to a java.lang.String (useful for comparison in |
|
435 |
* XmlUnit tests etc). |
|
436 |
* |
|
437 |
* @param source |
|
438 |
* @return |
|
439 |
* @throws TransformerException |
|
440 |
*/ |
|
441 |
public String transformSourceToString(Source source) throws TransformerException { |
|
442 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
443 |
Transformer transformer = transformerFactory.newTransformer(); |
|
444 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
445 |
Result result = new StreamResult(outputStream); |
|
446 |
transformer.transform(source, result); |
|
447 |
|
|
448 |
return new String(outputStream.toByteArray()); |
|
449 |
} |
|
450 |
|
|
451 |
} |
cdmlib-test/src/main/java/eu/etaxonomy/cdm/test/integration/CdmIntegrationTest.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.cdm.test.integration; |
11 | 11 |
|
12 |
import java.io.ByteArrayOutputStream; |
|
13 |
import java.io.File; |
|
14 | 12 |
import java.io.FileNotFoundException; |
15 |
import java.io.FileOutputStream; |
|
16 |
import java.io.IOException; |
|
17 | 13 |
import java.io.OutputStream; |
18 |
import java.io.OutputStreamWriter; |
|
19 | 14 |
import java.sql.SQLException; |
20 | 15 |
import java.util.ArrayList; |
21 | 16 |
import java.util.List; |
22 | 17 |
|
23 | 18 |
import javax.sql.DataSource; |
24 |
import javax.xml.transform.Result; |
|
25 | 19 |
import javax.xml.transform.Source; |
26 |
import javax.xml.transform.Transformer; |
|
27 | 20 |
import javax.xml.transform.TransformerException; |
28 |
import javax.xml.transform.TransformerFactory; |
|
29 |
import javax.xml.transform.stream.StreamResult; |
|
30 | 21 |
|
31 | 22 |
import org.apache.log4j.Logger; |
32 | 23 |
import org.dbunit.database.DatabaseConfig; |
33 | 24 |
import org.dbunit.database.DatabaseConnection; |
34 |
import org.dbunit.database.DatabaseDataSet; |
|
35 | 25 |
import org.dbunit.database.IDatabaseConnection; |
36 |
import org.dbunit.dataset.IDataSet; |
|
37 |
import org.dbunit.dataset.ITableIterator; |
|
38 |
import org.dbunit.dataset.filter.ExcludeTableFilter; |
|
39 | 26 |
import org.dbunit.dataset.filter.ITableFilterSimple; |
40 |
import org.dbunit.dataset.xml.FlatDtdDataSet; |
|
41 |
import org.dbunit.dataset.xml.FlatXmlDataSet; |
|
42 |
import org.dbunit.dataset.xml.FlatXmlWriter; |
|
43 | 27 |
import org.dbunit.ext.h2.H2DataTypeFactory; |
44 | 28 |
import org.h2.tools.Server; |
45 | 29 |
import org.junit.Before; |
... | ... | |
47 | 31 |
import org.springframework.transaction.support.DefaultTransactionDefinition; |
48 | 32 |
import org.unitils.UnitilsJUnit4; |
49 | 33 |
import org.unitils.database.annotations.TestDataSource; |
50 |
import org.unitils.dbunit.util.MultiSchemaXmlDataSetReader; |
|
51 | 34 |
import org.unitils.orm.hibernate.annotation.HibernateSessionFactory; |
52 | 35 |
import org.unitils.spring.annotation.SpringApplicationContext; |
53 | 36 |
import org.unitils.spring.annotation.SpringBeanByType; |
54 | 37 |
|
55 |
import eu.etaxonomy.cdm.test.unitils.FlatFullXmlWriter;
|
|
38 |
import eu.etaxonomy.cdm.database.DataBaseTablePrinter;
|
|
56 | 39 |
|
57 | 40 |
/** |
58 | 41 |
* Abstract base class for integration testing a spring / hibernate application using |
... | ... | |
114 | 97 |
|
115 | 98 |
private PlatformTransactionManager transactionManager; |
116 | 99 |
|
100 |
@SpringBeanByType |
|
101 |
private DataBaseTablePrinter dbTablePrinter; |
|
102 |
|
|
117 | 103 |
protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition(); |
118 | 104 |
|
119 | 105 |
@SpringBeanByType |
... | ... | |
121 | 107 |
this.transactionManager = transactionManager; |
122 | 108 |
} |
123 | 109 |
|
110 |
|
|
124 | 111 |
protected IDatabaseConnection getConnection() { |
125 | 112 |
IDatabaseConnection connection = null; |
126 | 113 |
try { |
... | ... | |
188 | 175 |
* annotation (that executes after the test is run), or use |
189 | 176 |
* {@link CdmTransactionalIntegrationTest}. |
190 | 177 |
* |
178 |
* @see {@link DataBaseTablePrinter} |
|
191 | 179 |
* @param out The OutputStream to write to. |
192 | 180 |
* @see FlatFullXmlWriter |
193 | 181 |
*/ |
194 | 182 |
public void printDataSet(OutputStream out) { |
195 |
IDatabaseConnection connection = null; |
|
196 |
|
|
197 |
try { |
|
198 |
connection = getConnection(); |
|
199 |
IDataSet actualDataSet = connection.createDataSet(); |
|
200 |
FlatXmlDataSet.write(actualDataSet, out); |
|
201 |
} catch (Exception e) { |
|
202 |
logger.error(e); |
|
203 |
} finally { |
|
204 |
try { |
|
205 |
if (connection != null){ |
|
206 |
connection.close(); |
|
207 |
} |
|
208 |
} catch (SQLException sqle) { |
|
209 |
logger.error(sqle); |
|
210 |
} |
|
211 |
} |
|
183 |
dbTablePrinter.printDataSet(out); |
|
212 | 184 |
} |
213 | 185 |
|
214 | 186 |
/** |
... | ... | |
219 | 191 |
* This was necessary to make this xml database export compatible to the |
220 | 192 |
* {@link MultiSchemaXmlDataSetReader} which is used in Unitils since version 3.x |
221 | 193 |
* <p> |
194 |
* @see {@link DataBaseTablePrinter} |
|
222 | 195 |
* @param out out The OutputStream to write to. |
223 | 196 |
* @param includeTableNames |
224 | 197 |
*/ |
225 | 198 |
public void printDataSetWithNull(OutputStream out, String[] includeTableNames) { |
226 |
printDataSetWithNull(out, null, null, includeTableNames);
|
|
199 |
dbTablePrinter.printDataSetWithNull(out, includeTableNames);
|
|
227 | 200 |
} |
228 | 201 |
|
229 | 202 |
/** |
... | ... | |
242 | 215 |
* annotation (that executes after the test is run), or use |
243 | 216 |
* {@link CdmTransactionalIntegrationTest}. |
244 | 217 |
* |
218 |
*@see {@link DataBaseTablePrinter} |
|
245 | 219 |
* @param out The OutputStream to write to. |
246 | 220 |
* @see FlatFullXmlWriter |
247 | 221 |
*/ |
248 | 222 |
public void printDataSetWithNull(OutputStream out) { |
249 |
printDataSetWithNull(out, null, null, null);
|
|
223 |
dbTablePrinter.printDataSetWithNull(out);
|
|
250 | 224 |
} |
251 | 225 |
|
252 | 226 |
/** |
... | ... | |
257 | 231 |
public void printDataSetWithNull(OutputStream out, Boolean excludeTermLoadingTables, |
258 | 232 |
ITableFilterSimple excludeFilterOrig, String[] includeTableNames) { |
259 | 233 |
|
260 |
ITableFilterSimple excludeFilter = excludeFilterOrig; |
|
261 |
if(excludeTermLoadingTables != null && excludeTermLoadingTables.equals(true)){ |
|
262 |
ExcludeTableFilter excludeTableFilter = new ExcludeTableFilter(); |
|
263 |
|
|
264 |
for(String tname : termLoadingTables){ |
|
265 |
excludeTableFilter.excludeTable(tname); |
|
266 |
} |
|
267 |
excludeFilter = excludeTableFilter; |
|
268 |
} |
|
269 |
|
|
270 |
if( excludeFilter != null && includeTableNames != null){ |
|
271 |
throw new RuntimeException("Ambiguous parameters: excludeFilter can not be used together with includeTableNames or excludeTermLoadingTable."); |
|
272 |
} |
|
273 |
|
|
274 |
IDatabaseConnection connection = null; |
|
275 |
try { |
|
276 |
connection = getConnection(); |
|
277 |
IDataSet dataSet; |
|
278 |
if (includeTableNames != null) { |
|
279 |
dataSet = connection.createDataSet(includeTableNames); |
|
280 |
} else { |
|
281 |
if (excludeFilter == null){ |
|
282 |
excludeFilter = new ExcludeTableFilter(); |
|
283 |
} |
|
284 |
dataSet = new DatabaseDataSet(connection, false, excludeFilter); |
|
285 |
} |
|
286 |
FlatFullXmlWriter writer = new FlatFullXmlWriter(out); |
|
287 |
writer.write(dataSet); |
|
288 |
} catch (Exception e) { |
|
289 |
logger.error("Error on writing dataset:", e); |
|
290 |
} finally { |
|
291 |
try { |
|
292 |
if (connection != null){ |
|
293 |
connection.close(); |
|
294 |
} |
|
295 |
} catch (SQLException sqle) { |
|
296 |
logger.error(sqle); |
|
297 |
} |
|
298 |
} |
|
234 |
dbTablePrinter.printDataSetWithNull(out, excludeTermLoadingTables, excludeFilterOrig, includeTableNames); |
|
299 | 235 |
} |
300 | 236 |
|
301 | 237 |
/** |
... | ... | |
304 | 240 |
* @param formatString can be null, otherwise a format string like eg. "< %1$s />" see also {@link String#format(String, Object...)} |
305 | 241 |
*/ |
306 | 242 |
public void printTableNames(OutputStream out, String formatString) { |
307 |
IDatabaseConnection connection = null; |
|
308 |
OutputStreamWriter writer = new OutputStreamWriter(out); |
|
309 | 243 |
|
310 |
try { |
|
311 |
connection = getConnection(); |
|
312 |
IDataSet actualDataSet = connection.createDataSet(); |
|
313 |
ITableIterator tableIterator = actualDataSet.iterator(); |
|
314 |
String tableName = null; |
|
315 |
while(tableIterator.next()){ |
|
316 |
tableName = tableIterator.getTable().getTableMetaData().getTableName(); |
|
317 |
if(formatString != null){ |
|
318 |
tableName = String.format(formatString, tableName); |
|
319 |
} |
|
320 |
writer.append(tableName).append("\n"); |
|
321 |
} |
|
322 |
} catch (Exception e) { |
|
323 |
logger.error(e); |
|
324 |
} finally { |
|
325 |
try { |
|
326 |
writer.close(); |
|
327 |
} catch (IOException ioe) { |
|
328 |
logger.error(ioe); |
|
329 |
} |
|
330 |
try { |
|
331 |
if (connection != null){ |
|
332 |
connection.close(); |
|
333 |
} |
|
334 |
} catch (SQLException sqle) { |
|
335 |
logger.error(sqle); |
|
336 |
} |
|
337 |
} |
|
244 |
dbTablePrinter.printTableNames(out, formatString); |
|
338 | 245 |
} |
339 | 246 |
|
340 | 247 |
/** |
... | ... | |
354 | 261 |
* annotation (that executes after the test is run), or use |
355 | 262 |
* {@link CdmTransactionalIntegrationTest}. |
356 | 263 |
* |
264 |
* @see {@link DataBaseTablePrinter} |
|
265 |
* |
|
357 | 266 |
* @see {@link #printDataSet(OutputStream)} |
358 | 267 |
* @see FlatFullXmlWriter |
359 | 268 |
* |
... | ... | |
363 | 272 |
* the names of tables to print (should be in upper case letters) |
364 | 273 |
*/ |
365 | 274 |
public void printDataSet(OutputStream out, String[] includeTableNames) { |
366 |
IDatabaseConnection connection = null; |
|
367 | 275 |
|
368 |
if(includeTableNames == null){ |
|
369 |
return; |
|
370 |
} |
|
276 |
dbTablePrinter.printDataSet(out, includeTableNames); |
|
371 | 277 |
|
372 |
try { |
|
373 |
connection = getConnection(); |
|
374 |
IDataSet actualDataSet = connection.createDataSet(includeTableNames); |
|
375 |
FlatXmlDataSet.write(actualDataSet, out); |
|
376 |
|
|
377 |
} catch (Exception e) { |
|
378 |
logger.error(e); |
|
379 |
} finally { |
|
380 |
try { |
|
381 |
if (connection != null){ |
|
382 |
connection.close(); |
|
383 |
} |
|
384 |
} catch (SQLException sqle) { |
|
385 |
logger.error(sqle); |
|
386 |
} |
|
387 |
} |
|
388 | 278 |
} |
389 | 279 |
|
390 | 280 |
/** |
... | ... | |
404 | 294 |
* annotation (that executes after the test is run), or use |
405 | 295 |
* {@link CdmTransactionalIntegrationTest}. |
406 | 296 |
* |
297 |
* @see {@link DataBaseTablePrinter} |
|
298 |
* |
|
407 | 299 |
* @see {@link #printDataSet(OutputStream)} |
408 | 300 |
* @see FlatFullXmlWriter |
409 | 301 |
* @param out |
410 | 302 |
* @param filter |
411 | 303 |
*/ |
412 | 304 |
public void printDataSet(OutputStream out, ITableFilterSimple filter) { |
413 |
if (filter == null){ |
|
414 |
filter = new ExcludeTableFilter(); |
|
415 |
} |
|
416 |
|
|
417 |
IDatabaseConnection connection = null; |
|
418 |
|
|
419 |
try { |
|
420 |
connection = getConnection(); |
|
421 |
// FlatXmlDataSet.write(actualDataSet, out); |
|
422 |
|
|
423 |
IDataSet dataSet = new DatabaseDataSet(connection, false, filter); |
|
424 |
|
|
425 |
FlatXmlWriter writer = new FlatXmlWriter(out); |
|
426 |
writer.write(dataSet); |
|
427 | 305 |
|
428 |
} catch (Exception e) { |
|
429 |
logger.error(e); |
|
430 |
} finally { |
|
431 |
try { |
|
432 |
if (connection != null){ |
|
433 |
connection.close(); |
|
434 |
} |
|
435 |
} catch (SQLException sqle) { |
|
436 |
logger.error(sqle); |
|
437 |
} |
|
438 |
} |
|
306 |
dbTablePrinter.printDataSet(out, filter); |
|
439 | 307 |
} |
440 | 308 |
|
441 | 309 |
|
... | ... | |
447 | 315 |
* @see org.dbunit.dataset.xml.FlatDtdDataSet |
448 | 316 |
*/ |
449 | 317 |
public void printDtd(OutputStream out) { |
450 |
IDatabaseConnection connection = null; |
|
451 | 318 |
|
452 |
try { |
|
453 |
connection = getConnection(); |
|
454 |
IDataSet actualDataSet = connection.createDataSet(); |
|
455 |
FlatDtdDataSet.write(actualDataSet, out); |
|
456 |
} catch (Exception e) { |
|
457 |
logger.error(e); |
|
458 |
} finally { |
|
459 |
try { |
|
460 |
if (connection != null){ |
|
461 |
connection.close(); |
|
462 |
} |
|
463 |
} catch (SQLException sqle) { |
|
464 |
logger.error(sqle); |
|
465 |
} |
|
466 |
} |
|
319 |
dbTablePrinter.printDtd(out); |
|
467 | 320 |
} |
468 | 321 |
|
469 | 322 |
/** |
... | ... | |
485 | 338 |
* {@link org.dbunit.dataset.xml.FlatXmlWriter}. It |
486 | 339 |
* inserts <code>'[null]'</code> place holders for null values instead of skipping them. |
487 | 340 |
* |
341 |
* see {@link DataBaseTablePrinter} |
|
488 | 342 |
* |
489 | 343 |
* @param includeTableNames |
490 | 344 |
* @throws FileNotFoundException |
491 | 345 |
*/ |
492 | 346 |
public void writeDbUnitDataSetFile(String[] includeTableNames) throws FileNotFoundException { |
493 |
writeDbUnitDataSetFile(includeTableNames, null); |
|
347 |
dbTablePrinter.writeDbUnitDataSetFile(includeTableNames, this.getClass()); |
|
348 |
|
|
494 | 349 |
} |
495 | 350 |
|
496 | 351 |
/** |
... | ... | |
498 | 353 |
* Extension of method mentioned in "see also" where you can specify an appendix for the |
499 | 354 |
* generated DbUnit data set file. |
500 | 355 |
* |
356 |
* see {@link DataBaseTablePrinter} |
|
357 |
* |
|
501 | 358 |
* @param includeTableNames |
502 | 359 |
* @param fileAppendix the appendix of the generated DbUnit dataset file |
503 | 360 |
* @throws FileNotFoundException |
... | ... | |
505 | 362 |
*/ |
506 | 363 |
public void writeDbUnitDataSetFile(String[] includeTableNames, String fileAppendix) throws FileNotFoundException { |
507 | 364 |
|
508 |
String pathname = "src" + File.separator + "test" + File.separator + "resources" + File.separator + this.getClass().getName().replace(".", File.separator); |
|
509 |
if(fileAppendix!=null){ |
|
510 |
pathname += "."+fileAppendix; |
|
511 |
} |
|
512 |
pathname += ".xml"; |
|
513 |
File file = new File(pathname); |
|
514 |
|
|
515 |
if (file.exists()){ |
|
516 |
logger.warn("** OVERWRITING DbUnit dataset file " + file.getAbsolutePath()); |
|
517 |
} else { |
|
518 |
logger.warn("Writing new DbUnit dataset file to " + file.getAbsolutePath()); |
|
519 |
} |
|
520 |
|
|
521 |
printDataSetWithNull( |
|
522 |
new FileOutputStream(file), |
|
523 |
false, |
|
524 |
null, |
|
525 |
includeTableNames |
|
526 |
); |
|
365 |
dbTablePrinter.writeDbUnitDataSetFile(includeTableNames, this.getClass(), fileAppendix); |
|
527 | 366 |
} |
528 | 367 |
|
529 | 368 |
/** |
530 |
* Transforms a javax.xml.transform.Source to a java.lang.String (useful for comparison in |
|
531 |
* XmlUnit tests etc). |
|
532 |
* |
|
533 |
* @param source |
|
534 |
* @return |
|
535 |
* @throws TransformerException |
|
369 |
* see {@link DataBaseTablePrinter#transformSourceToString(Source) |
|
536 | 370 |
*/ |
537 | 371 |
protected String transformSourceToString(Source source) throws TransformerException { |
538 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
539 |
Transformer transformer = transformerFactory.newTransformer(); |
|
540 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
541 |
Result result = new StreamResult(outputStream); |
|
542 |
transformer.transform(source, result); |
|
543 | 372 |
|
544 |
return new String(outputStream.toByteArray());
|
|
373 |
return dbTablePrinter.transformSourceToString(source);
|
|
545 | 374 |
} |
546 | 375 |
|
547 | 376 |
|
cdmlib-test/src/main/resources/eu/etaxonomy/cdm/applicationContext-test.common.xml | ||
---|---|---|
21 | 21 |
<context:component-scan base-package="eu/etaxonomy/cdm/persistence/"/> |
22 | 22 |
|
23 | 23 |
<bean id="testingTermVocabularyDao" class="eu.etaxonomy.cdm.database.TestingTermVocabularyDao"/> |
24 |
|
|
25 |
<bean id="dataBaseTablePrinter" class="eu.etaxonomy.cdm.database.DataBaseTablePrinter"/> |
|
24 | 26 |
|
25 | 27 |
<bean id="termInitializer" class="eu.etaxonomy.cdm.database.TestingTermInitializer"> |
26 | 28 |
<property name="termsDataSet" value="classpath:/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"/> |
Also available in: Unified diff
Creating spring bean DataBaseTablePrinter responsible for all database printing tasks, CdmIntegrationTest only wraps around this bean now