KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > sql > CommonMockPreparedStatement


1 /*
2  *
3  * ====================================================================
4  *
5  * The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 2002 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution, if
23  * any, must include the following acknowlegement:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowlegement may appear in the software itself,
27  * if and wherever such third-party acknowlegements normally appear.
28  *
29  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
30  * Foundation" must not be used to endorse or promote products derived
31  * from this software without prior written permission. For written
32  * permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache"
35  * nor may "Apache" appear in their names without prior written
36  * permission of the Apache Group.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  *
57  */

58
59 // ------------------------------------------------------------------------ 78
60

61 package com.mockobjects.sql;
62
63 import java.sql.*;
64 import java.util.Calendar JavaDoc;
65 import java.io.Reader JavaDoc;
66 import java.math.BigDecimal JavaDoc;
67 import com.mockobjects.*;
68
69 /**
70  * Abstract PreparedStatement for use with mock testing.
71  * If a notImplemented method is are needed for your project,
72  * please submit a patch.
73  * @see <a HREF="http://java.sun.com/j2se/1.3/docs/api/java/sql/PreparedStatement.html">java.sql.PreparedStatement</a>
74  * @author Jeff Martin
75  * @author Ted Husted
76  * @version $Revision: 1.4 $ $Date: 2003/04/23 11:52:46 $
77  */

78 abstract class CommonMockPreparedStatement
79     extends CommonMockStatement implements PreparedStatement {
80
81     private ExpectationSet mySetParameters =
82         new ExpectationSet("CommonMockPreparedStatement.setParameters");
83
84     private ExpectationCounter myClearParametersCalls =
85         new ExpectationCounter
86             ("CommonMockPreparedStatement.clearParameters() calls");
87
88     private ExpectationSet myTargetSQLTypes =
89         new ExpectationSet("CommonMockPreparedStatement.targetSQLTypes");
90     private final ReturnObjectList myResultSets = new ReturnObjectList("result sets");
91     private final ReturnObjectList executeUpdates = new ReturnObjectList("update count");
92
93     public void addResultSet(MockResultSet aResultSet) {
94          myResultSets.addObjectToReturn(aResultSet);
95      }
96
97     public void addExpectedSetParameter(int parameterIndex, int intValue) {
98         addExpectedSetParameter(parameterIndex, new Integer JavaDoc(intValue));
99     }
100
101     public void addExpectedSetParameter(int parameterIndex,
102         Object JavaDoc parameterValue) {
103         mySetParameters.addExpected(new MapEntry(new Integer JavaDoc(parameterIndex),
104             parameterValue));
105     }
106
107     public void addExpectedSetParameters(Object JavaDoc[] parameters) {
108         for (int i = 0; i < parameters.length; ++i) {
109             addExpectedSetParameter(i + 1, parameters[i]);
110         }
111     }
112
113     public void addExpectedTargetSQLType(int aTargetSQLType){
114         myTargetSQLTypes.addExpected(new Integer JavaDoc(aTargetSQLType));
115     }
116
117 // -------------------------------------------------------------- setExpected*
118

119     public void setExpectedClearParametersCalls(int callCount) {
120         myClearParametersCalls.setExpected(callCount);
121     }
122
123     public void setExpectingNoSetParameters() {
124         mySetParameters.setExpectNothing();
125     }
126
127 // --------------------------------------------------------------- implemented
128

129     public void clearParameters() throws SQLException {
130         myClearParametersCalls.inc();
131     }
132
133     /**
134      * Calls innerExecute() (see CommonMockStatement). Returns false.
135      */

136     public boolean execute() throws SQLException {
137         innerExecute();
138         return false;
139     }
140
141     /**
142      * Returns executeQuery(String) with an empty String.
143      */

144     public ResultSet executeQuery() throws SQLException {
145         innerExecute();
146         return (ResultSet)myResultSets.nextReturnObject();
147     }
148
149     /**
150      * Added value to be returned by executeUpdate()
151      * @param count
152      * @see #executeUpdate
153      */

154     public void addUpdateCount(int count){
155         this.executeUpdates.addObjectToReturn(count);
156     }
157
158     /**
159      * Returns value set with addUpdateCount
160      * @see #addUpdateCount
161      */

162     public int executeUpdate() throws SQLException {
163         innerExecute();
164         return ((Integer JavaDoc)executeUpdates.nextReturnObject()).intValue();
165     }
166
167     public void setInt(int parameterIndex, int x) throws SQLException {
168         setObject(parameterIndex, new Integer JavaDoc(x));
169     }
170
171     public void setString(int parameterIndex, String JavaDoc x) throws SQLException {
172         setObject(parameterIndex, x);
173     }
174
175     public void setTimestamp(int param, Timestamp timestamp) throws
176         SQLException {
177         setObject(param, timestamp);
178     }
179
180     public void setClob(int param, Clob clob) throws SQLException {
181         setObject(param, clob);
182     }
183
184     public void setLong(int param, long aLong) throws SQLException {
185         setObject(param, new Long JavaDoc(aLong));
186     }
187
188     public void setNull(int param, int param1) throws SQLException {
189         setObject(param, null);
190     }
191
192     public void setArray(int param, Array array) throws SQLException {
193         setObject(param, array);
194     }
195
196     public void setShort(int param, short aShort) throws SQLException {
197         setObject(param, new Short JavaDoc(aShort));
198     }
199
200     public void setTime(int param, Time time, Calendar JavaDoc calendar) throws
201         SQLException {
202         setObject(param, time);
203     }
204
205     public void setObject(int param, Object JavaDoc anObject, int aTargetSQLType)
206     throws SQLException {
207         setObject(param, anObject);
208         myTargetSQLTypes.addActual(new Integer JavaDoc(aTargetSQLType));
209     }
210
211     public void setRef(int param, Ref ref) throws SQLException {
212         setObject(param, ref);
213     }
214
215     public void setDate(int param, Date date) throws SQLException {
216         setObject(param, date);
217     }
218
219     public void setFloat(int param, float aFloat) throws SQLException {
220         setObject(param, new Float JavaDoc(aFloat));
221     }
222
223     public void setBlob(int param, Blob blob) throws SQLException {
224         setObject(param, blob);
225     }
226
227     public void setDate(int param, Date date, Calendar JavaDoc calendar) throws
228         SQLException {
229         setDate(param, date);
230     }
231
232     public void setBytes(int param, byte[] values) throws SQLException {
233         setObject(param, values);
234     }
235
236     public void setObject(int param, Object JavaDoc anObject) throws SQLException {
237         mySetParameters.addActual(new MapEntry(new Integer JavaDoc(param), anObject));
238     }
239
240     public void setByte(int param, byte aByte) throws SQLException {
241         setObject(param, new Byte JavaDoc(aByte));
242     }
243
244     public void setDouble(int param, double aDouble) throws SQLException {
245         setObject(param, new Double JavaDoc(aDouble));
246     }
247
248     public void setTime(int param, Time time) throws SQLException {
249         setObject(param, time);
250     }
251
252     public void setBoolean(int param, boolean aBoolean) throws SQLException {
253         setObject(param, new Boolean JavaDoc(aBoolean));
254     }
255
256     public void setBigDecimal(int param, BigDecimal JavaDoc bigDecimal) throws
257         SQLException {
258         setObject(param, bigDecimal);
259     }
260
261 // ------------------------------------------------------------ notImplemented
262

263     /**
264      * Calls notImplemented.
265      */

266    public void addBatch() throws SQLException {
267         notImplemented();
268     }
269
270     /**
271      * Calls notImplemented.
272      */

273     public void setObject(int param, Object JavaDoc anObject, int targetSqlType,
274         int scale) throws SQLException {
275         notImplemented();
276     }
277
278     /**
279      * Calls notImplemented.
280      */

281     public void setCharacterStream(int param, Reader JavaDoc reader, int length)
282         throws SQLException {
283         notImplemented();
284     }
285
286     /**
287      * Calls notImplemented.
288      */

289     public void setAsciiStream(int param, java.io.InputStream JavaDoc inputStream,
290         int length) throws SQLException {
291         notImplemented();
292     }
293
294     /**
295      * Calls notImplemented.
296      */

297     public void setBinaryStream(int param, java.io.InputStream JavaDoc inputStream,
298         int length) throws SQLException {
299         notImplemented();
300     }
301
302     /**
303      * Calls notImplemented.
304      */

305     public void setNull(int param, int param1, String JavaDoc typeName) throws
306         SQLException {
307         notImplemented();
308     }
309
310     /**
311      * Calls notImplemented.
312      */

313     public void setUnicodeStream(int param, java.io.InputStream JavaDoc inputStream,
314         int length) throws SQLException {
315         notImplemented();
316     }
317
318     /**
319      * Calls notImplemented. Returns null.
320      */

321     public ResultSetMetaData getMetaData() throws SQLException {
322         notImplemented();
323         return null;
324     }
325
326     /**
327      * Calls notImplemented.
328      */

329     public void setTimestamp(int param, Timestamp timestamp, Calendar JavaDoc
330         calendar) throws SQLException {
331         notImplemented();
332     }
333
334 } // end CommonMockPreparedStatement
335
Popular Tags