Пример #1
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);
            }
            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);
            }
Пример #3
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);
            }