Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Framework/Data In directory sc8-pr-cvs1:/tmp/cvs-serv20785/DotNetMock.Framework/Data Added Files: MockCommand.cs MockDataParameter.cs MockDataParameterCollection.cs MockDataReader.cs MockDbConnection.cs MockTransaction.cs Log Message: --- NEW FILE: MockCommand.cs --- using System; using System.Data; namespace DotNetMock.Data { /// <summary> /// This Mock Object implements the IDbCommand interface /// </summary> public class MockCommand : MockObject, IDbCommand { private ExpectationString _commandText = new ExpectationString("MockCommand.CommandText"); private ExpectationValue _commandTimeout = new ExpectationValue("MockCommand.CommandTimeout"); private ExpectationValue _commandType = new ExpectationValue("MockCommand.CommandType"); private ExpectationCounter _executeCalls = new ExpectationCounter("MockCommand.ExecuteCalls"); private Exception _executeException = null; private int _updateCount = 0; private MockDbConnection _connection = null; private MockTransaction _transaction = null; private MockDataParameterCollection _parameters = null; private MockDataReader _reader = null; private UpdateRowSource _updateRowSource = UpdateRowSource.None; /// <summary> /// Default constructor /// </summary> public MockCommand() { _parameters = new MockDataParameterCollection(); } #region Mock Methods /// <summary> /// Set update count to be returned from ExecuteNonQuery() /// </summary> /// <param name="count">Count to return</param> public void SetUpdateCount(int count) { _updateCount = count; } /// <summary> /// Set number of Execute calls to be expected /// </summary> /// <param name="calls">Calls to expect</param> public void SetExpectedExecuteCalls(int calls) { _executeCalls.Expected = calls; } /// <summary> /// Set Command Text to be expected /// </summary> /// <param name="commandText">Command Text to be expected</param> public void SetExpectedCommandText(string commandText) { _commandText.Expected = commandText; } /// <summary> /// Set Command Timeout to be expected /// </summary> /// <param name="commandTimeout">Timeout to expect</param> public void SetExpectedCommandTimeout(int commandTimeout) { _commandTimeout.Expected = commandTimeout; } /// <summary> /// Set Command Type to expected. <see cref="System.Data.CommandType"/> /// </summary> /// <param name="commandType">Command Type to expect</param> public void SetExpectedCommandType(System.Data.CommandType commandType) { _commandType.Expected = commandType; } /// <summary> /// Add parameter to expected parameters /// </summary> /// <param name="parameter">Parameter to add</param> public void SetExpectedParameter(IDataParameter parameter) { _parameters.AddExpected(parameter); } /// <summary> /// Set expected Data Reader to return from ExecuteReader() /// </summary> /// <param name="reader">Mock Data Reader to use</param> public void SetExpectedReader(IDataReader reader) { _reader = (MockDataReader)reader; } /// <summary> /// Set exception to throw on execute calls /// </summary> /// <param name="exception">Exception to call</param> public void SetExecuteException(System.Exception exception) { _executeException = exception; } /// <summary> /// Private function called by all execute methods. Serves two functions: /// Increments the ExecuteCalls counter and throws an exception if one is set /// </summary> private void innerExecute() { _executeCalls.Inc(); if (_executeException != null) { throw _executeException; } } #endregion #region Implementation of IDbCommand #region IDbCommand Methods /// <summary> /// Cancel the execution of the command. Currently Not Implemented /// </summary> public void Cancel() { throw new NotImplementedException("Canceling a command is currently not implemented."); } /// <summary> /// Creates a compiled version of the command. Currently Not Implemented /// </summary> public void Prepare() { throw new NotImplementedException("Canceling a command is currently not implemented."); } /// <summary> /// Executes the command, with the given behavior, then returns a Data Reader of results. /// Currently Not Implemented /// </summary> /// <param name="behavior">Command Behavior to use. <see cref="System.Data.CommandBehavior"/></param> /// <returns></returns> public System.Data.IDataReader ExecuteReader(System.Data.CommandBehavior behavior) { throw new NotImplementedException("ExecuteReader with a CommandBehavior parameter currently not implemented."); } /// <summary> /// Increments the number of execute calls and returns the MockDataReader previously setup. /// <see cref="SetExpectedReader()"/> /// </summary> /// <returns>MockDataReader</returns> public System.Data.IDataReader ExecuteReader() { innerExecute(); return _reader; } /// <summary> /// Increments the number of execute calls and returns a null object /// </summary> /// <returns>Null</returns> public object ExecuteScalar() { innerExecute(); return null; } /// <summary> /// Increments the number of execute calls and returns update count. /// <see cref="SetUpdateCount()"/> /// </summary> /// <returns>Update Count</returns> public int ExecuteNonQuery() { innerExecute(); return _updateCount; } /// <summary> /// Creates a new MockDataParameter /// </summary> /// <returns>MockDataParameter</returns> public System.Data.IDbDataParameter CreateParameter() { return (IDbDataParameter) new MockDataParameter(); } #endregion #region IDbCommand Properties // TODO: Implement other CommandTypes: StoredProcedure & TableDirect /// <summary> /// Gets/Sets the actual command type to use. Note: Only CommandType.Text is supported /// <see cref="System.Data.CommandType"/> /// </summary> public System.Data.CommandType CommandType { get { return (CommandType)_commandType.Actual; } set { if (!value.Equals(CommandType.Text)) { throw new NotImplementedException("Only CommandType.Text is currently supported."); } _commandType.Actual = value; } } // TODO: Implement actual CommandTimeout usage /// <summary> /// Gets/Sets actual command timeout to use. Note: Currently, only values of 0 are supported /// </summary> public int CommandTimeout { get { return (int)_commandTimeout.Actual; } set { if (value != 0) { throw new NotImplementedException("Custom CommandTimeout is not currently supported."); } _commandTimeout.Actual = value; } } /// <summary> /// Gets/Sets connection to associate with this command /// </summary> public System.Data.IDbConnection Connection { get { return _connection; } set { if (_connection != value) { this.Transaction = null; } _connection = (MockDbConnection)value; } } /// <summary> /// Gets/Sets how command results are applied to the <see cref="System.Data.DataRow">DataRow</see> /// when used by the <see cref="System.Data.Common.DbDataAdapter.Update">Update</see> method /// of a <see cref="System.Data.Common.DbDataAdapter">DbDataAdapter</see> /// </summary> public System.Data.UpdateRowSource UpdatedRowSource { get { return _updateRowSource; } set { _updateRowSource = value; } } /// <summary> /// Gets/Sets actual command text for this command /// </summary> public string CommandText { get { return _commandText.Actual; } set { _commandText.Actual = value; } } /// <summary> /// Gets the <see cref="DotNetMock.Data.MockDataParameterCollection">MockDataParameterCollection</see> /// </summary> public MockDataParameterCollection Parameters { get { return _parameters; } } /// <summary> /// Gets the <see cref="System.Data.IDataParameterCollection">IDataParameterCollection</see> /// </summary> System.Data.IDataParameterCollection IDbCommand.Parameters { get { return _parameters; } } // TODO: Takes steps to ensure that transaction is compatible with the current connection /// <summary> /// Gets/Sets current transaction associated with this command /// </summary> public System.Data.IDbTransaction Transaction { get { return _transaction; } set { _transaction = (MockTransaction)value; } } #endregion #endregion #region Implementation of IDisposable public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { if (_connection != null) _connection.Dispose(); } } ~MockCommand() { Dispose (false); } #endregion } } --- NEW FILE: MockDataParameter.cs --- using System; using System.Data; namespace DotNetMock.Data { /// <summary> /// This is a Mock object that implements the IDataParameter interface. /// </summary> public class MockDataParameter : MockObject, IDataParameter { private ExpectationBool _nullable = new ExpectationBool("MockDataParameter.IsNullable"); private ExpectationString _parameterName = new ExpectationString("MockDataParameter.Name"); private ExpectationValue _parameterValue = new ExpectationValue("MockDataParameter.Value"); private ExpectationString _parameterSourceColumn = new ExpectationString("MockDataParameter.SourceColumn"); private ExpectationValue _parameterType = new ExpectationValue("MockDataParameter.Type"); /// <summary> /// Default, Empty Constructor /// </summary> public MockDataParameter() {} /// <summary> /// Initializes a new MockDataParameter with the supplied name and of the supplied type /// </summary> /// <param name="name">Name for new parameter</param> /// <param name="newType">Type for new parameter</param> public MockDataParameter(string name, DbType newType) { _parameterName.Actual = name; _parameterType.Actual = newType; } /// <summary> /// Initializes a new MockDataParameter with the supplied name and value /// </summary> /// <param name="name">Name for the new parameter</param> /// <param name="value">Value for the new parameter</param> public MockDataParameter(string name, string value) { _parameterName.Actual = name; _parameterValue.Actual = value; } /// <summary> /// Initializes a new MockDataParameter with supplied name, and of the supplied type, corresponding to the /// supplied column /// </summary> /// <param name="name">Name for the new parameter</param> /// <param name="newType">Type for new parameter</param> /// <param name="sourceColumn">Corresponding column for the parameter</param> public MockDataParameter(string name, DbType newType, string sourceColumn) { _parameterName.Actual = name; _parameterType.Actual = newType; _parameterSourceColumn.Actual = sourceColumn; } /// <summary> /// Sets expected parameter name /// </summary> /// <param name="name">Parameter name to expect</param> public void SetExpectedName(string name) { _parameterName.Expected = name; } /// <summary> /// Sets expected parameter value /// </summary> /// <param name="value">Value to expect</param> public void SetExpectedValue(string value) { _parameterValue.Expected = value; } /// <summary> /// Sets IsNullable or not /// </summary> /// <param name="nullable">True/False</param> public void SetExpectedNullable(bool nullable) { _nullable.Expected = nullable; } /// <summary> /// Sets expected parameter type /// </summary> /// <param name="expectedType">Type to expect</param> public void SetExpectedType(DbType expectedType) { _parameterType.Expected = expectedType; } /// <summary> /// Sets expected source column /// </summary> /// <param name="column">Expected source column</param> public void SetExpectedSourceColumn(string column) { _parameterSourceColumn.Expected = column; } #region Implementation of IDataParameter // TODO: Implement complete directions: Input, Output, InputOutput, and ReturnValue /// <summary> /// Parameter Direction /// </summary> public System.Data.ParameterDirection Direction { get { return new System.Data.ParameterDirection(); } set { } } /// <summary> /// Paramter Type /// </summary> public System.Data.DbType DbType { get { return (DbType)_parameterType.Actual; } set { } } /// <summary> /// Parameter Value /// </summary> public object Value { get { return _parameterValue.Actual; } set { _parameterValue.Actual = value; _parameterType.Actual = inferType(value); } } /// <summary> /// Is Nullable /// </summary> public bool IsNullable { set { _nullable.Actual = value; } get { return _nullable.Actual; } } // TODO: Implement different DataRowVersion information: Current, Proposed, Default, and Original /// <summary> /// Parameter Source Version to use /// </summary> public System.Data.DataRowVersion SourceVersion { get { return DataRowVersion.Current; } set { } } /// <summary> /// Paramter Name /// </summary> public string ParameterName { get { return _parameterName.Actual; } set { } } /// <summary> /// Source Column for the parameter /// </summary> public string SourceColumn { get { return _parameterSourceColumn.Actual; } set { } } #endregion /// <summary> /// Infers corresponding DbType from the given value /// </summary> /// <param name="value">Value to use</param> /// <returns>Inferred DbType from given value</returns> private DbType inferType(Object value) { switch (Type.GetTypeCode(value.GetType())) { case TypeCode.Object: return DbType.Object; case TypeCode.Empty: case TypeCode.DBNull: case TypeCode.Char: case TypeCode.SByte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: throw new ApplicationException("Unsupported data type"); case TypeCode.Boolean: return DbType.Boolean; case TypeCode.Byte: return DbType.Byte; case TypeCode.Int16: return DbType.Int16; case TypeCode.Int32: return DbType.Int32; case TypeCode.Int64: return DbType.Int64; case TypeCode.Single: return DbType.Single; case TypeCode.Double: return DbType.Double; case TypeCode.Decimal: return DbType.Decimal; case TypeCode.DateTime: return DbType.DateTime; case TypeCode.String: return DbType.String; default: throw new ApplicationException("Value is of unknown data type"); } } } } --- NEW FILE: MockDataParameterCollection.cs --- using System; using System.Data; using System.Collections; using System.Globalization; namespace DotNetMock.Data { /// <summary> /// This is a Mock Object that implements the IDataParameterCollection interface. It holds a collection of IDataParamter objects. /// </summary> public class MockDataParameterCollection : MockObject, IDataParameterCollection { /// <summary> /// Internal collection of parameters. /// </summary> private ExpectationArrayList _parameterCollection = new ExpectationArrayList("MockDataParameterCollection.Tests"); /// <summary> /// Flag indicating read-only status. /// </summary> private bool _isReadOnly = false; /// <summary> /// Flag indicating fixed-size status. /// </summary> private bool _isFixedSize = false; /// <summary> /// Default constructor /// </summary> public MockDataParameterCollection() { } /// <summary> /// Adds expected paramter to the expected collection /// </summary> /// <param name="parameter">Parameter to add</param> public void AddExpected(IDataParameter parameter) { _parameterCollection.AddExpected(parameter.ParameterName); } #region Implementation of IDataParameterCollection /// <summary> /// Removes a parameter from the Actual collection /// </summary> /// <param name="parameterName">Name of parameter to remove</param> public void RemoveAt(string parameterName) { IList actual = _parameterCollection.ActualCollection; if (!this.Contains(parameterName) ) { throw new ApplicationException("Parameter by that name cannot be found."); } for (int i = 0; i < actual.Count; i++) { if (actual[i].Equals(parameterName)) { this.RemoveAt(i); } } } /// <summary> /// Verifies if the collection contains a parameter with the given name /// </summary> /// <param name="parameterName">Name of parameter to check</param> /// <returns>True/False</returns> public bool Contains(string parameterName) { bool result = false; IList actual = _parameterCollection.ActualCollection; for (int i = 0; i < actual.Count; i++) { if (actual[i].Equals(parameterName)) { result = true; } } return result; } /// <summary> /// Returns the index of the parameter with the given name /// </summary> /// <param name="parameterName">Name of the parameter</param> /// <returns>Zero based index of the parameter</returns> public int IndexOf(string parameterName) { if (!this.Contains(parameterName) ) { throw new ApplicationException("Parameter by that name cannot be found."); } int index = 0; IList actual = _parameterCollection.ActualCollection; for (int i = 0; i < actual.Count; i++) { if (actual[i].Equals(parameterName)) { index = i; } } return index; } /// <summary> /// Property: Provides index-based access to the parameter collection /// </summary> public object this[string parameterName] { get { IList actual = _parameterCollection.ActualCollection; return actual[IndexOf(parameterName)]; } set { IList actual = _parameterCollection.ActualCollection; actual[IndexOf(parameterName)] = value; } } #endregion #region Implementation of IList /// <summary> /// Removes the parameter at the given index /// </summary> /// <param name="index">Index to remove</param> public void RemoveAt(int index) { IList actual = _parameterCollection.ActualCollection; actual.RemoveAt(index); } /// <summary> /// Inserts a parameter at the given index. Currently *Not Implemented* /// </summary> /// <param name="index">Index to use</param> /// <param name="value">Parameter to use</param> public void Insert(int index, object value) { throw new NotImplementedException("Not implemented. Please use one of the Add() methods instead"); } /// <summary> /// Removes parameter. Currently *Not Implemented*. Please use RemoveAt() instead. /// </summary> /// <param name="value">Parameter to remove</param> public void Remove(object value) { throw new NotImplementedException("Not implemented. Please use RemoveAt() instead"); } /// <summary> /// Verifies object is contained in the collection. Currently *Not Implemented*. Please use Contains(string parameterName) instead. /// </summary> /// <param name="value">Parameter to check</param> /// <returns>True/False</returns> public bool Contains(object value) { throw new NotImplementedException("Not implemented. Please use IDataParameterCollection.Contains(string parameterName) instead"); } /// <summary> /// Clears Actual Collection /// </summary> public void Clear() { IList actual = _parameterCollection.ActualCollection; actual.Clear(); } /// <summary> /// Returns index of value. Currently *Not Implemented*. Please use IndexOf(string parameterName) instead. /// </summary> /// <param name="value">Value to check</param> /// <returns>Index of value</returns> public int IndexOf(object value) { throw new NotImplementedException("Not implemented. Please use IDataParameterCollection.IndexOf(string parameterName) instead"); } /// <summary> /// Adds a new MockDataParameter to the collection, with the given name and value. /// </summary> /// <param name="parameterName">Parameter name to use.</param> /// <param name="parameterValue">Parameter value to use.</param> /// <returns>Parameter added</returns> public IDataParameter Add(string parameterName, string parameterValue) { Add(parameterName); return null; } /// <summary> /// Adds IDataParameter to the collection. /// </summary> /// <param name="value">Parameter to add.</param> /// <returns>Parameter added</returns> public IDataParameter Add(IDataParameter value) { Add(value.ParameterName); return null; } /// <summary> /// Adds a new MockDataParameter to the collection, with the given name and type. /// </summary> /// <param name="parameterName">Parameter name to use.</param> /// <param name="type">Type of new parameter</param> /// <returns>Parameter added</returns> public IDataParameter Add(string parameterName, DbType type) { Add(parameterName); return null; } /// <summary> /// Adds value to the collection, as a IDataParameter. /// </summary> /// <param name="value">Value to add</param> /// <returns>0</returns> public int Add(object value) { _parameterCollection.AddActual(value); return 0; } /// <summary> /// Gets value indicating if the collection is read-only. /// </summary> public bool IsReadOnly { get { return _isReadOnly; } set { _isReadOnly = value; } } /// <summary> /// Provides zero-based index access to the collection. /// </summary> public object this[int index] { get { IList actual = _parameterCollection.ActualCollection; return (object)actual[index]; } set { IList actual = _parameterCollection.ActualCollection; actual[index] = (IDataParameter)value; } } /// <summary> /// Gets value indicating if the collection is fixed-size or not. /// </summary> public bool IsFixedSize { get { return _isFixedSize; } set { _isFixedSize = value; } } #endregion #region Implementation of ICollection /// <summary> /// Copies section of collection to given array. Currently *Not Implemented* /// </summary> /// <param name="array">Array to use</param> /// <param name="index">Collection index to start at</param> public void CopyTo(System.Array array, int index) { throw new NotImplementedException("Not Implemented."); } /// <summary> /// Gets count of collection /// </summary> public int Count { get { IList actual = _parameterCollection.ActualCollection; return actual.Count; } } // TODO: Implement thread-safe collection /// <summary> /// Gets value indicating if the collection is synchronized for thread-safe use. /// </summary> public bool IsSynchronized { get { throw new NotImplementedException("Not implemented."); } } // TODO: Implement thread-safe collection /// <summary> /// Returns synchronized root of the collection. /// </summary> public object SyncRoot { get { throw new NotImplementedException("Not implemented."); } } #endregion #region Implementation of IEnumerable /// <summary> /// Gets IEnumerator that can enumerate over the collection /// </summary> /// <returns></returns> public System.Collections.IEnumerator GetEnumerator() { IList actual = _parameterCollection.ActualCollection; return actual.GetEnumerator(); } #endregion } } --- NEW FILE: MockDataReader.cs --- using System; using System.Data; using DotNetMock; namespace DotNetMock.Data { /// <summary> /// This Mock Object implements the IDataReader interface. /// </summary> public class MockDataReader : MockObject, IDataReader { private int _recordsAffectedCount = -1; private bool _isClosed = false; private ExpectationCounter _closeCalls = new ExpectationCounter("MockDataReader.CloseCalls"); private ExpectationCounter _readCalls = new ExpectationCounter("MockDataReader.ReadCalls"); private DataTable _schemaTable = null; private object[,] _rows = new object[0,0]; private int _currentRow = -1; private Exception _getException = null; public MockDataReader() {} #region Mock Methods /// <summary> /// Set records affected count to return from RecordsAffected property /// </summary> /// <param name="count">Count to expect</param> public void SetRecordsAffectedCount(int count) { _recordsAffectedCount = count; } /// <summary> /// Set number of Close() calls to expect /// </summary> /// <param name="calls">Count to expect</param> public void SetExpectedCloseCalls(int calls) { _closeCalls.Expected = calls; } /// <summary> /// Sets Schema Table to used /// </summary> /// <param name="schemaTable">DataTable to describe columns</param> public void SetSchemaTable(DataTable schemaTable) { _schemaTable = schemaTable; } /// <summary> /// Set number of Read() calls to expect /// </summary> /// <param name="readCalls">Count to expect</param> public void SetExpectedReadCalls(int readCalls) { _readCalls.Expected = readCalls; } /// <summary> /// Set value of IsClosed property /// </summary> /// <param name="isClosed">True/False</param> public void SetIsClosedValue(bool isClosed) { _isClosed = isClosed; } /// <summary> /// Set data to use for all get calls /// </summary> /// <param name="rows">Two-Dimensional array to read data from. Format: [Row,Column]</param> public void SetRows(object[,] rows) { _rows = rows; } /// <summary> /// Set exception to throw on any GetXXX() calls /// </summary> /// <param name="exception">Exception to throw</param> public void SetGetException(Exception exception) { _getException = exception; } #endregion #region Implementation of IDataReader public void Close() { _closeCalls.Inc(); _isClosed = true; } /// <summary> /// Gets SchemaTable /// </summary> /// <returns></returns> public System.Data.DataTable GetSchemaTable() { return _schemaTable; } /// <summary> /// Advances the data reader to the next result, when reading the result batch SQL statements /// Currently Not Implemented /// </summary> /// <returns>True</returns> public bool NextResult() { this.NotImplemented("MockDataReader.NextResult"); return true; } public bool Read() { _readCalls.Inc(); if (_currentRow + 1 >= _rows.GetLength(0)) { return false; } _currentRow++; return true; } /// <summary> /// Gets a value indicating the depth of nesting for the current row. /// Currently Not Implemented /// </summary> public int Depth { get { this.NotImplemented("MockDataReader.Depth"); return 0; } } /// <summary> /// Gets a value indicating whether the data reader is closed /// </summary> public bool IsClosed { get { return _isClosed; } } /// <summary> /// Gets the number of rows changed, inserted, or deleted by executing the SQL statement /// Returns the value set by SetRecordsAffectedCount() /// </summary> public int RecordsAffected { get { return _recordsAffectedCount; } } #region Implementation of IDisposable public void Dispose() { } #endregion #region Implementation of IDataRecord /// <summary> /// Gets the 32-bit signed integer value of the specified field /// </summary> /// <param name="i">Index of the field to find</param> /// <returns>The 32-bit signed integer value of the specified field.</returns> public int GetInt32(int i) { return Convert.ToInt32(GetValue(i)); } /// <summary> /// Returns the value of the specified field /// </summary> /// <param name="i">Index of the field to find</param> /// <returns>The Object which will contain the field value upon return.</returns> public object GetValue(int i) { if (_getException != null) { throw _getException; } return _rows[_currentRow, i - 1]; } /// <summary> /// Return whether the specified field is set to null /// </summary> /// <param name="i">Index of the field to find</param> /// <returns>true if the specified field is set to null, otherwise false.</returns> public bool IsDBNull(int i) { bool result = true; if (!GetValue(i).Equals("")) { result = false; } return result; } /// <summary> /// Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset. Currently Not Implemented /// </summary> /// <param name="i">The zero-based column ordinal. </param> /// <param name="fieldOffset">The index within the field from which to begin the read operation. </param> /// <param name="buffer">The buffer into which to read the stream of bytes.</param> /// <param name="bufferoffset">The index for buffer to begin the read operation.</param> /// <param name="length">The number of bytes to read.</param> /// <returns>The actual number of bytes read.</returns> public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) { this.NotImplemented("MockDataReader.GetBytes"); return 0; } /// <summary> /// Gets the 8-bit unsigned integer value of the specified column. /// </summary> /// <param name="i">The zero-based column ordinal.</param> /// <returns>The 8-bit unsigned integer value of the specified column.</returns> public byte GetByte(int i) { return Convert.ToByte(GetValue(i)); } /// <summary> /// Gets the Type information corresponding to the type of Object that would be returned from GetValue. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The Type information corresponding to the type of Object that would be returned from GetValue.</returns> public System.Type GetFieldType(int i) { this.NotImplemented("MockDataReader.GetFieldType"); return null; } /// <summary> /// Gets the fixed-position numeric value of the specified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The fixed-position numeric value of the specified field.</returns> public decimal GetDecimal(int i) { return Convert.ToDecimal(GetValue(i)); } /// <summary> /// Gets all the attribute fields in the collection for the current record. /// </summary> /// <param name="values">An array of Object to copy the attribute fields into. </param> /// <returns>The number of instances of Object in the array.</returns> public int GetValues(object[] values) { this.NotImplemented("MockDataReader.GetValues"); return 0; } /// <summary> /// Gets the name for the field to find. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The name of the field or the empty string (""), if there is no value to return.</returns> public string GetName(int i) { this.NotImplemented("MockDataReader.GetName"); return null; } /// <summary> /// Gets the 64-bit signed integer value of the specified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The 64-bit signed integer value of the specified field.</returns> public long GetInt64(int i) { return Convert.ToInt64(GetValue(i)); } /// <summary> /// Gets the double-precision floating point number of the specified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The double-precision floating point number of the specified field.</returns> public double GetDouble(int i) { return Convert.ToDouble(GetValue(i)); } /// <summary> /// Gets the value of the specified column as a Boolean. /// </summary> /// <param name="i">The zero-based column ordinal. </param> /// <returns>The value of the column.</returns> public bool GetBoolean(int i) { return Convert.ToBoolean(GetValue(i)); } /// <summary> /// Returns the guid value of the specified field. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The guid value of the specified field.</returns> public System.Guid GetGuid(int i) { this.NotImplemented("MockDataReader.GetGuid"); return new System.Guid(); } /// <summary> /// Gets the date and time data value of the spcified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The date and time data value of the spcified field.</returns> public System.DateTime GetDateTime(int i) { return Convert.ToDateTime(GetValue(i)); } /// <summary> /// Return the index of the named field. Currently Not Implemented /// </summary> /// <param name="name">The name of the field to find. </param> /// <returns>The index of the named field.</returns> public int GetOrdinal(string name) { this.NotImplemented("MockDataReader.GetOrdinal"); return 0; } /// <summary> /// Gets the data type information for the specified field. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The data type information for the specified field.</returns> public string GetDataTypeName(int i) { this.NotImplemented("MockDataReader.GetDataTypeName"); return null; } /// <summary> /// Gets the single-precision floating point number of the specified field. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The single-precision floating point number of the specified field.</returns> public float GetFloat(int i) { this.NotImplemented("MockDataReader.GetFloat"); return 0; } /// <summary> /// Gets an IDataReader to be used when the field points to more remote structured data. Currently Not Implemented /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>An IDataReader to be used when the field points to more remote structured data.</returns> public System.Data.IDataReader GetData(int i) { this.NotImplemented("MockDataReader.GetData"); return null; } /// <summary> /// Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset. /// </summary> /// <param name="i">The zero-based column ordinal. </param> /// <param name="fieldoffset">The index within the row from which to begin the read operation. </param> /// <param name="buffer">The buffer into which to read the stream of bytes. </param> /// <param name="bufferoffset">The index for buffer to begin the read operation. </param> /// <param name="length">The number of bytes to read. </param> /// <returns></returns> public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) { this.NotImplemented("MockDataReader.GetChars"); return 0; } /// <summary> /// Gets the string value of the specified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The string value of the specified field.</returns> public string GetString(int i) { return Convert.ToString(GetValue(i)); } /// <summary> /// Gets the character value of the specified column. /// </summary> /// <param name="i">The zero-based column ordinal. </param> /// <returns>The character value of the specified column.</returns> public char GetChar(int i) { return Convert.ToChar(GetValue(i)); } /// <summary> /// Gets the 16-bit signed integer value of the specified field. /// </summary> /// <param name="i">The index of the field to find. </param> /// <returns>The 16-bit signed integer value of the specified field.</returns> public short GetInt16(int i) { return Convert.ToInt16(GetValue(i)); } /// <summary> /// Gets the specified column by name. /// </summary> public object this[string name] { get { return this[findColumnIndexForName(name)]; } } /// <summary> /// Gets the specified column by index /// </summary> public object this[int i] { get { return GetValue(i); } } /// <summary> /// Gets the number of columns in the current row. Currently Not Implemented /// </summary> public int FieldCount { get { this.NotImplemented("MockDataReader.FieldCount"); return 0; } } #endregion #endregion /// <summary> /// Finds the index of the given name /// </summary> /// <param name="name">Name of the column to find</param> /// <returns>Column index of the given name</returns> private int findColumnIndexForName(string name) { if (_schemaTable != null) { for (int i = 0; i < _schemaTable.Columns.Count; ++i) { if (_schemaTable.Columns[i].ColumnName.Equals(name)) { return i + 1; } } } throw new IndexOutOfRangeException("Column name: " + name + " not found"); } } } --- NEW FILE: MockDbConnection.cs --- using System; using System.Data; using DotNetMock; namespace DotNetMock.Data { /// <summary> /// This Mock Object implements the IDbConnection interface /// </summary> public class MockDbConnection : MockObject, IDbConnection { private IDbCommand _command; private ExpectationCounter _closeCalls = new ExpectationCounter("MockDbConnection.CloseCalls"); private ExpectationCounter _createCalls = new ExpectationCounter("MockDbConnection.CreateCalls"); private ExpectationString _connectionString = new ExpectationString("MockDbConnection.ConnectionString"); private Exception _createCommandException = null; /// <summary> /// Default Constructor /// </summary> public MockDbConnection() { } #region Mock Methods public void SetExpectedConnectionString(string connectionString) { _connectionString.Expected = connectionString; } /// <summary> /// Set expected command to return on CreateCommand() /// </summary> /// <param name="command">Command to return</param> public void SetupCommand(IDbCommand command) { _command = command; } /// <summary> /// Set expected number of Close() calls to expect /// </summary> /// <param name="calls">Count to expect</param> public void SetExpectedCloseCalls(int calls) { _closeCalls.Expected = calls; } /// <summary> /// Set exception to throw when CreateCommand() is called /// </summary> /// <param name="exception">Exception to throw</param> public void SetCreateCommandException(Exception exception) { _createCommandException = exception; } /// <summary> /// Set expected number of CreateCommand() calls /// </summary> /// <param name="calls">Count to expect</param> public void SetExpectedCreateCalls(int calls) { _createCalls.Expected = calls; } #endregion #region Implementation of IDbConnection /// <summary> /// Changes the current database for an open connection object. Current Not Implemented /// </summary> /// <param name="databaseName">Database to change to</param> public void ChangeDatabase(string databaseName) { this.NotImplemented("MockDbConnection.ChangeDatabase"); } /// <summary> /// Begins a database transaction. Currently Not Implemented /// </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) { this.NotImplemented("MockDbConnection.BeginTransaction"); return null; } /// <summary> /// Begins a database transaction. /// </summary> /// <returns>An object representing the new transaction.</returns> public System.Data.IDbTransaction BeginTransaction() { return null; } /// <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) { return _command; } else { throw _createCommandException; } } /// <summary> /// Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object. /// </summary> public void Open() { this.NotImplemented("MockDbConnection.Open"); } /// <summary> /// Increments CloseCalls counter /// </summary> public void Close() { _closeCalls.Inc(); } /// <summary> /// Gets the current state of the Connection /// </summary> public System.Data.ConnectionState State { get { return new System.Data.ConnectionState(); } } /// <summary> /// Gets/Sets the string used to open a database /// </summary> public string 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> public string Database { get { return null; } } /// <summary> /// Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error /// </summary> public int ConnectionTimeout { get { return 0; } } #endregion #region Implementation of IDisposable /// <summary> /// Releases resources used by MockDbConnection /// </summary> public void Dispose() { } #endregion } } --- NEW FILE: MockTransaction.cs --- using System; using System.Data; namespace DotNetMock.Data { /// <summary> /// This Mock Object implements the IDbTransaction interface /// </summary> public class MockTransaction : MockObject, IDbTransaction { /// <summary> /// Default Constructor /// </summary> public MockTransaction() { } #region Implementation of IDbTransaction /// <summary> /// Rolls back a transaction from a pending state. Currently Not Implemented /// </summary> public void Rollback() { this.NotImplemented("MockTransaction.Rollback"); } /// <summary> /// Commits the database transaction /// </summary> public void Commit() { this.NotImplemented("MockTransaction.Commit"); } /// <summary> /// Gets the Connection object associated with the transaction. Currently Not Implemented /// </summary> public System.Data.IDbConnection Connection { get { this.NotImplemented("MockTransaction.Connection"); return null; } } /// <summary> /// Gets the IsolationLevel for this transaction. Currently Not Implemented /// </summary> public System.Data.IsolationLevel IsolationLevel { get { this.NotImplemented("MockTransaction.IsolationLevel"); return System.Data.IsolationLevel.Unspecified; } } #endregion #region Implementation of IDisposable /// <summary> /// Releases the resources used by MockTransaciton /// </summary> public void Dispose() { } #endregion } } |