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