예제 #1
0
        public void AppendCallInformation()
        {
            var invocationStub = Substitute.For <IInvocation>();

            var methodInfo =
                MethodInfoUtil.GetMethodInfo <DummyObjectWithGenerics <int> >(
                    obj => obj.DoSomething(1, "value"));
            var args = new object[]
            {
                1,
                "value"
            };

            logUtil.AppendCallInformation(stringBuilder, methodInfo, args);

            var actual = stringBuilder.ToString();

            Assert.That(actual, Does.Contain(
                            "YetiCommon.Tests.Logging.DummyObjectWithGenerics`1[System.Int32]"));
            Assert.That(actual, Does.Contain(
                            "Void DoSomething[Int32,String](Int32, System.String)"));
            Assert.That(actual, Does.Contain("Args [ToString]:"));
            Assert.That(actual, Does.Contain("param1=1"));
            Assert.That(actual, Does.Contain("param2=value"));
            Assert.That(actual, Does.Contain("Args [Json]:"));
            Assert.That(actual, Does.Contain(JoinLines(
                                                 @"(",
                                                 @"{",
                                                 @"  ""param1"": 1,",
                                                 @"  ""param2"": ""value""",
                                                 @"}",
                                                 @")",
                                                 @"")));
        }
예제 #2
0
        public void SetUp()
        {
            methodInfo     = MethodInfoUtil.GetMethodInfo <DummyObject>(x => x.SetValue(1));
            mockInvocation = Substitute.For <IInvocation>();
            mockInvocation.MethodInvocationTarget.Returns(methodInfo);

            mockRecorder            = Substitute.For <IExceptionRecorder>();
            exceptionRecorderAspect = new ExceptionRecorderAspect(mockRecorder);
        }
        public void ShouldForwardNullReturnValueOnInterfaceFunction()
        {
            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo <DummyObject.Factory>(factory => factory.Create()));
            mockInvocation.ReturnValue.Returns(null);

            classUnderTest.Intercept(mockInvocation);
            Assert.Null(mockInvocation.ReturnValue);
        }
예제 #4
0
        public void ShouldInterceptVirtualCreateMethodWithInterfaceReturnType()
        {
            var sut = new CreateFunctionProxyHook();

            Assert.True(sut.ShouldInterceptMethod(
                            typeof(DummyObject.Factory),
                            MethodInfoUtil.GetMethodInfo <DummyObject.Factory>(
                                factory => factory.Create()
                                )));
        }
예제 #5
0
        public void ValidUnconventionalFactoryShouldIntercept()
        {
            var sut = new CreateFunctionProxyHook();

            Assert.True(sut.ShouldInterceptMethod(
                            typeof(UnconventionalFactory),
                            MethodInfoUtil.GetMethodInfo <UnconventionalFactory>(
                                factory => factory.ValidCreate()
                                )));
        }
예제 #6
0
        public void SetUp()
        {
            methodInfo     = MethodInfoUtil.GetMethodInfo <DummyObject>(x => x.SetValue(1));
            mockInvocation = Substitute.For <IInvocation>();
            mockInvocation.MethodInvocationTarget.Returns(methodInfo);

            fakeTimeSource = new MonotonicTimeSource();
            mockMethodInvocationRecorder = Substitute.For <IMethodInvocationRecorder>();
            metricsCollectionAspect      = new MetricsCollectionAspect(
                mockMethodInvocationRecorder, fakeTimeSource);
        }
예제 #7
0
        public void ShouldSendEventInfoToLogger()
        {
            var mockInvocation = Substitute.For <IInvocation>();

            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo <DummyObject>(x => x.SetValue(1)));
            mockInvocation.TargetType.Returns(typeof(DummyObject));

            _classUnderTest.Intercept(mockInvocation);
            _mockTraceLogger.Received().TraceEvent("SetValue", EventType.Sync, typeof(DummyObject),
                                                   Arg.Is <long>(1), Arg.Is <long>(1),
                                                   Arg.Any <int>());
        }
        public void ShouldThrowInvalidOperationWhenProxyingVoidFunction()
        {
            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo <DummyObject.FactoryWithVoidCreateMethod>(
                    factory => factory.CreateNothing()));

            var exceptionThrown = Assert.Throws(typeof(InvalidOperationException), () =>
            {
                classUnderTest.Intercept(mockInvocation);
            });

            Assert.That(exceptionThrown.Message, Does.Contain("System.Void"));
        }
        public void ShouldThrowInvalidOperationWhenProxyingPrimativeReturnTypeFunction()
        {
            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo <DummyObject.FactoryWithCreateReturnType <int> >(
                    factory => factory.CreateWithGivenReturnType()));

            var exceptionThrown = Assert.Throws(typeof(InvalidOperationException), () =>
            {
                classUnderTest.Intercept(mockInvocation);
            });

            Assert.That(exceptionThrown.Message, Does.Contain("System.Int32"));
        }
예제 #10
0
        public void InvalidUnconventionalFactoryShouldFail()
        {
            var sut             = new CreateFunctionProxyHook();
            var exceptionThrown = Assert.Throws <ArgumentException>(() =>
                                                                    sut.ShouldInterceptMethod(
                                                                        typeof(UnconventionalFactory),
                                                                        MethodInfoUtil.GetMethodInfo <UnconventionalFactory>(
                                                                            factory => factory.InvalidCreate()
                                                                            )));

            Assert.That(exceptionThrown.Message,
                        Does.Contain(nameof(CreateFunctionProxyHook.ShouldInterceptAttribute)));
        }
예제 #11
0
        public void ShouldThrowArgumentExceptionForPrimitiveCreateMethod()
        {
            var exceptionThrown = Assert.Throws <ArgumentException>(() =>
            {
                var sut = new CreateFunctionProxyHook();
                sut.ShouldInterceptMethod(typeof(DummyObject.FactoryWithCreateReturnType <int>),
                                          MethodInfoUtil.GetMethodInfo <DummyObject.FactoryWithCreateReturnType <int> >(
                                              factory => factory.CreateWithGivenReturnType()
                                              ));
            });

            Assert.That(exceptionThrown.Message, Does.Contain("System.Int32"));
        }
예제 #12
0
        public void ShouldThrowArgumentExceptionForClassCreateMethod()
        {
            var exceptionThrown = Assert.Throws <ArgumentException>(() =>
            {
                var sut = new CreateFunctionProxyHook();
                sut.ShouldInterceptMethod(typeof(DummyObject.FactoryWithConcreteCreate),
                                          MethodInfoUtil.GetMethodInfo <DummyObject.FactoryWithConcreteCreate>(
                                              factory => factory.CreateConcrete()
                                              ));
            });

            Assert.That(exceptionThrown.Message, Does.Contain("DummyObject"));
        }
예제 #13
0
        public void ShouldThrowArgumentExceptionForVoidCreateMethod()
        {
            var exceptionThrown = Assert.Throws <ArgumentException>(() =>
            {
                var sut = new CreateFunctionProxyHook();
                sut.ShouldInterceptMethod(typeof(DummyObject.FactoryWithVoidCreateMethod),
                                          MethodInfoUtil.GetMethodInfo <DummyObject.FactoryWithVoidCreateMethod>(
                                              factory => factory.CreateNothing()
                                              ));
            });

            Assert.That(exceptionThrown.Message, Does.Contain("System.Void"));
        }
        public void TargetSelfIsSetToProxy()
        {
            var factory   = new Factory();
            var targetObj = factory.Create();

            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo(() => factory.Create()));
            mockInvocation.ReturnValue.Returns(targetObj);

            Assert.AreEqual(targetObj, mockInvocation.ReturnValue);
            classUnderTest.Intercept(mockInvocation);
            Assert.AreNotEqual(targetObj, mockInvocation.ReturnValue);
            Assert.AreSame(((TargetObjImpl)targetObj).Self, mockInvocation.ReturnValue);
        }
        public void ShouldReturnInterfaceProxy()
        {
            var          dummyObjectFactory = new DummyObject.Factory();
            IDummyObject dummyObject        = dummyObjectFactory.Create();

            mockInvocation.Method.Returns(
                MethodInfoUtil.GetMethodInfo(() => dummyObjectFactory.Create()));
            mockInvocation.ReturnValue.Returns(dummyObject);

            Assert.AreEqual(dummyObject, mockInvocation.ReturnValue);
            classUnderTest.Intercept(mockInvocation);
            Assert.AreNotEqual(dummyObject, mockInvocation.ReturnValue);

            IDummyObject proxy = mockInvocation.ReturnValue as IDummyObject;

            Assert.NotNull(proxy);
            Assert.AreEqual(0, aspect.CallCount);
            proxy.SetValue(0);
            Assert.AreEqual(1, aspect.CallCount);
        }