From: Ted H. <th...@us...> - 2002-09-07 18:52:35
|
Update of /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/sql In directory usw-pr-cvs1:/tmp/cvs-serv23360 Modified Files: CommonMockConnection.java Log Message: For consistency, add addExpectedPreparedStatement and deprecate setupAddPreparedStatement. For consistency, add setupIsClosed and depcrecate setupIsClose. Add license, JavaDocs. Add code-dividers for addExpected, setExpected, implemented, notImplemented, et cetera. Sort members alphabetically by property or member name within each division. Wrap lines at 78 characters. Index: CommonMockConnection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/sql/CommonMockConnection.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- CommonMockConnection.java 8 Jul 2002 18:33:58 -0000 1.2 +++ CommonMockConnection.java 7 Sep 2002 18:52:31 -0000 1.3 @@ -1,72 +1,277 @@ +/* + * + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowlegement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowlegement may appear in the software itself, + * if and wherever such third-party acknowlegements normally appear. + * + * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact ap...@ap.... + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +// ------------------------------------------------------------------------ 78 + +/** + * Abstract Connection for use with mock testing. + * Can verify the expected SQL strings with PreparedStatement or Statement + * instances (mock versions can be used). + * Can verify the state of closed and autoCommit. + * Can verify the number of calls to close, commit, createStatement, and + * rollBack. + * Exceptions to throw can be registered for calling createStatement, + * preparedStatement, or close. + * Several less-often-used methods are not implemented. + * If a notImplemented method is are needed for your project, please submit + * a patch. + * @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/sql/Connection.html">javax.sql.Connection</a> + * @author + * @author Jeff Martin + * @author Ted Husted + * @version $Revision$ $Date$ + */ package com.mockobjects.sql; -import com.mockobjects.*; - -import java.sql.*; +import com.mockobjects.ExpectationCollection; +import com.mockobjects.ExpectationCounter; +import com.mockobjects.ExpectationList; +import com.mockobjects.ExpectationValue; +import com.mockobjects.MockObject; +import com.mockobjects.ReturnObjectList; +import java.sql.CallableStatement; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.Statement; +import java.sql.SQLException; +import java.sql.SQLWarning; import java.util.Map; public abstract class CommonMockConnection extends MockObject implements Connection { - private ExpectationCounter myCommitCalls = new ExpectationCounter("CommonMockConnection.commit"); - private ExpectationCounter myRollbackCalls = new ExpectationCounter("CommonMockConnection.rollback"); - private ExpectationCounter myCloseCalls = new ExpectationCounter("CommonMockConnection.close"); - private ExpectationCollection myPreparedStatementStrings = new ExpectationList("CommonMockConnection.preparedStatementString"); - private ReturnObjectList myPreparedStatements = new ReturnObjectList("statements"); - private SQLException myStatementException = null; +// -------------------------------------------------------------------- fields - private ExpectationCounter myCreateStatementCalls = new ExpectationCounter("CommonMockConnection.createStatement"); - private ExpectationValue myAutoCommit = new ExpectationValue("CommonMockConnection.setAutoCommit"); - private Statement myStatement; - private DatabaseMetaData myMetaData; + private ExpectationValue myAutoCommit = + new ExpectationValue("CommonMockConnection.setAutoCommit"); + + private ExpectationCounter myCloseCalls = + new ExpectationCounter("CommonMockConnection.close"); + + private ExpectationCounter myCommitCalls = + new ExpectationCounter("CommonMockConnection.commit"); + + private ExpectationCounter myCreateStatementCalls = + new ExpectationCounter("CommonMockConnection.createStatement"); private boolean myIsClosed; - private SQLException myIsClosedException; + private SQLException myCloseException; + private SQLException myIsClosedException; + + private DatabaseMetaData myMetaData; + + private ReturnObjectList myPreparedStatements = + new ReturnObjectList("CommonMockConnection.PreparedStatements"); + + private ExpectationCollection myPreparedStatementStrings = + new ExpectationList("CommonMockConnection.preparedStatementString"); + + private ExpectationCounter myRollbackCalls = + new ExpectationCounter("CommonMockConnection.rollback"); + + private Statement myStatement; + + private SQLException myStatementException = null; + +// --------------------------------------------------------------- addExpected + + /** + * Add an SQL string to use with a prepared staement. + */ + public void addExpectedPreparedStatementString(String sql) { + myPreparedStatementStrings.addExpected(sql); + } + + /** + * Add a PreparedStatement instance for use with tests. + */ + public void addExpectedPreparedStatement(PreparedStatement prepared) { + myPreparedStatements.addObjectToReturn(prepared); + } + +// --------------------------------------------------------------- setExpected + + /** + * Register the anticipated value for autoCommit during testing. + */ + public void setExpectedAutoCommit(boolean autoCommit) { + myAutoCommit.setExpected(autoCommit); + } + + + /** + * Register the number of close calls the test should make. + * The valid method will report any discrepancy with the + * actual count. + */ public void setExpectedCloseCalls(int callCount) { myCloseCalls.setExpected(callCount); } + /** + * Register the number of commit calls the test should make. + * The valid method will report any discrepancy with the + * actual count. + */ public void setExpectedCommitCalls(int callCount) { myCommitCalls.setExpected(callCount); } + /** + * Register the number of create statement calls the test should make. + * The valid method will report any discrepancy with the + * actual count. + */ public void setExpectedCreateStatementCalls(int calls) { myCreateStatementCalls.setExpected(calls); } - public void addExpectedPreparedStatementString(String sql) { - myPreparedStatementStrings.addExpected(sql); - } - + /** + * Register the number of roll back calls the test should make. + * The valid method will report any discrepancy with the + * actual count. + */ public void setExpectedRollbackCalls(int callCount) { myRollbackCalls.setExpected(callCount); } +// --------------------------------------------------------------------- setup + + /** + * @deprecated Use addExpectedPreparedStatement. + */ public void setupAddPreparedStatement(PreparedStatement prepared) { myPreparedStatements.addObjectToReturn(prepared); } - public void setupStatement(Statement statement) { - myStatement = statement; + /** + * Pass the SQL exception to throw if close is called + * during a test. + */ + public void setupCloseException(SQLException aCloseException) { + myCloseException = aCloseException; } - public void setupThrowExceptionOnPrepareOrCreate(SQLException exception) { - myStatementException = exception; + /** + * @deprecated Use setupIsClosed + */ + public void setupIsClose(boolean aIsClosed) { + myIsClosed = aIsClosed; + } + + /** + * Pass the value to return if isClosed is called during a test. + * This is returned unless an isClosedException has been set. + */ + public void setupIsClosed(boolean aIsClosed) { + myIsClosed = aIsClosed; + } + + /** + * Pass the SQL exception instance to throw if isClosed + * is called during a test. + */ + public void setupIsClosedException(SQLException aIsClosedException) { + myIsClosedException = aIsClosedException; } + /** + * Pass the DataBaseMetaData instance for use with tests. + */ public void setupMetaData(DatabaseMetaData metaData) { myMetaData = metaData; } - public void clearWarnings() throws SQLException { - notImplemented(); + /** + * Pass the Statement instance for use with tests. + */ + public void setupStatement(Statement statement) { + myStatement = statement; } - public void setupCloseException(SQLException aCloseException) { - myCloseException = aCloseException; + /** + * Pass the SQL exception to throw if preparedStatement or createStatement + * is called during a test. + */ + public void setupThrowExceptionOnPrepareOrCreate(SQLException exception) { + myStatementException = exception; + } + +// --------------------------------------------------------------------- throw + + /** + * Throw the Statement instance passed to + * setupThrowExceptionOnPrepareOrCreate, if any. + */ + private void throwStatementExceptionIfAny() throws SQLException { + if(null != myStatementException) { + throw myStatementException; + } } +// --------------------------------------------------------------- implemented + + /** + * Will throw the CloseException if it has been set, + * or otherwise increment the number or close calls. + */ public void close() throws SQLException { if(myCloseException != null) { throw myCloseException; @@ -74,16 +279,84 @@ myCloseCalls.inc(); } + /** + * Increments the number of commit calls. + */ public void commit() throws SQLException { myCommitCalls.inc(); } + /** + * Will throw either of the statement exceptions if one has been + * set, + * or otherwise return the Statement passed to + * setupStatement. + */ public Statement createStatement() throws SQLException { myCreateStatementCalls.inc(); throwStatementExceptionIfAny(); return myStatement; } + /** + * Returns the DatabaseMetaData instance passed to setupMetaData. + */ + public DatabaseMetaData getMetaData() + throws SQLException { + return myMetaData; + } + + /** + * Throw the isClosedException if it has been set, + * or otherwise return the value passed to setupIsClosed. + */ + public boolean isClosed() throws SQLException { + if(myIsClosedException != null) { + throw myIsClosedException; + } + return myIsClosed; + } + + /** + * Throws a statement exception if one has been registered + * (@see throwStatementExceptionIfAny) + * or returns the next PreparedStatement instance passed to + * setupAddPreparedStatement. + */ + public PreparedStatement prepareStatement(String sql) throws SQLException { + myPreparedStatementStrings.addActual(sql); + throwStatementExceptionIfAny(); + return (PreparedStatement) myPreparedStatements.nextReturnObject(); + } + + /** + * Increments the number of roll back calls. + */ + public void rollback() throws SQLException { + myRollbackCalls.inc(); + } + + /** + * Stores the value passed for comparison with the value passed + * to setupAutoCommit. + * The validate method will report any discrepency. + */ + public void setAutoCommit(boolean autoCommit) throws SQLException { + myAutoCommit.setActual(autoCommit); + } + +// ------------------------------------------------------------ notImplemented + + /** + * Calls notImplemented. + */ + public void clearWarnings() throws SQLException { + notImplemented(); + } + + /** + * Calls notImplemented. Returns null. + */ public Statement createStatement( int resultSetType, int resultSetConcurrency) @@ -92,67 +365,74 @@ return null; } + /** + * Calls notImplemented. Returns false. + */ public boolean getAutoCommit() throws SQLException { notImplemented(); return false; } + /** + * Calls notImplemented. Returns null. + */ public String getCatalog() throws SQLException { notImplemented(); return null; } - public DatabaseMetaData getMetaData() - throws SQLException { - return myMetaData; - } - + /** + * Calls notImplemented. Returns 0. + */ public int getTransactionIsolation() throws SQLException { notImplemented(); return 0; } + /** + * Calls notImplemented. Returns null. + */ public Map getTypeMap() throws SQLException { notImplemented(); return null; } + /** + * Calls notImplemented. Returns null. + */ public SQLWarning getWarnings() throws SQLException { notImplemented(); return null; } - public void setupIsClosedException(SQLException aIsClosedException) { - myIsClosedException = aIsClosedException; - } - - public void setupIsClose(boolean aIsClosed) { - myIsClosed = aIsClosed; - } - - public boolean isClosed() throws SQLException { - if(myIsClosedException != null) { - throw myIsClosedException; - } - return myIsClosed; - } - + /** + * Calls notImplemented. Returns false. + */ public boolean isReadOnly() throws SQLException { notImplemented(); return false; } + /** + * Calls notImplemented. Returns null. + */ public String nativeSQL(String sql) throws SQLException { notImplemented(); return null; } + /** + * Calls notImplemented. Returns null. + */ public CallableStatement prepareCall(String sql) throws SQLException { notImplemented(); return null; } + /** + * Calls notImplemented. Returns null. + */ public CallableStatement prepareCall( String sql, int resultSetType, @@ -162,12 +442,9 @@ return null; } - public PreparedStatement prepareStatement(String sql) throws SQLException { - myPreparedStatementStrings.addActual(sql); - throwStatementExceptionIfAny(); - return (PreparedStatement) myPreparedStatements.nextReturnObject(); - } - + /** + * Calls notImplemented. Returns null. + */ public PreparedStatement prepareStatement( String sql, int resultSetType, @@ -177,38 +454,33 @@ return null; } - public void rollback() throws SQLException { - myRollbackCalls.inc(); - } - - public void setExpectedAutoCommit(boolean autoCommit) { - myAutoCommit.setExpected(autoCommit); - } - - public void setAutoCommit(boolean autoCommit) throws SQLException { - myAutoCommit.setActual(autoCommit); - } - + /** + * Calls notImplemented. + */ public void setCatalog(String catalog) throws SQLException { notImplemented(); } + /** + * Calls notImplemented. + */ public void setReadOnly(boolean readOnly) throws SQLException { notImplemented(); } + /** + * Calls notImplemented. + */ public void setTransactionIsolation(int level) throws SQLException { notImplemented(); } + /** + * Calls notImplemented. + */ public void setTypeMap(Map map) throws SQLException { notImplemented(); } - private void throwStatementExceptionIfAny() throws SQLException { - if(null != myStatementException) { - throw myStatementException; - } - } -} +} // end CommonMockConnection |