001: /*
002: Loader - tool for transfering data from one JDBC source to another and
003: doing transformations during copy.
004: Copyright (C) 2002 Together
005: This library is free software; you can redistribute it and/or
006: modify it under the terms of the GNU Lesser General Public
007: License as published by the Free Software Foundation; either
008: version 2.1 of the License, or (at your option) any later version.
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013: You should have received a copy of the GNU Lesser General Public
014: License along with this library; if not, write to the Free Software
015: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: /*
017: * DatabaseTestCase.java Aug 29, 2002
018: * Sinisa Milosevic sinisami@eunet.yu
019: *
020: */
021:
022: package org.webdocwf.util.loader.test;
023:
024: import java.sql.Connection;
025: import org.webdocwf.util.loader.test.DatabaseOperation;
026: import org.webdocwf.util.loader.test.LoaderOperation;
027: import org.webdocwf.util.loader.Loader;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Basic class for using Loader as a test case (extends Junit Test class)
033: *
034: * @author Sinisa Milosevic
035: * @version $Revision: 1.1 $
036: */
037: public abstract class LoaderTestCase extends TestCase {
038:
039: public LoaderTestCase(String name) {
040: super (name);
041: }
042:
043: /**
044: * Returns the test database connection.
045: */
046: protected abstract Connection getConnection() throws Exception;
047:
048: /**
049: * Returns the name of test database.
050: */
051:
052: protected String getDatabaseName() throws Exception {
053: return "LoaderTest";
054: }
055:
056: /**
057: * Returns the test Loader class (loaderjob).
058: */
059: protected abstract Loader getLoader() throws Exception;
060:
061: /**
062: * Close the specified connection. Override this method of you want to
063: * keep your connection alive between tests.
064: */
065: protected void closeConnection(Connection connection)
066: throws Exception {
067: connection.close();
068: }
069:
070: /**
071: * Returns the database operations executed in test setup. First operation will be
072: * executed dbOperation[0], then dbOperation[1]...
073: * Override this method of you want to change database operations before the start test.
074: */
075: protected DatabaseOperation[] getSetUpOperation() throws Exception {
076: DatabaseOperation[] dbOperation = new DatabaseOperation[2];
077: dbOperation[0] = new CreateDatabaseOperation(getDatabaseName());
078: dbOperation[1] = new LoaderOperation(getLoader());
079:
080: return dbOperation;
081: }
082:
083: /**
084: * Returns the database operation executed in test cleanup.
085: * First operation will be executed dbOperation[0], then dbOperation[1]...
086: * Override this method of you want to change database operations after the test execution.
087: */
088: protected DatabaseOperation[] getTearDownOperation()
089: throws Exception {
090: DatabaseOperation[] dbOperation = new DatabaseOperation[1];
091: dbOperation[0] = new DropDatabaseOperation(getDatabaseName());
092:
093: return dbOperation;
094: }
095:
096: private void executeOperation(DatabaseOperation dbOperation)
097: throws Exception {
098: if (dbOperation != null) {
099: if (!dbOperation.getDatabaseOperationType().equals(
100: DatabaseOperation.NONE)) {
101: if (!dbOperation.getDatabaseOperationType().equals(
102: DatabaseOperation.LOADER)) {
103: Connection conn = getConnection();
104: try {
105: if (conn != null)
106: dbOperation.execute(conn);
107: } finally {
108: if (conn != null)
109: closeConnection(conn);
110: }
111: } else
112: ((LoaderOperation) dbOperation).execute();
113: }
114: }
115: }
116:
117: protected void setUp() throws Exception {
118: super .setUp();
119:
120: for (int i = 0; i < getSetUpOperation().length; i++)
121: executeOperation(getSetUpOperation()[i]);
122: }
123:
124: protected void tearDown() throws Exception {
125: super .tearDown();
126:
127: for (int i = 0; i < getTearDownOperation().length; i++)
128: executeOperation(getTearDownOperation()[i]);
129: }
130: }
|