2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.unit
.db
;
19 import java
.sql
.SQLException
;
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
;
39 public class DbUnitDeployment
implements Deployment
{
40 private static Log log
= LogFactory
.getLog(DbUnitDeployment
.class);
42 private JdbcAware jdbcAware
;
43 private DbUnitDeploymentData deploymentData
;
44 private DbModel dbModel
;
48 IDatabaseTester databaseTester
= new DataSourceDatabaseTester(
49 jdbcAware
.getDataSource());
50 databaseTester
.setSetUpOperation(new DatabaseOperation() {
53 public void execute(IDatabaseConnection connection
,
54 IDataSet dataSet
) throws DatabaseUnitException
,
56 if (dbModel
!= null) {
57 dbModel
.createSchema(connection
.getConnection());
58 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
60 DatabaseOperation
.UPDATE
.execute(connection
, dataSet
);
65 databaseTester
.setDataSet(deploymentData
.createDataSet());
66 databaseTester
.onSetup();
67 databaseTester
.onTearDown();
69 log
.info("Database deployed.");
70 } catch (Exception e
) {
71 throw new SlcException("Could not initialize the database", e
);
75 public DeployedSystem
getDeployedSystem() {
76 throw new UnsupportedOperationException();
79 public void setDbModel(DbModel dbModel
) {
80 this.dbModel
= dbModel
;
83 public void setDeploymentData(DeploymentData deploymentData
) {
84 this.deploymentData
= (DbUnitDeploymentData
) deploymentData
;
87 public void setTargetData(TargetData targetData
) {
88 this.jdbcAware
= (JdbcAware
) targetData
;
92 public void setDistribution(Distribution distribution
) {
93 throw new UnsupportedException("Method not supported");