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