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,61 @@ 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+ TestsContext . AssumeSystemTypeIsSerializable ( ) ;
41+
42+ var factory = new StaticProxyFactory ( ) ;
43+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
44+ var proxy = factory . GetProxy ( 2 , null ) ;
45+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
46+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
47+ Assert . That ( proxy . HibernateLazyInitializer , Is . Not . Null , "HibernateLazyInitializer" ) ;
48+
49+ var impl = new SimpleTestClass { Id = 2 } ;
50+ proxy . HibernateLazyInitializer . SetImplementation ( impl ) ;
51+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . True , "proxy not initialized after setting implementation" ) ;
52+
53+ var serializer = new BinaryFormatter ( ) ;
54+ object deserialized ;
55+ using ( var memoryStream = new MemoryStream ( ) )
56+ {
57+ serializer . Serialize ( memoryStream , proxy ) ;
58+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
59+ deserialized = serializer . Deserialize ( memoryStream ) ;
60+ }
61+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
62+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
63+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . True , "proxy no more initialized after deserialization" ) ;
64+ Assert . That ( deserialized , Is . InstanceOf < SimpleTestClass > ( ) ) ;
65+ Assert . That ( ( ( SimpleTestClass ) deserialized ) . Id , Is . EqualTo ( 2 ) ) ;
66+ }
67+
68+ [ Test ]
69+ public void NonInitializedProxyStaysNonInitializedAfterSerialization ( )
70+ {
71+ TestsContext . AssumeSystemTypeIsSerializable ( ) ;
72+
73+ var factory = new StaticProxyFactory ( ) ;
74+ factory . PostInstantiate ( typeof ( SimpleTestClass ) . FullName , typeof ( SimpleTestClass ) , new HashSet < System . Type > { typeof ( INHibernateProxy ) } , null , null , null ) ;
75+ var proxy = factory . GetProxy ( 2 , null ) ;
76+ Assert . That ( proxy , Is . Not . Null , "proxy" ) ;
77+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy already initialized after creation" ) ;
78+
79+ var serializer = new BinaryFormatter ( ) ;
80+ object deserialized ;
81+ using ( var memoryStream = new MemoryStream ( ) )
82+ {
83+ serializer . Serialize ( memoryStream , proxy ) ;
84+ Assert . That ( NHibernateUtil . IsInitialized ( proxy ) , Is . False , "proxy initialized after serialization" ) ;
85+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
86+ deserialized = serializer . Deserialize ( memoryStream ) ;
87+ }
88+ Assert . That ( deserialized , Is . Not . Null , "deserialized" ) ;
89+ Assert . That ( deserialized , Is . InstanceOf < INHibernateProxy > ( ) ) ;
90+ Assert . That ( NHibernateUtil . IsInitialized ( deserialized ) , Is . False , "proxy initialized after deserialization" ) ;
91+ }
2792 }
2893}
0 commit comments