]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeployment.java
Centralize primitive support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.unit / src / main / java / org / argeo / slc / unit / db / DbUnitDeployment.java
1 package org.argeo.slc.unit.db;
2
3 import java.sql.SQLException;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.slc.SlcException;
8 import org.argeo.slc.UnsupportedException;
9 import org.argeo.slc.build.Distribution;
10 import org.argeo.slc.deploy.DeployedSystem;
11 import org.argeo.slc.deploy.Deployment;
12 import org.argeo.slc.deploy.DeploymentData;
13 import org.argeo.slc.deploy.TargetData;
14 import org.argeo.slc.support.deploy.db.DbModel;
15 import org.argeo.slc.support.deploy.db.JdbcAware;
16 import org.dbunit.DataSourceDatabaseTester;
17 import org.dbunit.DatabaseUnitException;
18 import org.dbunit.IDatabaseTester;
19 import org.dbunit.database.IDatabaseConnection;
20 import org.dbunit.dataset.IDataSet;
21 import org.dbunit.operation.DatabaseOperation;
22
23 public class DbUnitDeployment implements Deployment {
24 private static Log log = LogFactory.getLog(DbUnitDeployment.class);
25
26 private JdbcAware jdbcAware;
27 private DbUnitDeploymentData deploymentData;
28 private DbModel dbModel;
29
30 public void run() {
31 try {
32 IDatabaseTester databaseTester = new DataSourceDatabaseTester(
33 jdbcAware.getDataSource());
34 databaseTester.setSetUpOperation(new DatabaseOperation() {
35
36 @Override
37 public void execute(IDatabaseConnection connection,
38 IDataSet dataSet) throws DatabaseUnitException,
39 SQLException {
40 if (dbModel != null) {
41 dbModel.createSchema(connection.getConnection());
42 DatabaseOperation.INSERT.execute(connection, dataSet);
43 } else {
44 DatabaseOperation.UPDATE.execute(connection, dataSet);
45 }
46 }
47
48 });
49 databaseTester.setDataSet(deploymentData.createDataSet());
50 databaseTester.onSetup();
51 databaseTester.onTearDown();
52
53 log.info("Database deployed.");
54 } catch (Exception e) {
55 throw new SlcException("Could not initialize the database", e);
56 }
57 }
58
59 public DeployedSystem getDeployedSystem() {
60 throw new UnsupportedOperationException();
61 }
62
63 public void setDbModel(DbModel dbModel) {
64 this.dbModel = dbModel;
65 }
66
67 public void setDeploymentData(DeploymentData deploymentData) {
68 this.deploymentData = (DbUnitDeploymentData) deploymentData;
69 }
70
71 public void setTargetData(TargetData targetData) {
72 this.jdbcAware = (JdbcAware) targetData;
73
74 }
75
76 public void setDistribution(Distribution distribution) {
77 throw new UnsupportedException("Method not supported");
78 }
79
80 }