1
1
using System . IO ;
2
2
using System . Linq ;
3
3
using System . Runtime . Serialization . Formatters . Binary ;
4
+ using NHibernate . Bytecode ;
5
+ using NHibernate . Bytecode . Lightweight ;
4
6
using NHibernate . Cfg ;
5
7
using NHibernate . Cfg . MappingSchema ;
6
- using NHibernate . Linq ;
7
8
using NHibernate . Mapping . ByCode ;
9
+ using NHibernate . Properties ;
8
10
using NUnit . Framework ;
9
11
10
12
namespace NHibernate . Test . NHSpecificTest . NH3119
11
13
{
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
14
[ TestFixture ]
21
15
public class ByCodeFixture : TestCaseMappingByCode
22
16
{
@@ -36,9 +30,13 @@ protected override HbmMapping GetMappings()
36
30
return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
37
31
}
38
32
33
+ private static IBytecodeProvider _backupByteCodeProvider ;
34
+
39
35
protected override void OnSetUp ( )
40
36
{
41
- if ( ! Cfg . Environment . UseReflectionOptimizer )
37
+ _backupByteCodeProvider = Environment . BytecodeProvider ;
38
+
39
+ if ( ! Environment . UseReflectionOptimizer )
42
40
{
43
41
Assert . Ignore ( "Test only works with reflection optimization enabled" ) ;
44
42
}
@@ -52,10 +50,17 @@ protected override void OnSetUp()
52
50
session . Flush ( ) ;
53
51
transaction . Commit ( ) ;
54
52
}
53
+
54
+ // Change refelection optimizer and recreate the configuration and factory
55
+ Environment . BytecodeProvider = new TestBytecodeProviderImpl ( ) ;
56
+ Configure ( ) ;
57
+ RebuildSessionFactory ( ) ;
55
58
}
56
59
57
60
protected override void OnTearDown ( )
58
61
{
62
+ Environment . BytecodeProvider = _backupByteCodeProvider ;
63
+
59
64
using ( ISession session = OpenSession ( ) )
60
65
using ( ITransaction transaction = session . BeginTransaction ( ) )
61
66
{
@@ -72,11 +77,9 @@ public void PocoComponentTuplizer_Instantiate_UsesReflectonOptimizer()
72
77
using ( ISession freshSession = OpenSession ( ) )
73
78
using ( freshSession . BeginTransaction ( ) )
74
79
{
75
- Entity entity = freshSession . Query < Entity > ( ) . Single ( ) ;
76
-
77
- string stackTrace = entity . Component . LastCtorStackTrace ;
78
-
79
- StringAssert . Contains ( "NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance" , stackTrace ) ;
80
+ ComponentTestReflectionOptimizer . IsCalledForComponent = false ;
81
+ freshSession . Query < Entity > ( ) . Single ( ) ;
82
+ Assert . That ( ComponentTestReflectionOptimizer . IsCalledForComponent , Is . True ) ;
80
83
}
81
84
}
82
85
@@ -95,12 +98,38 @@ public void PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_UsesRef
95
98
using ( ISession deserializedSession = factoryFromDeserializedConfig . OpenSession ( ) )
96
99
using ( deserializedSession . BeginTransaction ( ) )
97
100
{
98
- Entity entity = deserializedSession . Query < Entity > ( ) . Single ( ) ;
101
+ ComponentTestReflectionOptimizer . IsCalledForComponent = false ;
102
+ deserializedSession . Query < Entity > ( ) . Single ( ) ;
103
+ Assert . That ( ComponentTestReflectionOptimizer . IsCalledForComponent , Is . True ) ;
104
+ }
105
+ }
106
+ }
99
107
100
- string stackTrace = entity . Component . LastCtorStackTrace ;
108
+ public class TestBytecodeProviderImpl : AbstractBytecodeProvider
109
+ {
110
+ public override IReflectionOptimizer GetReflectionOptimizer ( System . Type mappedClass , IGetter [ ] getters , ISetter [ ] setters )
111
+ {
112
+ return new ComponentTestReflectionOptimizer ( mappedClass , getters , setters ) ;
113
+ }
114
+ }
101
115
102
- StringAssert . Contains ( "NHibernate.Bytecode.Lightweight.ReflectionOptimizer.CreateInstance" , stackTrace ) ;
103
- }
116
+ public class ComponentTestReflectionOptimizer : ReflectionOptimizer
117
+ {
118
+ private readonly bool _logCall ;
119
+
120
+ public static bool IsCalledForComponent { get ; set ; }
121
+
122
+ public ComponentTestReflectionOptimizer ( System . Type mappedType , IGetter [ ] getters , ISetter [ ] setters ) :
123
+ base ( mappedType , getters , setters )
124
+ {
125
+ _logCall = mappedType == typeof ( Component ) ;
126
+ }
127
+
128
+ public override object CreateInstance ( )
129
+ {
130
+ if ( _logCall )
131
+ IsCalledForComponent = true ;
132
+ return base . CreateInstance ( ) ;
104
133
}
105
134
}
106
135
}
0 commit comments