From: Jeff M. <cus...@us...> - 2002-12-04 12:39:26
|
Update of /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/sql In directory sc8-pr-cvs1:/tmp/cvs-serv26378/src/jdk/common/com/mockobjects/sql Modified Files: CommonMockConnection.java CommonMockMultiRowResultSet.java Added Files: CommonMockConnection2.java Log Message: Added a new implementation of MockConnection called MockConnection2 which starts to make use of ReturnObjectBag Added new constructor to MockMultiRowResultSet to provide better naming of mocks when raising errors --- NEW FILE: CommonMockConnection2.java --- /* * * ==================================================================== * * 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: 1.1 $ $Date: 2002/12/04 12:39:23 $ */ package com.mockobjects.sql; import com.mockobjects.ReturnObjectBag; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public abstract class CommonMockConnection2 extends CommonMockConnection implements Connection { private final ReturnObjectBag myPreparedStatements; public CommonMockConnection2() { this(CommonMockConnection2.class.getName()); } public CommonMockConnection2(String name) { super(name); myPreparedStatements = new ReturnObjectBag(name+".preparedStatements"); } /** * Adds a PreparedStatement to be return by prepareStatement */ public void setupAddPreparedStatement(String sql, PreparedStatement prepared) { myPreparedStatements.putObjectToReturn(sql, prepared); } /** * 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 { throwStatementExceptionIfAny(); return (PreparedStatement) myPreparedStatements.getNextReturnObject(sql); } } Index: CommonMockConnection.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/sql/CommonMockConnection.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- CommonMockConnection.java 10 Nov 2002 10:56:57 -0000 1.4 +++ CommonMockConnection.java 4 Dec 2002 12:39:23 -0000 1.5 @@ -78,18 +78,9 @@ */ package com.mockobjects.sql; -import com.mockobjects.ExpectationCollection; -import com.mockobjects.ExpectationCounter; -import com.mockobjects.ExpectationList; -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 com.mockobjects.*; + +import java.sql.*; import java.util.Map; public abstract class CommonMockConnection extends MockObject implements Connection { @@ -98,17 +89,10 @@ private boolean autoCommit = false; - private ExpectationList myAutoCommit = - new ExpectationList("CommonMockConnection.setAutoCommit"); - - private ExpectationCounter myCloseCalls = - new ExpectationCounter("CommonMockConnection.close"); - - private ExpectationCounter myCommitCalls = - new ExpectationCounter("CommonMockConnection.commit"); - - private ExpectationCounter myCreateStatementCalls = - new ExpectationCounter("CommonMockConnection.createStatement"); + private final ExpectationList myAutoCommit; + private final ExpectationCounter myCloseCalls; + private final ExpectationCounter myCommitCalls; + private final ExpectationCounter myCreateStatementCalls; private boolean myIsClosed; @@ -118,35 +102,46 @@ private DatabaseMetaData myMetaData; - private ReturnObjectList myPreparedStatements = - new ReturnObjectList("CommonMockConnection.PreparedStatements"); + /** + * @deprecated + * @see CommonMockConnection2 + */ + private final ReturnObjectList myPreparedStatements; - private ExpectationCollection myPreparedStatementStrings = - new ExpectationList("CommonMockConnection.preparedStatementString"); + /** + * @deprecated use myPreparedStatementsBag + * @see CommonMockConnection2 + */ + private final ExpectationCollection myPreparedStatementStrings; - private ExpectationCounter myRollbackCalls = - new ExpectationCounter("CommonMockConnection.rollback"); + private final ExpectationCounter myRollbackCalls; private Statement myStatement; private SQLException myStatementException = null; -// --------------------------------------------------------------- addExpected + public CommonMockConnection() { + this(CommonMockConnection.class.getName()); + } + + public CommonMockConnection(String name) { + myAutoCommit = new ExpectationList(name + ".setAutoCommit"); + myCloseCalls = new ExpectationCounter(name + ".close"); + myCommitCalls = new ExpectationCounter(name + ".commit"); + myCreateStatementCalls = new ExpectationCounter(name + ".createStatement"); + myPreparedStatements = new ReturnObjectList(name + ".PreparedStatements"); + myPreparedStatementStrings = new ExpectationList(name + ".preparedStatementString"); + myRollbackCalls = new ExpectationCounter(name + ".rollback"); + } /** + * @deprecated * 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 /** @@ -196,7 +191,9 @@ // --------------------------------------------------------------------- setup /** - * @deprecated Use addExpectedPreparedStatement. + * @deprecated + * Adds a PreparedStatement to be return by prepareStatement + * @see CommonMockConnection2 */ public void setupAddPreparedStatement(PreparedStatement prepared) { myPreparedStatements.addObjectToReturn(prepared); @@ -265,8 +262,8 @@ * Throw the Statement instance passed to * setupThrowExceptionOnPrepareOrCreate, if any. */ - private void throwStatementExceptionIfAny() throws SQLException { - if(null != myStatementException) { + void throwStatementExceptionIfAny() throws SQLException { + if (null != myStatementException) { throw myStatementException; } } @@ -278,7 +275,7 @@ * or otherwise increment the number or close calls. */ public void close() throws SQLException { - if(myCloseException != null) { + if (myCloseException != null) { throw myCloseException; } myCloseCalls.inc(); @@ -316,7 +313,7 @@ * or otherwise return the value passed to setupIsClosed. */ public boolean isClosed() throws SQLException { - if(myIsClosedException != null) { + if (myIsClosedException != null) { throw myIsClosedException; } return myIsClosed; @@ -484,4 +481,51 @@ notImplemented(); } -} // end CommonMockConnection + public void setHoldability(int holdability) throws SQLException { + notImplemented(); + } + + public int getHoldability() throws SQLException { + notImplemented(); + return 0; + } + + public Statement createStatement(int resultSetType, int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + notImplemented(); + return null; + } + + public PreparedStatement prepareStatement(String sql, int resultSetType, + int resultSetConcurrency, int resultSetHoldability) + throws SQLException { + notImplemented(); + return null; + } + + public CallableStatement prepareCall(String sql, int resultSetType, + int resultSetConcurrency, + int resultSetHoldability) throws SQLException { + notImplemented(); + return null; + } + + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) + throws SQLException { + notImplemented(); + return null; + } + + public PreparedStatement prepareStatement(String sql, int columnIndexes[]) + throws SQLException { + notImplemented(); + return null; + } + + public PreparedStatement prepareStatement(String sql, String columnNames[]) + throws SQLException { + notImplemented(); + return null; + } + +} Index: CommonMockMultiRowResultSet.java =================================================================== RCS file: /cvsroot/mockobjects/mockobjects-java/src/jdk/common/com/mockobjects/sql/CommonMockMultiRowResultSet.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- CommonMockMultiRowResultSet.java 28 Jun 2002 17:35:39 -0000 1.1 +++ CommonMockMultiRowResultSet.java 4 Dec 2002 12:39:23 -0000 1.2 @@ -26,6 +26,10 @@ super(); } + public CommonMockMultiRowResultSet(String name) { + super(name); + } + public void setupColumnNames(String[] columnNames) { myColumnNames = columnNames; } |