Source code: com/mockobjects/sql/CommonMockDataSource.java
1 package com.mockobjects.sql;
2
3 import java.io.PrintWriter;
4 import java.sql.Connection;
5 import javax.sql.DataSource;
6 import com.mockobjects.ExpectationCounter;
7 import com.mockobjects.MockObject;
8
9 /**
10 * Abstract DataSource for use with mock testing.
11 * Only the connection methods have been implemented here.
12 * If testing of the log methods is needed, please submit a patch.
13 * @see <a href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/sql/DataSource.html">javax.sql.DataSource</a>
14 * @author Ted Husted
15 * @version $Revision: 1.1 $ $Date: 2002/08/27 16:34:04 $
16 */
17 public abstract class CommonMockDataSource extends MockObject
18 implements DataSource {
19
20 private Connection myConnection;
21
22 private ExpectationCounter myConnectCalls =
23 new ExpectationCounter("CommonMockDataSource.connection");
24
25 /**
26 * Register the number of connections the test should make.
27 * The valid method will report any discrepancy with the
28 * actual count.
29 */
30 public void setExpectedConnectCalls(int callCount) {
31 myConnectCalls.setExpected(callCount);
32 }
33
34 /**
35 * Pass the connection instance for use with tests.
36 * This instance will be returned until replaced with another.
37 */
38 public void setupConnection(Connection aConnection) {
39 myConnection = aConnection;
40 }
41
42 // -------------------------------------------------------- implemented
43
44 /**
45 * Returns connection instance passed by setupConnection,
46 * and increments the number of connect calls.
47 */
48 public Connection getConnection() {
49 myConnectCalls.inc();
50 return myConnection;
51 }
52
53 // ------------------------------------------------------ notImplemented
54
55 /**
56 * Calls notImplemented. Returns null.
57 */
58 public Connection getConnection(String username,
59 String password) {
60 notImplemented();
61 return null;
62 }
63
64 /**
65 * Calls notImplemented. Returns 0.
66 */
67 public int getLoginTimeout() {
68 notImplemented();
69 return 0;
70 }
71
72 /**
73 * Calls notImplemented. Returns null.
74 */
75 public PrintWriter getLogWriter() {
76 notImplemented();
77 return null;
78 }
79
80 /**
81 * Calls notImplemented.
82 */
83 public void setLoginTimeout(int seconds) {
84 notImplemented();
85 }
86
87 /**
88 * Calls notImplemented.
89 */
90 public void setLogWriter(PrintWriter out) {
91 notImplemented();
92 }
93 }
94
95
96 /*
97 *
98 * ====================================================================
99 *
100 * The Apache Software License, Version 1.1
101 *
102 * Copyright (c) 2002 The Apache Software Foundation. All rights
103 * reserved.
104 *
105 * Redistribution and use in source and binary forms, with or without
106 * modification, are permitted provided that the following conditions
107 * are met:
108 *
109 * 1. Redistributions of source code must retain the above copyright
110 * notice, this list of conditions and the following disclaimer.
111 *
112 * 2. Redistributions in binary form must reproduce the above copyright
113 * notice, this list of conditions and the following disclaimer in
114 * the documentation and/or other materials provided with the
115 * distribution.
116 *
117 * 3. The end-user documentation included with the redistribution, if
118 * any, must include the following acknowlegement:
119 * "This product includes software developed by the
120 * Apache Software Foundation (http://www.apache.org/)."
121 * Alternately, this acknowlegement may appear in the software itself,
122 * if and wherever such third-party acknowlegements normally appear.
123 *
124 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
125 * Foundation" must not be used to endorse or promote products derived
126 * from this software without prior written permission. For written
127 * permission, please contact apache@apache.org.
128 *
129 * 5. Products derived from this software may not be called "Apache"
130 * nor may "Apache" appear in their names without prior written
131 * permission of the Apache Group.
132 *
133 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
134 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
135 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
136 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
137 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
138 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
139 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
140 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
141 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
142 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
143 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
144 * SUCH DAMAGE.
145 * ====================================================================
146 *
147 * This software consists of voluntary contributions made by many
148 * individuals on behalf of the Apache Software Foundation. For more
149 * information on the Apache Software Foundation, please see
150 * <http://www.apache.org/>.
151 *
152 */