2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.hibernate
.unit
;
19 import java
.io
.InputStream
;
20 import java
.sql
.SQLException
;
22 import javax
.sql
.DataSource
;
24 import org
.apache
.commons
.io
.IOUtils
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.unit
.AbstractSpringTestCase
;
27 import org
.dbunit
.DataSourceDatabaseTester
;
28 import org
.dbunit
.DatabaseUnitException
;
29 import org
.dbunit
.IDatabaseTester
;
30 import org
.dbunit
.database
.IDatabaseConnection
;
31 import org
.dbunit
.dataset
.IDataSet
;
32 import org
.dbunit
.dataset
.ReplacementDataSet
;
33 import org
.dbunit
.dataset
.xml
.FlatXmlDataSet
;
34 import org
.dbunit
.operation
.DatabaseOperation
;
35 import org
.hibernate
.tool
.hbm2ddl
.SchemaExport
;
36 import org
.springframework
.core
.io
.ClassPathResource
;
37 import org
.springframework
.core
.io
.Resource
;
40 * Helper to make db vendor independent tests using DbUnit data sets. Based on
41 * {@link DbModelHibernate}.
43 public abstract class IndependentDbTestCase
extends AbstractSpringTestCase
{
44 private IDatabaseTester databaseTester
;
46 /** Creates the DDL of the data model and loads the data. */
48 protected void setUp() throws Exception
{
51 databaseTester
= new DataSourceDatabaseTester(getDataSource());
52 databaseTester
.setSetUpOperation(new DatabaseOperation() {
55 public void execute(IDatabaseConnection connection
, IDataSet dataSet
)
56 throws DatabaseUnitException
, SQLException
{
57 DbModelHibernate dbModel
= getDbModel();
58 SchemaExport schemaExport
= dbModel
59 .createSchemaExport(connection
.getConnection());
60 schemaExport
.create(true, true);
62 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
66 databaseTester
.setDataSet(createDataSet());
67 databaseTester
.onSetup();
71 protected void tearDown() throws Exception
{
72 if (databaseTester
!= null) {
73 databaseTester
.onTearDown();
79 * The data source to use. The default implementation returns a bean named
80 * {@link #getDataSourceBeanName}
82 protected DataSource
getDataSource() {
83 return getBean(DataSource
.class);
87 * Creates the data set to use. The default implementation creates a
88 * <code>FlatXmlDataSet</code> load from the resource defined in
89 * {@link #getDataSetResource()}
91 protected IDataSet
createDataSet() {
92 InputStream in
= null;
94 in
= getDataSetResource().getInputStream();
95 String
[] replaceStrings
= getReplacementStrings();
97 if (replaceStrings
.length
== 0) {
98 dataSet
= new FlatXmlDataSet(in
);
100 dataSet
= new ReplacementDataSet(new FlatXmlDataSet(in
));
101 for (String str
: replaceStrings
) {
102 replace((ReplacementDataSet
) dataSet
, str
);
106 } catch (Exception e
) {
107 throw new SlcException("Cannot create data set", e
);
109 IOUtils
.closeQuietly(in
);
114 * To be overridden. Return an empty array by default.
116 * @return the array of strings to replace in the dataset
118 protected String
[] getReplacementStrings() {
119 return new String
[0];
123 * Set the object replacing the given string. To be overridden. Does nothing
126 protected void replace(ReplacementDataSet dataSet
, String str
)
132 * Replace the given string by the content of the resource with the same
133 * name in the same package, as a byte array.
135 protected void replaceByRessource(ReplacementDataSet dataSet
, String str
)
137 Resource zipResource
= new ClassPathResource(inPackage(str
));
139 dataSet
.addReplacementObject(str
, IOUtils
.toByteArray(zipResource
144 * The resource of the data set to load. The default implementation loads a
145 * <code>ClassPathResource</code> located at
146 * {@link #getDataSetResourceLocation()}.
148 protected Resource
getDataSetResource() {
149 return new ClassPathResource(getDataSetResourceLocation());
153 * The location of the data set to load. The default implementation loads
154 * <i>dataSet.xml</i> found in the same package as the test.
156 protected String
getDataSetResourceLocation() {
157 return inPackage("dataSet.xml");
161 * The DB model to us to create the DDL of the testes database. The default
162 * implementation loads a bean named after {@link #getDbModelBeanName()}.
164 protected DbModelHibernate
getDbModel() {
165 return (DbModelHibernate
) getContext().getBean(getDbModelBeanName());
169 * The name of the bean to load. The default implementation returns
172 protected String
getDbModelBeanName() {