MockIPrincipal.cs :  » Testing » MockObjects » DotNetMock » Framework » Security » Principal » 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 » Security » Principal » MockIPrincipal.cs
using System;
using System.Collections;
using System.Security.Principal;
using DotNetMock;

namespace DotNetMock.Framework.Security.Principal{
  /// <summary>
  /// Mock Object to implement the IPrincipal interface
  /// </summary>
  public class MockIPrincipal : MockObject, IPrincipal
  {
    /// <summary>
    /// Current Identity associated with this Principal
    /// </summary>
    private IIdentity _expectedIdentity = null;
    /// <summary>
    /// Expected number of IsInRole() calls
    /// </summary>
    private ExpectationCounter _isInRoleCalls = new ExpectationCounter("MockIPrincipal.IsIsRoleCalls");
    /// <summary>
    /// Lists of roles the current Principal belongs to
    /// </summary>
    private ArrayList _roles = null;
    /// <summary>
    /// Default Constructor
    /// </summary>
    public MockIPrincipal() : base("MockIPrincipal")
    {
      _roles = new ArrayList();
    }
    #region MockMethods
    /// <summary>
    /// Sets the expected value of Identity to return
    /// </summary>
    /// <param name="identity">Identity for current Principal</param>
    public void SetExpectedIdentity( IIdentity identity ) 
    {
      _expectedIdentity = identity;
    }
    /// <summary>
    /// Sets the number of calls to IsInRole()
    /// </summary>
    /// <param name="count">expected number of calls to IsInRol()</param>
    public void SetExpectedIsInRoleCount( int count )
    {
      _isInRoleCalls.Expected = count;  
    }
    /// <summary>
    /// Adds the given role to the list of roles this Principal belongs to
    /// </summary>
    /// <param name="role">role to add</param>
    public void AddExpectedRole( string role ) 
    {
      _roles.Add( role );
    }
    /// <summary>
    /// Adds the given roles to the list of roles this Principal belongs to
    /// </summary>
    /// <param name="roles"></param>
    public void AddExpectedRoles( string[] roles )
    {
      for (int i = 0; i < roles.Length; i++) 
      {
        AddExpectedRole( roles[i] );
      }
    }
    #endregion
    #region Implementation of IPrincipal
    /// <summary>
    /// Returns true/false if the current principal belongs to the given role
    /// </summary>
    /// <param name="roleToSearch">Role to check for</param>
    /// <returns>True/False</returns>
    public bool IsInRole(string roleToSearch)
    {
      _isInRoleCalls.Inc();
      bool found = false;
      foreach (string role in _roles) 
      {
        if (role.Equals(roleToSearch)) 
        {
          found = true;
        }
      }
      return found;
    }
    /// <summary>
    /// Returns the current Identity associated with this Principal
    /// </summary>
    public System.Security.Principal.IIdentity Identity
    {
      get
      {
        if (_expectedIdentity == null) 
        {
          return new MockIIdentity();
        }
        return _expectedIdentity;
      }
    }
    #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.