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