1
+ using System ;
2
+ using System . IO ;
3
+ using System . Reflection ;
4
+ using System . Runtime . Serialization . Formatters . Binary ;
5
+ using NHibernate . Cfg . MappingSchema ;
6
+ using NHibernate . Engine ;
7
+ using NHibernate . Mapping ;
8
+ using NHibernate . Mapping . ByCode ;
9
+ using NHibernate . Type ;
10
+ using NUnit . Framework ;
11
+
12
+ namespace NHibernate . Test . NHSpecificTest . NH3383
13
+ {
14
+ public class ByCodeFixture : TestCaseMappingByCode
15
+ {
16
+ protected override HbmMapping GetMappings ( )
17
+ {
18
+ var mapper = new ModelMapper ( ) ;
19
+ return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
20
+ }
21
+
22
+
23
+ [ Test ]
24
+ public void DeserializedCascadeStyleRefersToSameObject ( )
25
+ {
26
+ CascadeStyle deserializedCascadeStyle ;
27
+
28
+ using ( var configMemoryStream = new MemoryStream ( ) )
29
+ {
30
+ var formatter = new BinaryFormatter ( ) ;
31
+ formatter . Serialize ( configMemoryStream , CascadeStyle . Evict ) ;
32
+ configMemoryStream . Position = 0 ;
33
+ deserializedCascadeStyle = ( CascadeStyle ) formatter . Deserialize ( configMemoryStream ) ;
34
+ }
35
+
36
+ Assert . That ( deserializedCascadeStyle , Is . SameAs ( CascadeStyle . Evict ) ) ;
37
+ }
38
+
39
+
40
+ [ Test ]
41
+ public void CanRoundTripSerializedMultipleCascadeStyle ( )
42
+ {
43
+ CascadeStyle startingCascadeStyle =
44
+ new CascadeStyle . MultipleCascadeStyle ( new [ ] { CascadeStyle . Delete , CascadeStyle . Lock } ) ;
45
+ CascadeStyle deserializedCascadeStyle ;
46
+
47
+ using ( var configMemoryStream = new MemoryStream ( ) )
48
+ {
49
+ var formatter = new BinaryFormatter ( ) ;
50
+ formatter . Serialize ( configMemoryStream , startingCascadeStyle ) ;
51
+ configMemoryStream . Position = 0 ;
52
+ deserializedCascadeStyle = ( CascadeStyle ) formatter . Deserialize ( configMemoryStream ) ;
53
+ }
54
+
55
+ Assert . That ( deserializedCascadeStyle , Is . TypeOf < CascadeStyle . MultipleCascadeStyle > ( ) ) ;
56
+ Assert . That ( deserializedCascadeStyle . ToString ( ) ,
57
+ Is . EqualTo (
58
+ "[NHibernate.Engine.CascadeStyle+DeleteCascadeStyle,NHibernate.Engine.CascadeStyle+LockCascadeStyle]" ) ) ;
59
+ }
60
+
61
+
62
+ [ Test ]
63
+ public void DeserializedPropertyMapping_RefersToSameCascadeStyle ( )
64
+ {
65
+ var classMapping = CreateMappingClasses ( ) ;
66
+
67
+ RootClass deserializedClassMapping ;
68
+
69
+ using ( MemoryStream configMemoryStream = new MemoryStream ( ) )
70
+ {
71
+ BinaryFormatter formatter = new BinaryFormatter ( ) ;
72
+ formatter . Serialize ( configMemoryStream , classMapping ) ;
73
+ configMemoryStream . Position = 0 ;
74
+ deserializedClassMapping = ( RootClass ) formatter . Deserialize ( configMemoryStream ) ;
75
+ }
76
+
77
+ AssertDeserializedMappingClasses ( deserializedClassMapping ) ;
78
+ }
79
+
80
+ // This test uses a seperate AppDomain to simulate the loading of a Configuration that was
81
+ // serialized to the disk and is later deserialized in a new process.
82
+ [ Test ]
83
+ public void DeserializedPropertyMapping_CascadeStyleNotYetInitializedOnDeserialization_RefersToSameCascadeStyle ( )
84
+ {
85
+ var classMapping = CreateMappingClasses ( ) ;
86
+
87
+ using ( MemoryStream configMemoryStream = new MemoryStream ( ) )
88
+ {
89
+ BinaryFormatter formatter = new BinaryFormatter ( ) ;
90
+ formatter . Serialize ( configMemoryStream , classMapping ) ;
91
+ configMemoryStream . Position = 0 ;
92
+
93
+ var secondAppDomain = AppDomain . CreateDomain (
94
+ "SecondAppDomain" ,
95
+ null ,
96
+ AppDomain . CurrentDomain . SetupInformation ) ;
97
+
98
+ try
99
+ {
100
+ var helper = ( AppDomainHelper ) secondAppDomain . CreateInstanceAndUnwrap (
101
+ Assembly . GetExecutingAssembly ( ) . FullName ,
102
+ typeof ( AppDomainHelper ) . FullName ) ;
103
+
104
+ helper . DeserializeAndAssert ( configMemoryStream ) ;
105
+ }
106
+ finally
107
+ {
108
+ AppDomain . Unload ( secondAppDomain ) ;
109
+ }
110
+ }
111
+ }
112
+
113
+ private static RootClass CreateMappingClasses ( )
114
+ {
115
+ var classMapping = new RootClass ( ) ;
116
+ var componentMapping = new NHibernate . Mapping . Component ( classMapping ) ;
117
+
118
+ var componentPropertyMapping = new Property ( componentMapping ) ;
119
+ componentPropertyMapping . Name = "ComponentPropertyInClass" ;
120
+ classMapping . AddProperty ( componentPropertyMapping ) ;
121
+
122
+ var stringValue = new SimpleValue ( ) ;
123
+ stringValue . TypeName = typeof ( string ) . FullName ;
124
+
125
+ var stringPropertyInComponentMapping = new Property ( stringValue ) ;
126
+ stringPropertyInComponentMapping . Name = "StringPropertyInComponent" ;
127
+ componentMapping . AddProperty ( stringPropertyInComponentMapping ) ;
128
+
129
+ var componentType = ( IAbstractComponentType ) componentMapping . Type ;
130
+
131
+ Assume . That ( CascadeStyle . None == stringPropertyInComponentMapping . CascadeStyle ) ;
132
+ Assume . That ( CascadeStyle . None == componentType . GetCascadeStyle ( 0 ) ) ;
133
+ Assume . That ( CascadeStyle . None == componentPropertyMapping . CascadeStyle ) ;
134
+
135
+ return classMapping ;
136
+ }
137
+
138
+ private static void AssertDeserializedMappingClasses ( RootClass deserializedClassMapping )
139
+ {
140
+ var deserializedComponentPropertyMapping = deserializedClassMapping . GetProperty ( "ComponentPropertyInClass" ) ;
141
+ var deserializedComponentMapping = ( NHibernate . Mapping . Component ) deserializedComponentPropertyMapping . Value ;
142
+ var deserializedComponentType = ( IAbstractComponentType ) deserializedComponentMapping . Type ;
143
+ var deserializedStringPropertyInComponentMapping = deserializedComponentMapping . GetProperty ( "StringPropertyInComponent" ) ;
144
+
145
+ // Must be all the same objects since CascadeStyles are singletons and are
146
+ // compared with "==" and "!=" operators.
147
+ Assert . AreSame ( CascadeStyle . None , deserializedStringPropertyInComponentMapping . CascadeStyle ) ;
148
+ Assert . AreSame ( CascadeStyle . None , deserializedComponentType . GetCascadeStyle ( 0 ) ) ;
149
+ Assert . AreSame ( CascadeStyle . None , deserializedComponentPropertyMapping . CascadeStyle ) ;
150
+ }
151
+
152
+ private sealed class AppDomainHelper : MarshalByRefObject
153
+ {
154
+ public void DeserializeAndAssert ( MemoryStream configMemoryStream )
155
+ {
156
+ BinaryFormatter formatter = new BinaryFormatter ( ) ;
157
+ var deserializedClassMapping = ( RootClass ) formatter . Deserialize ( configMemoryStream ) ;
158
+
159
+ AssertDeserializedMappingClasses ( deserializedClassMapping ) ;
160
+ }
161
+ }
162
+ }
163
+ }
0 commit comments