Skip to content

Commit 10724ce

Browse files
committed
- Re-Added IInterceptor.SetSession feature with test
- Tests for various interceptor implementations (from H3) SVN: trunk@3565
1 parent 2487354 commit 10724ce

14 files changed

+522
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NHibernate.Type;
2+
namespace NHibernate.Test.Interceptor
3+
{
4+
public class CollectionInterceptor : EmptyInterceptor
5+
{
6+
public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
7+
{
8+
((User)entity).Actions.Add("updated");
9+
return false;
10+
}
11+
12+
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
13+
{
14+
((User)entity).Actions.Add("created");
15+
return false;
16+
}
17+
}
18+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace NHibernate.Test.Interceptor
2+
{
3+
public class Image
4+
{
5+
private Detail details;
6+
private long id;
7+
private string name;
8+
9+
public virtual long Id
10+
{
11+
get { return id; }
12+
set { id = value; }
13+
}
14+
15+
public virtual string Name
16+
{
17+
get { return name; }
18+
set { name = value; }
19+
}
20+
21+
public virtual Detail Details
22+
{
23+
get { return details; }
24+
set { details = value; }
25+
}
26+
27+
public override string ToString()
28+
{
29+
return "Image/" + (details == null ? "no details" : details.ToString());
30+
}
31+
32+
#region Nested type: Detail
33+
34+
public class Detail
35+
{
36+
private string comment;
37+
private long perm1 = -1; // all bits turned on.
38+
39+
public virtual long Perm1
40+
{
41+
get { return perm1; }
42+
set { perm1 = value; }
43+
}
44+
45+
public virtual string Comment
46+
{
47+
get { return comment; }
48+
set { comment = value; }
49+
}
50+
51+
public override string ToString()
52+
{
53+
return "Details=" + perm1;
54+
}
55+
}
56+
57+
#endregion
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.Interceptor">
5+
6+
<class name="Image" table="image" abstract="false" select-before-update="true" >
7+
<id name="Id" type="int" column="id">
8+
<generator class="native"/>
9+
</id>
10+
<component name="Details" class="Image+Detail">
11+
<property name="Perm1" not-null="true" type="long" column="permissions"/>
12+
<property name="Comment" type="string" column="comment_txt"/>
13+
</component>
14+
<property name="Name" type="string" column="name" not-null="true"/>
15+
</class>
16+
17+
</hibernate-mapping>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
3+
namespace NHibernate.Test.Interceptor
4+
{
5+
public class Log
6+
{
7+
private long id;
8+
private string entityName;
9+
private string entityId;
10+
private string action;
11+
private DateTime time;
12+
13+
public Log() {}
14+
15+
public Log(string action, string entityId, string entityName)
16+
{
17+
this.action = action;
18+
this.entityId = entityId;
19+
this.entityName = entityName;
20+
time = DateTime.Now;
21+
}
22+
23+
public virtual long Id
24+
{
25+
get { return id; }
26+
set { id = value; }
27+
}
28+
29+
public virtual string EntityName
30+
{
31+
get { return entityName; }
32+
set { entityName = value; }
33+
}
34+
35+
public virtual string EntityId
36+
{
37+
get { return entityId; }
38+
set { entityId = value; }
39+
}
40+
41+
public virtual string Action
42+
{
43+
get { return action; }
44+
set { action = value; }
45+
}
46+
47+
public virtual DateTime Time
48+
{
49+
get { return time; }
50+
set { time = value; }
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using NHibernate.Type;
3+
namespace NHibernate.Test.Interceptor
4+
{
5+
public class PropertyInterceptor : EmptyInterceptor
6+
{
7+
public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
8+
{
9+
currentState[1] = DateTime.Now;
10+
return true;
11+
}
12+
13+
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
14+
{
15+
state[2] = DateTime.Now;
16+
return true;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)