Inheritance: Castle.Core.Interceptor.StandardInterceptor
		public void InterfaceInheritance()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			IService service = (IExtendedService)
			                   generator.CreateInterfaceProxyWithTarget(
			                   	typeof(IExtendedService), new ServiceImpl(), logger);

			Assert.AreEqual(3, service.Sum(1, 2));

			Assert.AreEqual("Sum ", logger.LogContents);
		}
Ejemplo n.º 2
0
		public void ProtectedMethods()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			NonPublicMethodsClass proxy = (NonPublicMethodsClass)
										  generator.CreateClassProxy(typeof(NonPublicMethodsClass), logger);

			proxy.DoSomething();
			Assert.AreEqual(2, logger.Invocations.Count);
			Assert.AreEqual("DoSomething", logger.Invocations[0]);
			Assert.AreEqual("DoOtherThing", logger.Invocations[1]);
		}
		public void BasicInterfaceProxyWithValidTarget2()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			IService2 service = (IService2)
			                    generator.CreateInterfaceProxyWithTarget(
			                    	typeof(IService2), new Service2(), logger);

			service.DoOperation2();

			Assert.AreEqual("DoOperation2 ", logger.LogContents);
		}
		public void ExplicitInterfaceMethods_AreIgnored_OnClassProxy()
		{
			LogInvocationInterceptor interceptor = new LogInvocationInterceptor();
			ClassWithExplicitInterface instance = generator.CreateClassProxy<ClassWithExplicitInterface>(interceptor);

			instance.DoVirtual();
			int result = ((ISimpleInterface) instance).Do ();
			instance.DoVirtual();

			Assert.AreEqual(2, interceptor.Invocations.Count);
			Assert.AreEqual("DoVirtual", interceptor.Invocations[0]);
			Assert.AreEqual("DoVirtual", interceptor.Invocations[1]);

			Assert.AreEqual(5, result);
		}
Ejemplo n.º 5
0
		public void HookIsUsedForConcreteClassProxy()
		{
			var logger = new LogInvocationInterceptor();
			var hook = new LogHook(typeof(ServiceClass), true);

			var options = new ProxyGenerationOptions(hook);

			var proxy = (ServiceClass)generator.CreateClassProxy(typeof(ServiceClass), options, logger);

			Assert.IsTrue(hook.Completed);
			Assert.AreEqual(13, hook.AskedMembers.Count, "Asked members");
			Assert.AreEqual(2, hook.NonVirtualMembers.Count, "Non-virtual members");

			proxy.Sum(1, 2);
			Assert.IsFalse(proxy.Valid);

			Assert.AreEqual("get_Valid ", logger.LogContents);
		}
		public void HookIsUsedForConcreteClassProxy()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();
			LogHook hook = new LogHook(typeof (ServiceClass), true);

			ProxyGenerationOptions options = new ProxyGenerationOptions(hook);

			ServiceClass proxy = (ServiceClass) generator.CreateClassProxy(typeof (ServiceClass), options, logger);

			Assert.IsTrue(hook.Completed);
			Assert.AreEqual(10, hook.AskedMembers.Count, "Asked members");

			Assert.AreEqual(2, hook.NonVirtualMembers.Count, "Non-virtual members");// <-- this would fail due to superfulous method check

			proxy.Sum(1, 2);
			Assert.IsFalse(proxy.Valid);

			Assert.AreEqual("get_Valid ", logger.LogContents);
		}
		public void ExplicitInterface_AsAdditionalInterfaceToProxy_OnClassProxy_WithoutBaseCalls()
		{
			LogInvocationInterceptor interceptor = new LogInvocationInterceptor ();
			interceptor.Proceed = false;

			ClassWithExplicitInterface instance = (ClassWithExplicitInterface) generator.CreateClassProxy (typeof (ClassWithExplicitInterface),
				new[] { typeof (ISimpleInterface) }, interceptor);

			instance.DoVirtual ();
			int result = ((ISimpleInterface) instance).Do ();
			instance.DoVirtual ();

			Assert.AreEqual (3, interceptor.Invocations.Count);
			Assert.AreEqual ("DoVirtual", interceptor.Invocations[0]);
			Assert.AreEqual ("Do", interceptor.Invocations[1]);
			Assert.AreEqual ("DoVirtual", interceptor.Invocations[2]);

			Assert.AreEqual (0, result); // indicates that original method was not called
		}
Ejemplo n.º 8
0
		public void HookDetectsNonVirtualAlthoughInterfaceImplementation()
		{
			var logger = new LogInvocationInterceptor();
			var hook = new LogHook(typeof(ServiceImpl), true);

			var options = new ProxyGenerationOptions(hook);

			// we are creating a class proxy although the creation of an interface proxy is possible too...
			// since the members of our implementation are not explicitly marked as virtual, the runtime
			// marks them as virtual but final --> not good for us, but intended by .net :-(
			//
			// see: https://msdn.microsoft.com/library/system.reflection.methodbase.isvirtual
			//
			// thus, a non virtual notification for this particular situation is appropriate
			generator.CreateClassProxy(typeof(ServiceImpl), options, logger);

			Assert.IsTrue(hook.Completed);
			Assert.AreEqual(3, hook.AskedMembers.Count);
			Assert.AreEqual(11, hook.NonVirtualMembers.Count);
		}
		public void HookIsUsedForInterfaceProxy()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();
			LogHook hook = new LogHook(typeof (IService), false);

			ProxyGenerationOptions options = new ProxyGenerationOptions(hook);

			IService proxy = (IService)
			                 generator.CreateInterfaceProxyWithTarget(
			                 	typeof (IService), new ServiceImpl(), options, logger);

			Assert.IsTrue(hook.Completed);
			Assert.AreEqual(10, hook.AskedMembers.Count);
			Assert.AreEqual(0, hook.NonVirtualMembers.Count);

			Assert.AreEqual(3, proxy.Sum(1, 2));
			Assert.IsFalse(proxy.Valid);

			Assert.AreEqual("Sum get_Valid ", logger.LogContents);
		}
Ejemplo n.º 10
0
		public void ClassWithDifferentAccessLevelOnProperties()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			object proxy = generator.CreateClassProxy(typeof(DiffAccessLevelOnProperties), logger);

			Assert.IsNotNull(proxy);
			Assert.IsInstanceOfType(typeof(DiffAccessLevelOnProperties), proxy);

			DiffAccessLevelOnProperties type = (DiffAccessLevelOnProperties)proxy;

			type.SetProperties();

			Assert.AreEqual("10 11 12 13 name", type.ToString());
		}
		public void ChangingInvocationTargetSucceeds()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			IService service = (IService)
			                   generator.CreateInterfaceProxyWithTargetInterface(
			                   	typeof(IService), new AlwaysThrowsServiceImpl(), new ChangeTargetInterceptor(new ServiceImpl()),
			                   	logger);

			Assert.AreEqual(20, service.Sum(10, 10));
		}
		public void InterfaceTargetTypeProducesInvocationsThatCanChangeTarget()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();
			AssertCanChangeTargetInterceptor invocationChecker = new AssertCanChangeTargetInterceptor();

			IService2 service = (IService2)
			                    generator.CreateInterfaceProxyWithTargetInterface(
			                    	typeof(IService2), new Service2(), invocationChecker, logger);

			service.DoOperation2();

			Assert.AreEqual("DoOperation2 ", logger.LogContents);
		}
Ejemplo n.º 13
0
		public void ProxyForClassWithIndexer()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			object proxy = generator.CreateClassProxy(typeof(ClassWithIndexer), logger);

			Assert.IsNotNull(proxy);
			Assert.IsInstanceOfType(typeof(ClassWithIndexer), proxy);

			ClassWithIndexer type = (ClassWithIndexer)proxy;

			type["name"] = 10;
			Assert.AreEqual(10, type["name"]);

			Assert.AreEqual("set_Item get_Item ", logger.LogContents);
		}
Ejemplo n.º 14
0
		public void ProxyTypeWithMultiDimentionalArrayAsParameters()
		{
			LogInvocationInterceptor log = new LogInvocationInterceptor();

			ClassWithMultiDimentionalArray proxy =
				generator.CreateClassProxy<ClassWithMultiDimentionalArray>(log);

			int[,] x = new int[1,2];

			proxy.Do(new int[] {1});
			proxy.Do2(x);
			proxy.Do3(new string[] {"1", "2"});

			Assert.AreEqual("Do Do2 Do3 ", log.LogContents);
		}
Ejemplo n.º 15
0
		public void ProxyForCharReturnType()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();
			object proxy = generator.CreateClassProxy(typeof(ClassWithCharRetType), logger);
			Assert.IsNotNull(proxy);
			ClassWithCharRetType classProxy = (ClassWithCharRetType)proxy;
			Assert.AreEqual('c', classProxy.DoSomething());
		}
		public void Indexer()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			InterfaceWithIndexer service = (InterfaceWithIndexer)
			                               generator.CreateInterfaceProxyWithTarget(
			                               	typeof(InterfaceWithIndexer), new ClassWithIndexer(), logger);

			Assert.AreEqual(1, service[1]);

			Assert.AreEqual("get_Item ", logger.LogContents);
		}
		public void ExplicitInterface_properties_should_be_public_class()
		{
			var interceptor = new LogInvocationInterceptor { Proceed = false };

			var proxy = generator.CreateClassProxy(typeof(ExplicitInterfaceWithPropertyImplementation), new[] { typeof(ISimpleInterfaceWithProperty) },
			                                       interceptor);
			Assert.IsNotEmpty(proxy.GetType().GetProperties());
		}
		public void ExplicitInterface_properties_should_be_public_interface()
		{
			var interceptor = new LogInvocationInterceptor { Proceed = false };

			var proxy = generator.CreateInterfaceProxyWithoutTarget(typeof(ISimpleInterfaceWithProperty), interceptor);
			Assert.IsNotEmpty(proxy.GetType().GetProperties());
		}
		public override void Init()
		{
			base.Init();
			logger = new LogInvocationInterceptor();
		}
Ejemplo n.º 20
0
		public void ClassWithInheritance()
		{
			LogInvocationInterceptor logger = new LogInvocationInterceptor();

			object proxy = generator.CreateClassProxy(typeof(ExtendedServiceClass), logger);

			Assert.IsNotNull(proxy);

			ExtendedServiceClass extended = (ExtendedServiceClass)proxy;

			extended.Sum2(1, 2);
			extended.Sum(1, 2);

			Assert.AreEqual("Sum2 Sum ", logger.LogContents);
		}