From: <gc...@us...> - 2003-03-11 20:30:57
|
Update of /cvsroot/dotnetmock/dotnetmock/DotNetMock.Framework/Security/Principal In directory sc8-pr-cvs1:/tmp/cvs-serv20785/DotNetMock.Framework/Security/Principal Added Files: MockIIdentity.cs MockIPrincipal.cs Log Message: --- NEW FILE: MockIIdentity.cs --- using System; using DotNetMock; using System.Security.Principal; namespace DotNetMock.Security.Principal { /// <summary> /// MockObject implementing the IIdentity interface. Since the IIdentity interface only includes getters, /// this MockObject does not do much. /// </summary> public class MockIIdentity : MockObject, IIdentity { /// <summary> /// The expected AuthenticationType /// </summary> private string _expectedAuthType = ""; /// <summary> /// The expected Name /// </summary> private string _expectedName = ""; /// <summary> /// The expected value for IsAuthenticated /// </summary> private bool _expectedIsAuth = false; /// <summary> /// Default Constructor /// </summary> public MockIIdentity() { this.name = "MockIdentity"; } #region Mock Methods /// <summary> /// Sets the expected AuthenticationType value to return /// </summary> /// <param name="authType">Authentication Type to return</param> public void SetExpectedAuthenticationType( string authType ) { _expectedAuthType = authType; } /// <summary> /// Sets the expected IsAuthenticated value to return /// </summary> /// <param name="isAuth">IsAuthenticated value to return</param> public void SetExpectedIsAuthenticated( bool isAuth ) { _expectedIsAuth = isAuth; } /// <summary> /// Sets the expected Name value to return /// </summary> /// <param name="name">Name value to return</param> public void SetExpectedName( string name ) { _expectedName = name; } #endregion #region Implementation of IIdentity /// <summary> /// Returns true/false if the current Identity is Authenticated or not /// </summary> public bool IsAuthenticated { get { return _expectedIsAuth; } } /// <summary> /// Returns the name of the current Identity /// </summary> public string Name { get { return _expectedName; } } /// <summary> /// Returns the current Authentication Type /// </summary> public string AuthenticationType { get { return _expectedAuthType; } } #endregion } } --- NEW FILE: MockIPrincipal.cs --- using System; using System.Collections; using System.Security.Principal; using DotNetMock; namespace DotNetMock.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() { this.name = "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 { return _expectedIdentity; } } #endregion } } |