1
+ using System . Collections ;
2
+ using NUnit . Framework ;
3
+ using NHibernate . Type ;
4
+
5
+ namespace NHibernate . Test . Interceptor
6
+ {
7
+ [ TestFixture ]
8
+ public class InterceptorFixture : TestCase
9
+ {
10
+ protected override string MappingsAssembly
11
+ {
12
+ get { return "NHibernate.Test" ; }
13
+ }
14
+
15
+ protected override IList Mappings
16
+ {
17
+ get
18
+ {
19
+ return new string [ ] { "Interceptor.User.hbm.xml" , "Interceptor.Image.hbm.xml" } ;
20
+ }
21
+ }
22
+
23
+ public void CollectionIntercept ( )
24
+ {
25
+ ISession s = OpenSession ( new CollectionInterceptor ( ) ) ;
26
+ ITransaction t = s . BeginTransaction ( ) ;
27
+ User u = new User ( "Gavin" , "nivag" ) ;
28
+ s . Persist ( u ) ;
29
+ u . Password = "vagni" ;
30
+ t . Commit ( ) ;
31
+ s . Close ( ) ;
32
+
33
+ s = OpenSession ( ) ;
34
+ t = s . BeginTransaction ( ) ;
35
+ u = s . Get < User > ( "Gavin" ) ;
36
+ Assert . AreEqual ( 2 , u . Actions . Count ) ;
37
+ s . Delete ( u ) ;
38
+ t . Commit ( ) ;
39
+ s . Close ( ) ;
40
+ }
41
+
42
+ public void PropertyIntercept ( )
43
+ {
44
+ ISession s = OpenSession ( new PropertyInterceptor ( ) ) ;
45
+ ITransaction t = s . BeginTransaction ( ) ;
46
+ User u = new User ( "Gavin" , "nivag" ) ;
47
+ s . Persist ( u ) ;
48
+ u . Password = "vagni" ;
49
+ t . Commit ( ) ;
50
+ s . Close ( ) ;
51
+
52
+ s = OpenSession ( ) ;
53
+ t = s . BeginTransaction ( ) ;
54
+ u = s . Get < User > ( "Gavin" ) ;
55
+ Assert . IsTrue ( u . Created . HasValue ) ;
56
+ Assert . IsTrue ( u . LastUpdated . HasValue ) ;
57
+ s . Delete ( u ) ;
58
+ t . Commit ( ) ;
59
+ s . Close ( ) ;
60
+ }
61
+
62
+ private class HHH1921Interceptor : EmptyInterceptor
63
+ {
64
+ public override bool OnFlushDirty ( object entity , object id , object [ ] currentState , object [ ] previousState , string [ ] propertyNames , IType [ ] types )
65
+ {
66
+ currentState [ 0 ] = "test" ;
67
+ return true ;
68
+ }
69
+ }
70
+
71
+ /*
72
+ * Here the interceptor resets the
73
+ * current-state to the same thing as the current db state; this
74
+ * causes EntityPersister.FindDirty() to return no dirty properties.
75
+ */
76
+ public void PropertyIntercept2 ( )
77
+ {
78
+ ISession s = OpenSession ( ) ;
79
+ ITransaction t = s . BeginTransaction ( ) ;
80
+ User u = new User ( "Josh" , "test" ) ;
81
+ s . Persist ( u ) ;
82
+ t . Commit ( ) ;
83
+ s . Close ( ) ;
84
+
85
+ s = OpenSession ( new HHH1921Interceptor ( ) ) ;
86
+ t = s . BeginTransaction ( ) ;
87
+ u = s . Get < User > ( u . Name ) ;
88
+ u . Password = "nottest" ;
89
+ t . Commit ( ) ;
90
+ s . Close ( ) ;
91
+
92
+ s = OpenSession ( ) ;
93
+ t = s . BeginTransaction ( ) ;
94
+ u = s . Get < User > ( "Josh" ) ;
95
+ Assert . AreEqual ( "test" , u . Password ) ;
96
+ s . Delete ( u ) ;
97
+ t . Commit ( ) ;
98
+ s . Close ( ) ;
99
+ }
100
+ private class MyComponentInterceptor : EmptyInterceptor
101
+ {
102
+ readonly int checkPerm ;
103
+ readonly string checkComment ;
104
+ public MyComponentInterceptor ( int checkPerm , string checkComment )
105
+ {
106
+ this . checkPerm = checkPerm ;
107
+ this . checkComment = checkComment ;
108
+ }
109
+
110
+ public override bool OnSave ( object entity , object id , object [ ] state , string [ ] propertyNames , IType [ ] types )
111
+ {
112
+ if ( state [ 0 ] == null )
113
+ {
114
+ Image . Detail detail = new Image . Detail ( ) ;
115
+ detail . Perm1 = checkPerm ;
116
+ detail . Comment = checkComment ;
117
+ state [ 0 ] = detail ;
118
+ }
119
+ return true ;
120
+ }
121
+ }
122
+
123
+ public void ComponentInterceptor ( )
124
+ {
125
+
126
+ const int checkPerm = 500 ;
127
+ const string checkComment = "generated from interceptor" ;
128
+
129
+ ISession s = OpenSession ( new MyComponentInterceptor ( checkPerm , checkComment ) ) ;
130
+ ITransaction t = s . BeginTransaction ( ) ;
131
+ Image i = new Image ( ) ;
132
+ i . Name = "compincomp" ;
133
+ i = ( Image ) s . Merge ( i ) ;
134
+ Assert . IsNotNull ( i . Details ) ;
135
+ Assert . AreEqual ( checkPerm , i . Details . Perm1 ) ;
136
+ Assert . AreEqual ( checkComment , i . Details . Comment ) ;
137
+ t . Commit ( ) ;
138
+ s . Close ( ) ;
139
+
140
+ s = OpenSession ( ) ;
141
+ t = s . BeginTransaction ( ) ;
142
+ i = s . Get < Image > ( i . Id ) ;
143
+ Assert . IsNotNull ( i . Details ) ;
144
+ Assert . AreEqual ( checkPerm , i . Details . Perm1 ) ;
145
+ Assert . AreEqual ( checkComment , i . Details . Comment ) ;
146
+ s . Delete ( i ) ;
147
+ t . Commit ( ) ;
148
+ s . Close ( ) ;
149
+ }
150
+
151
+ public void StatefulIntercept ( )
152
+ {
153
+ StatefulInterceptor statefulInterceptor = new StatefulInterceptor ( ) ;
154
+ ISession s = OpenSession ( statefulInterceptor ) ;
155
+ Assert . IsNotNull ( statefulInterceptor . Session ) ;
156
+
157
+ ITransaction t = s . BeginTransaction ( ) ;
158
+ User u = new User ( "Gavin" , "nivag" ) ;
159
+ s . Persist ( u ) ;
160
+ u . Password = "vagni" ;
161
+ t . Commit ( ) ;
162
+ s . Close ( ) ;
163
+
164
+ s = OpenSession ( ) ;
165
+ t = s . BeginTransaction ( ) ;
166
+ IList logs = s . CreateCriteria ( typeof ( Log ) ) . List ( ) ;
167
+ Assert . AreEqual ( 2 , logs . Count ) ;
168
+ s . Delete ( u ) ;
169
+ s . Delete ( "from Log" ) ;
170
+ t . Commit ( ) ;
171
+ s . Close ( ) ;
172
+ }
173
+
174
+ }
175
+ }
0 commit comments