1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Services
.Transaction
.Tests
19 using NUnit
.Framework
;
22 public class TransactionManagerTestCase
24 private DefaultTransactionManager tm
;
29 tm
= new DefaultTransactionManager(new TransientActivityManager());
33 public void SynchronizationsAndCommit()
35 ITransaction transaction
=
36 tm
.CreateTransaction(TransactionMode
.Unspecified
, IsolationMode
.Unspecified
);
40 SynchronizationImpl sync
= new SynchronizationImpl();
42 transaction
.RegisterSynchronization( sync
);
44 Assert
.AreEqual( DateTime
.MinValue
, sync
.Before
);
45 Assert
.AreEqual( DateTime
.MinValue
, sync
.After
);
49 Assert
.IsTrue( sync
.Before
> DateTime
.MinValue
);
50 Assert
.IsTrue( sync
.After
> DateTime
.MinValue
);
54 public void SynchronizationsAndRollback()
56 ITransaction transaction
=
57 tm
.CreateTransaction(TransactionMode
.Unspecified
, IsolationMode
.Unspecified
);
61 SynchronizationImpl sync
= new SynchronizationImpl();
63 transaction
.RegisterSynchronization( sync
);
65 Assert
.AreEqual( DateTime
.MinValue
, sync
.Before
);
66 Assert
.AreEqual( DateTime
.MinValue
, sync
.After
);
68 transaction
.Rollback();
70 Assert
.IsTrue( sync
.Before
> DateTime
.MinValue
);
71 Assert
.IsTrue( sync
.After
> DateTime
.MinValue
);
75 public void ResourcesAndCommit()
77 ITransaction transaction
=
78 tm
.CreateTransaction(TransactionMode
.Unspecified
, IsolationMode
.Unspecified
);
80 ResourceImpl resource
= new ResourceImpl();
82 transaction
.Enlist( resource
);
84 Assert
.IsFalse( resource
.Started
);
85 Assert
.IsFalse( resource
.Committed
);
86 Assert
.IsFalse( resource
.Rolledback
);
90 Assert
.IsTrue( resource
.Started
);
91 Assert
.IsFalse( resource
.Committed
);
92 Assert
.IsFalse( resource
.Rolledback
);
96 Assert
.IsTrue( resource
.Started
);
97 Assert
.IsTrue( resource
.Committed
);
98 Assert
.IsFalse( resource
.Rolledback
);
102 public void ResourcesAndRollback()
104 ITransaction transaction
=
105 tm
.CreateTransaction(TransactionMode
.Unspecified
, IsolationMode
.Unspecified
);
107 ResourceImpl resource
= new ResourceImpl();
109 transaction
.Enlist( resource
);
111 Assert
.IsFalse( resource
.Started
);
112 Assert
.IsFalse( resource
.Committed
);
113 Assert
.IsFalse( resource
.Rolledback
);
117 Assert
.IsTrue( resource
.Started
);
118 Assert
.IsFalse( resource
.Committed
);
119 Assert
.IsFalse( resource
.Rolledback
);
121 transaction
.Rollback();
123 Assert
.IsTrue( resource
.Started
);
124 Assert
.IsTrue( resource
.Rolledback
);
125 Assert
.IsFalse( resource
.Committed
);
129 [ExpectedException( typeof(TransactionException
) )]
130 public void InvalidBegin()
132 ITransaction transaction
= tm
.CreateTransaction(
133 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
140 [ExpectedException( typeof(TransactionException
) )]
141 public void InvalidCommit()
143 ITransaction transaction
= tm
.CreateTransaction(
144 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
147 transaction
.Rollback();
149 transaction
.Commit();
153 public void TransactionCreatedEvent()
155 bool transactionCreatedEventTriggered
= false;
157 tm
.TransactionCreated
+= delegate { transactionCreatedEventTriggered = true; }
;
159 Assert
.IsFalse(transactionCreatedEventTriggered
);
161 ITransaction transaction
= tm
.CreateTransaction(
162 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
164 Assert
.IsTrue(transactionCreatedEventTriggered
);
168 public void TransactionDisposedEvent()
170 bool transactionDisposedEventTriggered
= false;
172 tm
.TransactionDisposed
+= delegate { transactionDisposedEventTriggered = true; }
;
174 ITransaction transaction
= tm
.CreateTransaction(
175 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
177 Assert
.IsFalse(transactionDisposedEventTriggered
);
181 Assert
.IsFalse(transactionDisposedEventTriggered
);
183 transaction
.Commit();
185 Assert
.IsFalse(transactionDisposedEventTriggered
);
187 tm
.Dispose(transaction
);
189 Assert
.IsTrue(transactionDisposedEventTriggered
);
193 public void TransactionCommittedEvent()
195 bool transactionCommittedEventTriggered
= false;
196 bool transactionRolledBackEventTriggered
= false;
197 bool transactionFailedEventTriggered
= false;
199 tm
.TransactionCommitted
+= delegate { transactionCommittedEventTriggered = true; }
;
200 tm
.TransactionRolledback
+= delegate { transactionRolledBackEventTriggered = true; }
;
201 tm
.TransactionFailed
+= delegate { transactionFailedEventTriggered = true; }
;
203 ITransaction transaction
= tm
.CreateTransaction(
204 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
206 ResourceImpl resource
= new ResourceImpl();
208 transaction
.Enlist(resource
);
210 Assert
.IsFalse(transactionCommittedEventTriggered
);
211 Assert
.IsFalse(transactionRolledBackEventTriggered
);
212 Assert
.IsFalse(transactionFailedEventTriggered
);
216 Assert
.IsFalse(transactionCommittedEventTriggered
);
217 Assert
.IsFalse(transactionRolledBackEventTriggered
);
218 Assert
.IsFalse(transactionFailedEventTriggered
);
220 transaction
.Commit();
222 Assert
.IsTrue(transactionCommittedEventTriggered
);
223 Assert
.IsFalse(transactionRolledBackEventTriggered
);
224 Assert
.IsFalse(transactionFailedEventTriggered
);
228 public void TransactionRolledBackEvent()
230 bool transactionCommittedEventTriggered
= false;
231 bool transactionRolledBackEventTriggered
= false;
232 bool transactionFailedEventTriggered
= false;
234 tm
.TransactionCommitted
+= delegate { transactionCommittedEventTriggered = true; }
;
235 tm
.TransactionRolledback
+= delegate { transactionRolledBackEventTriggered = true; }
;
236 tm
.TransactionFailed
+= delegate { transactionFailedEventTriggered = true; }
;
238 ITransaction transaction
= tm
.CreateTransaction(
239 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
241 ResourceImpl resource
= new ResourceImpl();
243 transaction
.Enlist(resource
);
245 Assert
.IsFalse(transactionCommittedEventTriggered
);
246 Assert
.IsFalse(transactionRolledBackEventTriggered
);
247 Assert
.IsFalse(transactionFailedEventTriggered
);
251 Assert
.IsFalse(transactionCommittedEventTriggered
);
252 Assert
.IsFalse(transactionRolledBackEventTriggered
);
253 Assert
.IsFalse(transactionFailedEventTriggered
);
255 transaction
.Rollback();
257 Assert
.IsFalse(transactionCommittedEventTriggered
);
258 Assert
.IsTrue(transactionRolledBackEventTriggered
);
259 Assert
.IsFalse(transactionFailedEventTriggered
);
263 public void TransactionFailedOnCommitEvent()
265 bool transactionCommittedEventTriggered
= false;
266 bool transactionRolledBackEventTriggered
= false;
267 bool transactionFailedEventTriggered
= false;
269 tm
.TransactionCommitted
+= delegate { transactionCommittedEventTriggered = true; }
;
270 tm
.TransactionRolledback
+= delegate { transactionRolledBackEventTriggered = true; }
;
271 tm
.TransactionFailed
+= delegate { transactionFailedEventTriggered = true; }
;
273 ITransaction transaction
= tm
.CreateTransaction(
274 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
276 ResourceImpl resource
= new ThrowsExceptionResourceImpl(true, false);
278 transaction
.Enlist(resource
);
280 Assert
.IsFalse(transactionCommittedEventTriggered
);
281 Assert
.IsFalse(transactionRolledBackEventTriggered
);
282 Assert
.IsFalse(transactionFailedEventTriggered
);
286 Assert
.IsFalse(transactionCommittedEventTriggered
);
287 Assert
.IsFalse(transactionRolledBackEventTriggered
);
288 Assert
.IsFalse(transactionFailedEventTriggered
);
290 TransactionException exception
= null;
294 transaction
.Commit();
296 catch (TransactionException transactionError
)
298 exception
= transactionError
;
301 Assert
.IsFalse(transactionCommittedEventTriggered
);
302 Assert
.IsFalse(transactionRolledBackEventTriggered
);
303 Assert
.IsTrue(transactionFailedEventTriggered
);
305 Assert
.IsNotNull(exception
);
306 Assert
.IsInstanceOfType(typeof(CommitResourceException
), exception
);
310 public void TransactionFailedOnRollbackEvent()
312 bool transactionCommittedEventTriggered
= false;
313 bool transactionRolledBackEventTriggered
= false;
314 bool transactionFailedEventTriggered
= false;
316 tm
.TransactionCommitted
+= delegate { transactionCommittedEventTriggered = true; }
;
317 tm
.TransactionRolledback
+= delegate { transactionRolledBackEventTriggered = true; }
;
318 tm
.TransactionFailed
+= delegate { transactionFailedEventTriggered = true; }
;
320 ITransaction transaction
= tm
.CreateTransaction(
321 TransactionMode
.Requires
, IsolationMode
.Unspecified
);
323 ResourceImpl resource
= new ThrowsExceptionResourceImpl(false, true);
325 transaction
.Enlist(resource
);
327 Assert
.IsFalse(transactionCommittedEventTriggered
);
328 Assert
.IsFalse(transactionRolledBackEventTriggered
);
329 Assert
.IsFalse(transactionFailedEventTriggered
);
333 Assert
.IsFalse(transactionCommittedEventTriggered
);
334 Assert
.IsFalse(transactionRolledBackEventTriggered
);
335 Assert
.IsFalse(transactionFailedEventTriggered
);
337 TransactionException exception
= null;
341 transaction
.Rollback();
343 catch (TransactionException transactionError
)
345 exception
= transactionError
;
348 Assert
.IsFalse(transactionCommittedEventTriggered
);
349 Assert
.IsFalse(transactionRolledBackEventTriggered
);
350 Assert
.IsTrue(transactionFailedEventTriggered
);
352 Assert
.IsNotNull(exception
);
353 Assert
.IsInstanceOfType(typeof(RollbackResourceException
), exception
);