KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > test > LoaderTestCase


1 /*
2   Loader - tool for transfering data from one JDBC source to another and
3   doing transformations during copy.
4     Copyright (C) 2002 Together
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12     Lesser General Public License for more details.
13     You should have received a copy of the GNU Lesser General Public
14     License along with this library; if not, write to the Free Software
15     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  /*
17   * DatabaseTestCase.java Aug 29, 2002
18   * Sinisa Milosevic sinisami@eunet.yu
19   *
20   */

21
22 package org.webdocwf.util.loader.test;
23
24 import java.sql.Connection JavaDoc;
25 import org.webdocwf.util.loader.test.DatabaseOperation;
26 import org.webdocwf.util.loader.test.LoaderOperation;
27 import org.webdocwf.util.loader.Loader;
28
29 import junit.framework.TestCase;
30
31 /**
32  * Basic class for using Loader as a test case (extends Junit Test class)
33  *
34  * @author Sinisa Milosevic
35  * @version $Revision: 1.1 $
36  */

37 public abstract class LoaderTestCase
38     extends TestCase {
39
40   public LoaderTestCase(String JavaDoc name) {
41     super(name);
42   }
43
44   /**
45    * Returns the test database connection.
46    */

47   protected abstract Connection JavaDoc getConnection() throws Exception JavaDoc;
48
49   /**
50    * Returns the name of test database.
51    */

52
53   protected String JavaDoc getDatabaseName() throws Exception JavaDoc {
54     return "LoaderTest";
55   }
56
57   /**
58    * Returns the test Loader class (loaderjob).
59    */

60   protected abstract Loader getLoader() throws Exception JavaDoc;
61
62   /**
63    * Close the specified connection. Override this method of you want to
64    * keep your connection alive between tests.
65    */

66   protected void closeConnection(Connection JavaDoc connection) throws Exception JavaDoc {
67     connection.close();
68   }
69
70   /**
71    * Returns the database operations executed in test setup. First operation will be
72    * executed dbOperation[0], then dbOperation[1]...
73    * Override this method of you want to change database operations before the start test.
74    */

75   protected DatabaseOperation[] getSetUpOperation() throws Exception JavaDoc {
76     DatabaseOperation[] dbOperation = new DatabaseOperation[2];
77     dbOperation[0] = new CreateDatabaseOperation(getDatabaseName());
78     dbOperation[1] = new LoaderOperation(getLoader());
79
80     return dbOperation;
81   }
82
83   /**
84    * Returns the database operation executed in test cleanup.
85    * First operation will be executed dbOperation[0], then dbOperation[1]...
86    * Override this method of you want to change database operations after the test execution.
87    */

88   protected DatabaseOperation[] getTearDownOperation() throws Exception JavaDoc {
89     DatabaseOperation[] dbOperation = new DatabaseOperation[1];
90     dbOperation[0] = new DropDatabaseOperation(getDatabaseName());
91
92     return dbOperation;
93   }
94
95   private void executeOperation(DatabaseOperation dbOperation) throws Exception JavaDoc {
96     if (dbOperation != null) {
97       if (!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.NONE)) {
98         if (!dbOperation.getDatabaseOperationType().equals(DatabaseOperation.LOADER)) {
99         Connection JavaDoc conn = getConnection();
100           try {
101             if (conn != null)
102               dbOperation.execute(conn);
103           }
104           finally {
105             if (conn != null)
106               closeConnection(conn);
107           }
108         } else
109           ( (LoaderOperation)dbOperation).execute();
110       }
111     }
112   }
113
114   protected void setUp() throws Exception JavaDoc {
115     super.setUp();
116
117     for (int i = 0; i < getSetUpOperation().length; i++)
118       executeOperation(getSetUpOperation()[i]);
119   }
120
121   protected void tearDown() throws Exception JavaDoc {
122     super.tearDown();
123
124     for (int i = 0; i < getTearDownOperation().length; i++)
125       executeOperation(getTearDownOperation()[i]);
126   }
127 }
128
Popular Tags