1 package org
.argeo
.slc
.hibernate
.unit
;
3 import java
.io
.InputStream
;
4 import java
.sql
.SQLException
;
6 import javax
.sql
.DataSource
;
8 import org
.apache
.commons
.io
.IOUtils
;
9 import org
.argeo
.slc
.core
.SlcException
;
10 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
11 import org
.dbunit
.DataSourceDatabaseTester
;
12 import org
.dbunit
.DatabaseUnitException
;
13 import org
.dbunit
.IDatabaseTester
;
14 import org
.dbunit
.database
.IDatabaseConnection
;
15 import org
.dbunit
.dataset
.IDataSet
;
16 import org
.dbunit
.dataset
.ReplacementDataSet
;
17 import org
.dbunit
.dataset
.xml
.FlatXmlDataSet
;
18 import org
.dbunit
.operation
.DatabaseOperation
;
19 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
20 import org
.springframework
.core
.io
.ClassPathResource
;
21 import org
.springframework
.core
.io
.Resource
;
24 * Helper to make db vendor independent tests using DbUnit data sets. Based on
25 * {@link DbModelHibernate}.
27 public abstract class IndependentDbTestCase
extends AbstractSpringTestCase
{
28 private IDatabaseTester databaseTester
;
30 /** Creates the DDL of the data model and loads the data. */
32 protected void setUp() throws Exception
{
35 databaseTester
= new DataSourceDatabaseTester(getDataSource());
36 databaseTester
.setSetUpOperation(new DatabaseOperation() {
39 public void execute(IDatabaseConnection connection
, IDataSet dataSet
)
40 throws DatabaseUnitException
, SQLException
{
41 DbModelHibernate dbModel
= getDbModel();
42 SchemaExport schemaExport
= dbModel
43 .createSchemaExport(connection
.getConnection());
44 schemaExport
.create(true, true);
46 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
50 databaseTester
.setDataSet(createDataSet());
51 databaseTester
.onSetup();
55 protected void tearDown() throws Exception
{
56 if (databaseTester
!= null) {
57 databaseTester
.onTearDown();
63 * The data source to use. The default implementation returns a bean named
64 * {@link #getDataSourceBeanName}
66 protected DataSource
getDataSource() {
67 return (DataSource
) getContext().getBean(getDataSourceBeanName());
71 * The name of the data source bean to use. The default implementation
72 * returns <i>dataSource</i>.
74 protected String
getDataSourceBeanName() {
75 return "slcDefault.dataSource.inMemoryTestHSQL";
79 * Creates the data set to use. The default implementation creates a
80 * <code>FlatXmlDataSet</code> load from the resource defined in
81 * {@link #getDataSetResource()}
83 protected IDataSet
createDataSet() {
84 InputStream in
= null;
86 in
= getDataSetResource().getInputStream();
87 String
[] replaceStrings
= getReplacementStrings();
89 if (replaceStrings
.length
== 0) {
90 dataSet
= new FlatXmlDataSet(in
);
92 dataSet
= new ReplacementDataSet(new FlatXmlDataSet(in
));
93 for (String str
: replaceStrings
) {
94 replace((ReplacementDataSet
) dataSet
, str
);
98 } catch (Exception e
) {
99 throw new SlcException("Cannot create data set", e
);
101 IOUtils
.closeQuietly(in
);
106 * To be overridden. Return an empty array by default.
108 * @return the array of strings to replace in the dataset
110 protected String
[] getReplacementStrings() {
111 return new String
[0];
115 * Set the object replacing the given string. To be overridden. Does nothing
118 protected void replace(ReplacementDataSet dataSet
, String str
)
124 * Replace the given string by the content of the resource with the same
125 * name in the same package, as a byte array.
127 protected void replaceByRessource(ReplacementDataSet dataSet
, String str
)
129 Resource zipResource
= new ClassPathResource(inPackage(str
));
131 dataSet
.addReplacementObject(str
, IOUtils
.toByteArray(zipResource
136 * The resource of the data set to load. The default implementation loads a
137 * <code>ClassPathResource</code> located at
138 * {@link #getDataSetResourceLocation()}.
140 protected Resource
getDataSetResource() {
141 return new ClassPathResource(getDataSetResourceLocation());
145 * The location of the data set to load. The default implementation loads
146 * <i>dataSet.xml</i> found in the same package as the test.
148 protected String
getDataSetResourceLocation() {
149 return inPackage("dataSet.xml");
153 * The DB model to us to create the DDL of the testes database. The default
154 * implementation loads a bean named after {@link #getDbModelBeanName()}.
156 protected DbModelHibernate
getDbModel() {
157 return (DbModelHibernate
) getContext().getBean(getDbModelBeanName());
161 * The name of the bean to load. The default implementation returns
164 protected String
getDbModelBeanName() {