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