Пример #1
0
            public void GivenBuiltProxyWithMixin_WhenMemberIsCalled_ThenMixinMemberIsIntecepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aInterceptor = new InterceptorA();
                var abMixin      = new MixinAB(mixinAProperty: 4);
                var proxy        = subjectBuilder
                                   .ForClass <ClassA>()
                                   .WithoutTraget()
                                   .AddInterceptor(aInterceptor)
                                   .WithMixin(abMixin)
                                   .Build();

                // When
                int aValue = 0;

                proxy.AMethod(() => aValue += 5);
                (proxy as IMixinA).MixinAMethod(() => aValue += 5);
                (proxy as IMixinB).MixinBMethod(() => aValue += 5);

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(3, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(4, (proxy as IMixinA).MixinAProperty);
                Assert.AreEqual(15, aValue);
            }
Пример #2
0
            public void GivenBuiltProxyWithInterceptor_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aInterceptor = new InterceptorA();
                var proxy        = subjectBuilder
                                   .ForClass <ClassA>()
                                   .WithoutTraget()
                                   .AddInterceptor(aInterceptor)
                                   .Build();

                // When
                int aValue = 0;

                proxy.AMethod(() => aValue = 10);
                proxy.AP1 = "a1";
                proxy.AP2 = new ClassB {
                    P1 = 1, P2 = "2"
                };

                var retAP1 = proxy.AP1;
                var retAP2 = proxy.AP2;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(5, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(10, aValue);
                Assert.AreEqual("a1", retAP1);
                Assert.AreEqual(1, retAP2.P1);
                Assert.AreEqual("2", retAP2.P2);
            }
            public void GivenBuiltProxyWithTarget_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aTarget = new ClassA();

                aTarget.AP1 = "val1";
                var aInterceptor = new InterceptorA();
                var proxy        = subjectBuilder
                                   .ForClass <ClassA>()
                                   .WithTarget(aTarget)
                                   .AddInterceptor(aInterceptor)
                                   .Build();

                // When
                int aValue = 0;

                proxy.AMethod(() => aValue = 10);
                var ret1_AP1 = proxy.AP1;

                proxy.AP1 = "val2";
                var ret2_AP1 = proxy.AP1;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(4, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(10, aValue);
                Assert.AreEqual("val1", ret1_AP1);
                Assert.AreEqual("val2", ret2_AP1);
            }
            public void GivenBuiltProxyWithTargetInterface_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var cTarget1 = new ClassC { CP1 = 5 };
                var cTarget2 = new ClassC { CP1 = 15 };
                var interceptor1 = new Inteceptor1();
                IClassC proxy = subjectBuilder
                    .ForInterface<IClassC>()
                    .WithTargetInterface(cTarget1)
                    .AddInterceptor(interceptor1)
                    .Build();

                // When
                interceptor1.ChangeProxyTarget(cTarget2);
                interceptor1.ChangeInvocationTarget(cTarget2);

                int aValue = 0;
                proxy.CMethod(() => aValue = 10);
                var ret1_CP1 = proxy.CP1;
                proxy.CP1 = 17;
                var ret2_CP1 = proxy.CP1;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(4, interceptor1.InterceptReceivedCall);
                Assert.AreEqual(10, aValue);
                Assert.AreEqual(15, ret1_CP1);
                Assert.AreEqual(17, ret2_CP1);
            }
Пример #5
0
            public void GivenBuiltProxyWithTarget_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var cTarget = new ClassC {
                    CP1 = 5
                };
                var     aInterceptor = new InterceptorA();
                var     bInterceptor = new InterceptorB();
                IClassC proxy        = subjectBuilder
                                       .ForInterface <IClassC>()
                                       .WithTarget(cTarget)
                                       .AddInterceptors(aInterceptor, bInterceptor)
                                       .Build();

                // When
                int aValue = 0;

                proxy.CMethod(() => aValue = 10);
                var ret1_CP1 = proxy.CP1;

                proxy.CP1 = 7;
                var ret2_CP1 = proxy.CP1;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(4, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(4, bInterceptor.InterceptReceivedCall);
                Assert.AreEqual(10, aValue);
                Assert.AreEqual(5, ret1_CP1);
                Assert.AreEqual(7, ret2_CP1);
            }
Пример #6
0
            public void GivenBuiltProxyWithoutTarget_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var     testInterceptor1 = new TestInterceptor3(() => 1);
                var     testInterceptor2 = new TestInterceptor4(() => 2);
                IClassC proxy            = subjectBuilder
                                           .ForInterface <IClassC>()
                                           .WithoutTraget()
                                           .AddInterceptors(testInterceptor1, testInterceptor2)
                                           .Build();

                // When
                int aValue = 0;

                proxy.CMethod(() => aValue = 10); // the test method does have target and action is not called
                var retValue = proxy.CP1;         // the first interceptor in the chain determine the return value

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(2, testInterceptor1.InterceptReceivedCall);
                Assert.AreEqual(2, testInterceptor2.InterceptReceivedCall);
                Assert.AreEqual(0, aValue);
                Assert.AreEqual(1, retValue);
            }
            public void GivenBuiltProxyWithNoInterceptFilter_WhenMemberIsCalled_ThenMemberShouldNotBeIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aInterceptor  = new InterceptorA();
                var subAClassType = typeof(ClassSubA);
                var proxy         = subjectBuilder
                                    .ForClass <ClassSubA>()
                                    .WithoutTraget()
                                    .AddInterceptor(aInterceptor)
                                    .ShouldNotIntercept(subAClassType, subAClassType.GetMethod("SubAMethod"))
                                    .ShouldNotIntercept(subAClassType, subAClassType.GetMethod("set_SubAP1"))
                                    .Build();

                // When
                var aValue = 0;

                proxy.AMethod(() => aValue    += 5);
                proxy.SubAMethod(() => aValue += 10);
                proxy.SubAP1 = 20;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(1, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(15, aValue);
                Assert.AreEqual(20, proxy.SubAP1);
            }
            public void GivenBuiltProxyWithTargetAndMixin_WhenMemberIsCalled_ThenMixinMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aTarget = new ClassA();

                aTarget.AP1 = "val1";
                var aInterceptor = new InterceptorA();
                var abMixin      = new MixinAB(mixinAProperty: 2)
                {
                    MixinBProperty = 3
                };
                var proxy = subjectBuilder
                            .ForClass <ClassA>()
                            .WithTarget(aTarget)
                            .AddInterceptor(aInterceptor)
                            .WithMixin(abMixin)
                            .Build();

                // When
                int aValue = 0;

                proxy.AMethod(() => aValue += 5);
                (proxy as IMixinA).MixinAMethod(() => aValue += 5);
                (proxy as IMixinB).MixinBMethod(() => aValue += 5);

                var ret1_AP1            = proxy.AP1;
                var ret1_MixinAProperty = (proxy as IMixinA).MixinAProperty;
                var ret1_MixinBProperty = (proxy as IMixinB).MixinBProperty;

                proxy.AP1 = "val2";
                (proxy as IMixinB).MixinBProperty = 4;
                var ret2_AP1            = proxy.AP1;
                var ret2_MixinBProperty = (proxy as IMixinB).MixinBProperty;


                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(10, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(15, aValue);
                Assert.AreEqual("val1", ret1_AP1);
                Assert.AreEqual("val2", ret2_AP1);
                Assert.AreEqual(2, ret1_MixinAProperty);
                Assert.AreEqual(3, ret1_MixinBProperty);
                Assert.AreEqual(4, ret2_MixinBProperty);
            }
Пример #9
0
            public void GivenBuiltProxyWithTwoMixins_WhenMemberIsCalled_ThenMixinMembersAreIntecepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aInterceptor = new InterceptorA();
                var aMixin       = new MixinA(mixinAProperty: 4);
                var bMixin       = new MixinB();
                var proxy        = subjectBuilder
                                   .ForClass <ClassA>()
                                   .WithoutTraget()
                                   .AddInterceptor(aInterceptor)
                                   .WithMixin(aMixin)
                                   .WithMixin(bMixin)
                                   .Build();

                // When
                int aValue = 0;

                proxy.AMethod(() => aValue += 2); // i1
                proxy.AP1 = "a1";                 // i2
                proxy.AP2 = new ClassB {
                    P1 = 11, P2 = "22"
                };                                                  // i3
                (proxy as IMixinA).MixinAMethod(() => aValue += 4); // i4
                (proxy as IMixinB).MixinBMethod(() => aValue += 8); // i5
                (proxy as IMixinB).MixinBProperty             = 33; // i6

                var retAP1            = proxy.AP1;                  // i7
                var retAP2            = proxy.AP2;                  // i8
                var retAP2_P1         = retAP2.P1;
                var retAP2_P2         = retAP2.P2;
                var retAP2_PowerTwo   = proxy.AP2.PowerTwo(4);             // i9
                var retMixinAProperty = (proxy as IMixinA).MixinAProperty; // i10
                var retMixinBProperty = (proxy as IMixinB).MixinBProperty; // i11

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(11, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual("a1", retAP1);
                Assert.AreEqual(11, retAP2_P1);
                Assert.AreEqual("22", retAP2_P2);
                Assert.AreEqual(16, retAP2_PowerTwo);
                Assert.AreEqual(4, retMixinAProperty);
                Assert.AreEqual(33, retMixinBProperty);
            }
Пример #10
0
            public void GivenBuiltProxyWithInterceptors_WhenMemberIsCalled_ThenMemberIsIntercepted()
            {
                var subjectBuilder = new DynamicProxyBuilder();

                // Given
                var aInterceptor = new InterceptorA();
                var bInterceptor = new InterceptorB();
                var proxy        = subjectBuilder
                                   .ForClass <ClassA>()
                                   .WithoutTraget()
                                   .AddInterceptors(aInterceptor, bInterceptor)
                                   .Build();

                // When
                proxy.AP1 = "a1";
                var retAP1 = proxy.AP1;

                // Then
                Assert.IsTrue(ProxyUtil.IsProxy(proxy), "object is not a proxy");
                Assert.AreEqual(2, aInterceptor.InterceptReceivedCall);
                Assert.AreEqual(2, bInterceptor.InterceptReceivedCall);
                Assert.AreEqual("a1", retAP1);
            }