]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java
Take into account OSGiBoot renaming
[gpl/argeo-slc.git] / runtime / org.argeo.slc.unit / src / main / java / org / argeo / slc / hibernate / unit / IndependentDbTestCase.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.hibernate.unit;
18
19 import java.io.InputStream;
20 import java.sql.SQLException;
21
22 import javax.sql.DataSource;
23
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;
38
39 /**
40 * Helper to make db vendor independent tests using DbUnit data sets. Based on
41 * {@link DbModelHibernate}.
42 */
43 public abstract class IndependentDbTestCase extends AbstractSpringTestCase {
44 private IDatabaseTester databaseTester;
45
46 /** Creates the DDL of the data model and loads the data. */
47 @Override
48 protected void setUp() throws Exception {
49 super.setUp();
50
51 databaseTester = new DataSourceDatabaseTester(getDataSource());
52 databaseTester.setSetUpOperation(new DatabaseOperation() {
53
54 @Override
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);
61
62 DatabaseOperation.INSERT.execute(connection, dataSet);
63 }
64
65 });
66 databaseTester.setDataSet(createDataSet());
67 databaseTester.onSetup();
68 }
69
70 @Override
71 protected void tearDown() throws Exception {
72 if (databaseTester != null) {
73 databaseTester.onTearDown();
74 }
75 super.tearDown();
76 }
77
78 /**
79 * The data source to use. The default implementation returns a bean named
80 * {@link #getDataSourceBeanName}
81 */
82 protected DataSource getDataSource() {
83 return getBean(DataSource.class);
84 }
85
86 /**
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()}
90 */
91 protected IDataSet createDataSet() {
92 InputStream in = null;
93 try {
94 in = getDataSetResource().getInputStream();
95 String[] replaceStrings = getReplacementStrings();
96 IDataSet dataSet;
97 if (replaceStrings.length == 0) {
98 dataSet = new FlatXmlDataSet(in);
99 } else {
100 dataSet = new ReplacementDataSet(new FlatXmlDataSet(in));
101 for (String str : replaceStrings) {
102 replace((ReplacementDataSet) dataSet, str);
103 }
104 }
105 return dataSet;
106 } catch (Exception e) {
107 throw new SlcException("Cannot create data set", e);
108 } finally {
109 IOUtils.closeQuietly(in);
110 }
111 }
112
113 /**
114 * To be overridden. Return an empty array by default.
115 *
116 * @return the array of strings to replace in the dataset
117 */
118 protected String[] getReplacementStrings() {
119 return new String[0];
120 }
121
122 /**
123 * Set the object replacing the given string. To be overridden. Does nothing
124 * by default.
125 */
126 protected void replace(ReplacementDataSet dataSet, String str)
127 throws Exception {
128
129 }
130
131 /**
132 * Replace the given string by the content of the resource with the same
133 * name in the same package, as a byte array.
134 */
135 protected void replaceByRessource(ReplacementDataSet dataSet, String str)
136 throws Exception {
137 Resource zipResource = new ClassPathResource(inPackage(str));
138
139 dataSet.addReplacementObject(str, IOUtils.toByteArray(zipResource
140 .getInputStream()));
141 }
142
143 /**
144 * The resource of the data set to load. The default implementation loads a
145 * <code>ClassPathResource</code> located at
146 * {@link #getDataSetResourceLocation()}.
147 */
148 protected Resource getDataSetResource() {
149 return new ClassPathResource(getDataSetResourceLocation());
150 }
151
152 /**
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.
155 */
156 protected String getDataSetResourceLocation() {
157 return inPackage("dataSet.xml");
158 }
159
160 /**
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()}.
163 */
164 protected DbModelHibernate getDbModel() {
165 return (DbModelHibernate) getContext().getBean(getDbModelBeanName());
166 }
167
168 /**
169 * The name of the bean to load. The default implementation returns
170 * <i>dbModel</i>.
171 */
172 protected String getDbModelBeanName() {
173 return "dbModel";
174 }
175 }