Skip to content

Commit c87995a

Browse files
committed
Merge branch '3.4.x'
2 parents 37d9a67 + 3ac4de1 commit c87995a

File tree

11 files changed

+336
-94
lines changed

11 files changed

+336
-94
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3234
5+
{
6+
public class GridLevel
7+
{
8+
public Guid Id { get; set; }
9+
}
10+
11+
public class GridWidget
12+
{
13+
public GridWidget()
14+
{
15+
Levels = new List<GridLevel>();
16+
}
17+
18+
public Guid Id { get; set; }
19+
20+
public ICollection<GridLevel> Levels { get; private set; }
21+
}
22+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3234
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
private void Evict(ISession session, GridWidget widget)
10+
{
11+
session.Evict(widget);
12+
sessions.Evict(widget.GetType());
13+
}
14+
15+
private static void Save(ISession session, GridWidget widget)
16+
{
17+
if (widget.Id != Guid.Empty && !session.Contains(widget))
18+
widget = session.Merge(widget);
19+
20+
session.SaveOrUpdate(widget);
21+
session.Flush();
22+
}
23+
24+
protected override void OnTearDown()
25+
{
26+
using (var session=OpenSession())
27+
using (var tx = session.BeginTransaction())
28+
{
29+
session.Delete("from System.Object");
30+
tx.Commit();
31+
}
32+
}
33+
34+
[Test]
35+
public void ShouldNotFailWhenAddingNewLevels()
36+
{
37+
using (var session = OpenSession())
38+
{
39+
var widget = new GridWidget
40+
{
41+
Levels =
42+
{
43+
new GridLevel(),
44+
new GridLevel()
45+
},
46+
};
47+
48+
Save(session, widget);
49+
Evict(session, widget);
50+
51+
widget.Levels.Add(new GridLevel());
52+
53+
Save(session, widget);
54+
Evict(session, widget);
55+
56+
var loaded = session.Get<GridWidget>(widget.Id);
57+
58+
Assert.That(loaded.Levels.Count, Is.EqualTo(3));
59+
}
60+
}
61+
62+
[Test]
63+
public void ShouldNotFailWhenReplacingLevels()
64+
{
65+
using (var session = OpenSession())
66+
{
67+
var widget = new GridWidget
68+
{
69+
Levels =
70+
{
71+
new GridLevel(),
72+
new GridLevel()
73+
},
74+
};
75+
76+
Save(session, widget);
77+
Evict(session, widget);
78+
79+
widget.Levels.Clear();
80+
widget.Levels.Add(new GridLevel());
81+
82+
Save(session, widget);
83+
Evict(session, widget);
84+
85+
var loaded = session.Get<GridWidget>(widget.Id);
86+
87+
Assert.That(loaded.Levels.Count, Is.EqualTo(1));
88+
}
89+
}
90+
}
91+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
4+
assembly="NHibernate.Test"
5+
namespace="NHibernate.Test.NHSpecificTest.NH3234">
6+
7+
<class name="GridLevel" table="GridLevel" lazy="false">
8+
<id name="Id" generator="guid" />
9+
</class>
10+
11+
<class name="GridWidget" lazy="false">
12+
<id name="Id" generator="guid" />
13+
<bag name="Levels" schema="rm" table="GridLevel" lazy="false" cascade="all-delete-orphan">
14+
<key column="WidgetId" not-null="true" />
15+
<one-to-many class="GridLevel" />
16+
</bag>
17+
</class>
18+
19+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@
764764
<Compile Include="NHSpecificTest\NH3202\Fixture.cs" />
765765
<Compile Include="NHSpecificTest\NH646\Domain.cs" />
766766
<Compile Include="NHSpecificTest\NH646\Fixture.cs" />
767+
<Compile Include="NHSpecificTest\NH3234\Fixture.cs" />
768+
<Compile Include="NHSpecificTest\NH3234\Domain.cs" />
767769
<Compile Include="NHSpecificTest\Futures\LinqToFutureValueFixture.cs" />
768770
<Compile Include="NHSpecificTest\NH2955\Employee.cs" />
769771
<Compile Include="NHSpecificTest\NH2955\Fixture.cs" />
@@ -1182,6 +1184,7 @@
11821184
<Compile Include="ReadOnly\TextHolder.cs" />
11831185
<Compile Include="ReadOnly\VersionedNode.cs" />
11841186
<Compile Include="SqlCommandTest\SqlTokenizerFixture.cs" />
1187+
<Compile Include="RecordingInterceptor.cs" />
11851188
<Compile Include="Stateless\Contact.cs" />
11861189
<Compile Include="Stateless\Country.cs" />
11871190
<Compile Include="Stateless\FetchingLazyCollections\LazyCollectionFetchTests.cs" />
@@ -3035,6 +3038,7 @@
30353038
<EmbeddedResource Include="NHSpecificTest\NH2033\Mappings.hbm.xml" />
30363039
<EmbeddedResource Include="NHSpecificTest\NH3202\Mappings.hbm.xml" />
30373040
<EmbeddedResource Include="NHSpecificTest\NH3221\Mappings.hbm.xml" />
3041+
<EmbeddedResource Include="NHSpecificTest\NH3234\Mappings.hbm.xml" />
30383042
<EmbeddedResource Include="NHSpecificTest\NH3057\Mappings.hbm.xml" />
30393043
<EmbeddedResource Include="NHSpecificTest\NH2969\Mappings.hbm.xml" />
30403044
<EmbeddedResource Include="NHSpecificTest\NH2806\Mappings.hbm.xml">
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace NHibernate.Test
2+
{
3+
public class RecordingInterceptor : EmptyInterceptor
4+
{
5+
public int afterTransactionBeginCalled;
6+
public int afterTransactionCompletionCalled;
7+
public int beforeTransactionCompletionCalled;
8+
9+
public override void AfterTransactionBegin(ITransaction tx)
10+
{
11+
afterTransactionBeginCalled++;
12+
}
13+
14+
public override void AfterTransactionCompletion(ITransaction tx)
15+
{
16+
afterTransactionCompletionCalled++;
17+
}
18+
19+
public override void BeforeTransactionCompletion(ITransaction tx)
20+
{
21+
beforeTransactionCompletionCalled++;
22+
}
23+
}
24+
}

src/NHibernate.Test/SystemTransactions/TransactionNotificationFixture.cs

Lines changed: 91 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System;
12
using System.Collections;
3+
using System.Data;
24
using System.Transactions;
35
using NUnit.Framework;
46

@@ -12,32 +14,11 @@ protected override IList Mappings
1214
get { return new string[] {}; }
1315
}
1416

15-
public class RecordingInterceptor : EmptyInterceptor
16-
{
17-
public int afterTransactionBeginCalled;
18-
public int afterTransactionCompletionCalled;
19-
public int beforeTransactionCompletionCalled;
20-
21-
public override void AfterTransactionBegin(ITransaction tx)
22-
{
23-
afterTransactionBeginCalled++;
24-
}
25-
26-
public override void AfterTransactionCompletion(ITransaction tx)
27-
{
28-
afterTransactionCompletionCalled++;
29-
}
30-
31-
public override void BeforeTransactionCompletion(ITransaction tx)
32-
{
33-
beforeTransactionCompletionCalled++;
34-
}
35-
}
3617

3718
[Test]
3819
public void NoTransaction()
3920
{
40-
RecordingInterceptor interceptor = new RecordingInterceptor();
21+
var interceptor = new RecordingInterceptor();
4122
using (sessions.OpenSession(interceptor))
4223
{
4324
Assert.AreEqual(0, interceptor.afterTransactionBeginCalled);
@@ -49,7 +30,7 @@ public void NoTransaction()
4930
[Test]
5031
public void AfterBegin()
5132
{
52-
RecordingInterceptor interceptor = new RecordingInterceptor();
33+
var interceptor = new RecordingInterceptor();
5334
using (new TransactionScope())
5435
using (sessions.OpenSession(interceptor))
5536
{
@@ -62,9 +43,9 @@ public void AfterBegin()
6243
[Test]
6344
public void Complete()
6445
{
65-
RecordingInterceptor interceptor = new RecordingInterceptor();
46+
var interceptor = new RecordingInterceptor();
6647
ISession session;
67-
using(TransactionScope scope = new TransactionScope())
48+
using(var scope = new TransactionScope())
6849
{
6950
session = sessions.OpenSession(interceptor);
7051
scope.Complete();
@@ -78,13 +59,13 @@ public void Complete()
7859
[Test]
7960
public void Rollback()
8061
{
81-
RecordingInterceptor interceptor = new RecordingInterceptor();
62+
var interceptor = new RecordingInterceptor();
8263
using (new TransactionScope())
8364
using (sessions.OpenSession(interceptor))
8465
{
8566
}
8667
Assert.AreEqual(0, interceptor.beforeTransactionCompletionCalled);
87-
Assert.AreEqual(2, interceptor.afterTransactionCompletionCalled);
68+
Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
8869
}
8970

9071
[Test]
@@ -126,5 +107,88 @@ public void OneTransactionScopesInsideOneSession()
126107
Assert.AreEqual(1, interceptor.beforeTransactionCompletionCalled);
127108
Assert.AreEqual(1, interceptor.afterTransactionCompletionCalled);
128109
}
110+
111+
112+
[Description("NH2128, NH3572")]
113+
[Theory]
114+
public void ShouldNotifyAfterDistributedTransaction(bool doCommit)
115+
{
116+
// Note: For distributed transaction, calling Close() on the session isn't
117+
// supported, so we don't need to test that scenario.
118+
119+
var interceptor = new RecordingInterceptor();
120+
ISession s1 = null;
121+
ISession s2 = null;
122+
123+
using (var tx = new TransactionScope())
124+
{
125+
try
126+
{
127+
s1 = OpenSession(interceptor);
128+
s2 = OpenSession(interceptor);
129+
130+
s1.CreateCriteria<object>().List();
131+
s2.CreateCriteria<object>().List();
132+
}
133+
finally
134+
{
135+
if (s1 != null)
136+
s1.Dispose();
137+
if (s2 != null)
138+
s2.Dispose();
139+
}
140+
141+
if (doCommit)
142+
tx.Complete();
143+
}
144+
145+
Assert.That(s1.IsOpen, Is.False);
146+
Assert.That(s2.IsOpen, Is.False);
147+
Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(2));
148+
}
149+
150+
151+
[Description("NH2128")]
152+
[Theory]
153+
public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doCommit)
154+
{
155+
// Note: For distributed transaction, calling Close() on the session isn't
156+
// supported, so we don't need to test that scenario.
157+
158+
var interceptor = new RecordingInterceptor();
159+
ISession s1 = null;
160+
ISession s2 = null;
161+
162+
using (var tx = new TransactionScope())
163+
{
164+
using (IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection())
165+
using (IDbConnection ownConnection2 = sessions.ConnectionProvider.GetConnection())
166+
{
167+
try
168+
{
169+
s1 = sessions.OpenSession(ownConnection1, interceptor);
170+
s2 = sessions.OpenSession(ownConnection2, interceptor);
171+
172+
s1.CreateCriteria<object>().List();
173+
s2.CreateCriteria<object>().List();
174+
}
175+
finally
176+
{
177+
if (s1 != null)
178+
s1.Dispose();
179+
if (s2 != null)
180+
s2.Dispose();
181+
}
182+
183+
if (doCommit)
184+
tx.Complete();
185+
}
186+
}
187+
188+
Assert.That(s1.IsOpen, Is.False);
189+
Assert.That(s2.IsOpen, Is.False);
190+
Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(2));
191+
}
192+
129193
}
130194
}

0 commit comments

Comments
 (0)