示例#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));
        }
        public void CanInterceptOnFormsClass()
        {
            CallCountHandler countHandler = new CallCountHandler();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPolicySet(countHandler));

            IMyForm magicForm = factory.Create<MyForm, IMyForm>();
        }
示例#3
0
        public void CanInterceptOnFormsClass()
        {
            CallCountHandler       countHandler = new CallCountHandler();
            RemotingPolicyInjector factory      = new RemotingPolicyInjector(GetPolicySet(countHandler));

            IMyForm magicForm = factory.Create <MyForm, IMyForm>();
        }
示例#4
0
        public void ShouldBeAbleToCacheNullReturnValues()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetCachingPolicies());
            CachingTarget          target  = factory.Create <CachingTarget>();

            string result  = target.SometimesReturnsNull(false);
            string result2 = target.SometimesReturnsNull(true);
        }
示例#5
0
        public void ShouldRethrowFromNoOpPolicy()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

            target.ThrowFromFunctionWithReturnValue();
        }
示例#6
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);
        }
示例#7
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));
        }
示例#8
0
        public void ShouldNotInterceptAnyMethodsOnClassThatHasNoPolicyAttribute()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

            Assert.IsFalse(RemotingServices.IsTransparentProxy(dal));
        }
示例#9
0
        public void ShouldThrowWhenSwallowingExceptionFromPropertyGet()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

            int foo = target.MyProperty;
        }
示例#10
0
        public void ShouldTranslateException()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

            target.WillThrowException();
        }
示例#11
0
        public void ShouldBeAbleToSwallowExceptionFromPropertySet()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

            target.MyProperty = 5;
        }
示例#12
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);
        }
示例#13
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());
        }
示例#14
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");
        }
示例#15
0
        public void ShouldThrowCorrectException()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

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

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

            target.WillThrowException();
        }
示例#16
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));
        }
示例#17
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"));
        }
示例#18
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));
        }
示例#19
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);
        }
示例#20
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);
        }
示例#21
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));
        }
示例#22
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);
        }
示例#23
0
        public void CanCastAcrossInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

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

            IMonitor monitor = dal as IMonitor;

            Assert.IsNotNull(monitor);
        }
示例#24
0
        public void ShouldNotBeAbleToCastToUnimplementedInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

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

            ICallHandler ch = dal as ICallHandler;

            Assert.IsNull(ch);
        }
示例#25
0
        public void CanCastToImplementedInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

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

            IDal iDal = dal as IDal;

            Assert.IsNotNull(iDal);
        }
示例#26
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));
        }
示例#27
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);
        }
示例#28
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);
        }
示例#29
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);
        }
示例#30
0
        public void ShouldCallHandlersOnPropertyGet()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddPropertiesPolicy(factory);

            MockDal dal          = factory.Create <MockDal>();
            double  startBalance = dal.Balance;

            dal.Balance = 162.3;
            double endBalance = dal.Balance;

            Assert.AreEqual(2, countHandler.CallCount);
        }
示例#31
0
        public void ShouldBeAbleToDisableCallCount()
        {
            ResetCounters();
            RemotingPolicyInjector factory = new RemotingPolicyInjector(GetPerfMonPolicies());

            callHandler.IncrementNumberOfCalls = false;
            MonitorTarget target = factory.Create <MonitorTarget>();

            Assert.AreEqual(0L, GetTotalCallCount());
            Assert.AreEqual(0L, GetNumberOfCallsCount(TestInstanceName));
            target.DoSomething();
            Assert.AreEqual(0L, GetNumberOfCallsCount(TestInstanceName));
            Assert.AreEqual(0L, GetTotalCallCount());
        }