]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/unit/IndependentDbTestCase.java
baf611ef6c69399df4d39af730f2a3cd5d8ece03
[gpl/argeo-slc.git] / org.argeo.slc / 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.xml.FlatXmlDataSet;
14 import org.dbunit.operation.DatabaseOperation;
15 import org.springframework.core.io.ClassPathResource;
16 import org.springframework.core.io.Resource;
17
18 import org.hibernate.tool.hbm2ddl.SchemaExport;
19
20 import org.argeo.slc.core.SlcException;
21
22 /**
23 * Helper to make db vendor independent tests using DbUnit data sets. Based on
24 * {@link DbModel}.
25 */
26 public class IndependentDbTestCase extends SpringBasedTestCase {
27 private IDatabaseTester databaseTester;
28
29 /** Creates the DDL of the data model and loads the data. */
30 @Override
31 protected void setUp() throws Exception {
32 super.setUp();
33
34 databaseTester = new DataSourceDatabaseTester(getDataSource());
35 databaseTester.setSetUpOperation(new DatabaseOperation() {
36
37 @Override
38 public void execute(IDatabaseConnection connection, IDataSet dataSet)
39 throws DatabaseUnitException, SQLException {
40 DbModel dbModel = getDbModel();
41 SchemaExport schemaExport = dbModel
42 .createSchemaExport(connection.getConnection());
43 schemaExport.create(true, true);
44
45 DatabaseOperation.INSERT.execute(connection, dataSet);
46 }
47
48 });
49 databaseTester.setDataSet(createDataSet());
50 databaseTester.onSetup();
51 }
52
53 @Override
54 protected void tearDown() throws Exception {
55 if (databaseTester != null) {
56 databaseTester.onTearDown();
57 }
58 super.tearDown();
59 }
60
61 /**
62 * The data source to use. The default implementation returns a bean named
63 * {@link #getDataSourceBeanName}
64 */
65 protected DataSource getDataSource() {
66 return (DataSource) getContext().getBean(getDataSourceBeanName());
67 }
68
69 /**
70 * The name of the data source bean to use. The default implementation
71 * returns <i>dataSource</i>.
72 */
73 protected String getDataSourceBeanName() {
74 return "dataSource";
75 }
76
77 /**
78 * Creates the data set to use. The default implementation creates a
79 * <code>FlatXmlDataSet</code> load from the resource defined in
80 * {@link #getDataSetResource()}
81 */
82 protected IDataSet createDataSet() {
83 try {
84 InputStream in = getDataSetResource().getInputStream();
85 IDataSet dataSet = new FlatXmlDataSet(in);
86 in.close();
87 return dataSet;
88 } catch (Exception e) {
89 throw new SlcException("Cannot create data set", e);
90 }
91 }
92
93 /**
94 * The resource of the data set to load. The default implementation loads a
95 * <code>ClassPathResource</code> located at
96 * {@link #getDataSetResourceLocation()}.
97 */
98 protected Resource getDataSetResource() {
99 return new ClassPathResource(getDataSetResourceLocation());
100 }
101
102 /**
103 * The location of the data set to load. The default implementation loads
104 * <i>dataSet.xml</i> found in the same package as the test.
105 */
106 protected String getDataSetResourceLocation() {
107 return inPackage("dataSet.xml");
108 }
109
110 /**
111 * The DB model to us to create the DDL of the testes database. The default
112 * implementation loads a bean named after {@link #getDbModelBeanName()}.
113 */
114 protected DbModel getDbModel() {
115 return (DbModel) getContext().getBean(getDbModelBeanName());
116 }
117
118 /**
119 * The name of the bean to load. The default implementation returns
120 * <i>dbModel</i>.
121 */
122 protected String getDbModelBeanName() {
123 return "dbModel";
124 }
125 }