]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java
Merge error management fixes from trunk -r:1685:1690
[gpl/argeo-slc.git] / org.argeo.slc.hibernate / src / main / java / org / argeo / slc / hibernate / unit / IndependentDbTestCase.java
1 package org.argeo.slc.hibernate.unit;
2
3 import java.io.InputStream;
4 import java.sql.SQLException;
5
6 import javax.sql.DataSource;
7
8 import org.apache.commons.io.IOUtils;
9 import org.argeo.slc.core.SlcException;
10 import org.argeo.slc.unit.AbstractSpringTestCase;
11 import org.dbunit.DataSourceDatabaseTester;
12 import org.dbunit.DatabaseUnitException;
13 import org.dbunit.IDatabaseTester;
14 import org.dbunit.database.IDatabaseConnection;
15 import org.dbunit.dataset.IDataSet;
16 import org.dbunit.dataset.ReplacementDataSet;
17 import org.dbunit.dataset.xml.FlatXmlDataSet;
18 import org.dbunit.operation.DatabaseOperation;
19 import org.hibernate.tool.hbm2ddl.SchemaExport;
20 import org.springframework.core.io.ClassPathResource;
21 import org.springframework.core.io.Resource;
22
23 /**
24 * Helper to make db vendor independent tests using DbUnit data sets. Based on
25 * {@link DbModelHibernate}.
26 */
27 public abstract class IndependentDbTestCase extends AbstractSpringTestCase {
28 private IDatabaseTester databaseTester;
29
30 /** Creates the DDL of the data model and loads the data. */
31 @Override
32 protected void setUp() throws Exception {
33 super.setUp();
34
35 databaseTester = new DataSourceDatabaseTester(getDataSource());
36 databaseTester.setSetUpOperation(new DatabaseOperation() {
37
38 @Override
39 public void execute(IDatabaseConnection connection, IDataSet dataSet)
40 throws DatabaseUnitException, SQLException {
41 DbModelHibernate dbModel = getDbModel();
42 SchemaExport schemaExport = dbModel
43 .createSchemaExport(connection.getConnection());
44 schemaExport.create(true, true);
45
46 DatabaseOperation.INSERT.execute(connection, dataSet);
47 }
48
49 });
50 databaseTester.setDataSet(createDataSet());
51 databaseTester.onSetup();
52 }
53
54 @Override
55 protected void tearDown() throws Exception {
56 if (databaseTester != null) {
57 databaseTester.onTearDown();
58 }
59 super.tearDown();
60 }
61
62 /**
63 * The data source to use. The default implementation returns a bean named
64 * {@link #getDataSourceBeanName}
65 */
66 protected DataSource getDataSource() {
67 return (DataSource) getContext().getBean(getDataSourceBeanName());
68 }
69
70 /**
71 * The name of the data source bean to use. The default implementation
72 * returns <i>dataSource</i>.
73 */
74 protected String getDataSourceBeanName() {
75 return "slcDefault.dataSource.inMemoryTestHSQL";
76 }
77
78 /**
79 * Creates the data set to use. The default implementation creates a
80 * <code>FlatXmlDataSet</code> load from the resource defined in
81 * {@link #getDataSetResource()}
82 */
83 protected IDataSet createDataSet() {
84 InputStream in = null;
85 try {
86 in = getDataSetResource().getInputStream();
87 String[] replaceStrings = getReplacementStrings();
88 IDataSet dataSet;
89 if (replaceStrings.length == 0) {
90 dataSet = new FlatXmlDataSet(in);
91 } else {
92 dataSet = new ReplacementDataSet(new FlatXmlDataSet(in));
93 for (String str : replaceStrings) {
94 replace((ReplacementDataSet) dataSet, str);
95 }
96 }
97 return dataSet;
98 } catch (Exception e) {
99 throw new SlcException("Cannot create data set", e);
100 } finally {
101 IOUtils.closeQuietly(in);
102 }
103 }
104
105 /**
106 * To be overridden. Return an empty array by default.
107 *
108 * @return the array of strings to replace in the dataset
109 */
110 protected String[] getReplacementStrings() {
111 return new String[0];
112 }
113
114 /**
115 * Set the object replacing the given string. To be overridden. Does nothing
116 * by default.
117 */
118 protected void replace(ReplacementDataSet dataSet, String str)
119 throws Exception {
120
121 }
122
123 /**
124 * Replace the given string by the content of the resource with the same
125 * name in the same package, as a byte array.
126 */
127 protected void replaceByRessource(ReplacementDataSet dataSet, String str)
128 throws Exception {
129 Resource zipResource = new ClassPathResource(inPackage(str));
130
131 dataSet.addReplacementObject(str, IOUtils.toByteArray(zipResource
132 .getInputStream()));
133 }
134
135 /**
136 * The resource of the data set to load. The default implementation loads a
137 * <code>ClassPathResource</code> located at
138 * {@link #getDataSetResourceLocation()}.
139 */
140 protected Resource getDataSetResource() {
141 return new ClassPathResource(getDataSetResourceLocation());
142 }
143
144 /**
145 * The location of the data set to load. The default implementation loads
146 * <i>dataSet.xml</i> found in the same package as the test.
147 */
148 protected String getDataSetResourceLocation() {
149 return inPackage("dataSet.xml");
150 }
151
152 /**
153 * The DB model to us to create the DDL of the testes database. The default
154 * implementation loads a bean named after {@link #getDbModelBeanName()}.
155 */
156 protected DbModelHibernate getDbModel() {
157 return (DbModelHibernate) getContext().getBean(getDbModelBeanName());
158 }
159
160 /**
161 * The name of the bean to load. The default implementation returns
162 * <i>dbModel</i>.
163 */
164 protected String getDbModelBeanName() {
165 return "dbModel";
166 }
167 }