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