11using System . IO ;
22using System . Linq ;
33using System . Runtime . Serialization . Formatters . Binary ;
4+ using NHibernate . Bytecode ;
5+ using NHibernate . Bytecode . Lightweight ;
46using NHibernate . Cfg ;
57using NHibernate . Cfg . MappingSchema ;
6- using NHibernate . Linq ;
78using NHibernate . Mapping . ByCode ;
9+ using NHibernate . Properties ;
810using NUnit . Framework ;
911
1012namespace NHibernate . Test . NHSpecificTest . NH3119
1113{
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>
2014 [ TestFixture ]
2115 public class ByCodeFixture : TestCaseMappingByCode
2216 {
@@ -36,9 +30,13 @@ protected override HbmMapping GetMappings()
3630 return mapper . CompileMappingForAllExplicitlyAddedEntities ( ) ;
3731 }
3832
33+ private static IBytecodeProvider _backupByteCodeProvider ;
34+
3935 protected override void OnSetUp ( )
4036 {
41- if ( ! Cfg . Environment . UseReflectionOptimizer )
37+ _backupByteCodeProvider = Environment . BytecodeProvider ;
38+
39+ if ( ! Environment . UseReflectionOptimizer )
4240 {
4341 Assert . Ignore ( "Test only works with reflection optimization enabled" ) ;
4442 }
@@ -52,10 +50,17 @@ protected override void OnSetUp()
5250 session . Flush ( ) ;
5351 transaction . Commit ( ) ;
5452 }
53+
54+ // Change refelection optimizer and recreate the configuration and factory
55+ Environment . BytecodeProvider = new TestBytecodeProviderImpl ( ) ;
56+ Configure ( ) ;
57+ RebuildSessionFactory ( ) ;
5558 }
5659
5760 protected override void OnTearDown ( )
5861 {
62+ Environment . BytecodeProvider = _backupByteCodeProvider ;
63+
5964 using ( ISession session = OpenSession ( ) )
6065 using ( ITransaction transaction = session . BeginTransaction ( ) )
6166 {
@@ -72,11 +77,9 @@ public void PocoComponentTuplizer_Instantiate_UsesReflectonOptimizer()
7277 using ( ISession freshSession = OpenSession ( ) )
7378 using ( freshSession . BeginTransaction ( ) )
7479 {
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 ) ;
8083 }
8184 }
8285
@@ -95,12 +98,38 @@ public void PocoComponentTuplizerOfDeserializedConfiguration_Instantiate_UsesRef
9598 using ( ISession deserializedSession = factoryFromDeserializedConfig . OpenSession ( ) )
9699 using ( deserializedSession . BeginTransaction ( ) )
97100 {
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+ }
99107
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+ }
101115
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 ( ) ;
104133 }
105134 }
106135}
0 commit comments