Esempio n. 1
0
        public void Aspects06Test()
        {
            // Testing interception of methods on classes, implicite interface methods
            // and explicit interface methods.

            TestableAdviceAttribute.ResetCalls();

            Sample2.SampleClass     instance = new Sample2.SampleClass(76);
            Sample2.SampleInterface iface    = instance;

            instance.InterfaceMethodA();
            iface.InterfaceMethodA();
            iface.InterfaceMethodB();
            instance.InterfaceMethodC();

            foreach (ICallContext cc in TestableAdviceAttribute.Calls)
            {
                Console.WriteLine(
                    "\r\n{0}_{1}\r\n  => {2}_{3} ({4})",
                    cc.MethodCall.MethodBase.DeclaringType,
                    cc.MethodCall.MethodBase.Name,
                    cc.Method.DeclaringType,
                    cc.Method.Name,
                    cc.GetProperty("Name")
                    );

                // Assert only advices on class operated:
                Assert.IsTrue(cc.GetProperty("Name").ToString().StartsWith("Class."));

                // Assert
                Assert.AreEqual(instance.Id, ((Sample2.SampleClass)cc.Instance).Id);
            }

            // All method calls except constructor intercepted:
            Assert.AreEqual(4, TestableAdviceAttribute.Calls.Count);

            // MethodCall.MethodBase of explicit interface implementation is interface's:
            Assert.AreEqual(typeof(Sample2.SampleInterface), TestableAdviceAttribute.Calls[2].MethodCall.MethodBase.DeclaringType);
            Assert.AreEqual("InterfaceMethodB", TestableAdviceAttribute.Calls[2].MethodCall.MethodBase.Name);

            // While Method of explicit interface implementation is classe's:
            Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[2].Method.DeclaringType);

            // So ALL Methods are classe's:
            Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[0].Method.DeclaringType);
            Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[1].Method.DeclaringType);
            Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[2].Method.DeclaringType);
            Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[3].Method.DeclaringType);

            // Also check methodnames:
            Assert.AreEqual("InterfaceMethodA", TestableAdviceAttribute.Calls[0].Method.Name);
            Assert.AreEqual("InterfaceMethodA", TestableAdviceAttribute.Calls[1].Method.Name);
            Assert.AreEqual(typeof(Sample2.SampleInterface).ToString() + ".InterfaceMethodB", TestableAdviceAttribute.Calls[2].Method.Name);
            Assert.AreEqual("InterfaceMethodC", TestableAdviceAttribute.Calls[3].Method.Name);
        }
Esempio n. 2
0
		public void Aspects06Test()
		{
			// Testing interception of methods on classes, implicite interface methods
			// and explicit interface methods.

			TestableAdviceAttribute.ResetCalls();

			Sample2.SampleClass instance = new Sample2.SampleClass(76);
			Sample2.SampleInterface iface = instance;

			instance.InterfaceMethodA();
			iface.InterfaceMethodA();
			iface.InterfaceMethodB();
			instance.InterfaceMethodC();

			foreach (ICallContext cc in TestableAdviceAttribute.Calls)
			{
				Console.WriteLine(
					"\r\n{0}_{1}\r\n  => {2}_{3} ({4})",
					cc.MethodCall.MethodBase.DeclaringType,
					cc.MethodCall.MethodBase.Name,
					cc.Method.DeclaringType,
					cc.Method.Name,
					cc.GetProperty("Name")
				);

				// Assert only advices on class operated:
				Assert.IsTrue(cc.GetProperty("Name").ToString().StartsWith("Class."));

				// Assert
				Assert.AreEqual(instance.Id, ((Sample2.SampleClass)cc.Instance).Id);
			}

			// All method calls except constructor intercepted:
			Assert.AreEqual(4, TestableAdviceAttribute.Calls.Count);

			// MethodCall.MethodBase of explicit interface implementation is interface's:
			Assert.AreEqual(typeof(Sample2.SampleInterface), TestableAdviceAttribute.Calls[2].MethodCall.MethodBase.DeclaringType);
			Assert.AreEqual("InterfaceMethodB", TestableAdviceAttribute.Calls[2].MethodCall.MethodBase.Name);

			// While Method of explicit interface implementation is classe's:
			Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[2].Method.DeclaringType);

			// So ALL Methods are classe's:
			Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[0].Method.DeclaringType);
			Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[1].Method.DeclaringType);
			Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[2].Method.DeclaringType);
			Assert.AreEqual(typeof(Sample2.SampleClass), TestableAdviceAttribute.Calls[3].Method.DeclaringType);

			// Also check methodnames:
			Assert.AreEqual("InterfaceMethodA", TestableAdviceAttribute.Calls[0].Method.Name);
			Assert.AreEqual("InterfaceMethodA", TestableAdviceAttribute.Calls[1].Method.Name);
			Assert.AreEqual(typeof(Sample2.SampleInterface).ToString() + ".InterfaceMethodB", TestableAdviceAttribute.Calls[2].Method.Name);
			Assert.AreEqual("InterfaceMethodC", TestableAdviceAttribute.Calls[3].Method.Name);
		}