1 package org
.argeo
.slc
.unit
;
3 import java
.io
.InputStream
;
4 import java
.sql
.SQLException
;
6 import javax
.sql
.DataSource
;
8 import org
.dbunit
.DataSourceDatabaseTester
;
9 import org
.dbunit
.DatabaseUnitException
;
10 import org
.dbunit
.IDatabaseTester
;
11 import org
.dbunit
.database
.IDatabaseConnection
;
12 import org
.dbunit
.dataset
.IDataSet
;
13 import org
.dbunit
.dataset
.ReplacementDataSet
;
14 import org
.dbunit
.dataset
.xml
.FlatXmlDataSet
;
15 import org
.dbunit
.operation
.DatabaseOperation
;
16 import org
.springframework
.core
.io
.ClassPathResource
;
17 import org
.springframework
.core
.io
.Resource
;
19 import org
.apache
.commons
.io
.IOUtils
;
21 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
23 import org
.argeo
.slc
.core
.SlcException
;
26 * Helper to make db vendor independent tests using DbUnit data sets. Based on
29 public abstract class IndependentDbTestCase
extends AbstractSpringTestCase
{
30 private IDatabaseTester databaseTester
;
32 /** Creates the DDL of the data model and loads the data. */
34 protected void setUp() throws Exception
{
37 databaseTester
= new DataSourceDatabaseTester(getDataSource());
38 databaseTester
.setSetUpOperation(new DatabaseOperation() {
41 public void execute(IDatabaseConnection connection
, IDataSet dataSet
)
42 throws DatabaseUnitException
, SQLException
{
43 DbModel dbModel
= getDbModel();
44 SchemaExport schemaExport
= dbModel
45 .createSchemaExport(connection
.getConnection());
46 schemaExport
.create(true, true);
48 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
52 databaseTester
.setDataSet(createDataSet());
53 databaseTester
.onSetup();
57 protected void tearDown() throws Exception
{
58 if (databaseTester
!= null) {
59 databaseTester
.onTearDown();
65 * The data source to use. The default implementation returns a bean named
66 * {@link #getDataSourceBeanName}
68 protected DataSource
getDataSource() {
69 return (DataSource
) getContext().getBean(getDataSourceBeanName());
73 * The name of the data source bean to use. The default implementation
74 * returns <i>dataSource</i>.
76 protected String
getDataSourceBeanName() {
81 * Creates the data set to use. The default implementation creates a
82 * <code>FlatXmlDataSet</code> load from the resource defined in
83 * {@link #getDataSetResource()}
85 protected IDataSet
createDataSet() {
86 InputStream in
= null;
88 in
= getDataSetResource().getInputStream();
89 String
[] replaceStrings
= getReplacementStrings();
91 if (replaceStrings
.length
== 0) {
92 dataSet
= new FlatXmlDataSet(in
);
94 dataSet
= new ReplacementDataSet(new FlatXmlDataSet(in
));
95 for (String str
: replaceStrings
) {
96 replace((ReplacementDataSet
) dataSet
, str
);
100 } catch (Exception e
) {
101 throw new SlcException("Cannot create data set", e
);
103 IOUtils
.closeQuietly(in
);
108 * To be overridden. Return an empty array by default.
110 * @return the array of strings to replace in the dataset
112 protected String
[] getReplacementStrings() {
113 return new String
[0];
117 * Set the object replacing the given string. To be overridden. Does nothing
120 protected void replace(ReplacementDataSet dataSet
, String str
)
126 * Replace the given string by the content of the resource with the same
127 * name in the same package, as a byte array.
129 protected void replaceByRessource(ReplacementDataSet dataSet
, String str
)
131 Resource zipResource
= new ClassPathResource(inPackage(str
));
133 dataSet
.addReplacementObject(str
, IOUtils
.toByteArray(zipResource
138 * The resource of the data set to load. The default implementation loads a
139 * <code>ClassPathResource</code> located at
140 * {@link #getDataSetResourceLocation()}.
142 protected Resource
getDataSetResource() {
143 return new ClassPathResource(getDataSetResourceLocation());
147 * The location of the data set to load. The default implementation loads
148 * <i>dataSet.xml</i> found in the same package as the test.
150 protected String
getDataSetResourceLocation() {
151 return inPackage("dataSet.xml");
155 * The DB model to us to create the DDL of the testes database. The default
156 * implementation loads a bean named after {@link #getDbModelBeanName()}.
158 protected DbModel
getDbModel() {
159 return (DbModel
) getContext().getBean(getDbModelBeanName());
163 * The name of the bean to load. The default implementation returns
166 protected String
getDbModelBeanName() {