1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
3
+ using NHibernate . Cfg ;
4
+ using NHibernate . Cfg . Loquacious ;
5
+ using NHibernate . Dialect ;
6
+ using NUnit . Framework ;
7
+ using SharpTestsEx ;
8
+
9
+ namespace NHibernate . Test . CfgTest
10
+ {
11
+ public class ConfigurationAddMappingEvents
12
+ {
13
+ private const string ProductLineMapping =
14
+ @"<?xml version='1.0' encoding='utf-8' ?>
15
+ <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
16
+ <class entity-name='ProductLine'>
17
+ <id name='Id' type='int'>
18
+ <generator class='hilo'/>
19
+ </id>
20
+ <property name='Description' not-null='true' length='200' type='string'/>
21
+ <bag name='Models' cascade='all' inverse='true'>
22
+ <key column='productId'/>
23
+ <one-to-many class='Model'/>
24
+ </bag>
25
+ </class>
26
+ </hibernate-mapping>
27
+ " ;
28
+ private const string ModelMapping =
29
+ @"<?xml version='1.0' encoding='utf-8' ?>
30
+ <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'>
31
+ <class entity-name='Model'>
32
+ <id name='Id' type='int'>
33
+ <generator class='hilo'/>
34
+ </id>
35
+
36
+ <property name='Name' not-null='true' length='25' type='string'/>
37
+ <property name='Description' not-null='true' length='200' type='string'/>
38
+ <many-to-one name='ProductLine' column='productId' not-null='true' class='ProductLine'/>
39
+ </class>
40
+ </hibernate-mapping>
41
+ " ;
42
+ [ Test ]
43
+ public void WhenSubscribedToBeforeBindThenRaiseEventForEachMapping ( )
44
+ {
45
+ var listOfCalls = new List < BindMappingEventArgs > ( ) ;
46
+ var configuration = new Configuration ( ) ;
47
+ configuration . DataBaseIntegration ( x => x . Dialect < MsSql2008Dialect > ( ) ) ;
48
+ configuration . BeforeBindMapping += ( sender , args ) => { sender . Should ( ) . Be . SameInstanceAs ( configuration ) ; listOfCalls . Add ( args ) ; } ;
49
+
50
+ configuration . AddXmlString ( ProductLineMapping ) ;
51
+ configuration . AddXmlString ( ModelMapping ) ;
52
+
53
+ listOfCalls . Count . Should ( ) . Be ( 2 ) ;
54
+ listOfCalls . Select ( x => x . FileName ) . All ( x => x . Satisfy ( filename => filename != null ) ) ;
55
+ listOfCalls . Select ( x => x . Mapping ) . All ( x => x . Satisfy ( mappingDoc => mappingDoc != null ) ) ;
56
+ listOfCalls . Select ( x => x . Dialect ) . All ( x => x . Satisfy ( dialect => dialect . GetType ( ) == typeof ( MsSql2008Dialect ) ) ) ;
57
+ }
58
+
59
+ [ Test ]
60
+ public void WhenSubscribedToAfterBindThenRaiseEventForEachMapping ( )
61
+ {
62
+ var listOfCalls = new List < BindMappingEventArgs > ( ) ;
63
+ var configuration = new Configuration ( ) ;
64
+ configuration . DataBaseIntegration ( x => x . Dialect < MsSql2008Dialect > ( ) ) ;
65
+ configuration . AfterBindMapping += ( sender , args ) => { sender . Should ( ) . Be . SameInstanceAs ( configuration ) ; listOfCalls . Add ( args ) ; } ;
66
+
67
+ configuration . AddXmlString ( ProductLineMapping ) ;
68
+ configuration . AddXmlString ( ModelMapping ) ;
69
+
70
+ listOfCalls . Count . Should ( ) . Be ( 2 ) ;
71
+ listOfCalls . Select ( x => x . FileName ) . All ( x => x . Satisfy ( filename => filename != null ) ) ;
72
+ listOfCalls . Select ( x => x . Mapping ) . All ( x => x . Satisfy ( mappingDoc => mappingDoc != null ) ) ;
73
+ listOfCalls . Select ( x => x . Dialect ) . All ( x => x . Satisfy ( dialect => dialect . GetType ( ) == typeof ( MsSql2008Dialect ) ) ) ;
74
+ }
75
+ }
76
+ }
0 commit comments