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