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
|
package eu.etaxonomy.cdm.test.integration;
|
10
|
|
11
|
import java.io.FileNotFoundException;
|
12
|
import java.io.OutputStream;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.Collection;
|
16
|
import java.util.List;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import javax.sql.DataSource;
|
20
|
import javax.xml.transform.Source;
|
21
|
import javax.xml.transform.TransformerException;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.dbunit.database.DatabaseConfig;
|
25
|
import org.dbunit.database.DatabaseConnection;
|
26
|
import org.dbunit.database.IDatabaseConnection;
|
27
|
import org.dbunit.dataset.filter.ITableFilterSimple;
|
28
|
import org.dbunit.dataset.xml.FlatXmlDataSet;
|
29
|
import org.dbunit.ext.h2.H2DataTypeFactory;
|
30
|
import org.h2.tools.Server;
|
31
|
import org.junit.Before;
|
32
|
import org.springframework.transaction.PlatformTransactionManager;
|
33
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
34
|
import org.unitils.UnitilsJUnit4;
|
35
|
import org.unitils.database.annotations.TestDataSource;
|
36
|
import org.unitils.dbunit.util.MultiSchemaXmlDataSetReader;
|
37
|
import org.unitils.orm.hibernate.annotation.HibernateSessionFactory;
|
38
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
39
|
import org.unitils.spring.annotation.SpringBeanByType;
|
40
|
|
41
|
import eu.etaxonomy.cdm.database.DataBaseTablePrinter;
|
42
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
43
|
import eu.etaxonomy.cdm.test.unitils.FlatFullXmlWriter;
|
44
|
|
45
|
/**
|
46
|
* Abstract base class for integration testing a spring / hibernate application using
|
47
|
* the unitils testing framework and dbunit.
|
48
|
*
|
49
|
* <h2>Data base server</h2>
|
50
|
* The database being used is configured in
|
51
|
* the maven module specific unitils.properties file. By default the database is a H2 in memory data base.
|
52
|
* <p>
|
53
|
* The H2 can be monitored during the test execution if the system property <code>h2Server</code> is given as
|
54
|
* java virtual machine argument:
|
55
|
* <code>-Dh2Server</code>.
|
56
|
* An internal h2 database application will be started and listens at port 8082.
|
57
|
* The port to listen on can be specified by passing a second argument, e.g.: <code>-Dh2Server 8083</code>.
|
58
|
*
|
59
|
* <h2>Creating create DbUnit dataset files</h2>
|
60
|
* In order to create DbUnit datasets for integration tests it is highly recommended method to use the
|
61
|
* {@link #writeDbUnitDataSetFile(String[])} method.
|
62
|
*
|
63
|
* From {@link http://www.unitils.org/tutorial-database.html}, by default every test is executed in a transaction,
|
64
|
* which is committed at the end of the test. This can be disabled using @Transactional(TransactionMode.DISABLED)
|
65
|
*
|
66
|
* @see <a href="http://www.unitils.org">unitils home page</a>
|
67
|
*
|
68
|
* @author ben.clark
|
69
|
* @author a.kohlbecker (2013)
|
70
|
*/
|
71
|
@SpringApplicationContext("file:./target/test-classes/eu/etaxonomy/cdm/applicationContext-test.xml")
|
72
|
// @HibernateSessionFactory is only needed for test phases like afterTestTearDown
|
73
|
// which are configured to run without a spring application context.
|
74
|
// for further details, see /cdmlib-test/src/main/resources/eu/etaxonomy/cdm/hibernate-test.cfg.xml
|
75
|
@HibernateSessionFactory({"/eu/etaxonomy/cdm/hibernate.cfg.xml", "/eu/etaxonomy/cdm/hibernate-test.cfg.xml"})
|
76
|
public abstract class CdmIntegrationTest extends UnitilsJUnit4 {
|
77
|
|
78
|
protected static final Logger logger = Logger.getLogger(CdmIntegrationTest.class);
|
79
|
|
80
|
private static final String PROPERTY_H2_SERVER = "h2Server";
|
81
|
private static final String H2_SERVER_RUNNING = "h2ServerIsRunning";
|
82
|
|
83
|
/**
|
84
|
* List of the tables which are initially being populated during term loading. {@link PersistentTermInitializer}
|
85
|
*/
|
86
|
public static final String[] termLoadingTables = new String[]{
|
87
|
"DEFINEDTERMBASE",
|
88
|
"DEFINEDTERMBASE_AUD",
|
89
|
"DEFINEDTERMBASE_CONTINENT",
|
90
|
"DEFINEDTERMBASE_REPRESENTATION",
|
91
|
"DEFINEDTERMBASE_REPRESENTATION_AUD",
|
92
|
"HIBERNATE_SEQUENCES",
|
93
|
"TERMBASE_INVERSEREPRESENTATION",
|
94
|
"TERMBASE_INVERSEREPRESENTATION_AUD",
|
95
|
"REPRESENTATION",
|
96
|
"REPRESENTATION_AUD",
|
97
|
"TERMVOCABULARY",
|
98
|
"TERMVOCABULARY_AUD",
|
99
|
"TERMVOCABULARY_REPRESENTATION",
|
100
|
"TERMVOCABULARY_REPRESENTATION_AUD"};
|
101
|
|
102
|
@TestDataSource
|
103
|
protected DataSource dataSource;
|
104
|
|
105
|
private PlatformTransactionManager transactionManager;
|
106
|
|
107
|
@SpringBeanByType
|
108
|
private DataBaseTablePrinter dbTablePrinter;
|
109
|
|
110
|
protected DefaultTransactionDefinition txDefinition = new DefaultTransactionDefinition();
|
111
|
|
112
|
@SpringBeanByType
|
113
|
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
114
|
this.transactionManager = transactionManager;
|
115
|
}
|
116
|
|
117
|
|
118
|
protected IDatabaseConnection getConnection() {
|
119
|
IDatabaseConnection connection = null;
|
120
|
try {
|
121
|
/// FIXME must use unitils.properties: database.schemaNames
|
122
|
connection = new DatabaseConnection(dataSource.getConnection(), "PUBLIC");
|
123
|
|
124
|
DatabaseConfig config = connection.getConfig();
|
125
|
|
126
|
// FIXME must use unitils.properties: org.unitils.core.dbsupport.DbSupport.implClassName
|
127
|
// & database.dialect to find configured DataTypeFactory
|
128
|
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
|
129
|
new H2DataTypeFactory());
|
130
|
} catch (Exception e) {
|
131
|
logger.error(e);
|
132
|
}
|
133
|
return connection;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* How to use:
|
138
|
* <ol>
|
139
|
* <li>Add <code>-Dh2Server</code> as jvm argument to activate.</li>
|
140
|
* <li>uncomment <code>database.url= jdbc:h2:tcp://localhost/~/cdm</code> in <code>/cdmlib-test/src/main/resources/unitils.properties</code></li>
|
141
|
* @throws Exception
|
142
|
*/
|
143
|
@Before
|
144
|
public void startH2Server() throws Exception {
|
145
|
|
146
|
if(System.getProperty(PROPERTY_H2_SERVER) != null && System.getProperty(H2_SERVER_RUNNING) == null){
|
147
|
try {
|
148
|
// printing to System.out, so that developers get feedback always
|
149
|
System.out.println("####################################################");
|
150
|
System.out.println(" Starting h2 web server ...");
|
151
|
List<String> args = new ArrayList<>();
|
152
|
Integer port = null;
|
153
|
try {
|
154
|
port = Integer.parseInt(System.getProperty(PROPERTY_H2_SERVER));
|
155
|
args.add("-webPort");
|
156
|
args.add(port.toString());
|
157
|
} catch (Exception e) {
|
158
|
// the default port is 8082
|
159
|
}
|
160
|
Server.createWebServer(args.toArray(new String[]{})).start();
|
161
|
System.setProperty(H2_SERVER_RUNNING, "true");
|
162
|
System.out.println(" you can connect to the h2 web server by opening");
|
163
|
System.out.println(" http://localhost:" + (port != null ? port : "8082") + "/ in your browser");
|
164
|
System.out.println("#####################################################");
|
165
|
} catch (SQLException e) {
|
166
|
e.printStackTrace();
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
|
171
|
|
172
|
/**
|
173
|
* Prints the data set to an output stream, using the
|
174
|
* {@link FlatXmlDataSet}.
|
175
|
* <p>
|
176
|
* Remember, if you've just called save() or
|
177
|
* update(), the data isn't written to the database until the
|
178
|
* transaction is committed, and that isn't until after the
|
179
|
* method exits. Consequently, if you want to test writing to
|
180
|
* the database, either use the {@literal @ExpectedDataSet}
|
181
|
* annotation (that executes after the test is run), or use
|
182
|
* {@link CdmTransactionalIntegrationTest}.
|
183
|
*
|
184
|
* @see {@link DataBaseTablePrinter}
|
185
|
* @param out The OutputStream to write to.
|
186
|
* @see FlatFullXmlWriter
|
187
|
*/
|
188
|
public void printDataSet(OutputStream out) {
|
189
|
dbTablePrinter.printDataSet(out);
|
190
|
}
|
191
|
|
192
|
/**
|
193
|
* Prints the data set to an output stream, using the
|
194
|
* {@link FlatFullXmlWriter}.
|
195
|
* which is a variant of the {@link org.dbunit.dataset.xml.FlatXmlWriter}. It
|
196
|
* inserts '[null]' place holders for null values instead of skipping them.
|
197
|
* This was necessary for some time to make this xml database export compatible to the
|
198
|
* {@link MultiSchemaXmlDataSetReader} which is used in unitils since version 3.x.
|
199
|
* Newer versions of unitils handle <code>null</code> values correctly so this method
|
200
|
* is now deprecated as explicitly mentioning <code>null</code> values litters the test
|
201
|
* data and disturb during model updating.
|
202
|
* <p>
|
203
|
* @see {@link DataBaseTablePrinter}
|
204
|
* @param out the OutputStream to write to.
|
205
|
* @param includeTableNames
|
206
|
* @deprecated use {@link #printDataSet(OutputStream, String...) as <code>null</code> values should not be mentioned in test data anymore
|
207
|
* (with very few where the null value is the value that is to be tested, these values
|
208
|
* should be added manually)
|
209
|
*/
|
210
|
@Deprecated
|
211
|
public void printDataSetWithNull(OutputStream out, String[] includeTableNames) {
|
212
|
dbTablePrinter.printDataSetWithNull(out, includeTableNames);
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Prints the data set to an output stream, using the
|
217
|
* {@link FlatFullXmlWriter}.
|
218
|
* which is a variant of the {@link org.dbunit.dataset.xml.FlatXmlWriter}. It
|
219
|
* inserts '[null]' place holders for null values instead of skipping them.
|
220
|
* This was necessary for some time to make this xml database export compatible to the
|
221
|
* {@link MultiSchemaXmlDataSetReader} which is used in unitils since version 3.x
|
222
|
* but in the meanwhile handling of <code>null</code> values has been fixed in
|
223
|
* unitils.
|
224
|
* <p>
|
225
|
* Remember, if you've just called save() or
|
226
|
* update(), the data isn't written to the database until the
|
227
|
* transaction is committed, and that isn't until after the
|
228
|
* method exits. Consequently, if you want to test writing to
|
229
|
* the database, either use the {@literal @ExpectedDataSet}
|
230
|
* annotation (that executes after the test is run), or use
|
231
|
* {@link CdmTransactionalIntegrationTest}.
|
232
|
*
|
233
|
* @see {@link DataBaseTablePrinter}
|
234
|
* @param out The OutputStream to write to.
|
235
|
* @see FlatFullXmlWriter
|
236
|
* @deprecated use {@link #printDataSet(OutputStream)} instead, <code>null</code> values
|
237
|
* litter the test data and disturb model updating and are not necessary anymore
|
238
|
*/
|
239
|
@Deprecated
|
240
|
public void printDataSetWithNull(OutputStream out) {
|
241
|
dbTablePrinter.printDataSetWithNull(out);
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @param out
|
246
|
* @param excludeTermLoadingTables
|
247
|
* @param excludeFilter the tables to be <em>excluded</em>
|
248
|
*/
|
249
|
public void printDataSet(OutputStream out, Boolean excludeTermLoadingTables,
|
250
|
ITableFilterSimple excludeFilterOrig, String[] includeTableNames) {
|
251
|
dbTablePrinter.printDataSet(out, excludeTermLoadingTables, excludeFilterOrig, includeTableNames, false);
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* @param out
|
256
|
* @param excludeTermLoadingTables
|
257
|
* @param excludeFilter the tables to be <em>excluded</em>
|
258
|
* @deprecated use {@link #printDataSet(OutputStream, Boolean, ITableFilterSimple, String[])
|
259
|
* instead, <code>null</code> values litter the test data and disturb model updating and are not necessary anymore
|
260
|
*/
|
261
|
@Deprecated
|
262
|
public void printDataSetWithNull(OutputStream out, Boolean excludeTermLoadingTables,
|
263
|
ITableFilterSimple excludeFilterOrig, String[] includeTableNames) {
|
264
|
dbTablePrinter.printDataSetWithNull(out, excludeTermLoadingTables, excludeFilterOrig, includeTableNames);
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
*
|
269
|
* @param out
|
270
|
* @param formatString can be null, otherwise a format string like eg. "< %1$s />" see also {@link String#format(String, Object...)}
|
271
|
*/
|
272
|
public void printTableNames(OutputStream out, String formatString) {
|
273
|
dbTablePrinter.printTableNames(out, formatString);
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Prints the named tables to an output stream, using dbunit's
|
278
|
* {@link org.dbunit.dataset.xml.FlatXmlDataSet}.
|
279
|
* <p>
|
280
|
* Remember, if you've just called save() or
|
281
|
* update(), the data isn't written to the database until the
|
282
|
* transaction is committed, and that isn't until after the
|
283
|
* method exits. Consequently, if you want to test writing to
|
284
|
* the database, either use the {@literal @ExpectedDataSet}
|
285
|
* annotation (that executes after the test is run), or use
|
286
|
* {@link CdmTransactionalIntegrationTest}.
|
287
|
*
|
288
|
* @see {@link DataBaseTablePrinter}
|
289
|
*
|
290
|
* @see {@link #printDataSet(OutputStream)}
|
291
|
* @see FlatFullXmlWriter
|
292
|
*
|
293
|
* @param out
|
294
|
* the OutputStream to write the XML to
|
295
|
* @param includeTableNames
|
296
|
* the names of tables to print (should be in upper case letters)
|
297
|
*/
|
298
|
public void printDataSet(OutputStream out, String ... includeTableNames) {
|
299
|
dbTablePrinter.printDataSet(out, includeTableNames);
|
300
|
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Prints the named tables to an output stream, using dbunit's
|
305
|
* {@link org.dbunit.dataset.xml.FlatXmlWriter}.
|
306
|
* <p>
|
307
|
* Remember, if you've just called save() or
|
308
|
* update(), the data isn't written to the database until the
|
309
|
* transaction is committed, and that isn't until after the
|
310
|
* method exits. Consequently, if you want to test writing to
|
311
|
* the database, either use the {@literal @ExpectedDataSet}
|
312
|
* annotation (that executes after the test is run), or use
|
313
|
* {@link CdmTransactionalIntegrationTest}.
|
314
|
*
|
315
|
* @see {@link DataBaseTablePrinter}
|
316
|
*
|
317
|
* @see {@link #printDataSet(OutputStream)}
|
318
|
* @see FlatFullXmlWriter
|
319
|
* @param out
|
320
|
* @param filter
|
321
|
*/
|
322
|
public void printDataSet(OutputStream out, ITableFilterSimple filter) {
|
323
|
dbTablePrinter.printDataSet(out, filter);
|
324
|
}
|
325
|
|
326
|
|
327
|
/**
|
328
|
* Prints a dtd to an output stream, using dbunit's
|
329
|
* {@link org.dbunit.dataset.xml.FlatDtdDataSet}.
|
330
|
*
|
331
|
* @param out The OutputStream to write to.
|
332
|
* @see org.dbunit.dataset.xml.FlatDtdDataSet
|
333
|
*/
|
334
|
public void printDtd(OutputStream out) {
|
335
|
dbTablePrinter.printDtd(out);
|
336
|
}
|
337
|
|
338
|
/**
|
339
|
* <b>WARNING</b> read this doc before using this method.
|
340
|
* <p>
|
341
|
* This is the recommended method to create DbUnit data set for integration tests.
|
342
|
* This method will create the DbUnit data set file for the specific test class
|
343
|
* it has been called from. This happens in full compliance with the DbUnit conventions,
|
344
|
* so that the test class will immediately be able to use the
|
345
|
* newly created file during the next run.
|
346
|
* This also means that using <code>writeDbUnitDataSetFile()</code>
|
347
|
* in a test class <b>will overwrite the existing data set file</b>. This is not
|
348
|
* considered to be harmful since we expect that any development always is
|
349
|
* backed up by a VCS like git, svn and therefore recovery of overwritten
|
350
|
* data source files should not cause any problems.
|
351
|
* <p>
|
352
|
* Writes a DbUnit dataset file from the current data base connection using the
|
353
|
* {@link FlatFullXmlWriter} which is a variant of the
|
354
|
* {@link org.dbunit.dataset.xml.FlatXmlWriter}. It
|
355
|
* inserts <code>'[null]'</code> place holders for null values instead of skipping them.
|
356
|
*
|
357
|
* see {@link DataBaseTablePrinter}
|
358
|
*
|
359
|
* @param includeTableNames
|
360
|
* @throws FileNotFoundException
|
361
|
*/
|
362
|
public void writeDbUnitDataSetFile(String[] includeTableNames) throws FileNotFoundException {
|
363
|
dbTablePrinter.writeDbUnitDataSetFile(includeTableNames, this.getClass());
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
*
|
368
|
* Extension of method mentioned in "see also" where you can specify an appendix for the
|
369
|
* generated DbUnit data set file.
|
370
|
*
|
371
|
* see {@link DataBaseTablePrinter}
|
372
|
*
|
373
|
* @param includeTableNames
|
374
|
* @param fileAppendix the appendix of the generated DbUnit dataset file
|
375
|
* @throws FileNotFoundException
|
376
|
* @see #writeDbUnitDataSetFile(String[])
|
377
|
*/
|
378
|
public void writeDbUnitDataSetFile(String[] includeTableNames, String fileAppendix, boolean withNullValues) throws FileNotFoundException {
|
379
|
dbTablePrinter.writeDbUnitDataSetFile(includeTableNames, this.getClass(), fileAppendix, withNullValues);
|
380
|
}
|
381
|
|
382
|
/**
|
383
|
* see {@link DataBaseTablePrinter#transformSourceToString(Source)
|
384
|
*/
|
385
|
protected String transformSourceToString(Source source) throws TransformerException {
|
386
|
return dbTablePrinter.transformSourceToString(source);
|
387
|
}
|
388
|
|
389
|
|
390
|
protected <T extends CdmBase> T getEntityFromCollection(Collection<T> cdmBases, UUID uuid) {
|
391
|
for (T cdmBase : cdmBases){
|
392
|
if (cdmBase.getUuid().equals(uuid)){
|
393
|
return cdmBase;
|
394
|
}
|
395
|
}
|
396
|
return null;
|
397
|
}
|
398
|
|
399
|
protected boolean existsInCollection(Collection<? extends CdmBase> cdmBases, UUID uuid) {
|
400
|
return getEntityFromCollection(cdmBases, uuid) != null;
|
401
|
}
|
402
|
|
403
|
|
404
|
/**
|
405
|
* This is the common method to create test data xml files for integration tests.
|
406
|
*
|
407
|
* With {@link CdmTransactionalIntegrationTestExample#createTestDataSet} an example
|
408
|
* implementation of this method is provided.
|
409
|
*
|
410
|
* @throws FileNotFoundException
|
411
|
*/
|
412
|
public abstract void createTestDataSet() throws FileNotFoundException;
|
413
|
}
|