KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
62  * Abstract Connection for use with mock testing.
63  * Can verify the expected SQL strings with PreparedStatement or Statement
64  * instances (mock versions can be used).
65  * Can verify the state of closed and autoCommit.
66  * Can verify the number of calls to close, commit, createStatement, and
67  * rollBack.
68  * Exceptions to throw can be registered for calling createStatement,
69  * preparedStatement, or close.
70  * Several less-often-used methods are not implemented.
71  * If a notImplemented method is needed for your project, please submit
72  * a patch.
73  * @see <a HREF="http://java.sun.com/j2se/1.3/docs/api/java/sql/Connection.html">javax.sql.Connection</a>
74  * @author
75  * @author Jeff Martin
76  * @author Ted Husted
77  * @version $Revision: 1.7 $ $Date: 2003/02/21 12:17:38 $
78  */

79 package com.mockobjects.sql;
80
81 import com.mockobjects.*;
82
83 import java.sql.*;
84 import java.util.Map JavaDoc;
85
86 abstract class CommonMockConnection extends MockObject implements Connection {
87
88 // -------------------------------------------------------------------- fields
89

90     private boolean autoCommit = false;
91
92     private final ExpectationValue myResultSetConcurrency;
93     private final ExpectationValue myResultSetType;
94
95     private final ExpectationList myAutoCommit;
96     private final ExpectationCounter myCloseCalls;
97     private final ExpectationCounter myCommitCalls;
98     private final ExpectationCounter myCreateStatementCalls;
99
100     private boolean myIsClosed;
101
102     private SQLException myCloseException;
103
104     private SQLException myIsClosedException;
105
106     private DatabaseMetaData myMetaData;
107
108     /**
109      * @deprecated
110      * @see CommonMockConnection2
111      */

112     private final ReturnObjectList myPreparedStatements;
113
114     /**
115      * @deprecated use myPreparedStatementsBag
116      * @see CommonMockConnection2
117      */

118     private final ExpectationCollection myPreparedStatementStrings;
119
120     private final ExpectationCounter myRollbackCalls;
121
122     private Statement myStatement;
123
124     private SQLException myStatementException = null;
125
126     public CommonMockConnection() {
127         this(CommonMockConnection.class.getName());
128     }
129
130     public CommonMockConnection(String JavaDoc name) {
131         myAutoCommit = new ExpectationList(name + ".setAutoCommit");
132         myCloseCalls = new ExpectationCounter(name + ".close");
133         myCommitCalls = new ExpectationCounter(name + ".commit");
134         myCreateStatementCalls = new ExpectationCounter(name + ".createStatement");
135         myPreparedStatements = new ReturnObjectList(name + ".PreparedStatements");
136         myPreparedStatementStrings = new ExpectationList(name + ".preparedStatementString");
137         myRollbackCalls = new ExpectationCounter(name + ".rollback");
138         myResultSetType = new ExpectationValue(name + ".resultSetType");
139         myResultSetConcurrency = new ExpectationValue(name + ".resultSetConcurrency");
140     }
141
142     /**
143      * @deprecated
144      * Add an SQL string to use with a prepared staement.
145      */

146     public void addExpectedPreparedStatementString(String JavaDoc sql) {
147         myPreparedStatementStrings.addExpected(sql);
148     }
149
150 // --------------------------------------------------------------- setExpected
151

152     /**
153      * Register the anticipated value for autoCommit during testing.
154      */

155     public void addExpectedAutoCommit(boolean autoCommit) {
156         myAutoCommit.addExpected(new Boolean JavaDoc(autoCommit));
157     }
158
159
160     /**
161      * Register the number of close calls the test should make.
162      * The valid method will report any discrepancy with the
163      * actual count.
164      */

165     public void setExpectedCloseCalls(int callCount) {
166         myCloseCalls.setExpected(callCount);
167     }
168
169     /**
170      * Register the number of commit calls the test should make.
171      * The valid method will report any discrepancy with the
172      * actual count.
173      */

174     public void setExpectedCommitCalls(int callCount) {
175         myCommitCalls.setExpected(callCount);
176     }
177
178     /**
179      * Register the number of create statement calls the test should make.
180      * The valid method will report any discrepancy with the
181      * actual count.
182      */

183     public void setExpectedCreateStatementCalls(int calls) {
184         myCreateStatementCalls.setExpected(calls);
185     }
186
187     /**
188      * Register the number of roll back calls the test should make.
189      * The valid method will report any discrepancy with the
190      * actual count.
191      */

192     public void setExpectedRollbackCalls(int callCount) {
193         myRollbackCalls.setExpected(callCount);
194     }
195
196 // --------------------------------------------------------------------- setup
197

198     /**
199      * Sets expectations about the possible value of the
200      * <code>resultSetConcurrency</code> parameter of
201      * {@link #createStatement(int, int) createStatement()} calls.
202      *
203      * @param resultSetConcurrency One of the constants starting with CONCUR
204      * in {@link java.sql.ResultSet}.
205      */

206     public void setExpectedResultSetConcurrency(int resultSetConcurrency) {
207         myResultSetConcurrency.setExpected(resultSetConcurrency);
208     }
209
210     /**
211      * Sets expectations about the possible value of the
212      * <code>resultSetType</code> parameter of
213      * {@link #createStatement(int, int) createStatement()} calls.
214      *
215      * @param resultSetType One of the constants starting with TYPE
216      * in {@link java.sql.ResultSet}.
217      */

218     public void setExpectedResultSetType(int resultSetType) {
219         myResultSetType.setExpected(resultSetType);
220     }
221
222
223     /**
224      * @deprecated
225      * Adds a PreparedStatement to be return by prepareStatement
226      * @see CommonMockConnection2
227      */

228     public void setupAddPreparedStatement(PreparedStatement prepared) {
229         myPreparedStatements.addObjectToReturn(prepared);
230     }
231
232     /**
233      * Pass the SQL exception to throw if close is called
234      * during a test.
235      */

236     public void setupCloseException(SQLException aCloseException) {
237         myCloseException = aCloseException;
238     }
239
240     /**
241      * @deprecated Use setupIsClosed
242      */

243     public void setupIsClose(boolean aIsClosed) {
244         myIsClosed = aIsClosed;
245     }
246
247     /**
248      * Pass the value to return if isClosed is called during a test.
249      * This is returned unless an isClosedException has been set.
250      */

251     public void setupIsClosed(boolean aIsClosed) {
252         myIsClosed = aIsClosed;
253     }
254
255     /**
256      * Pass the SQL exception instance to throw if isClosed
257      * is called during a test.
258      */

259     public void setupIsClosedException(SQLException aIsClosedException) {
260         myIsClosedException = aIsClosedException;
261     }
262
263     /**
264      * Pass the DataBaseMetaData instance for use with tests.
265      */

266     public void setupMetaData(DatabaseMetaData metaData) {
267         myMetaData = metaData;
268     }
269
270     /**
271      * Pass the Statement instance for use with tests.
272      */

273     public void setupStatement(Statement statement) {
274         myStatement = statement;
275     }
276
277     /**
278      * Pass the SQL exception to throw if preparedStatement or createStatement
279      * is called during a test.
280      */

281     public void setupThrowExceptionOnPrepareOrCreate(SQLException exception) {
282         myStatementException = exception;
283     }
284
285     public void setupAutoCommit(boolean autoCommitToReturn) {
286         autoCommit = autoCommitToReturn;
287     }
288
289 // --------------------------------------------------------------------- throw
290

291     /**
292      * Throw the Statement instance passed to
293      * setupThrowExceptionOnPrepareOrCreate, if any.
294      */

295     void throwStatementExceptionIfAny() throws SQLException {
296         if (null != myStatementException) {
297             throw myStatementException;
298         }
299     }
300
301 // --------------------------------------------------------------- implemented
302

303     /**
304      * Will throw the CloseException if it has been set,
305      * or otherwise increment the number or close calls.
306      */

307     public void close() throws SQLException {
308         if (myCloseException != null) {
309             throw myCloseException;
310         }
311         myCloseCalls.inc();
312     }
313
314     /**
315      * Increments the number of commit calls.
316      */

317     public void commit() throws SQLException {
318         myCommitCalls.inc();
319     }
320
321     /**
322      * Will throw either of the statement exceptions if one has been
323      * set,
324      * or otherwise return the Statement passed to
325      * setupStatement.
326      */

327     public Statement createStatement() throws SQLException {
328         myCreateStatementCalls.inc();
329         throwStatementExceptionIfAny();
330         return myStatement;
331     }
332
333     /**
334      * Returns the DatabaseMetaData instance passed to setupMetaData.
335      */

336     public DatabaseMetaData getMetaData()
337         throws SQLException {
338         return myMetaData;
339     }
340
341     /**
342      * Throw the isClosedException if it has been set,
343      * or otherwise return the value passed to setupIsClosed.
344      */

345     public boolean isClosed() throws SQLException {
346         if (myIsClosedException != null) {
347             throw myIsClosedException;
348         }
349         return myIsClosed;
350     }
351
352     /**
353      * Throws a statement exception if one has been registered
354      * (@see throwStatementExceptionIfAny)
355      * or returns the next PreparedStatement instance passed to
356      * setupAddPreparedStatement.
357      */

358     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
359         myPreparedStatementStrings.addActual(sql);
360         throwStatementExceptionIfAny();
361         return (PreparedStatement) myPreparedStatements.nextReturnObject();
362     }
363
364     /**
365      * Increments the number of roll back calls.
366      */

367     public void rollback() throws SQLException {
368         myRollbackCalls.inc();
369     }
370
371     /**
372      * Stores the value passed for comparison with the value passed
373      * to setupAutoCommit.
374      * The validate method will report any discrepency.
375      */

376     public void setAutoCommit(boolean autoCommit) throws SQLException {
377         myAutoCommit.addActual(new Boolean JavaDoc(autoCommit));
378     }
379
380 // ------------------------------------------------------------ notImplemented
381

382     /**
383      * Calls notImplemented.
384      */

385     public void clearWarnings() throws SQLException {
386         notImplemented();
387     }
388
389     /**
390      * Calls notImplemented. Returns null.
391      */

392     public Statement createStatement(
393         int resultSetType,
394         int resultSetConcurrency)
395         throws SQLException {
396         myCreateStatementCalls.inc();
397         throwStatementExceptionIfAny();
398
399         myResultSetType.setActual(resultSetType);
400         myResultSetConcurrency.setActual(resultSetConcurrency);
401
402         return myStatement;
403     }
404
405     public boolean getAutoCommit() throws SQLException {
406         return autoCommit;
407     }
408
409     /**
410      * Calls notImplemented. Returns null.
411      */

412     public String JavaDoc getCatalog() throws SQLException {
413         notImplemented();
414         return null;
415     }
416
417     /**
418      * Calls notImplemented. Returns 0.
419      */

420     public int getTransactionIsolation() throws SQLException {
421         notImplemented();
422         return 0;
423     }
424
425     /**
426      * Calls notImplemented. Returns null.
427      */

428     public Map JavaDoc getTypeMap() throws SQLException {
429         notImplemented();
430         return null;
431     }
432
433     /**
434      * Calls notImplemented. Returns null.
435      */

436     public SQLWarning getWarnings() throws SQLException {
437         notImplemented();
438         return null;
439     }
440
441     /**
442      * Calls notImplemented. Returns false.
443      */

444     public boolean isReadOnly() throws SQLException {
445         notImplemented();
446         return false;
447     }
448
449     /**
450      * Calls notImplemented. Returns null.
451      */

452     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
453         notImplemented();
454         return null;
455     }
456
457     /**
458      * Calls notImplemented. Returns null.
459      */

460     public CallableStatement prepareCall(String JavaDoc sql)
461         throws SQLException {
462         notImplemented();
463         return null;
464     }
465
466     /**
467      * Calls notImplemented. Returns null.
468      */

469     public CallableStatement prepareCall(
470         String JavaDoc sql,
471         int resultSetType,
472         int resultSetConcurrency)
473         throws SQLException {
474         notImplemented();
475         return null;
476     }
477
478     /**
479      * Calls notImplemented. Returns null.
480      */

481     public PreparedStatement prepareStatement(
482         String JavaDoc sql,
483         int resultSetType,
484         int resultSetConcurrency)
485         throws SQLException {
486         notImplemented();
487         return null;
488     }
489
490     /**
491      * Calls notImplemented.
492      */

493     public void setCatalog(String JavaDoc catalog) throws SQLException {
494         notImplemented();
495     }
496
497     /**
498      * Calls notImplemented.
499      */

500     public void setReadOnly(boolean readOnly) throws SQLException {
501         notImplemented();
502     }
503
504     /**
505      * Calls notImplemented.
506      */

507     public void setTransactionIsolation(int level)
508         throws SQLException {
509         notImplemented();
510     }
511
512     /**
513      * Calls notImplemented.
514      */

515     public void setTypeMap(Map JavaDoc map) throws SQLException {
516         notImplemented();
517     }
518
519     public void setHoldability(int holdability) throws SQLException {
520         notImplemented();
521     }
522
523     public int getHoldability() throws SQLException {
524         notImplemented();
525         return 0;
526     }
527
528     public Statement createStatement(int resultSetType, int resultSetConcurrency,
529         int resultSetHoldability) throws SQLException {
530         notImplemented();
531         return null;
532     }
533
534     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
535         int resultSetConcurrency, int resultSetHoldability)
536         throws SQLException {
537         notImplemented();
538         return null;
539     }
540
541     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
542         int resultSetConcurrency,
543         int resultSetHoldability) throws SQLException {
544         notImplemented();
545         return null;
546     }
547
548     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
549         throws SQLException {
550         notImplemented();
551         return null;
552     }
553
554     public PreparedStatement prepareStatement(String JavaDoc sql, int columnIndexes[])
555         throws SQLException {
556         notImplemented();
557         return null;
558     }
559
560     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
561         throws SQLException {
562         notImplemented();
563         return null;
564     }
565
566 }
567
Popular Tags