usingSystem;
usingSystem.Data;
usingDotNetMock;
namespace DotNetMock.Framework.Data{
/// <summary>
/// This Mock Object implements the IDbConnection interface
/// </summary>
publicclassMockDbConnection : MockObject, IDbConnection
{
privateIDbCommand _command = null;
privateExpectationCounter _closeCalls = newExpectationCounter("MockDbConnection.CloseCalls");
privateExpectationCounter _createCalls = newExpectationCounter("MockDbConnection.CreateCalls");
privateExpectationCounter _openCalls = newExpectationCounter("MockDbConnection.OpenCalls");
privateExpectationString _connectionString = newExpectationString("MockDbConnection.ConnectionString");
privateException _createCommandException = null;
privateConnectionState _connectionState = ConnectionState.Closed;
privateint _connectionTimeout = 0;
/// <summary>
/// Default Constructor
/// </summary>
publicMockDbConnection() {}
/// <summary>
/// Constructor that sets the name of the Mock Object
/// </summary>
/// <param name="name">Name of the Mock Object</param>
publicMockDbConnection( string name ) : base( name ) {}
#region Mock Methods
/// <summary>
/// Sets expected connection state for the State property
/// </summary>
/// <param name="state">ConnectionState to expect</param>
publicvoid SetExpectedConnectionState( ConnectionState state )
{
_connectionState = state;
}
/// <summary>
/// Sets expected number of Open() calls to expect
/// </summary>
/// <param name="openCalls">Number of calls to expect</param>
publicvoid SetExpectedOpenCalls( int openCalls )
{
_openCalls.Expected = openCalls;
}
/// <summary>
/// Sets expected connection timeout value for the ConnectionTimeout property
/// </summary>
/// <param name="timeout">Connection timeout to expect</param>
publicvoid SetExpectedConnectionTimeout( int timeout )
{
_connectionTimeout = timeout;
}
/// <summary>
/// Set expected connection string for connection string property
/// </summary>
/// <param name="connectionString">String to use</param>
publicvoid SetExpectedConnectionString(string connectionString)
{
_connectionString.Expected = connectionString;
}
/// <summary>
/// Set expected command to return on CreateCommand()
/// </summary>
/// <param name="command">Command to return</param>
publicvoid SetExpectedCommand(IDbCommand command)
{
_command = command;
}
/// <summary>
/// Set expected number of Close() calls to expect
/// </summary>
/// <param name="calls">Count to expect</param>
publicvoid SetExpectedCloseCalls(int calls)
{
_closeCalls.Expected = calls;
}
/// <summary>
/// Set exception to throw when CreateCommand() is called
/// </summary>
/// <param name="exception">Exception to throw</param>
publicvoid SetCreateCommandException(Exception exception)
{
_createCommandException = exception;
}
/// <summary>
/// Set expected number of CreateCommand() calls
/// </summary>
/// <param name="calls">Count to expect</param>
publicvoid SetExpectedCreateCalls(int calls)
{
_createCalls.Expected = calls;
}
#endregion
#region Implementation of IDbConnection
/// <summary>
/// Changes the current database for an open connection object. Currently does nothing
/// </summary>
/// <param name="databaseName">Database to change to</param>
publicvoid ChangeDatabase(string databaseName)
{
}
/// <summary>
/// Begins a database transaction. Currently returns a new MockTransaction object that is
/// associated with the current connection. Also, associated the given isolation level
/// with the new transaction.
/// </summary>
/// <param name="il">One of the IsolationLevel values.</param>
/// <returns>An object representing the new transaction.</returns>
public System.Data.IDbTransaction BeginTransaction(System.Data.IsolationLevel il)
{
IDbTransaction transaction = BeginTransaction();
((MockTransaction)transaction).SetTransactionIsolationLevel( il );
return transaction;
}
/// <summary>
/// Begins a database transaction. Currently returns a new MockTransaction object that is
/// associated with the current connection.
/// </summary>
/// <returns>An object representing the new transaction.</returns>
public System.Data.IDbTransaction BeginTransaction()
{
IDbTransaction transaction = newMockTransaction();
((MockTransaction )transaction).SetParentConnection( this );
return transaction;
}
/// <summary>
/// Increments Create calls counter then returns the command object designated by the
/// SetupCommand method or throws exception
/// </summary>
/// <returns>Command Object</returns>
public System.Data.IDbCommand CreateCommand()
{
_createCalls.Inc();
if (_createCommandException != null)
{
throw _createCommandException;
}
if (_command == null)
{
IDbCommand command = newMockCommand();
command.Connection = this;
return command;
}
else
{
_command.Connection = this;
return _command;
}
}
/// <summary>
/// Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object.
/// </summary>
publicvoid Open()
{
_openCalls.Inc();
_connectionState = ConnectionState.Open;
}
/// <summary>
/// Increments CloseCalls counter
/// </summary>
publicvoid Close()
{
_closeCalls.Inc();
_connectionState = ConnectionState.Closed;
}
/// <summary>
/// Gets the current state of the Connection
/// </summary>
public System.Data.ConnectionState State
{
get
{
return _connectionState;
}
}
/// <summary>
/// Gets/Sets the string used to open a database
/// </summary>
publicstring ConnectionString
{
get
{
return _connectionString.Actual;
}
set
{
_connectionString.Actual = value;
}
}
/// <summary>
/// Gets the name of the current database or the database to be used once a connection is open
/// </summary>
publicstring Database
{
get
{
returnnull;
}
}
/// <summary>
/// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error
/// </summary>
publicint ConnectionTimeout
{
get
{
return _connectionTimeout;
}
}
#endregion
#region Implementation of IDisposable
/// <summary>
/// Releases resources used by MockDbConnection
/// </summary>
publicvoid Dispose()
{
if (_connectionState != ConnectionState.Closed)
{
Close();
}
}
#endregion
}
}