1
+ using System . IO ;
2
+ using System . Linq ;
3
+ using System . Runtime . Serialization . Formatters . Binary ;
4
+ using NHibernate . Cfg ;
5
+ using NHibernate . Cfg . MappingSchema ;
6
+ using NHibernate . Linq ;
7
+ using NHibernate . Mapping . ByCode ;
8
+ using NUnit . Framework ;
9
+
10
+ namespace NHibernate . Test . NHSpecificTest . NH3119
11
+ {
12
+ /// <summary>
13
+ /// Fixture using 'by code' mappings
14
+ /// </summary>
15
+ /// <remarks>
16
+ /// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entity" /> mapping is performed
17
+ /// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach
18
+ /// if you prefer.
19
+ /// </remarks>
20
+ public class ByCodeFixture : TestCaseMappingByCode
21
+ {
22
+ protected override HbmMapping GetMappings ( )
23
+ {
24
+ var mapper = new ModelMapper ( ) ;
25
+ mapper . Class < Entity > ( rc =>
26
+ {
27
+ rc . Id ( x => x . Id , m => m . Generator ( Generators . GuidComb ) ) ;
28
+ rc . Property ( x => x . Name ) ;
29
+ rc . Component ( x => x . Component , c =>
30
+ {
31
+ c . Property ( x => x . Value ) ;
32
+ } ) ;
33
+ } ) ;
34
+
35
+ return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
36
+ }
37
+
38
+ protected override void OnSetUp ( )
39
+ {
40
+ if ( ! Cfg . Environment . UseReflectionOptimizer )
41
+ {
42
+ Assert . Ignore ( "Test only works with reflection optimization enabled" ) ;
43
+ }
44
+
45
+ using ( ISession session = OpenSession ( ) )
46
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
47
+ {
48
+ var e1 = new Entity { Name = "Name" , Component = new Component { Value = "Value" } } ;
49
+ session . Save ( e1 ) ;
50
+
51
+ session . Flush ( ) ;
52
+ transaction . Commit ( ) ;
53
+ }
54
+ }
55
+
56
+ protected override void OnTearDown ( )
57
+ {
58
+ using ( ISession session = OpenSession ( ) )
59
+ using ( ITransaction transaction = session . BeginTransaction ( ) )
60
+ {
61
+ session . Delete ( "from System.Object" ) ;
62
+
63
+ session . Flush ( ) ;
64
+ transaction . Commit ( ) ;
65
+ }
66
+ }
67
+
68
+ [ Test ]
69
+ public void PocoComponentTuplizer_Instantiate_UsesReflectonOptimizer ( )
70
+ {
71
+ using ( ISession freshSession = OpenSession ( ) )
72
+ using ( freshSession . BeginTransaction ( ) )
73
+ {
74
+ Entity entity = freshSession . Query < Entity > ( ) . Single ( ) ;
75
+
76
+ string stackTrace = entity . Component . LastCtorStackTrace ;
77
+
78
+ StringAssert . Contains ( "NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance" , stackTrace ) ;
79
+ }
80
+ }
81
+
82
+ [ Test ]
83
+ public void PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_UsesReflectonOptimizer ( )
84
+ {
85
+ MemoryStream configMemoryStream = new MemoryStream ( ) ;
86
+ BinaryFormatter writer = new BinaryFormatter ( ) ;
87
+ writer . Serialize ( configMemoryStream , cfg ) ;
88
+
89
+ configMemoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
90
+ BinaryFormatter reader = new BinaryFormatter ( ) ;
91
+ Configuration deserializedConfig = ( Configuration ) reader . Deserialize ( configMemoryStream ) ;
92
+ ISessionFactory factoryFromDeserializedConfig = deserializedConfig . BuildSessionFactory ( ) ;
93
+
94
+ using ( ISession deserializedSession = factoryFromDeserializedConfig . OpenSession ( ) )
95
+ using ( deserializedSession . BeginTransaction ( ) )
96
+ {
97
+ Entity entity = deserializedSession . Query < Entity > ( ) . Single ( ) ;
98
+
99
+ string stackTrace = entity . Component . LastCtorStackTrace ;
100
+
101
+ StringAssert . Contains ( "NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance" , stackTrace ) ;
102
+ }
103
+ }
104
+ }
105
+ }
0 commit comments