1 // Copyright 2004-2007 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();