From: Verma, N. (C. G. L529706) <Nit...@ge...> - 2002-01-08 21:57:57
|
package com.mockobjects.sql; import java.sql.*; import com.mockobjects.MockObject; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationValue; import junit.framework.AssertionFailedError; /** * Trial version * @author Nitin Verma * @version 1.0 24 Dec 2001 */ public class MockStatement extends MockObject implements Statement { /* Nested MockObjects */ protected MockResultSet myResultSet; /*Expectations objects that can be set */ protected ExpectationCounter myCloseCalls = new ExpectationCounter("MockStatement.closeCalls"); protected ExpectationCounter myExecuteCalls = new ExpectationCounter("MockStatement.executeCalls"); protected ExpectationValue myQueryString = new ExpectationValue("MockStatement.queryString"); protected ExpectationValue myAutoGeneratedKeysInt = new ExpectationValue("MockStatement.executeUpdate(String,int)"); /*Other objects*/ private int myUpdateCount = 0; private boolean myAutoGenerated = false; private boolean myExecuteReturn = false; private int myResultSetHoldability = 0; private SQLException myExecuteException = null; /* Methods for mockobject's internal use only */ protected void innerExecute() throws SQLException { myExecuteCalls.inc(); if (null != myExecuteException) { throw myExecuteException; } } /* Methods wrapped*/ public MockStatement() { super(); } public ResultSet executeQuery(String p_sql) throws SQLException{ myQueryString.setActual(p_sql); innerExecute(); return myResultSet; } public int executeUpdate(String p_sql) throws SQLException{ myQueryString.setActual(p_sql); innerExecute(); return myUpdateCount; } public void close() throws SQLException{ myCloseCalls.inc(); } public int getMaxFieldSize() throws SQLException{ throw new UnsupportedOperationException(); } public void setMaxFieldSize(int max) throws SQLException{ throw new UnsupportedOperationException(); } public int getMaxRows() throws SQLException{ throw new UnsupportedOperationException(); } public void setMaxRows(int max) throws SQLException{ throw new UnsupportedOperationException(); } public void setEscapeProcessing(boolean enable) throws SQLException{ throw new UnsupportedOperationException(); } public int getQueryTimeout() throws SQLException{ throw new UnsupportedOperationException(); } public void setQueryTimeout(int seconds) throws SQLException{ throw new UnsupportedOperationException(); } public void cancel() throws SQLException{ throw new UnsupportedOperationException(); } public SQLWarning getWarnings() throws SQLException{ throw new UnsupportedOperationException(); } public void clearWarnings() throws SQLException{ throw new UnsupportedOperationException(); } public void setCursorName(String name) throws SQLException{ throw new UnsupportedOperationException(); } public boolean execute(String p_sql) throws SQLException{ myQueryString.setActual(p_sql); innerExecute(); return myExecuteReturn; } public ResultSet getResultSet() throws SQLException{ throw new UnsupportedOperationException(); } public int getUpdateCount() throws SQLException{ throw new UnsupportedOperationException(); } public boolean getMoreResults() throws SQLException{ throw new UnsupportedOperationException(); } //--------------------------JDBC 2.0----------------------------- public void setFetchDirection(int direction) throws SQLException{ throw new UnsupportedOperationException(); } public int getFetchDirection() throws SQLException{ throw new UnsupportedOperationException(); } public void setFetchSize(int rows) throws SQLException{ throw new UnsupportedOperationException(); } public int getFetchSize() throws SQLException{ throw new UnsupportedOperationException(); } public int getResultSetConcurrency() throws SQLException{ throw new UnsupportedOperationException(); } public int getResultSetType() throws SQLException{ throw new UnsupportedOperationException(); } public void addBatch( String sql ) throws SQLException{ throw new UnsupportedOperationException(); } public void clearBatch() throws SQLException{ throw new UnsupportedOperationException(); } public int[] executeBatch() throws SQLException{ throw new UnsupportedOperationException(); } public Connection getConnection() throws SQLException{ throw new UnsupportedOperationException(); } //--------------------------JDBC 3.0----------------------------- public boolean getMoreResults(int current) throws SQLException{ throw new UnsupportedOperationException(); } public ResultSet getGeneratedKeys() throws SQLException{ if(myAutoGenerated == true) return myResultSet; else throw new SQLException("executeUpdate/execute not called"); } public int executeUpdate(String p_sql, int p_autoGeneratedKeys) throws SQLException{ myQueryString.setActual(p_sql); myAutoGeneratedKeysInt.setActual(p_autoGeneratedKeys); innerExecute(); myAutoGenerated = true; return myUpdateCount; } public int executeUpdate(String sql, int columnIndexes[]) throws SQLException{ throw new UnsupportedOperationException(); } public int executeUpdate(String sql, String columnNames[]) throws SQLException{ throw new UnsupportedOperationException(); } public boolean execute(String p_sql, int p_autoGeneratedKeys) throws SQLException{ myQueryString.setActual(p_sql); myAutoGeneratedKeysInt.setActual(p_autoGeneratedKeys); innerExecute(); myAutoGenerated = true; return myExecuteReturn; } public boolean execute(String sql, int columnIndexes[]) throws SQLException{ throw new UnsupportedOperationException(); } public boolean execute(String sql, String columnNames[]) throws SQLException{ throw new UnsupportedOperationException(); } public int getResultSetHoldability() throws SQLException{ return myResultSetHoldability; } /* Methods only to be called inside test classes */ public void setExpectedExecuteCalls(int p_callCount) { myExecuteCalls.setExpected(p_callCount); } public void setExpectedQueryString(String p_queryString) { myQueryString.setExpected(p_queryString); } public void setExpectedCloseCalls(int p_callCount) { myCloseCalls.setExpected(p_callCount); } public void setupResultSet(MockResultSet p_resultSet) { myResultSet = p_resultSet; } public void setupThrowExceptionOnExecute(SQLException p_exception) { myExecuteException = p_exception; } public void setupUpdateCount(int p_updateCount) { myUpdateCount = p_updateCount; } public void setupExecuteReturn(boolean p_executeReturn) { myExecuteReturn = p_executeReturn; } public void setupResultSetHoldability(int p_resultSetHoldability){ myResultSetHoldability = p_resultSetHoldability; } } "THIS E-MAIL MESSAGE ALONG WITH ANY ATTACHMENTS IS INTENDED ONLY FOR THE ADDRESSEE and may contain confidential and privileged information. If the reader of this message is not the intended recipient, you are notified that any dissemination, distribution or copy of this communication is strictly Prohibited. If you have received this message by error, please notify us immediately, return the original mail to the sender and delete the message from your system." |