예제 #1
0
 public CensorCapability(ConsumedCapability interceptedCapability, IInterceptionPolicy policy)
 {
     InterceptedCapability = interceptedCapability;
     interceptedCapability.AddRef();
     Policy = policy;
     MyVine = Vine.Create(this);
 }
예제 #2
0
        public void ReturnsValueOfSinglePolicy(bool expected, IInterceptionPolicy policy, IInvocation invocation)
        {
            policy.ShouldIntercept(invocation).Returns(expected);

            var sut = new AndInterceptionPolicy(policy);

            var result = sut.ShouldIntercept(invocation);

            result.Should().Be(expected);
        }
        public void ReturnsValueOfSinglePolicy(bool expected, IInterceptionPolicy policy, IInvocation invocation)
        {
            policy.ShouldIntercept(invocation).Returns(expected);

            var sut = new AndInterceptionPolicy(policy);

            var result = sut.ShouldIntercept(invocation);

            result.Should().Be(expected);
        }
        public void FalsePolicyDecisionCallsElseInterceptor(IInterceptionPolicy policy,
            IInterceptor interceptor,
            IInterceptor elseInterceptor,
            IInvocation invocation)
        {
            var sut = new FilteringInterceptor(policy, interceptor, elseInterceptor);
            policy.ShouldIntercept(invocation).Returns(false);

            sut.Intercept(invocation);

            elseInterceptor.Received().Intercept(invocation);
        }
        public void TruePolicyDecisionDoesntCallElseInterceptor(IInterceptionPolicy policy,
            IInterceptor interceptor,
            IInterceptor elseInterceptor,
            IInvocation invocation)
        {
            var sut = new FilteringInterceptor(policy, interceptor, elseInterceptor);
            policy.ShouldIntercept(invocation).Returns(false);

            sut.Intercept(invocation);

            interceptor.DidNotReceive().Intercept(invocation);
        }
        public FilteringInterceptor(IInterceptionPolicy policy, IInterceptor interceptor, IInterceptor elseInterceptor)
        {
            if (policy == null)
                throw new ArgumentNullException("policy");
            if (interceptor == null)
                throw new ArgumentNullException("interceptor");
            if (elseInterceptor == null)
                throw new ArgumentNullException("elseInterceptor");

            _interceptor = interceptor;
            _policy = policy;
            _elseInterceptor = elseInterceptor;
        }
예제 #7
0
        public void FalsePolicyDecisionCallsElseInterceptor(IInterceptionPolicy policy,
                                                            IInterceptor interceptor,
                                                            IInterceptor elseInterceptor,
                                                            IInvocation invocation)
        {
            var sut = new FilteringInterceptor(policy, interceptor, elseInterceptor);

            policy.ShouldIntercept(invocation).Returns(false);

            sut.Intercept(invocation);

            elseInterceptor.Received().Intercept(invocation);
        }
예제 #8
0
        public void TruePolicyDecisionDoesntCallElseInterceptor(IInterceptionPolicy policy,
                                                                IInterceptor interceptor,
                                                                IInterceptor elseInterceptor,
                                                                IInvocation invocation)
        {
            var sut = new FilteringInterceptor(policy, interceptor, elseInterceptor);

            policy.ShouldIntercept(invocation).Returns(false);

            sut.Intercept(invocation);

            interceptor.DidNotReceive().Intercept(invocation);
        }
예제 #9
0
        public void ReturnsAllOfMultiplePolicies(bool a, bool b, bool expected,
                                                 IInterceptionPolicy policyA,
                                                 IInterceptionPolicy policyB,
                                                 IInvocation invocation)
        {
            policyA.ShouldIntercept(invocation).Returns(a);
            policyB.ShouldIntercept(invocation).Returns(b);

            var sut = new AndInterceptionPolicy(policyA, policyB);

            var result = sut.ShouldIntercept(invocation);

            result.Should().Be(expected);
        }
        public void ReturnsAllOfMultiplePolicies(bool a, bool b, bool expected,
            IInterceptionPolicy policyA,
            IInterceptionPolicy policyB,
            IInvocation invocation)
        {
            policyA.ShouldIntercept(invocation).Returns(a);
            policyB.ShouldIntercept(invocation).Returns(b);

            var sut = new AndInterceptionPolicy(policyA, policyB);

            var result = sut.ShouldIntercept(invocation);

            result.Should().Be(expected);
        }
예제 #11
0
        /// <summary>
        /// Detach this policy from given (censored) capability.
        /// </summary>
        /// <typeparam name="TCap">Capability interface type</typeparam>
        /// <param name="policy">Policy to detach</param>
        /// <param name="cap">Capability to clean</param>
        /// <returns>Clean capability instance (at least, without this interception policy)</returns>
        /// <exception cref="ArgumentNullException"><paramref name="policy"/> is null or
        /// <paramref name="cap"/> is null</exception>
        public static TCap Detach <TCap>(this IInterceptionPolicy policy, TCap cap)
            where TCap : class
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            if (cap == null)
            {
                throw new ArgumentNullException(nameof(cap));
            }

            switch (cap)
            {
            case Proxy proxy:
                return((CapabilityReflection.CreateProxy <TCap>(Detach(policy, proxy.ConsumedCap !)) as TCap) !);

            case CensorCapability ccap:
            {
                CensorCapability?cur = ccap;
                var stk = new Stack <IInterceptionPolicy>();

                do
                {
                    if (policy.Equals(cur.Policy))
                    {
                        var cur2 = cur.InterceptedCapability;

                        foreach (var p in stk)
                        {
                            cur2 = p.Attach(cur2);
                        }
                        return((cur2 as TCap) !);
                    }

                    stk.Push(cur.Policy);
                    cur = cur.InterceptedCapability as CensorCapability;
                }while (cur != null);

                return((ccap as TCap) !);
            }

            default:
                return(cap);
            }
        }
예제 #12
0
        public FilteringInterceptor(IInterceptionPolicy policy, IInterceptor interceptor, IInterceptor elseInterceptor)
        {
            if (policy == null)
            {
                throw new ArgumentNullException("policy");
            }
            if (interceptor == null)
            {
                throw new ArgumentNullException("interceptor");
            }
            if (elseInterceptor == null)
            {
                throw new ArgumentNullException("elseInterceptor");
            }

            _interceptor     = interceptor;
            _policy          = policy;
            _elseInterceptor = elseInterceptor;
        }
예제 #13
0
        /// <summary>
        /// Attach this policy to given capability.
        /// </summary>
        /// <typeparam name="TCap">Capability interface type</typeparam>
        /// <param name="policy">Policy to attach</param>
        /// <param name="cap">Capability to censor</param>
        /// <returns>Censored capability instance</returns>
        /// <exception cref="ArgumentNullException"><paramref name="policy"/> is null or
        /// <paramref name="cap"/> is null</exception>
        public static TCap Attach <TCap>(this IInterceptionPolicy policy, TCap cap)
            where TCap : class
        {
            if (policy == null)
            {
                throw new ArgumentNullException(nameof(policy));
            }

            if (cap == null)
            {
                throw new ArgumentNullException(nameof(cap));
            }

            var cur = cap as CensorCapability;

            while (cur != null)
            {
                if (policy.Equals(cur.Policy))
                {
                    return(cap);
                }

                cur = cur.InterceptedCapability as CensorCapability;
            }

            switch (cap)
            {
            case Proxy proxy:
                return((CapabilityReflection.CreateProxy <TCap>(
                            Attach(policy, proxy.ConsumedCap !)) as TCap) !);

            case ConsumedCapability ccap:
                return((new CensorCapability(ccap, policy) as TCap) !);

            default:
                var temp = (CapabilityReflection.CreateProxy <TCap>(
                                CapabilityReflection.CreateSkeletonInternal(cap).AsCapability())) as TCap;
                return(Attach(policy, temp !) !);
            }
        }
 public InverseInterceptionPolicy(IInterceptionPolicy wrapped)
 {
     _wrapped = wrapped;
 }
        public void ThrowsOnNullElseInterceptor(IInterceptionPolicy policy, IInterceptor interceptor)
        {
            Action act = () => new FilteringInterceptor(policy, interceptor, null);

            act.ShouldThrow<ArgumentNullException>();
        }
예제 #16
0
 public FilteringInterceptor(IInterceptionPolicy policy, IInterceptor interceptor)
     : this(policy, interceptor, new NullInterceptor())
 {
 }
예제 #17
0
 public bool Equals(IInterceptionPolicy other)
 {
     return(other is MyPolicy myPolicy && _id.Equals(myPolicy._id));
 }
 public static IInterceptionPolicy And(this IInterceptionPolicy a, params IInterceptionPolicy[] others)
 {
     return(new AndInterceptionPolicy(new [] { a }.Concat(others)));
 }
 public FilteringInterceptor(IInterceptionPolicy policy, IInterceptor interceptor)
     : this (policy, interceptor, new NullInterceptor())
 { }
 private static IInterceptor If(IInterceptionPolicy policy, IInterceptor interceptor)
 {
     return(new FilteringInterceptor(policy, interceptor));
 }
예제 #21
0
        public void ThrowsOnNullElseInterceptor(IInterceptionPolicy policy, IInterceptor interceptor)
        {
            Action act = () => new FilteringInterceptor(policy, interceptor, null);

            act.ShouldThrow <ArgumentNullException>();
        }
예제 #22
0
 public ServiceRegistry()
 {
     _CatId2SupportedCategories = new ConcurrentDictionary <string, Rpc.Common.IdInformation>();
     _regId2Entry = new ConcurrentDictionary <string, RegData>();//Tuple<string, Registry.Entry, Common.Unregister>>();
     _savePolicy  = new InterceptPersistentPolicy(this);
 }
예제 #23
0
 public bool Equals([AllowNull] IInterceptionPolicy other)
 {
     return(this.Equals(other));
 }
 public InverseInterceptionPolicy(IInterceptionPolicy wrapped)
 {
     _wrapped = wrapped;
 }
예제 #25
0
 public CensorCapability(ConsumedCapability interceptedCapability, IInterceptionPolicy policy)
 {
     InterceptedCapability = interceptedCapability;
     interceptedCapability.AddRef();
     Policy = policy;
 }