2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.slc
.unit
.db
;
18 import java
.sql
.SQLException
;
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
;
38 public class DbUnitDeployment
implements Deployment
{
39 private static Log log
= LogFactory
.getLog(DbUnitDeployment
.class);
41 private JdbcAware jdbcAware
;
42 private DbUnitDeploymentData deploymentData
;
43 private DbModel dbModel
;
47 IDatabaseTester databaseTester
= new DataSourceDatabaseTester(
48 jdbcAware
.getDataSource());
49 databaseTester
.setSetUpOperation(new DatabaseOperation() {
52 public void execute(IDatabaseConnection connection
,
53 IDataSet dataSet
) throws DatabaseUnitException
,
55 if (dbModel
!= null) {
56 dbModel
.createSchema(connection
.getConnection());
57 DatabaseOperation
.INSERT
.execute(connection
, dataSet
);
59 DatabaseOperation
.UPDATE
.execute(connection
, dataSet
);
64 databaseTester
.setDataSet(deploymentData
.createDataSet());
65 databaseTester
.onSetup();
66 databaseTester
.onTearDown();
68 log
.info("Database deployed.");
69 } catch (Exception e
) {
70 throw new SlcException("Could not initialize the database", e
);
74 public DeployedSystem
getDeployedSystem() {
75 throw new UnsupportedOperationException();
78 public void setDbModel(DbModel dbModel
) {
79 this.dbModel
= dbModel
;
82 public void setDeploymentData(DeploymentData deploymentData
) {
83 this.deploymentData
= (DbUnitDeploymentData
) deploymentData
;
86 public void setTargetData(TargetData targetData
) {
87 this.jdbcAware
= (JdbcAware
) targetData
;
91 public void setDistribution(Distribution distribution
) {
92 throw new UnsupportedException("Method not supported");