Пример #1
0
        public void ShouldReplaceTokensInInstanceName()
        {
            string doSomethingInstanceName     = "Method DoSomething";
            string doSomethingElseInstanceName = "Method DoSomethingElse";

            ResetCounters(PerformanceCounterCallHandler.TotalInstanceName);
            ResetCounters(doSomethingInstanceName);
            ResetCounters(doSomethingElseInstanceName);

            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());

            callHandler.InstanceName = "Method {method}";
            MonitorTarget target = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetTotalCallCount());
            Assert.AreEqual(0L, GetNumberOfCallsCount(doSomethingInstanceName));
            Assert.AreEqual(0L, GetNumberOfCallsCount(doSomethingElseInstanceName));

            target.DoSomething();
            target.DoSomethingElse();
            target.DoSomething();
            target.DoSomething();
            target.DoSomethingElse();
            target.DoSomething();

            Assert.AreEqual(6L, GetTotalCallCount());
            Assert.AreEqual(4L, GetNumberOfCallsCount(doSomethingInstanceName));
            Assert.AreEqual(2L, GetNumberOfCallsCount(doSomethingElseInstanceName));
        }
Пример #2
0
        private RemotingPolicyInjector GetFactoryWithPolicies()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);
            return(factory);
        }
        public void CanInterceptOnFormsClass()
        {
            CallCountHandler countHandler = new CallCountHandler();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPolicySet(countHandler));

            IMyForm magicForm = factory.Create<MyForm, IMyForm>();
        }
Пример #4
0
        public void CanInterceptOnFormsClass()
        {
            CallCountHandler       countHandler = new CallCountHandler();
            RemotingPolicyInjector factory      = new RemotingPolicyInjector(GetPolicySet(countHandler));

            IMyForm magicForm = factory.Create <MyForm, IMyForm>();
        }
Пример #5
0
        public void ShouldBeAbleToCacheNullReturnValues()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetCachingPolicies());
            CachingTarget          target  = factory.Create <CachingTarget>();

            string result  = target.SometimesReturnsNull(false);
            string result2 = target.SometimesReturnsNull(true);
        }
Пример #6
0
        public void ShouldNotInterceptAnyMethodsOnClassThatHasNoPolicyAttribute()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);
            CriticalFakeDal dal = factory.Create <CriticalFakeDal>();

            Assert.IsFalse(RemotingServices.IsTransparentProxy(dal));
        }
Пример #7
0
        public void CanCastToMBROBaseClass()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(new PolicySet(GetCallCountingPolicy()));

            IDal dal = factory.Create <MockDal, IDal>();

            Assert.IsNotNull(dal as IMonitor);
            Assert.IsNotNull(dal as MockDal);
        }
Пример #8
0
        public void ShouldCreateRawObjectWhenNoPolicyPresent()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();
            MockDal dal = factory.Create <MockDal>();

            Assert.IsNotNull(dal);
            Assert.IsTrue(dal is MockDal);
            Assert.IsFalse(RemotingServices.IsTransparentProxy(dal));
        }
Пример #9
0
        public void ShouldBeAbleToSwallowExceptionFromPropertySet()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddExceptionPolicy(factory, "Swallow Exceptions", new TypeMatchingRule("TargetType"));
            TargetType target = factory.Create <TargetType>();

            target.MyProperty = 5;
        }
Пример #10
0
        public void ShouldRethrowFromNoOpPolicy()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddExceptionPolicy(factory, "No-Op Policy", new TypeMatchingRule("TargetType"));
            TargetType target = factory.Create <TargetType>();

            target.ThrowFromFunctionWithReturnValue();
        }
Пример #11
0
        public void ShouldTranslateException()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddExceptionPolicy(factory, "Translate Exceptions", new TypeMatchingRule("TargetType"));
            TargetType target = factory.Create <TargetType>();

            target.WillThrowException();
        }
Пример #12
0
        public void ShouldThrowWhenSwallowingExceptionFromPropertyGet()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddExceptionPolicy(factory, "Swallow Exceptions", new TypeMatchingRule("TargetType"));
            TargetType target = factory.Create <TargetType>();

            int foo = target.MyProperty;
        }
Пример #13
0
        public void CannotCastToNonMBROBaseClass()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(new PolicySet(GetCallCountingPolicy()));

            IDal dal = factory.Create <InterfacesOnlyDal, IDal>();

            Assert.IsNotNull(dal as IMonitor);
            Assert.IsNull(dal as InterfacesOnlyDal);
        }
Пример #14
0
        public void ShouldRecordOneCallInstance()
        {
            ResetCounters();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());
            MonitorTarget          target  = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetNumberOfCallsCount(TestInstanceName));
            target.DoSomething();
            Assert.AreEqual(1L, GetNumberOfCallsCount(TestInstanceName));
        }
Пример #15
0
        public void ShouldRecordOneCallTotal()
        {
            ResetCounters();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());
            MonitorTarget          target  = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetTotalCallCount());
            target.DoSomething();
            Assert.AreEqual(1L, GetTotalCallCount());
        }
Пример #16
0
        public void ShouldThrowWhenSwallowingExceptionFromNonVoidMethod()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddExceptionPolicy(factory, "Swallow Exceptions", new TypeMatchingRule("TargetType"));
            TargetType target = factory.Create <TargetType>();

            target.ThrowFromFunctionWithReturnValue();
            Assert.Fail("An exception should have been thrown");
        }
Пример #17
0
        public void ShouldThrowCorrectException()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddNoopPolicy(factory, new TypeMatchingRule("TargetType"));

            TargetType target = factory.Create <TargetType>();

            target.WillThrowException();
        }
Пример #18
0
        public void ShouldRequireInteceptionForTypeMatchingPolicy()
        {
            RemotingPolicyInjector factory = GetFactoryWithPolicies();

            bool shouldInterceptMockDal = factory.TypeRequiresInterception(typeof(MockDal));
            bool shouldInterceptString  = factory.TypeRequiresInterception(typeof(string));

            Assert.IsTrue(shouldInterceptMockDal);
            Assert.IsFalse(shouldInterceptString);
        }
Пример #19
0
        public void ShouldCallHandlersWhenCallingMethods()
        {
            RemotingPolicyInjector factory = GetFactoryWithPolicies();
            MockDal dal = factory.Create <MockDal>();

            Assert.AreEqual(0, countHandler.CallCount);
            dal.DoSomething("43");
            dal.DoSomething("63");
            dal.DoSomething("Hike!");
            Assert.AreEqual(3, countHandler.CallCount);
        }
Пример #20
0
        public void ShouldCallProperHandlersInThePresenceOfOverloadedMethods()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddOverloadsPolicy(factory);

            MockDalWithOverloads dal = factory.Create <MockDalWithOverloads>();

            Assert.AreEqual(42, dal.DoSomething("not intercepted"));
            Assert.IsNull(dal.DoSomething(42));
        }
Пример #21
0
        public void ShouldNotCacheVoidMethods()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetCachingPolicies());
            CachingTarget          target  = factory.Create <CachingTarget>();

            Assert.AreEqual(0, target.Count);
            target.IncrementCount();
            Assert.AreEqual(1, target.Count);
            target.IncrementCount();
            Assert.AreEqual(2, target.Count);
        }
Пример #22
0
        public void HandlerCanShortcutMethodExecution()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            factory.Policies.Add(GetShortcutPolicy());

            MockDal dal = factory.Create <MockDal>();

            Assert.AreEqual(42, dal.DoSomething("should return 42"));
            Assert.AreEqual(-1, dal.DoSomething("shortcut"));
        }
Пример #23
0
        public void ShouldInterceptInterfaceImplementationMethods()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

            IDal dal = factory.Create <MockDal, IDal>();

            Assert.AreEqual(0, countHandler.CallCount);
            dal.Deposit(200);
            Assert.AreEqual(1, countHandler.CallCount);
        }
Пример #24
0
        public void ShouldNotUpdateExceptionsPerSecondByDefault()
        {
            ResetCounters();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());
            MonitorTarget          target  = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetExceptionsPerSecondCount(TestInstanceName));

            CauseExceptions(target);

            Assert.AreEqual(0L, GetExceptionsPerSecondCount(TestInstanceName));
        }
Пример #25
0
        public void ShouldNotBeAbleToCastToUnimplementedInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

            IDal dal = factory.Create <MockDal, IDal>();

            ICallHandler ch = dal as ICallHandler;

            Assert.IsNull(ch);
        }
Пример #26
0
        public void CanCastAcrossInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

            IDal dal = factory.Create <MockDal, IDal>();

            IMonitor monitor = dal as IMonitor;

            Assert.IsNotNull(monitor);
        }
Пример #27
0
        public void ShouldDefaultToDisableExceptionCounter()
        {
            ResetCounters();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());
            MonitorTarget          target  = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetNumberOfExceptionsCount(TestInstanceName));

            CauseExceptions(target);

            Assert.AreEqual(0L, GetNumberOfExceptionsCount(TestInstanceName));
        }
Пример #28
0
        public void CanCastToImplementedInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

            MockDal dal = factory.Create <MockDal>();

            IDal iDal = dal as IDal;

            Assert.IsNotNull(iDal);
        }
Пример #29
0
        public void CanRewrapAnInterceptedObject()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(new PolicySet(GetCallCountingPolicy()));
            IDal dal = factory.Create <MockDal, IDal>();

            object dalTarget = ((InterceptingRealProxy)RemotingServices.GetRealProxy(dal)).Target;

            IMonitor monitor = factory.Wrap <IMonitor>(dal);

            object monitorTarget = ((InterceptingRealProxy)RemotingServices.GetRealProxy(monitor)).Target;

            Assert.AreSame(dalTarget, monitorTarget);
        }
Пример #30
0
        public void ShouldNotInterceptMethodsThatHaveNoPolicyAttribute()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

            MockDal dal = factory.Create <MockDal>();

            Assert.IsTrue(RemotingServices.IsTransparentProxy(dal));
            Assert.AreEqual(0, countHandler.CallCount);
            dal.SomethingCritical();
            Assert.AreEqual(0, countHandler.CallCount);
        }
Пример #31
0
        public void ShouldAddInterceptionAccordingToPolicy()
        {
            RemotingPolicyInjector factory = GetFactoryWithPolicies();

            MockDal dal = factory.Create <MockDal>();

            Assert.IsTrue(dal is MockDal);
            Assert.IsTrue(RemotingServices.IsTransparentProxy(dal));
            object realProxy = RemotingServices.GetRealProxy(dal);

            Assert.IsNotNull(realProxy);
            Assert.IsTrue(realProxy is InterceptingRealProxy);
        }