Applied patch from Jan Limpens 'ReflectionBasedDictionaryAdapter needs to check if...
[castle.git] / Facilities / IBatisNet / Castle.Facilities.IBatisNetIntegration.Tests / DaoTestCase.cs
blob0d0b7babace3aedf2d9685cb41456e5e3ccd63ec
1 #region License
3 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 // --
18 //
19 // This facility was a contribution kindly
20 // donated by Gilles Bayon <gilles.bayon@gmail.com>
21 //
22 // --
24 #endregion
26 namespace Castle.Facilities.IBatisNetIntegration.Tests
28 using System;
29 using System.Threading;
30 using Castle.Facilities.AutomaticTransactionManagement;
31 using Castle.Facilities.IBatisNetIntegration.Tests.Dao;
32 using Castle.Facilities.IBatisNetIntegration.Tests.Domain;
33 using NUnit.Framework;
35 [TestFixture]
36 public class DaoTestCase : AbstractIBatisNetTestCase
38 private ManualResetEvent _startEvent = new ManualResetEvent(false);
39 private ManualResetEvent _stopEvent = new ManualResetEvent(false);
40 private IAccountDao _dao = null;
42 [Test]
43 public void CommonUsage()
45 container = CreateConfiguredContainer();
46 container.AddFacility("IBatisNet", new IBatisNetFacility());
48 container.AddComponent("AccountDao", typeof (IAccountDao), typeof (AccountDao));
50 ResetDatabase();
52 IAccountDao dao = container["AccountDao"] as AccountDao;
53 Account account = new Account();
54 account.Id = 99;
55 account.EmailAddress = "ibatis@somewhere.com";
56 account.FirstName = "Gilles";
57 account.LastName = "Bayon";
59 dao.InsertAccount(account);
60 account = null;
61 account = dao.GetAccount(99) as Account;
63 Assert.IsNotNull(account);
64 Assert.AreEqual(99, account.Id, "account.Id");
67 [Test]
68 public void CommonUsageMultithread()
70 container = CreateConfiguredContainer();
71 container.AddFacility("IBatisNet", new IBatisNetFacility());
73 container.AddComponent("AccountDao", typeof (IAccountDao), typeof (AccountDao));
75 ResetDatabase();
77 _dao = container["AccountDao"] as AccountDao;
79 const int threadCount = 10;
81 Thread[] threads = new Thread[threadCount];
83 for (int i = 0; i < threadCount; i++)
85 threads[i] = new Thread(new ThreadStart(ExecuteMethodUntilSignal));
86 threads[i].Start();
89 _startEvent.Set();
91 Thread.CurrentThread.Join(1*2000);
93 _stopEvent.Set();
96 public void ExecuteMethodUntilSignal()
98 Random _random = new Random();
99 _startEvent.WaitOne(int.MaxValue, false);
101 while (!_stopEvent.WaitOne(1, false))
103 int id = _random.Next();
104 Account account = new Account();
105 account.Id = id;
106 account.EmailAddress = "ibatis@somewhere.com";
107 account.FirstName = "Gilles";
108 account.LastName = "Bayon";
110 _dao.InsertAccount(account);
111 account = null;
112 account = _dao.GetAccount(id);
113 Assert.IsNotNull(account);
117 [Test]
118 public void TransactionalUsageMultithread()
120 container = CreateConfiguredContainer();
121 container.AddFacility("IBatisNet", new IBatisNetFacility());
122 container.AddFacility("transaction", new TransactionFacility());
124 container.AddComponent("AccountDao", typeof (IAccountDao), typeof (AccountDao));
126 _dao = container["AccountDao"] as AccountDao;
128 const int threadCount = 10;
130 Thread[] threads = new Thread[threadCount];
132 for (int i = 0; i < threadCount; i++)
134 threads[i] = new Thread(new ThreadStart(ExecuteMethodUntilSignal));
135 threads[i].Start();
138 _startEvent.Set();
140 Thread.CurrentThread.Join(1*2000);
142 _stopEvent.Set();