Ejemplo n.º 1
0
        public void SimpleMixin_ClassProxy()
        {
            ProxyGenerationOptions options        = new ProxyGenerationOptions();
            SimpleMixin            mixin_instance = new SimpleMixin();

            options.AddMixinInstance(mixin_instance);

            AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();

            object proxy = generator.CreateClassProxy(typeof(SimpleClass), options, interceptor);

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            Assert.IsFalse(interceptor.Invoked);

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.IsNotNull(mixin);
            Assert.AreEqual(1, mixin.DoSomething());

            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin_instance, interceptor.mixin);
        }
Ejemplo n.º 2
0
        public void MixinForInterfaces()
        {
            ProxyGenerationOptions proxyGenerationOptions = new ProxyGenerationOptions();

            SimpleMixin mixin_instance = new SimpleMixin();

            proxyGenerationOptions.AddMixinInstance(mixin_instance);

            AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();

            MyInterfaceImpl target = new MyInterfaceImpl();

            object proxy = generator.CreateInterfaceProxyWithTarget(
                typeof(IMyInterface), target, proxyGenerationOptions, interceptor);

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(IMyInterface).IsAssignableFrom(proxy.GetType()));

            Assert.IsFalse(interceptor.Invoked);

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.IsNotNull(mixin);
            Assert.AreEqual(1, mixin.DoSomething());

            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin_instance, interceptor.mixin);
        }
Ejemplo n.º 3
0
        public void XmlSerialization()
        {
            ProxyObjectReference.ResetScope();

            GeneratorContext context = new GeneratorContext();
            SimpleMixin      mixin1  = new SimpleMixin();
            OtherMixin       mixin2  = new OtherMixin();

            context.AddMixinInstance(mixin1);
            context.AddMixinInstance(mixin2);

            object proxy = generator.CreateCustomClassProxy(
                typeof(SimpleClass), new StandardInterceptor(), context);

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(SimpleClass));
            MemoryStream stream = new MemoryStream();

            serializer.Serialize(stream, proxy);
            stream.Position = 0;
            SimpleClass otherProxy = (SimpleClass)serializer.Deserialize(stream);

            ISimpleMixin mixin = otherProxy as ISimpleMixin;

            Assert.AreEqual(1, mixin.DoSomething());

            IOtherMixin other = otherProxy as IOtherMixin;

            Assert.AreEqual(3, other.Sum(1, 2));
        }
Ejemplo n.º 4
0
        public void SimpleMixin()
        {
            GeneratorContext context        = new GeneratorContext();
            SimpleMixin      mixin_instance = new SimpleMixin();

            context.AddMixinInstance(mixin_instance);

            AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();

            object proxy = _generator.CreateCustomClassProxy(
                typeof(SimpleClass), interceptor, context);

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            Assert.IsFalse(interceptor.Invoked);

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.IsNotNull(mixin);
            Assert.AreEqual(1, mixin.DoSomething());

            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin_instance, interceptor.mixin);
        }
        public void SimpleCaseWithMixin()
        {
            String contents =
                "import Castle.Facilities.AspectSharp.Tests.Components in Castle.Facilities.AspectSharp.Tests " +
                "import Castle.Facilities.AspectSharp.Tests.Interceptors in Castle.Facilities.AspectSharp.Tests " +
                " " +
                " aspect MyAspect for SimpleService " +
                "   include SimpleMixin" +
                "   " +
                "   pointcut method|property(*)" +
                "     advice(LoggerInterceptor)" +
                "   end" +
                "   " +
                " end ";

            MutableConfiguration config = new MutableConfiguration("facility", contents);

            DefaultConfigurationStore store = new DefaultConfigurationStore();

            store.AddFacilityConfiguration("aop", config);

            WindsorContainer container = new WindsorContainer(store);

            container.AddFacility("aop", new AspectSharpFacility());
            container.AddComponent("comp1", typeof(SimpleService));

            SimpleService service = container[typeof(SimpleService)] as SimpleService;

            service.DoSomething();
            service.DoSomethingElse();

            Assert.AreEqual("Enter DoSomething\r\nEnter DoSomethingElse\r\n",
                            LoggerInterceptor.Messages.ToString());

            LoggerInterceptor.Messages.Length = 0;

            ISimpleMixin mixin = service as ISimpleMixin;

            Assert.IsNotNull(mixin);

            mixin.DoStuff();

            Assert.AreEqual("Enter DoStuff\r\n",
                            LoggerInterceptor.Messages.ToString());
        }
Ejemplo n.º 6
0
        public void TwoMixins()
        {
            ProxyGenerationOptions proxyGenerationOptions = new ProxyGenerationOptions();

            SimpleMixin mixin1 = new SimpleMixin();
            OtherMixin  mixin2 = new OtherMixin();

            proxyGenerationOptions.AddMixinInstance(mixin1);
            proxyGenerationOptions.AddMixinInstance(mixin2);

            AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor();

            object proxy = generator.CreateClassProxy(
                typeof(SimpleClass),
                proxyGenerationOptions,
                interceptor
                );

            Assert.IsFalse(interceptor.Invoked);

            Assert.IsNotNull(proxy);
            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.IsNotNull(mixin);
            Assert.AreEqual(1, mixin.DoSomething());

            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin1, interceptor.mixin);

            IOtherMixin other = proxy as IOtherMixin;

            Assert.IsNotNull(other);
            Assert.AreEqual(3, other.Sum(1, 2));
            Assert.IsTrue(interceptor.Invoked);
            Assert.AreSame(proxy, interceptor.proxy);
            Assert.AreSame(mixin2, interceptor.mixin);
        }
Ejemplo n.º 7
0
        public void MixinSerialization()
        {
            ProxyObjectReference.ResetScope();

            GeneratorContext context = new GeneratorContext();
            SimpleMixin      mixin1  = new SimpleMixin();
            OtherMixin       mixin2  = new OtherMixin();

            context.AddMixinInstance(mixin1);
            context.AddMixinInstance(mixin2);

            object proxy = generator.CreateCustomClassProxy(
                typeof(SimpleClass), new StandardInterceptor(), context);

            Assert.IsTrue(typeof(SimpleClass).IsAssignableFrom(proxy.GetType()));

            (proxy as SimpleClass).DoSome();

            ISimpleMixin mixin = proxy as ISimpleMixin;

            Assert.AreEqual(1, mixin.DoSomething());

            IOtherMixin other = proxy as IOtherMixin;

            Assert.AreEqual(3, other.Sum(1, 2));

            SimpleClass otherProxy = (SimpleClass)SerializeAndDeserialize(proxy);

            otherProxy.DoSome();

            mixin = otherProxy as ISimpleMixin;
            Assert.AreEqual(1, mixin.DoSomething());

            other = otherProxy as IOtherMixin;
            Assert.AreEqual(3, other.Sum(1, 2));
        }