1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Runtime . Serialization . Formatters . Binary ;
25using NHibernate . Proxy ;
36using NUnit . Framework ;
47
@@ -16,6 +19,12 @@ public class TestClass : ISomething
1619 public virtual int Id { get ; set ; }
1720 }
1821
22+ [ Serializable ]
23+ public class SimpleTestClass
24+ {
25+ public virtual int Id { get ; set ; }
26+ }
27+
1928 [ Test ]
2029 public void CanCreateProxyForClassWithInternalInterface ( )
2130 {
@@ -24,5 +33,57 @@ public void CanCreateProxyForClassWithInternalInterface()
2433 var proxy = factory . GetProxy ( 1 , null ) ;
2534 Assert . That ( proxy , Is . Not . Null ) ;
2635 }
36+
37+ [ Test ]
38+ public void InitializedProxyStaysInitializedAfterDeserialization ( )
39+ {
40+ var factory = new StaticProxyFactory ( ) ;
41+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
42+ var proxy = factory . GetProxy ( 2 , null ) ;
43+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
44+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
45+ Assert . That ( proxy . HibernateLazyInitializer , Is . Not . Null , "HibernateLazyInitializer" ) ;
46+
47+ var impl = new SimpleTestClass { Id = 2 } ;
48+ proxy . HibernateLazyInitializer . SetImplementation ( impl ) ;
49+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . True , "proxy not initialized after setting implementation" ) ;
50+
51+ var serializer = new BinaryFormatter ( ) ;
52+ object deserialized ;
53+ using ( var memoryStream = new MemoryStream ( ) )
54+ {
55+ serializer . Serialize ( memoryStream , proxy ) ;
56+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
57+ deserialized = serializer . Deserialize ( memoryStream ) ;
58+ }
59+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
60+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
61+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . True , "proxy no more initialized after deserialization" ) ;
62+ Assert . That ( deserialized , Is . InstanceOf < SimpleTestClass > ( ) ) ;
63+ Assert . That ( ( ( SimpleTestClass ) deserialized ) . Id , Is . EqualTo ( 2 ) ) ;
64+ }
65+
66+ [ Test ]
67+ public void NonInitializedProxyStaysNonInitializedAfterSerialization ( )
68+ {
69+ var factory = new StaticProxyFactory ( ) ;
70+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
71+ var proxy = factory . GetProxy ( 2 , null ) ;
72+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
73+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
74+
75+ var serializer = new BinaryFormatter ( ) ;
76+ object deserialized ;
77+ using ( var memoryStream = new MemoryStream ( ) )
78+ {
79+ serializer . Serialize ( memoryStream , proxy ) ;
80+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy initialized after serialization" ) ;
81+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
82+ deserialized = serializer . Deserialize ( memoryStream ) ;
83+ }
84+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
85+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
86+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . False , "proxy initialized after deserialization" ) ;
87+ }
2788 }
2889}
0 commit comments