Skip to content

Commit 82f0a84

Browse files
committed
NH-3771 - Update tests expectations when BatchVersionedData is enabled
1 parent 15c0eda commit 82f0a84

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

src/NHibernate.Test/NHSpecificTest/NH1553/MsSQL/SnapshotIsolationUpdateConflictTest.cs

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Data;
2-
using System.Data.Common;
32
using NHibernate.Cfg;
43
using NHibernate.Dialect;
54
using NUnit.Framework;
5+
using NUnit.Framework.Constraints;
66

77
namespace NHibernate.Test.NHSpecificTest.NH1553.MsSQL
88
{
@@ -64,18 +64,15 @@ public void UpdateConflictDetectedByNH()
6464
p2.IdentificationNumber += 2;
6565

6666
SavePerson(p1);
67-
Assert.AreEqual(person.Version + 1, p1.Version);
68-
try
69-
{
70-
SavePerson(p2);
71-
Assert.Fail("Expecting stale object state exception");
72-
}
73-
catch (StaleObjectStateException sose)
74-
{
75-
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
76-
Assert.AreEqual(p2.Id, sose.Identifier);
77-
// as expected.
78-
}
67+
Assert.That(p1.Version, Is.EqualTo(person.Version + 1));
68+
69+
var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
70+
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>()
71+
: Throws.InstanceOf<StaleObjectStateException>()
72+
.And.Property("EntityName").EqualTo(typeof(Person).FullName)
73+
.And.Property("Identifier").EqualTo(p2.Id);
74+
75+
Assert.That(() => SavePerson(p2), expectedException);
7976
}
8077

8178
/// <summary>
@@ -89,46 +86,43 @@ public void UpdateConflictDetectedBySQLServer()
8986

9087
p1.IdentificationNumber++;
9188

92-
using (ISession session1 = OpenSession())
89+
using (var session1 = OpenSession())
90+
using (var tr1 = BeginTransaction(session1))
9391
{
94-
using (ITransaction tr1 = BeginTransaction(session1))
92+
session1.SaveOrUpdate(p1);
93+
session1.Flush();
94+
95+
using (var session2 = OpenSession())
96+
using (var tr2 = BeginTransaction(session2))
9597
{
96-
session1.SaveOrUpdate(p1);
97-
session1.Flush();
98+
var p2 = session2.Get<Person>(person.Id);
99+
p2.IdentificationNumber += 2;
100+
101+
tr1.Commit();
102+
Assert.That(p1.Version, Is.EqualTo(person.Version + 1));
103+
104+
session2.SaveOrUpdate(p2);
105+
106+
var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
107+
? (IConstraint) Throws.InstanceOf<StaleStateException>()
108+
: Throws.InstanceOf<StaleObjectStateException>()
109+
.And.Property("EntityName").EqualTo(typeof(Person).FullName)
110+
.And.Property("Identifier").EqualTo(p2.Id);
98111

99-
using (ISession session2 = OpenSession())
100-
{
101-
using (ITransaction tr2 = BeginTransaction(session2))
112+
Assert.That(
113+
() =>
102114
{
103-
var p2 = session2.Get<Person>(person.Id);
104-
p2.IdentificationNumber += 2;
105-
106-
tr1.Commit();
107-
Assert.AreEqual(person.Version + 1, p1.Version);
108-
109-
try
110-
{
111-
session2.SaveOrUpdate(p2);
112-
session2.Flush();
113-
114-
tr2.Commit();
115-
Assert.Fail("StaleObjectStateException expected");
116-
}
117-
catch (StaleObjectStateException sose)
118-
{
119-
Assert.AreEqual(typeof (Person).FullName, sose.EntityName);
120-
Assert.AreEqual(p2.Id, sose.Identifier);
121-
// as expected
122-
}
123-
}
124-
}
115+
session2.Flush();
116+
tr2.Commit();
117+
},
118+
expectedException);
125119
}
126120
}
127121
}
128122

129123
protected override bool AppliesTo(Dialect.Dialect dialect)
130124
{
131-
return dialect is MsSql2005Dialect || dialect is MsSql2008Dialect;
125+
return dialect is MsSql2005Dialect;
132126
}
133127

134128
private void SetAllowSnapshotIsolation(bool on)

src/NHibernate.Test/NHSpecificTest/OptimisticConcurrencyFixture.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public void StaleObjectStateCheckWithNormalizedEntityPersister()
5555
}
5656

5757
top.Name = "new name";
58-
Assert.Throws<StaleObjectStateException>(() => session.Flush());
58+
59+
var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
60+
? Throws.InstanceOf<StaleStateException>()
61+
: Throws.InstanceOf<StaleObjectStateException>();
62+
63+
Assert.That(() => session.Flush(), expectedException);
5964
}
6065
}
6166
finally
@@ -89,7 +94,12 @@ public void StaleObjectStateCheckWithEntityPersisterAndOptimisticLock()
8994
}
9095

9196
optimistic.String = "new string";
92-
Assert.Throws<StaleObjectStateException>(() => session.Flush());
97+
98+
var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
99+
? Throws.InstanceOf<StaleStateException>()
100+
: Throws.InstanceOf<StaleObjectStateException>();
101+
102+
Assert.That(() => session.Flush(), expectedException);
93103
}
94104
}
95105
finally

src/NHibernate.Test/VersionTest/Db/MsSQL/GeneratedBinaryVersionFixture.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,26 @@ public void ShouldCheckStaleState()
8686

8787
try
8888
{
89-
using (ISession session = OpenSession())
89+
using (var session = OpenSession())
9090
{
9191
session.Save(versioned);
9292
session.Flush();
9393

94-
using (ISession concurrentSession = OpenSession())
94+
using (var concurrentSession = OpenSession())
9595
{
9696
var sameVersioned = concurrentSession.Get<SimpleVersioned>(versioned.Id);
9797
sameVersioned.Something = "another string";
9898
concurrentSession.Flush();
9999
}
100100

101101
versioned.Something = "new string";
102-
session.Flush();
102+
103+
var expectedException = sessions.Settings.IsBatchVersionedDataEnabled
104+
? Throws.InstanceOf<StaleStateException>()
105+
: Throws.InstanceOf<StaleObjectStateException>();
106+
107+
Assert.That(() => session.Flush(), expectedException);
103108
}
104-
Assert.Fail("Expected exception was not thrown");
105-
}
106-
catch (StaleObjectStateException)
107-
{
108-
// as expected
109109
}
110110
finally
111111
{

0 commit comments

Comments
 (0)