1 /*
2  * AbstractJdbcTests.java
3  *
4  * Copyright (C) 2002 by Interprise Software.  All rights reserved.
5  */
6 /*
7  * Copyright 2002-2005 the original author or authors.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 package org.springframework.jdbc;
23 
24 import java.sql.Connection;
25 
26 import javax.sql.DataSource;
27 
28 import junit.framework.TestCase;
29 import org.easymock.MockControl;
30 
31 /**
32  * @author Trevor D. Cook
33  */
34 public abstract class AbstractJdbcTests extends TestCase {
35 
36 	protected MockControl ctrlDataSource;
37 	protected DataSource mockDataSource;
38 	protected MockControl ctrlConnection;
39 	protected Connection mockConnection;
40 
41 	/**
42 	 * Set to true if the user wants verification, indicated
43 	 * by a call to replay(). We need to make this optional,
44 	 * otherwise we setUp() will always result in verification failures
45 	 */
46 	private boolean shouldVerify;
47 
setUp()48 	protected void setUp() throws Exception {
49 		this.shouldVerify = false;
50 		super.setUp();
51 
52 		ctrlConnection = MockControl.createControl(Connection.class);
53 		mockConnection = (Connection) ctrlConnection.getMock();
54 		mockConnection.getMetaData();
55 		ctrlConnection.setDefaultReturnValue(null);
56 		mockConnection.close();
57 		ctrlConnection.setDefaultVoidCallable();
58 
59 		ctrlDataSource = MockControl.createControl(DataSource.class);
60 		mockDataSource = (DataSource) ctrlDataSource.getMock();
61 		mockDataSource.getConnection();
62 		ctrlDataSource.setDefaultReturnValue(mockConnection);
63 	}
64 
replay()65 	protected void replay() {
66 		ctrlDataSource.replay();
67 		ctrlConnection.replay();
68 		this.shouldVerify = true;
69 	}
70 
tearDown()71 	protected void tearDown() throws Exception {
72 		super.tearDown();
73 
74 		// we shouldn't verify unless the user called replay()
75 		if (shouldVerify()) {
76 			ctrlDataSource.verify();
77 			//ctrlConnection.verify();
78 		}
79 	}
80 
shouldVerify()81 	protected boolean shouldVerify() {
82 		return this.shouldVerify;
83 	}
84 
85 }
86