001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
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 as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import junit.framework.TestCase;
028: import org.dbunit.database.IDatabaseConnection;
029: import org.dbunit.dataset.IDataSet;
030: import org.dbunit.operation.DatabaseOperation;
031:
032: /**
033: * @author Manuel Laflamme
034: * @version $Revision: 554 $
035: * @since Feb 17, 2002
036: * TODO: mark it as deprecated use #{DBTestCase} instead.
037: */
038: public abstract class DatabaseTestCase extends TestCase {
039:
040: /**
041: * Logger for this class
042: */
043: private static final Logger logger = LoggerFactory
044: .getLogger(DatabaseTestCase.class);
045:
046: private IDatabaseTester tester;
047:
048: public DatabaseTestCase() {
049: }
050:
051: public DatabaseTestCase(String name) {
052: super (name);
053: }
054:
055: /**
056: * Returns the test database connection.
057: */
058: protected abstract IDatabaseConnection getConnection()
059: throws Exception;
060:
061: /**
062: * Returns the test dataset.
063: */
064: protected abstract IDataSet getDataSet() throws Exception;
065:
066: /**
067: * Creates a IDatabaseTester for this testCase.<br>
068: *
069: * A {@link DefaultDatabaseTester} is used by default.
070: * @throws Exception
071: */
072: protected IDatabaseTester newDatabaseTester() throws Exception {
073: logger.debug("newDatabaseTester() - start");
074:
075: final IDatabaseConnection connection = getConnection();
076: final IDatabaseTester tester = new DefaultDatabaseTester(
077: connection);
078: return tester;
079: }
080:
081: /**
082: * Gets the IDatabaseTester for this testCase.<br>
083: * If the IDatabaseTester is not set yet, this method calls
084: * newDatabaseTester() to obtain a new instance.
085: * @throws Exception
086: */
087: protected IDatabaseTester getDatabaseTester() throws Exception {
088: logger.debug("getDatabaseTester() - start");
089:
090: if (this .tester == null) {
091: this .tester = newDatabaseTester();
092: }
093: return this .tester;
094: }
095:
096: /**
097: * Close the specified connection. Override this method of you want to
098: * keep your connection alive between tests.
099: */
100: protected void closeConnection(IDatabaseConnection connection)
101: throws Exception {
102: logger.debug("closeConnection(connection=" + connection
103: + ") - start");
104:
105: assertNotNull("DatabaseTester is not set", getDatabaseTester());
106: getDatabaseTester().closeConnection(connection);
107: }
108:
109: /**
110: * Returns the database operation executed in test setup.
111: */
112: protected DatabaseOperation getSetUpOperation() throws Exception {
113: logger.debug("getSetUpOperation() - start");
114:
115: return DatabaseOperation.CLEAN_INSERT;
116: }
117:
118: /**
119: * Returns the database operation executed in test cleanup.
120: */
121: protected DatabaseOperation getTearDownOperation() throws Exception {
122: logger.debug("getTearDownOperation() - start");
123:
124: return DatabaseOperation.NONE;
125: }
126:
127: ////////////////////////////////////////////////////////////////////////////
128: // TestCase class
129:
130: protected void setUp() throws Exception {
131: logger.debug("setUp() - start");
132:
133: super .setUp();
134: final IDatabaseTester databaseTester = getDatabaseTester();
135: assertNotNull("DatabaseTester is not set", databaseTester);
136: databaseTester.setSetUpOperation(getSetUpOperation());
137: databaseTester.setDataSet(getDataSet());
138: databaseTester.onSetup();
139: }
140:
141: protected void tearDown() throws Exception {
142: logger.debug("tearDown() - start");
143:
144: try {
145: final IDatabaseTester databaseTester = getDatabaseTester();
146: assertNotNull("DatabaseTester is not set", databaseTester);
147: databaseTester.setTearDownOperation(getTearDownOperation());
148: databaseTester.setDataSet(getDataSet());
149: databaseTester.onTearDown();
150: } finally {
151: super.tearDown();
152: }
153: }
154: }
|