2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.hibernate
.unit
;
18 import java
.io
.InputStream
;
19 import java
.sql
.SQLException
;
21 import javax
.sql
.DataSource
;
23 import org
.apache
.commons
.io
.IOUtils
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
26 import org
.dbunit
.DataSourceDatabaseTester
;
27 import org
.dbunit
.DatabaseUnitException
;
28 import org
.dbunit
.IDatabaseTester
;
29 import org
.dbunit
.database
.IDatabaseConnection
;
30 import org
.dbunit
.dataset
.IDataSet
;
31 import org
.dbunit
.dataset
.ReplacementDataSet
;
32 import org
.dbunit
.dataset
.xml
.FlatXmlDataSet
;
33 import org
.dbunit
.operation
.DatabaseOperation
;
34 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
35 import org
.springframework
.core
.io
.ClassPathResource
;
36 import org
.springframework
.core
.io
.Resource
;
39 * Helper to make db vendor independent tests using DbUnit data sets. Based on
40 * {@link DbModelHibernate}.
42 public abstract class IndependentDbTestCase
extends AbstractSpringTestCase
{
43 private IDatabaseTester databaseTester
;
45 /** Creates the DDL of the data model and loads the data. */
47 protected void setUp() throws Exception
{
50 databaseTester
= new DataSourceDatabaseTester(getDataSource());
51 databaseTester
.setSetUpOperation(new DatabaseOperation() {
54 public void execute(IDatabaseConnection connection
, IDataSet dataSet
)
55 throws DatabaseUnitException
, SQLException
{
56 DbModelHibernate dbModel
= getDbModel();
57 SchemaExport schemaExport
= dbModel
58 .createSchemaExport(connection
.getConnection());
59 schemaExport
.create(true, true);
61 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
65 databaseTester
.setDataSet(createDataSet());
66 databaseTester
.onSetup();
70 protected void tearDown() throws Exception
{
71 if (databaseTester
!= null) {
72 databaseTester
.onTearDown();
78 * The data source to use. The default implementation returns a bean named
79 * {@link #getDataSourceBeanName}
81 protected DataSource
getDataSource() {
82 return getBean(DataSource
.class);
86 * Creates the data set to use. The default implementation creates a
87 * <code>FlatXmlDataSet</code> load from the resource defined in
88 * {@link #getDataSetResource()}
90 protected IDataSet
createDataSet() {
91 InputStream in
= null;
93 in
= getDataSetResource().getInputStream();
94 String
[] replaceStrings
= getReplacementStrings();
96 if (replaceStrings
.length
== 0) {
97 dataSet
= new FlatXmlDataSet(in
);
99 dataSet
= new ReplacementDataSet(new FlatXmlDataSet(in
));
100 for (String str
: replaceStrings
) {
101 replace((ReplacementDataSet
) dataSet
, str
);
105 } catch (Exception e
) {
106 throw new SlcException("Cannot create data set", e
);
108 IOUtils
.closeQuietly(in
);
113 * To be overridden. Return an empty array by default.
115 * @return the array of strings to replace in the dataset
117 protected String
[] getReplacementStrings() {
118 return new String
[0];
122 * Set the object replacing the given string. To be overridden. Does nothing
125 protected void replace(ReplacementDataSet dataSet
, String str
)
131 * Replace the given string by the content of the resource with the same
132 * name in the same package, as a byte array.
134 protected void replaceByRessource(ReplacementDataSet dataSet
, String str
)
136 Resource zipResource
= new ClassPathResource(inPackage(str
));
138 dataSet
.addReplacementObject(str
, IOUtils
.toByteArray(zipResource
143 * The resource of the data set to load. The default implementation loads a
144 * <code>ClassPathResource</code> located at
145 * {@link #getDataSetResourceLocation()}.
147 protected Resource
getDataSetResource() {
148 return new ClassPathResource(getDataSetResourceLocation());
152 * The location of the data set to load. The default implementation loads
153 * <i>dataSet.xml</i> found in the same package as the test.
155 protected String
getDataSetResourceLocation() {
156 return inPackage("dataSet.xml");
160 * The DB model to us to create the DDL of the testes database. The default
161 * implementation loads a bean named after {@link #getDbModelBeanName()}.
163 protected DbModelHibernate
getDbModel() {
164 return (DbModelHibernate
) getContext().getBean(getDbModelBeanName());
168 * The name of the bean to load. The default implementation returns
171 protected String
getDbModelBeanName() {