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
.Facilities
.NHibernateIntegration
.Tests
.Transactions
19 using Castle
.Facilities
.AutomaticTransactionManagement
;
22 using NUnit
.Framework
;
25 public class TransactionsTestCase
: AbstractNHibernateTestCase
27 protected override void ConfigureContainer()
29 container
.AddFacility("transactions", new TransactionFacility());
31 container
.AddComponent("root", typeof(RootService
));
32 container
.AddComponent("myfirstdao", typeof(FirstDao
));
33 container
.AddComponent("myseconddao", typeof(SecondDao
));
37 public void TestTransaction()
39 ISessionManager sessionManager
= (ISessionManager
) container
[typeof(ISessionManager
)];
41 RootService service
= (RootService
) container
[typeof(RootService
)];
42 FirstDao dao
= (FirstDao
) container
[typeof(FirstDao
)];
44 Blog blog
= dao
.Create("Blog1");
48 service
.DoBlogRefOperation(blog
);
50 // Expects a constraint exception on Commit
52 Assert
.Fail("Must fail");
56 // transaction exception expected
61 // [Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
62 public void TransactionNotHijackingTheSession()
64 ISessionManager sessionManager
= (ISessionManager
)
65 container
[typeof(ISessionManager
)];
67 using(ISession session
= sessionManager
.OpenSession())
69 Assert
.IsFalse(session
.Transaction
.IsActive
);
71 FirstDao service
= (FirstDao
) container
["myfirstdao"];
73 // This call is transactional
74 Blog blog
= service
.Create();
76 // TODO: Assert transaction was committed
77 // Assert.IsTrue(session.Transaction.WasCommitted);
79 RootService rootService
= (RootService
) container
["root"];
81 Array blogs
= rootService
.FindAll(typeof(Blog
));
82 Assert
.AreEqual(1, blogs
.Length
);
87 // [Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
88 public void SessionBeingSharedByMultipleTransactionsInSequence()
90 ISessionManager sessionManager
= (ISessionManager
)
91 container
[typeof(ISessionManager
)];
93 using(ISession session
= sessionManager
.OpenSession())
95 Assert
.IsFalse(session
.Transaction
.IsActive
);
97 FirstDao service
= (FirstDao
) container
["myfirstdao"];
99 // This call is transactional
102 // TODO: Assert transaction was committed
103 // Assert.IsTrue(session.Transaction.WasCommitted);
105 // This call is transactional
106 service
.Create("ps2's blogs");
108 // TODO: Assert transaction was committed
109 // Assert.IsTrue(session.Transaction.WasCommitted);
111 // This call is transactional
112 service
.Create("game cube's blogs");
114 RootService rootService
= (RootService
) container
["root"];
116 Array blogs
= rootService
.FindAll(typeof(Blog
));
117 Assert
.AreEqual(3, blogs
.Length
);
122 // [Ignore("This doesn't work with the NH 1.2 transaction property, needs to be fixed")]
123 public void NonTransactionalRoot()
125 ISessionManager sessionManager
= (ISessionManager
)
126 container
[typeof(ISessionManager
)];
128 using(ISession session
= sessionManager
.OpenSession())
130 Assert
.IsFalse(session
.Transaction
.IsActive
);
132 FirstDao first
= (FirstDao
) container
["myfirstdao"];
133 SecondDao second
= (SecondDao
) container
["myseconddao"];
135 // This call is transactional
136 Blog blog
= first
.Create();
138 // TODO: Assert transaction was committed
139 // Assert.IsTrue(session.Transaction.WasCommitted);
143 second
.CreateWithException2(blog
);
150 // TODO: Assert transaction was rolledback
151 // Assert.IsTrue(session.Transaction.WasRolledBack);
153 RootService rootService
= (RootService
) container
["root"];
155 Array blogs
= rootService
.FindAll(typeof(Blog
));
156 Assert
.AreEqual(1, blogs
.Length
);
157 Array blogitems
= rootService
.FindAll(typeof(BlogItem
));
158 Assert
.IsEmpty(blogitems
);
163 public void SimpleAndSucessfulSituationUsingRootTransactionBoundary()
165 RootService service
= (RootService
) container
["root"];
167 service
.SuccessFullCall();
169 Array blogs
= service
.FindAll(typeof(Blog
));
170 Array blogitems
= service
.FindAll(typeof(BlogItem
));
172 Assert
.IsNotNull(blogs
);
173 Assert
.IsNotNull(blogitems
);
174 Assert
.AreEqual(1, blogs
.Length
);
175 Assert
.AreEqual(1, blogitems
.Length
);
179 public void CallWithException()
181 RootService service
= (RootService
) container
["root"];
185 service
.CallWithException();
191 // Ensure rollback happened
193 Array blogs
= service
.FindAll(typeof(Blog
));
194 Array blogitems
= service
.FindAll(typeof(BlogItem
));
196 Assert
.IsEmpty(blogs
);
197 Assert
.IsEmpty(blogitems
);
201 public void CallWithException2()
203 RootService service
= (RootService
) container
["root"];
207 service
.CallWithException2();
213 // Ensure rollback happened
215 Array blogs
= service
.FindAll(typeof(Blog
));
216 Array blogitems
= service
.FindAll(typeof(BlogItem
));
218 Assert
.IsEmpty(blogs
);
219 Assert
.IsEmpty(blogitems
);