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  import org.dbunit.assertion.DefaultFailureHandler;
25  import org.dbunit.assertion.SimpleAssert;
26  import org.dbunit.database.IDatabaseConnection;
27  import org.dbunit.dataset.IDataSet;
28  import org.dbunit.operation.DatabaseOperation;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Basic implementation of IDatabaseTester.<br>
34   * Implementations of IDatabaseTester may use this class as a starting point.
35   *
36   * @author Andres Almiray (aalmiray@users.sourceforge.net)
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since 2.2.0
40   */
41  public abstract class AbstractDatabaseTester extends SimpleAssert implements IDatabaseTester
42  {
43      /**
44       * Logger for this class
45       */
46      private static final Logger logger = LoggerFactory.getLogger(AbstractDatabaseTester.class);
47  
48      /**
49       * Enumeration of the valid {@link OperationType}s
50       */
51      private static final class OperationType
52      {
53          public static final OperationType SET_UP = new OperationType("setUp");
54          public static final OperationType TEAR_DOWN = new OperationType("tearDown");
55  
56          private final String key;
57  
58          private OperationType(String key)
59          {
60              this.key = key;
61          }
62  
63          @Override
64          public String toString()
65          {
66              return "OperationType: " + key;
67          }
68      }
69  
70      private IDataSet dataSet;
71      private String schema;
72      private DatabaseOperation setUpOperation = DatabaseOperation.CLEAN_INSERT;
73      private DatabaseOperation tearDownOperation = DatabaseOperation.NONE;
74      private IOperationListener operationListener;
75  
76      public AbstractDatabaseTester()
77      {
78          this(null);
79      }
80  
81      /**
82       * @param schema
83       *            The schema to be tested. Can be <code>null</code>
84       * @since 2.4.3
85       */
86      public AbstractDatabaseTester(String schema)
87      {
88          super(new DefaultFailureHandler());
89          this.schema = schema;
90      }
91  
92      public void closeConnection(IDatabaseConnection connection) throws Exception
93      {
94          logger.debug("closeConnection(connection={}) - start", connection);
95  
96          connection.close();
97      }
98  
99      public IDataSet getDataSet()
100     {
101         logger.debug("getDataSet() - start");
102 
103         return dataSet;
104     }
105 
106     public void onSetup() throws Exception
107     {
108         logger.debug("onSetup() - start");
109         executeOperation(getSetUpOperation(), OperationType.SET_UP);
110     }
111 
112     public void onTearDown() throws Exception
113     {
114         logger.debug("onTearDown() - start");
115         executeOperation(getTearDownOperation(), OperationType.TEAR_DOWN);
116     }
117 
118     public void setDataSet(IDataSet dataSet)
119     {
120         logger.debug("setDataSet(dataSet={}) - start", dataSet);
121 
122         this.dataSet = dataSet;
123     }
124 
125     public void setSchema(String schema)
126     {
127         logger.debug("setSchema(schema={}) - start", schema);
128 
129         logger.warn("setSchema() should not be used anymore");
130         this.schema = schema;
131     }
132 
133     public void setSetUpOperation(DatabaseOperation setUpOperation)
134     {
135         logger.debug("setSetUpOperation(setUpOperation={}) - start", setUpOperation);
136 
137         this.setUpOperation = setUpOperation;
138     }
139 
140     public void setTearDownOperation(DatabaseOperation tearDownOperation)
141     {
142         logger.debug("setTearDownOperation(tearDownOperation={}) - start", tearDownOperation);
143 
144         this.tearDownOperation = tearDownOperation;
145     }
146 
147     /**
148      * Returns the schema value.
149      */
150     protected String getSchema()
151     {
152         logger.trace("getSchema() - start");
153 
154         return schema;
155     }
156 
157     /**
158      * Returns the DatabaseOperation to call when starting the test.
159      */
160     public DatabaseOperation getSetUpOperation()
161     {
162         logger.trace("getSetUpOperation() - start");
163 
164         return setUpOperation;
165     }
166 
167     /**
168      * Returns the DatabaseOperation to call when ending the test.
169      */
170     public DatabaseOperation getTearDownOperation()
171     {
172         logger.trace("getTearDownOperation() - start");
173 
174         return tearDownOperation;
175     }
176 
177     /**
178      * Executes a DatabaseOperation with a IDatabaseConnection supplied by
179      * {@link getConnection()} and the test dataset.
180      */
181     private void executeOperation(DatabaseOperation operation, OperationType type) throws Exception
182     {
183         logger.debug("executeOperation(operation={}) - start", operation);
184 
185         if (operation != DatabaseOperation.NONE)
186         {
187             // Ensure that the operationListener is set
188             if (operationListener == null)
189             {
190                 logger.debug("OperationListener is null and will be defaulted.");
191                 operationListener = new DefaultOperationListener();
192             }
193 
194             IDatabaseConnection connection = getConnection();
195             operationListener.connectionRetrieved(connection);
196 
197             try
198             {
199                 operation.execute(connection, getDataSet());
200             } finally
201             {
202                 // Since 2.4.4 the OperationListener is responsible for closing
203                 // the connection at the right time
204                 if (type == OperationType.SET_UP)
205                 {
206                     operationListener.operationSetUpFinished(connection);
207                 } else if (type == OperationType.TEAR_DOWN)
208                 {
209                     operationListener.operationTearDownFinished(connection);
210                 } else
211                 {
212                     throw new DatabaseUnitRuntimeException("Cannot happen - unknown OperationType specified: " + type);
213                 }
214             }
215         }
216     }
217 
218     public void setOperationListener(IOperationListener operationListener)
219     {
220         logger.debug("setOperationListener(operationListener={}) - start", operationListener);
221         this.operationListener = operationListener;
222     }
223 
224     @Override
225     public String toString()
226     {
227         StringBuffer sb = new StringBuffer();
228         sb.append(getClass().getName()).append("[");
229         sb.append("schema=").append(schema);
230         sb.append(", dataSet=").append(dataSet);
231         sb.append(", setUpOperation=").append(setUpOperation);
232         sb.append(", tearDownOperation=").append(tearDownOperation);
233         sb.append(", operationListener=").append(operationListener);
234         sb.append("]");
235         return sb.toString();
236     }
237 }