Beispiel #1
0
		public void VerifyProxyForClassWithAdditionalInterface()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(PublicInterfaceTestClass).FullName,
				typeof(PublicInterfaceTestClass),
				// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
				// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
				// having an additional interface in the interface list, instead of just having INHibernateProxy.
				// (Quite a loosy semantic...)
				new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublic)},
				null, null, null);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<IPublic>());
					Assert.That(proxy, Is.Not.InstanceOf<PublicInterfaceTestClass>());
#if NETFX
				});
#endif
		}
Beispiel #2
0
		public void VerifyFieldInterceptorProxyWithAdditionalInterface()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(PublicInterfaceTestClass).FullName,
				typeof(PublicInterfaceTestClass),
				// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
				// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
				// having an additional interface in the interface list, instead of just having INHibernateProxy.
				// (Quite a loosy semantic...)
				// The field interceptor proxy ignores this setting, as it does not delegate its implementation
				// to an instance of the persistentClass, and so cannot implement interface methods if it does not
				// inherit the persitentClass.
				new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublic)},
				null, null, null);
#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var fieldProxy = factory.GetFieldInterceptionProxy(new PublicInterfaceTestClass());
					Assert.That(fieldProxy, Is.InstanceOf<PublicInterfaceTestClass>());
#if NETFX
				});
#endif
		}
Beispiel #3
0
		public void InitializedProxyStaysInitializedAfterDeserialization()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
			var proxy = factory.GetProxy(2, null);
			Assert.That(proxy, Is.Not.Null, "proxy");
			Assert.That(NHibernateUtil.IsInitialized(proxy), Is.False, "proxy already initialized after creation");
			Assert.That(proxy.HibernateLazyInitializer, Is.Not.Null, "HibernateLazyInitializer");

			var impl = new SimpleTestClass { Id = 2 };
			proxy.HibernateLazyInitializer.SetImplementation(impl);
			Assert.That(NHibernateUtil.IsInitialized(proxy), Is.True, "proxy not initialized after setting implementation");

			var serializer = GetFormatter();
			object deserialized;
			using (var memoryStream = new MemoryStream())
			{
				serializer.Serialize(memoryStream, proxy);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				deserialized = serializer.Deserialize(memoryStream);
			}
			Assert.That(deserialized, Is.Not.Null, "deserialized");
			Assert.That(deserialized, Is.InstanceOf<INHibernateProxy>());
			Assert.That(NHibernateUtil.IsInitialized(deserialized), Is.True, "proxy no more initialized after deserialization");
			Assert.That(deserialized, Is.InstanceOf<SimpleTestClass>());
			Assert.That(((SimpleTestClass) deserialized).Id, Is.EqualTo(2));
		}
		public void VerifyProxyForInterfaceWithEqualsAndGetHashCode()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(IWithEqualsAndGetHashCode).FullName,
				typeof(object),
				new HashSet<System.Type> {typeof(IWithEqualsAndGetHashCode), typeof(INHibernateProxy)},
				null, null, null, false);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<IWithEqualsAndGetHashCode>());
					var proxyType = proxy.GetType();
					var proxyMap = proxyType.GetInterfaceMap(typeof(IWithEqualsAndGetHashCode));
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.Property("Name").EqualTo("Equals").And.Property("IsPublic").EqualTo(true),
						"Equals is not implicitly implemented");
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.Property("Name").EqualTo("GetHashCode").And.Property("IsPublic").EqualTo(true),
						"GetHashCode is not implicitly implemented");
#if NETFX
				});
#endif
		}
		public void VerifyProxyForClassWithInterface()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(PublicInterfaceTestClass).FullName,
				typeof(PublicInterfaceTestClass),
				new HashSet<System.Type> {typeof(INHibernateProxy)},
				null, null, null, true);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<IPublic>());
					Assert.That(proxy, Is.InstanceOf<PublicInterfaceTestClass>());
					var proxyType = proxy.GetType();
					var proxyMap = proxyType.GetInterfaceMap(typeof(IPublic));
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.EqualTo(proxyType.GetProperty(nameof(PublicInterfaceTestClass.Name)).GetMethod),
						"Name getter does not implement IPublic");
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.EqualTo(proxyType.GetProperty(nameof(PublicInterfaceTestClass.Name)).SetMethod),
						"Name setter does not implement IPublic");
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.EqualTo(proxyType.GetProperty(nameof(PublicInterfaceTestClass.Id)).GetMethod),
						"Id setter does not implement IPublic");
					Assert.That(
						proxyMap.TargetMethods,
						Has.One.EqualTo(proxyType.GetProperty(nameof(PublicInterfaceTestClass.Id)).SetMethod),
						"Id setter does not implement IPublic");

					// Check interface and implicit implementations do both call the delegated state
					var state = new PublicInterfaceTestClass { Id = 5, Name = "State" };
					proxy.HibernateLazyInitializer.SetImplementation(state);
					var ent = (PublicInterfaceTestClass) proxy;
					IPublic pub = ent;
					Assert.That(pub.Id, Is.EqualTo(5), "IPublic.Id");
					Assert.That(ent.Id, Is.EqualTo(5), "entity.Id");
					Assert.That(pub.Name, Is.EqualTo("State"), "IPublic.Name");
					Assert.That(ent.Name, Is.EqualTo("State"), "entity.Name");
					ent.Id = 10;
					pub.Name = "Test";
					Assert.That(pub.Id, Is.EqualTo(10), "IPublic.Id");
					Assert.That(state.Id, Is.EqualTo(10), "state.Id");
					Assert.That(ent.Name, Is.EqualTo("Test"), "entity.Name");
					Assert.That(state.Name, Is.EqualTo("Test"), "state.Name");
#if NETFX
				});
#endif
		}
Beispiel #6
0
        public void CanCreateProxyForClassWithInternalInterface()
        {
            var factory = new StaticProxyFactory();

            factory.PostInstantiate(typeof(TestClass).FullName, typeof(TestClass), new HashSet <System.Type> {
                typeof(INHibernateProxy)
            }, null, null, null);
            var proxy = factory.GetProxy(1, null);

            Assert.That(proxy, Is.Not.Null);
        }
Beispiel #7
0
		public void VerifyFieldInterceptorProxyWithISerializableEntity()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(CustomSerializationClass).FullName, typeof(CustomSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var fieldProxy = factory.GetFieldInterceptionProxy(new CustomSerializationClass());
					Assert.That(fieldProxy, Is.InstanceOf<CustomSerializationClass>());
#if NETFX
				});
#endif
		}
		public void VerifyFieldInterceptorProxy()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var fieldProxy = factory.GetFieldInterceptionProxy();
					Assert.That(fieldProxy, Is.InstanceOf<InternalInterfaceTestClass>());
#if NETFX
				});
#endif
		}
Beispiel #9
0
		public void VerifyProxyForClassWithInternalInterface()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(InternalInterfaceTestClass).FullName, typeof(InternalInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
#if NETFX
				});
#endif
		}
Beispiel #10
0
		public void CanSerializeFieldInterceptorProxyWithExplicitISerializableEntity()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(CustomExplicitSerializationClass).FullName, typeof(CustomExplicitSerializationClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
			var proxy = (CustomExplicitSerializationClass) factory.GetFieldInterceptionProxy(new CustomExplicitSerializationClass());
			proxy.Id = 2;

			var serializer = GetFormatter();
			using (var memoryStream = new MemoryStream())
			{
				serializer.Serialize(memoryStream, proxy);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				proxy = (CustomExplicitSerializationClass) serializer.Deserialize(memoryStream);
				Assert.That(proxy.Id, Is.EqualTo(2));
			}
		}
		public void CanSerializeFieldInterceptorProxy()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(PublicInterfaceTestClass).FullName, typeof(PublicInterfaceTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null, true);
			var proxy = (PublicInterfaceTestClass) factory.GetFieldInterceptionProxy();
			proxy.Id = 1;

			var serializer = GetFormatter();
			using (var memoryStream = new MemoryStream())
			{
				serializer.Serialize(memoryStream, proxy);
				memoryStream.Seek(0L, SeekOrigin.Begin);
				proxy = (PublicInterfaceTestClass) serializer.Deserialize(memoryStream);
				Assert.That(proxy.Id, Is.EqualTo(1));
			}
		}
		public void VerifyProxyForRefOutClass()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(RefOutTestClass).FullName,
				typeof(RefOutTestClass),
				new HashSet<System.Type> { typeof(INHibernateProxy) },
				null,
				null,
				null,
				true);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);

					var state = new RefOutTestClass();
					proxy.HibernateLazyInitializer.SetImplementation(state);

					var entity = (RefOutTestClass) proxy;
					entity.Method1(out var x);
					Assert.That(x, Is.EqualTo(3));

					entity.Method2(ref x);
					Assert.That(x, Is.EqualTo(4));

					entity.Method3(out var y);
					Assert.That(y, Is.EqualTo(4));

					entity.Method4(ref y);
					Assert.That(y, Is.EqualTo(5));

					var dictionary = new Dictionary<string, string>();
					var param = dictionary;
					entity.Method(ref param);
					Assert.That(param, Is.Not.SameAs(dictionary));
#if NETFX
				});
#endif
		}
		public void VerifyProxyForClassWithExplicitInterface()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(PublicExplicitInterfaceTestClass).FullName,
				typeof(PublicExplicitInterfaceTestClass),
				new HashSet<System.Type> {typeof(INHibernateProxy)},
				null, null, null, true);
#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<IPublic>());
					Assert.That(proxy, Is.InstanceOf<PublicExplicitInterfaceTestClass>());
					var proxyType = proxy.GetType();
					Assert.That(proxyType.GetMethod($"get_{nameof(IPublic.Name)}"), Is.Null, "get Name is implicitly implemented");
					Assert.That(proxyType.GetMethod($"set_{nameof(IPublic.Name)}"), Is.Null, "set Name is implicitly implemented");
					Assert.That(proxyType.GetMethod($"get_{nameof(IPublic.Id)}"), Is.Null, "get Id is implicitly implemented");
					Assert.That(proxyType.GetMethod($"set_{nameof(IPublic.Id)}"), Is.Null, "set Id is implicitly implemented");

					// Check explicit implementation
					IPublic state = new PublicExplicitInterfaceTestClass();
					state.Id = 5;
					state.Name = "State";
					proxy.HibernateLazyInitializer.SetImplementation(state);
					var entity = (IPublic) proxy;
					Assert.That(entity.Id, Is.EqualTo(5), "Id");
					Assert.That(entity.Name, Is.EqualTo("State"), "Name");

					entity.Id = 10;
					entity.Name = "Test";
					Assert.That(entity.Id, Is.EqualTo(10), "entity.Id");
					Assert.That(state.Id, Is.EqualTo(10), "state.Id");
					Assert.That(entity.Name, Is.EqualTo("Test"), "entity.Name");
					Assert.That(state.Name, Is.EqualTo("Test"), "state.Name");
#if NETFX
				});
#endif
		}
Beispiel #14
0
		public void NonInitializedProxyStaysNonInitializedAfterSerialization()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(typeof(SimpleTestClass).FullName, typeof(SimpleTestClass), new HashSet<System.Type> {typeof(INHibernateProxy)}, null, null, null);
			var proxy = factory.GetProxy(2, null);
			Assert.That(proxy, Is.Not.Null, "proxy");
			Assert.That(NHibernateUtil.IsInitialized(proxy), Is.False, "proxy already initialized after creation");

			var serializer = GetFormatter();
			object deserialized;
			using (var memoryStream = new MemoryStream())
			{
				serializer.Serialize(memoryStream, proxy);
				Assert.That(NHibernateUtil.IsInitialized(proxy), Is.False, "proxy initialized after serialization");
				memoryStream.Seek(0L, SeekOrigin.Begin);
				deserialized = serializer.Deserialize(memoryStream);
			}
			Assert.That(deserialized, Is.Not.Null, "deserialized");
			Assert.That(deserialized, Is.InstanceOf<INHibernateProxy>());
			Assert.That(NHibernateUtil.IsInitialized(deserialized), Is.False, "proxy initialized after deserialization");
		}
		public void VerifyProxyForAbstractClass()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(AbstractTestClass).FullName,
				typeof(AbstractTestClass),
				new HashSet<System.Type> { typeof(INHibernateProxy) },
				null, null, null, true);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<IPublic>());
					Assert.That(proxy, Is.InstanceOf<AbstractTestClass>());
#if NETFX
				});
#endif
		}
		public void VerifyProxyForClassWithGenericNonVirtualMethod()
		{
			var factory = new StaticProxyFactory();
			factory.PostInstantiate(
				typeof(ClassWithGenericNonVirtualMethod).FullName,
				typeof(ClassWithGenericNonVirtualMethod),
				new HashSet<System.Type> { typeof(INHibernateProxy) },
				null, null, null, true);

#if NETFX
			VerifyGeneratedAssembly(
				() =>
				{
#endif
					var proxy = factory.GetProxy(1, null);
					Assert.That(proxy, Is.Not.Null);
					Assert.That(proxy, Is.InstanceOf<ClassWithGenericNonVirtualMethod>());

					Assert.That(factory.GetFieldInterceptionProxy(), Is.InstanceOf<ClassWithGenericNonVirtualMethod>());

#if NETFX
				});
#endif
		}