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
.xml
.FlatXmlDataSet
;
14 import org
.dbunit
.operation
.DatabaseOperation
;
15 import org
.springframework
.core
.io
.ClassPathResource
;
16 import org
.springframework
.core
.io
.Resource
;
18 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
20 import org
.argeo
.slc
.core
.SlcException
;
23 * Helper to make db vendor independent tests using DbUnit data sets. Based on
26 public class IndependentDbTestCase
extends SpringBasedTestCase
{
27 private IDatabaseTester databaseTester
;
29 /** Creates the DDL of the data model and loads the data. */
31 protected void setUp() throws Exception
{
34 databaseTester
= new DataSourceDatabaseTester(getDataSource());
35 databaseTester
.setSetUpOperation(new DatabaseOperation() {
38 public void execute(IDatabaseConnection connection
, IDataSet dataSet
)
39 throws DatabaseUnitException
, SQLException
{
40 DbModel dbModel
= getDbModel();
41 SchemaExport schemaExport
= dbModel
42 .createSchemaExport(connection
.getConnection());
43 schemaExport
.create(true, true);
45 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
49 databaseTester
.setDataSet(createDataSet());
50 databaseTester
.onSetup();
54 protected void tearDown() throws Exception
{
55 if (databaseTester
!= null) {
56 databaseTester
.onTearDown();
62 * The data source to use. The default implementation returns a bean named
63 * {@link #getDataSourceBeanName}
65 protected DataSource
getDataSource() {
66 return (DataSource
) getContext().getBean(getDataSourceBeanName());
70 * The name of the data source bean to use. The default implementation
71 * returns <i>dataSource</i>.
73 protected String
getDataSourceBeanName() {
78 * Creates the data set to use. The default implementation creates a
79 * <code>FlatXmlDataSet</code> load from the resource defined in
80 * {@link #getDataSetResource()}
82 protected IDataSet
createDataSet() {
84 InputStream in
= getDataSetResource().getInputStream();
85 IDataSet dataSet
= new FlatXmlDataSet(in
);
88 } catch (Exception e
) {
89 throw new SlcException("Cannot create data set", e
);
94 * The resource of the data set to load. The default implementation loads a
95 * <code>ClassPathResource</code> located at
96 * {@link #getDataSetResourceLocation()}.
98 protected Resource
getDataSetResource() {
99 return new ClassPathResource(getDataSetResourceLocation());
103 * The location of the data set to load. The default implementation loads
104 * <i>dataSet.xml</i> found in the same package as the test.
106 protected String
getDataSetResourceLocation() {
107 return inPackage("dataSet.xml");
111 * The DB model to us to create the DDL of the testes database. The default
112 * implementation loads a bean named after {@link #getDbModelBeanName()}.
114 protected DbModel
getDbModel() {
115 return (DbModel
) getContext().getBean(getDbModelBeanName());
119 * The name of the bean to load. The default implementation returns
122 protected String
getDbModelBeanName() {