1
1
using System . Data ;
2
- using System . Data . Common ;
3
2
using NHibernate . Cfg ;
4
3
using NHibernate . Dialect ;
5
4
using NUnit . Framework ;
5
+ using NUnit . Framework . Constraints ;
6
6
7
7
namespace NHibernate . Test . NHSpecificTest . NH1553 . MsSQL
8
8
{
@@ -64,18 +64,15 @@ public void UpdateConflictDetectedByNH()
64
64
p2 . IdentificationNumber += 2 ;
65
65
66
66
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 ) ;
79
76
}
80
77
81
78
/// <summary>
@@ -89,46 +86,43 @@ public void UpdateConflictDetectedBySQLServer()
89
86
90
87
p1 . IdentificationNumber ++ ;
91
88
92
- using ( ISession session1 = OpenSession ( ) )
89
+ using ( var session1 = OpenSession ( ) )
90
+ using ( var tr1 = BeginTransaction ( session1 ) )
93
91
{
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 ) )
95
97
{
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 ) ;
98
111
99
- using ( ISession session2 = OpenSession ( ) )
100
- {
101
- using ( ITransaction tr2 = BeginTransaction ( session2 ) )
112
+ Assert . That (
113
+ ( ) =>
102
114
{
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 ) ;
125
119
}
126
120
}
127
121
}
128
122
129
123
protected override bool AppliesTo ( Dialect . Dialect dialect )
130
124
{
131
- return dialect is MsSql2005Dialect || dialect is MsSql2008Dialect ;
125
+ return dialect is MsSql2005Dialect ;
132
126
}
133
127
134
128
private void SetAllowSnapshotIsolation ( bool on )
0 commit comments