001: /* DbUnitFixture.java
002: *
003: * DDSteps - Data Driven JUnit Test Steps
004: * Copyright (C) 2005 Jayway AB
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License version 2.1 as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, visit
017: * http://www.opensource.org/licenses/lgpl-license.php
018: */
019: package org.ddsteps.fixture.dbunit;
020:
021: import java.sql.SQLException;
022:
023: import org.apache.commons.lang.Validate;
024: import org.dbunit.DatabaseUnitException;
025: import org.dbunit.database.IDatabaseConnection;
026: import org.dbunit.dataset.IDataSet;
027: import org.dbunit.operation.DatabaseOperation;
028: import org.ddsteps.dbunit.DatabaseConnectionFactory;
029: import org.ddsteps.fixture.Fixture;
030:
031: /**
032: * @author adam
033: * @version $Id: DbUnitFixture.java,v 1.1 2005/12/03 12:51:41 adamskogman Exp $
034: */
035: public class DbUnitFixture implements Fixture {
036:
037: /**
038: * Serializable
039: */
040: private static final long serialVersionUID = -3983223493590352621L;
041:
042: /**
043: * Dependency: Database Connection Factory
044: */
045: protected final DatabaseConnectionFactory connectionFactory;
046:
047: /**
048: * Property: The data set containing the fixture.
049: */
050: protected final IDataSet dataSet;
051:
052: /**
053: * Property: The setup operation. Default is clean insert.
054: */
055: protected DatabaseOperation setUpOperation = DatabaseOperation.CLEAN_INSERT;
056:
057: /**
058: * Property: The tearDown operation. Default is none.
059: */
060: protected DatabaseOperation tearDownOperation = DatabaseOperation.NONE;
061:
062: /**
063: * Dependency Injection constructor.
064: *
065: * @param connectionFactory The connection factory
066: * @param dataSet The dataset
067: */
068: public DbUnitFixture(DatabaseConnectionFactory connectionFactory,
069: IDataSet dataSet) {
070: super ();
071: Validate.notNull(connectionFactory,
072: "Argument connectionFactory must not be null");
073: Validate.notNull(dataSet, "Argument dataSet must not be null");
074: this .connectionFactory = connectionFactory;
075: this .dataSet = dataSet;
076: }
077:
078: /**
079: * Executes the setup opertation on the provided dataset.
080: *
081: * @throws SQLException
082: * @throws DatabaseUnitException
083: * @see org.ddsteps.fixture.Fixture#setUp()
084: */
085: public void setUp() throws DatabaseUnitException, SQLException {
086: executeOperation(setUpOperation);
087: }
088:
089: /**
090: * Executes the teardown opertation on the provided dataset.
091: *
092: * @throws SQLException
093: * @throws DatabaseUnitException
094: * @see org.ddsteps.fixture.Fixture#tearDown()
095: */
096: public void tearDown() throws DatabaseUnitException, SQLException {
097: executeOperation(tearDownOperation);
098: }
099:
100: /**
101: * Courtesy of DbUnit... :-)
102: *
103: * @param operation The operation.
104: * @throws SQLException
105: * @throws DatabaseUnitException
106: */
107: protected void executeOperation(DatabaseOperation operation)
108: throws DatabaseUnitException, SQLException {
109: if (operation != DatabaseOperation.NONE) {
110: IDatabaseConnection connection = connectionFactory
111: .getConnection();
112: try {
113: operation.execute(connection, dataSet);
114: } finally {
115: connection.close();
116: }
117: }
118: }
119:
120: /**
121: * @return Returns the setUpOperation.
122: */
123: public DatabaseOperation getSetUpOperation() {
124: return setUpOperation;
125: }
126:
127: /**
128: * @param setUpOperation The setUpOperation to set.
129: */
130: public void setSetUpOperation(DatabaseOperation setUpOperation) {
131: this .setUpOperation = setUpOperation;
132: }
133:
134: /**
135: * @return Returns the tearDownOperation.
136: */
137: public DatabaseOperation getTearDownOperation() {
138: return tearDownOperation;
139: }
140:
141: /**
142: * @param tearDownOperation The tearDownOperation to set.
143: */
144: public void setTearDownOperation(DatabaseOperation tearDownOperation) {
145: this .tearDownOperation = tearDownOperation;
146: }
147:
148: /**
149: * Gets the backing dataset. Used for unit test mainly.
150: *
151: * @return Returns the dataSet.
152: */
153: public IDataSet getDataSet() {
154: return dataSet;
155: }
156:
157: }
|