Example #1
0
        public void CanProxyWithBehaviorThatAddsInterface()
        {
            var target  = new MockDal();
            var proxied = Intercept.ThroughProxy(target,
                                                 new TransparentProxyInterceptor(),
                                                 new[] { new AdditionalInterfaceBehavior() });

            Assert.IsNotNull(proxied);
        }
        public void BehaviorAddsInterface()
        {
            var target  = new MockDal();
            var proxied = Intercept.ThroughProxy <IDal>(target,
                                                        new InterfaceInterceptor(),
                                                        new[] { new AdditionalInterfaceBehavior() });

            Assert.IsNotNull(proxied as IAdditionalInterface);
        }
Example #3
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));
        }
        public void CanInvokeMethodOnManuallyAddedInterface()
        {
            var target  = new MockDal();
            var proxied = Intercept.ThroughProxyWithAdditionalInterfaces <IDal>(target,
                                                                                new InterfaceInterceptor(),
                                                                                new[] { new AdditionalInterfaceBehavior(false) },
                                                                                new[] { typeof(IAdditionalInterface) });

            Assert.AreEqual(10, ((IAdditionalInterface)proxied).DoNothing());
        }
        public void CanManuallyAddAdditionalInterface()
        {
            var target  = new MockDal();
            var proxied = Intercept.ThroughProxyWithAdditionalInterfaces <IDal>(target,
                                                                                new InterfaceInterceptor(),
                                                                                new[] { new AdditionalInterfaceBehavior(false) },
                                                                                new[] { typeof(IAdditionalInterface) });

            Assert.IsNotNull(proxied as IAdditionalInterface);
        }
Example #6
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"));
        }
Example #7
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);
        }
Example #8
0
        public void CanCastToImplementedInterfaces()
        {
            RemotingPolicyInjector factory = new RemotingPolicyInjector();

            AddCallCountingDalPolicy(factory);

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

            IDal iDal = dal as IDal;

            Assert.IsNotNull(iDal);
        }
Example #9
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);
        }
Example #10
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);
        }
Example #11
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);
        }
Example #12
0
        public void ShouldInterceptMethodsIfTypeImplementsMatchingInterface()
        {
            PolicySet policies = new PolicySet(GetPolicyThatMatchesInterface());

            Assert.IsTrue(policies.AppliesTo(typeof(MockDal)));

            RemotingPolicyInjector factory =
                new RemotingPolicyInjector(new PolicySet(GetPolicyThatMatchesInterface()));

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

            dal.Deposit(123.45);
            dal.Withdraw(54.32);
            mockDal.DoSomething("foo");

            Assert.AreEqual(2, countHandler.CallCount);
        }
Example #13
0
        public void ShouldCreateAllPipelinesForTargetWhenCreatingViaInterface()
        {
            RuleDrivenPolicy policy = new RuleDrivenPolicy("MockDal Policy");

            policy.RuleSet.Add(new TypeMatchingRule(typeof(MockDal)));
            countHandler = new CallCountHandler();
            policy.Handlers.Add(countHandler);

            RemotingPolicyInjector factory = new RemotingPolicyInjector(new PolicySet(policy));
            IDal dal = factory.Create <MockDal, IDal>();

            IMonitor monitor = (IMonitor)dal;

            monitor.Log("one");
            monitor.Log("two");
            monitor.Log("tree");

            MockDal target = (MockDal)dal;

            target.DoSomething("something");

            Assert.AreEqual(4, countHandler.CallCount);
        }