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 NestedTransactionsTestCase
24 private DefaultTransactionManager tm
;
29 tm
= new DefaultTransactionManager(new TransientActivityManager());
33 public void NestedRequiresWithCommits()
35 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
36 Assert
.IsTrue( root
is StandardTransaction
);
39 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
40 Assert
.IsTrue( child1
is ChildTransaction
);
43 ITransaction child2
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
44 Assert
.IsTrue( child2
is ChildTransaction
);
53 public void NestedRequiresAndRequiresNew()
55 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
56 Assert
.IsTrue( root
is StandardTransaction
);
59 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
60 Assert
.IsTrue( child1
is ChildTransaction
);
63 ITransaction innerRoot
= tm
.CreateTransaction( TransactionMode
.RequiresNew
, IsolationMode
.Unspecified
);
64 Assert
.IsFalse( innerRoot
is ChildTransaction
);
67 ITransaction child2
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
68 Assert
.IsTrue( child2
is ChildTransaction
);
79 public void SameResources()
81 ResourceImpl resource
= new ResourceImpl();
83 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
85 root
.Enlist(resource
);
87 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
88 Assert
.IsTrue( child1
is ChildTransaction
);
89 child1
.Enlist(resource
);
97 public void NotSupportedAndNoActiveTransaction()
99 ITransaction root
= tm
.CreateTransaction( TransactionMode
.NotSupported
, IsolationMode
.Unspecified
);
100 Assert
.IsNull( root
);
104 [ExpectedException( typeof(TransactionException
) )]
105 public void NotSupportedAndActiveTransaction()
107 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
110 tm
.CreateTransaction( TransactionMode
.NotSupported
, IsolationMode
.Unspecified
);
114 [ExpectedException( typeof(TransactionException
) )]
115 public void NestedRollback()
117 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
120 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
123 ITransaction child2
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
128 root
.Commit(); // Can't perform
132 [ExpectedException( typeof(ArgumentException
) )]
133 public void InvalidDispose1()
135 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
138 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
141 ITransaction child2
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
148 [ExpectedException( typeof(ArgumentException
) )]
149 public void InvalidDispose2()
151 ITransaction root
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
154 ITransaction child1
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);
157 ITransaction child2
= tm
.CreateTransaction( TransactionMode
.Requires
, IsolationMode
.Unspecified
);