1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Runtime . Serialization . Formatters . Binary ;
2
5
using NHibernate . Proxy ;
3
6
using NUnit . Framework ;
4
7
@@ -16,6 +19,12 @@ public class TestClass : ISomething
16
19
public virtual int Id { get ; set ; }
17
20
}
18
21
22
+ [ Serializable ]
23
+ public class SimpleTestClass
24
+ {
25
+ public virtual int Id { get ; set ; }
26
+ }
27
+
19
28
[ Test ]
20
29
public void CanCreateProxyForClassWithInternalInterface ( )
21
30
{
@@ -24,5 +33,61 @@ public void CanCreateProxyForClassWithInternalInterface()
24
33
var proxy = factory . GetProxy ( 1 , null ) ;
25
34
Assert . That ( proxy , Is . Not . Null ) ;
26
35
}
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
+ }
27
92
}
28
93
}
0 commit comments