View Javadoc
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  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import junit.framework.TestCase;
29  
30  import org.dbunit.database.DatabaseConfig;
31  import org.dbunit.database.IDatabaseConnection;
32  import org.dbunit.dataset.IDataSet;
33  import org.dbunit.operation.DatabaseOperation;
34  
35  /**
36   * Convenience class for writing JUnit tests with dbunit easily.
37   * <br />
38   * Note that there are some even more convenient classes available such
39   * as {@link DBTestCase}. 
40   * 
41   * @author Manuel Laflamme
42   * @version $Revision$
43   * @since 1.0 (Feb 17, 2002)
44   */
45  public abstract class DatabaseTestCase extends TestCase
46  {
47  
48      /**
49       * Logger for this class
50       */
51      private static final Logger logger = LoggerFactory.getLogger(DatabaseTestCase.class);
52  
53      private IDatabaseTester tester;
54  
55      private IOperationListener operationListener;
56  
57      public DatabaseTestCase()
58      {
59      }
60  
61      public DatabaseTestCase(String name)
62      {
63          super(name);
64      }
65  
66      /**
67       * Returns the test database connection.
68       */
69      protected abstract IDatabaseConnection getConnection() throws Exception;
70  
71      /**
72       * Returns the test dataset.
73       */
74      protected abstract IDataSet getDataSet() throws Exception;
75  
76      /**
77       * Creates a IDatabaseTester for this testCase.<br>
78       *
79       * A {@link DefaultDatabaseTester} is used by default.
80       * @throws Exception
81       */
82      protected IDatabaseTester newDatabaseTester() throws Exception{
83          logger.debug("newDatabaseTester() - start");
84  
85        final IDatabaseConnection connection = getConnection();
86        getOperationListener().connectionRetrieved(connection);
87        final IDatabaseTester tester = new DefaultDatabaseTester(connection);
88        return tester;
89      }
90  
91      /**
92       * Designed to be overridden by subclasses in order to set additional configuration
93       * parameters for the {@link IDatabaseConnection}.
94       * @param config The settings of the current {@link IDatabaseConnection} to be configured
95       */
96      protected void setUpDatabaseConfig(DatabaseConfig config) 
97      {
98          // Designed to be overridden.
99      }
100 
101     /**
102      * Gets the IDatabaseTester for this testCase.<br>
103      * If the IDatabaseTester is not set yet, this method calls
104      * newDatabaseTester() to obtain a new instance.
105      * @throws Exception
106      */
107     protected IDatabaseTester getDatabaseTester() throws Exception {
108       if ( this.tester == null ) {
109         this.tester = newDatabaseTester();
110       }
111       return this.tester;
112     }
113 
114     /**
115      * Close the specified connection. Override this method of you want to
116      * keep your connection alive between tests.
117      * @deprecated since 2.4.4 define a user defined {@link #getOperationListener()} in advance
118      */
119     protected void closeConnection(IDatabaseConnection connection) throws Exception
120     {
121         logger.debug("closeConnection(connection={}) - start", connection);
122 
123         assertNotNull( "DatabaseTester is not set", getDatabaseTester() );
124         getDatabaseTester().closeConnection( connection );
125     }
126 
127     /**
128      * Returns the database operation executed in test setup.
129      */
130     protected DatabaseOperation getSetUpOperation() throws Exception
131     {
132         return DatabaseOperation.CLEAN_INSERT;
133     }
134 
135     /**
136      * Returns the database operation executed in test cleanup.
137      */
138     protected DatabaseOperation getTearDownOperation() throws Exception
139     {
140         return DatabaseOperation.NONE;
141     }
142 
143     ////////////////////////////////////////////////////////////////////////////
144     // TestCase class
145 
146     protected void setUp() throws Exception
147     {
148         logger.debug("setUp() - start");
149 
150         super.setUp();
151         final IDatabaseTester databaseTester = getDatabaseTester();
152         assertNotNull( "DatabaseTester is not set", databaseTester );
153         databaseTester.setSetUpOperation( getSetUpOperation() );
154         databaseTester.setDataSet( getDataSet() );
155         databaseTester.setOperationListener(getOperationListener());
156         databaseTester.onSetup();
157     }
158 
159     protected void tearDown() throws Exception
160     {
161         logger.debug("tearDown() - start");
162 
163         try {
164             final IDatabaseTester databaseTester = getDatabaseTester();
165             assertNotNull( "DatabaseTester is not set", databaseTester );
166             databaseTester.setTearDownOperation( getTearDownOperation() );
167             databaseTester.setDataSet( getDataSet() );
168             databaseTester.setOperationListener(getOperationListener());
169             databaseTester.onTearDown();
170         } finally {
171             tester = null;
172             super.tearDown();
173         }
174     }
175     
176     /**
177      * @return The {@link IOperationListener} to be used by the {@link IDatabaseTester}.
178      * @since 2.4.4
179      */
180     protected IOperationListener getOperationListener()
181     {
182         logger.debug("getOperationListener() - start");
183         if(this.operationListener==null){
184             this.operationListener = new DefaultOperationListener(){
185                 public void connectionRetrieved(IDatabaseConnection connection) {
186                     super.connectionRetrieved(connection);
187                     // When a new connection has been created then invoke the setUp method
188                     // so that user defined DatabaseConfig parameters can be set.
189                     setUpDatabaseConfig(connection.getConfig());
190                 }
191             };
192         }
193         return this.operationListener;
194     }
195 }