コード例 #1
0
        public void SupplyingOpenGenericInterfacesAsAdditionalInterfacesThrows()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            Exception exception = null;

            try
            {
                object proxy =
                    interceptor.CreateProxy(
                        typeof(IHaveSomeProperties),
                        target,
                        typeof(IGenericOne <>));
            }
            catch (ArgumentException e)
            {
                exception = e;
            }

            // assert
            Assert.IsNotNull(exception);
        }
コード例 #2
0
        public void SupplyingANullArrayOfAdditionalInterfacesThrows()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            Exception exception = null;

            try
            {
                object proxy =
                    interceptor.CreateProxy(
                        typeof(IHaveSomeProperties),
                        target,
                        (Type[])null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }

            // assert
            Assert.IsNotNull(exception);
        }
コード例 #3
0
        [Ignore] // TODO: Known issue - we don't copy attributes yet
        public void ProxyPropertyReflectionReturnsAttributes()
        {
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            IInterceptingProxy   proxy       = interceptor.CreateProxy(typeof(IHaveSomeProperties), target);

            PropertyInfo    nameProp = proxy.GetType().GetProperty("Name");
            SampleAttribute attr     = (SampleAttribute)Attribute.GetCustomAttribute(nameProp, typeof(SampleAttribute));

            Assert.AreEqual("A name", attr.Name);
        }
コード例 #4
0
        public void ReflectingOverProxyTypeReturnsProperties()
        {
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            IInterceptingProxy   proxy       = interceptor.CreateProxy(typeof(IHaveSomeProperties), target);

            List <PropertyInfo> interfaceProperties = new List <PropertyInfo>(typeof(IHaveSomeProperties).GetProperties());
            List <PropertyInfo> proxyProperties     = new List <PropertyInfo>(proxy.GetType().GetProperties());

            Assert.AreEqual(interfaceProperties.Count, proxyProperties.Count);
        }
コード例 #5
0
        public void MultipleProxiesForTheSameInterfaceHaveTheSameType()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object proxy1 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target);
            object proxy2 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target);

            // assert
            Assert.AreSame(proxy1.GetType(), proxy2.GetType());
        }
コード例 #6
0
        public void AdditionalInterfaceIsImplementedExplicitly()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object           proxy            = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceTwo));
            InterfaceMapping interfaceMapping = proxy.GetType().GetInterfaceMap(typeof(IInterfaceTwo));

            // assert
            Assert.IsNull(interfaceMapping.TargetMethods.FirstOrDefault(mi => mi.IsPublic));
        }
コード例 #7
0
        public void CanCreateProxyForMultipleInterfaces()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne));

            // assert
            Assert.IsTrue(proxy is IHaveSomeProperties);
            Assert.IsTrue(proxy is IInterfaceOne);
        }
コード例 #8
0
        public void ProxiesForTheSameInterfaceButDifferentAdditionalInterfacesDoNotHaveTheSameType()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object proxy1 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IBaseInterface));
            object proxy2 = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IBaseInterface2));

            // assert
            Assert.AreNotSame(proxy1.GetType(), proxy2.GetType());
            Assert.IsTrue(proxy1 is IBaseInterface);
            Assert.IsTrue(proxy2 is IBaseInterface2);
        }
コード例 #9
0
        public void CanImplementAdditionalClosedGenericInterface()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object proxy =
                interceptor.CreateProxy(
                    typeof(IHaveSomeProperties),
                    target,
                    typeof(IGenericOne <string>));

            // assert
            Assert.IsTrue(proxy is IHaveSomeProperties);
            Assert.IsTrue(proxy is IGenericOne <string>);
        }
コード例 #10
0
        public void CanSuccessfullyInvokeAnAdditionalInterfaceMethodIfAnInterceptorDoesNotForwardTheCall()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            object proxy   = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne));
            bool   invoked = false;

            ((IInterceptingProxy)proxy).AddInterceptionBehavior(
                new DelegateInterceptionBehavior(
                    (input, getNext) => { invoked = true; return(input.CreateMethodReturn(null)); }));

            // act
            ((IInterfaceOne)proxy).TargetMethod();

            // assert
            Assert.IsTrue(invoked);
        }
コード例 #11
0
        public void CanImplementMultipleAdditionalInterfacesWithCommonAncestors()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();

            // act
            object proxy =
                interceptor.CreateProxy(
                    typeof(IHaveSomeProperties),
                    target,
                    typeof(IDerivedInterface1),
                    typeof(IDerivedInterface2));

            // assert
            Assert.IsTrue(proxy is IHaveSomeProperties);
            Assert.IsTrue(proxy is IDerivedInterface1);
            Assert.IsTrue(proxy is IDerivedInterface2);
        }
コード例 #12
0
        public void InvokingMethodOnAdditionalInterfaceThrowsIfNotHandledByInterceptor()
        {
            // arrange
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            object proxy = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(IInterfaceOne));

            // act
            Exception exception = null;

            try
            {
                ((IInterfaceOne)proxy).TargetMethod();
                Assert.Fail("should have thrown");
            }
            catch (NotImplementedException e)
            {
                exception = e;
            }

            // assert
            Assert.IsNotNull(exception);
        }
コード例 #13
0
        public void CanImplementINotifyPropertyChanged()
        {
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            object proxy          = interceptor.CreateProxy(typeof(IHaveSomeProperties), target, typeof(INotifyPropertyChanged));
            string changeProperty = null;
            PropertyChangedEventHandler handler = (sender, args) => changeProperty = args.PropertyName;

            ((IInterceptingProxy)proxy).AddInterceptionBehavior(new NaiveINotifyPropertyChangedInterceptionBehavior());
            ((INotifyPropertyChanged)proxy).PropertyChanged += handler;

            ((IHaveSomeProperties)proxy).IntProperty = 100;

            Assert.AreEqual(100, ((IHaveSomeProperties)proxy).IntProperty);
            Assert.AreEqual("IntProperty", changeProperty);

            changeProperty = null;
            ((INotifyPropertyChanged)proxy).PropertyChanged -= handler;

            ((IHaveSomeProperties)proxy).IntProperty = 200;

            Assert.AreEqual(200, ((IHaveSomeProperties)proxy).IntProperty);
            Assert.AreEqual(null, changeProperty);
        }