3 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 // This facility was a contribution kindly
20 // donated by Gilles Bayon <gilles.bayon@gmail.com>
26 namespace Castle
.Facilities
.IBatisNetIntegration
.Tests
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
;
36 public class DaoTestCase
: AbstractIBatisNetTestCase
38 private ManualResetEvent _startEvent
= new ManualResetEvent(false);
39 private ManualResetEvent _stopEvent
= new ManualResetEvent(false);
40 private IAccountDao _dao
= null;
43 public void CommonUsage()
45 container
= CreateConfiguredContainer();
46 container
.AddFacility("IBatisNet", new IBatisNetFacility());
48 container
.AddComponent("AccountDao", typeof (IAccountDao
), typeof (AccountDao
));
52 IAccountDao dao
= container
["AccountDao"] as AccountDao
;
53 Account account
= new Account();
55 account
.EmailAddress
= "ibatis@somewhere.com";
56 account
.FirstName
= "Gilles";
57 account
.LastName
= "Bayon";
59 dao
.InsertAccount(account
);
61 account
= dao
.GetAccount(99) as Account
;
63 Assert
.IsNotNull(account
);
64 Assert
.AreEqual(99, account
.Id
, "account.Id");
68 public void CommonUsageMultithread()
70 container
= CreateConfiguredContainer();
71 container
.AddFacility("IBatisNet", new IBatisNetFacility());
73 container
.AddComponent("AccountDao", typeof (IAccountDao
), typeof (AccountDao
));
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
));
91 Thread
.CurrentThread
.Join(1*2000);
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();
106 account
.EmailAddress
= "ibatis@somewhere.com";
107 account
.FirstName
= "Gilles";
108 account
.LastName
= "Bayon";
110 _dao
.InsertAccount(account
);
112 account
= _dao
.GetAccount(id
);
113 Assert
.IsNotNull(account
);
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
));
140 Thread
.CurrentThread
.Join(1*2000);