MockTransaction.cs :  » Testing » MockObjects » DotNetMock » Framework » Data » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Testing » MockObjects 
MockObjects » DotNetMock » Framework » Data » MockTransaction.cs
using System;
using System.Data;

namespace DotNetMock.Framework.Data{
  /// <summary>
  /// This Mock Object implements the IDbTransaction interface
  /// </summary>
  public class MockTransaction : MockObject, IDbTransaction
  {
    private IDbConnection _parentConnection = null;
    private IsolationLevel _isolationLevel = IsolationLevel.Unspecified;

    private ExpectationBool _commitCalled = new ExpectationBool( "MockDbTransaction.CommitCalled" );
    private ExpectationBool _rollbackCalled = new ExpectationBool( "MockDbTransaction.RollbackCalled" );

    private Exception _commitException = null;
    private Exception _rollbackException = null;

    private bool _hasCommitFailed = false; // to allow for rollback later

    /// <summary>
    /// Default Constructor
    /// </summary>
    public MockTransaction() {}


    #region Mock Methods
    /// <summary>
    /// Set expected <see cref="Exception"/> on commit.
    /// </summary>
    /// <param name="exception">expected <see cref="Exception"/></param>
    public void SetExpectedCommitException( Exception exception )
    {
      _commitException = exception;
    }
    /// <summary>
    /// Set expected <see cref="Exception"/> on rollback.
    /// </summary>
    /// <param name="exception">expected <see cref="Exception"/></param>
    public void SetExpectedRollbackException( Exception exception )
    {
      _rollbackException = exception;
    }
    /// <summary>
    /// Set whether a commit call is expected.
    /// </summary>
    /// <param name="commit">true if we expect a commit call</param>
    public void ExpectCommitCall( bool commit )
    {
      _commitCalled.Expected = commit;
    }
    /// <summary>
    /// Set whether a rollback call is expected.
    /// </summary>
    /// <param name="rollback">true if we expect a rollback call</param>
    public void ExpectRollbackCall( bool rollback )
    {
      _rollbackCalled.Expected = rollback;
    }
    #endregion

    #region Implementation of IDbTransaction
    /// <summary>
    /// Rolls back a transaction from a pending state.  Currently Not Implemented
    /// </summary>
    public void Rollback()
    {
      if ( _rollbackCalled.Actual ) 
      {
        throw new InvalidOperationException( "Cannot call rollback more than once." );
      }
      _rollbackCalled.Actual = true;
      if ( _commitCalled.Actual  && _hasCommitFailed == false) 
      {
        throw new InvalidOperationException( "Cannot call rollback after Commit() has been called." );
      }
      if ( _rollbackException != null ) 
      {
        throw _rollbackException;
      }
    }
    /// <summary>
    /// Commits the database transaction
    /// </summary>
    public void Commit()
    {
      if ( _commitCalled.Actual ) 
      {
        throw new InvalidOperationException( "Cannot call commit more than once." );
      }
      _commitCalled.Actual = true;
      if ( _commitException != null ) 
      {
        _hasCommitFailed = true;
        throw _commitException;
      }
    }
    /// <summary>
    /// Gets the Connection object associated with the transaction.  Currently Not Implemented
    /// </summary>
    public System.Data.IDbConnection Connection
    {
      get
      {
        return _parentConnection;
      }
    }
    /// <summary>
    /// Gets the IsolationLevel for this transaction.  Currently Not Implemented
    /// </summary>
    public System.Data.IsolationLevel IsolationLevel
    {
      get
      {
        return _isolationLevel;
      }
    }
    #endregion

    #region Implementation of IDisposable
    /// <summary>
    /// Releases the resources used by MockTransaciton
    /// </summary>
    public void Dispose() 
    {
    }
    #endregion

    #region Internal Methods
    internal void SetParentConnection( IDbConnection connection ) 
    {
      _parentConnection = connection;
    }
    internal void SetTransactionIsolationLevel( IsolationLevel isolationLevel )
    {
      _isolationLevel = isolationLevel;
    }
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.