1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
37
38
39
40
41 public abstract class AbstractDatabaseTester extends SimpleAssert implements IDatabaseTester
42 {
43
44
45
46
47 private static final Logger logger = LoggerFactory.getLogger(AbstractDatabaseTester.class);
48
49
50
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
78
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
143
144 protected String getSchema()
145 {
146 logger.trace("getSchema() - start");
147
148 return schema;
149 }
150
151
152
153
154 protected DatabaseOperation getSetUpOperation()
155 {
156 logger.trace("getSetUpOperation() - start");
157
158 return setUpOperation;
159 }
160
161
162
163
164 protected DatabaseOperation getTearDownOperation()
165 {
166 logger.trace("getTearDownOperation() - start");
167
168 return tearDownOperation;
169 }
170
171
172
173
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
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
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