public void Should_Order_Aspects_From_Left_To_Right()
        {
            var aspectLog = new List<string>();
            var serviceLog = new List<string>();

            //Given
            var firstAspect = new FirstAspect();
            firstAspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);

            var secondAspect = new SecondAspect();
            secondAspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);

            var simpleService = new SimpleService();
            simpleService.DoneSomething += (obj, ea) => serviceLog.Add(ea.Message);
            var shouldNotBeCalledAspect = new Mock<IAspect>(MockBehavior.Strict);

            var advisor = new Advisor(JoinPointDefinition.AttributeBasedJoinPointDefinition, new List<IAspect>() { shouldNotBeCalledAspect.Object, secondAspect, shouldNotBeCalledAspect.Object, firstAspect });
            var myService = advisor.GetAdvicedProxy<ISimpleService>(simpleService);

            //Then
            var result = myService.DoMore("muchmore");

            shouldNotBeCalledAspect.VerifyAll();

            Assert.AreEqual(aspectLog.Count, 4);
            Assert.AreEqual("FirstAspectBefore", aspectLog[0]);
            Assert.AreEqual("SecondAspectBefore", aspectLog[1]);
            Assert.AreEqual("SecondAspectAfter", aspectLog[2]);
            Assert.AreEqual("FirstAspectAfter", aspectLog[3]);
        }
        public void Should_Intercept_Only_Attributed_Methods()
        {
            var aspectLog = new List<string>();
            var serviceLog = new List<string>();

            //Given
            var aspect = new SimpleAspect();
            aspect.DoneSomething += (obj, ea) => aspectLog.Add(ea.Message);
            var simpleService = new SimpleService();
            simpleService.DoneSomething += (obj, ea) => serviceLog.Add(ea.Message);
            var shouldNotBeCalledAspect = new Mock<IAspect>(MockBehavior.Strict);

            var advisor = new Advisor(JoinPointDefinition.AttributeBasedJoinPointDefinition, new List<IAspect>() {aspect, shouldNotBeCalledAspect.Object});
            var myService = advisor.GetAdvicedProxy<ISimpleService>(simpleService);

            //Then
            var result = myService.DoSomething("hallo");

            //When
            Assert.AreEqual(aspectLog[0], "aspectCalled");
            Assert.AreEqual(aspectLog[1], "hallo");
            Assert.AreEqual(aspectLog[2], "DoneSomething");

            Assert.AreEqual(result, "executed");

            Assert.AreEqual(serviceLog[0], "hallo");

            shouldNotBeCalledAspect.VerifyAll();
        }
Ejemplo n.º 3
0
        public void Should_Only_Allow_Interfaces_to_proxy2()
        {
            //Given
            var advisor = new Advisor(new List<IAspect>());

            //Then
            MSTestHelper.ShouldThrowException<ArgumentException>(() => advisor.GetAdvicedProxy(typeof(Target), new Target()));
        }
Ejemplo n.º 4
0
        public void Should_Intercept_By_Aspects()
        {
            //Given
            var results = new List<string>();
            var simpleAspect = new SimpleAspect();
            var simpleService = new SimpleService();
            simpleAspect.DoneSomething += (sender, args) => results.Add(args.Message);

            var advisor = new Advisor(new List<IAspect>() {simpleAspect});
            var service = advisor.GetAdvicedProxy<ISimpleService>(simpleService);

            //Then
            var result = service.DoSomething("hllo");

            //When
            Assert.AreEqual("executed", result);
            Assert.AreEqual("hllo", results[0]);
            Assert.AreEqual("donesomething", results[1]);
        }