KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > DatabaseTestCase


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit;
23
24 import junit.framework.TestCase;
25 import org.dbunit.database.IDatabaseConnection;
26 import org.dbunit.dataset.IDataSet;
27 import org.dbunit.operation.DatabaseOperation;
28
29 /**
30  * @author Manuel Laflamme
31  * @version $Revision: 1.11 $
32  * @since Feb 17, 2002
33  */

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

48     protected abstract IDatabaseConnection getConnection() throws Exception JavaDoc;
49
50     /**
51      * Returns the test dataset.
52      */

53     protected abstract IDataSet getDataSet() throws Exception JavaDoc;
54
55     /**
56      * Close the specified connection. Ovverride this method of you want to
57      * keep your connection alive between tests.
58      */

59     protected void closeConnection(IDatabaseConnection connection) throws Exception JavaDoc
60     {
61         connection.close();
62     }
63
64     /**
65      * Returns the database operation executed in test setup.
66      */

67     protected DatabaseOperation getSetUpOperation() throws Exception JavaDoc
68     {
69         return DatabaseOperation.CLEAN_INSERT;
70     }
71
72     /**
73      * Returns the database operation executed in test cleanup.
74      */

75     protected DatabaseOperation getTearDownOperation() throws Exception JavaDoc
76     {
77         return DatabaseOperation.NONE;
78     }
79
80     private void executeOperation(DatabaseOperation operation) throws Exception JavaDoc
81     {
82         if (operation != DatabaseOperation.NONE)
83         {
84             IDatabaseConnection connection = getConnection();
85             try
86             {
87                 operation.execute(connection, getDataSet());
88             }
89             finally
90             {
91                 closeConnection(connection);
92             }
93         }
94     }
95
96
97     ////////////////////////////////////////////////////////////////////////////
98
// TestCase class
99

100     protected void setUp() throws Exception JavaDoc
101     {
102         super.setUp();
103
104         executeOperation(getSetUpOperation());
105     }
106
107     protected void tearDown() throws Exception JavaDoc
108     {
109         super.tearDown();
110
111         executeOperation(getTearDownOperation());
112     }
113 }
114
115
116
117
118
119
Popular Tags