// DataSource definition // ScriptRunner definition using System; using System.Configuration; using System.IO; using System.Threading; using IBatisNet.Common; using IBatisNet.Common.Utilities; using IBatisNet.DataAccess.Test.Dao.Interfaces; using IBatisNet.DataAccess.Test.Domain; using NUnit.Framework; namespace IBatisNet.DataAccess.Test.NUnit.DaoTests { /// /// Summary description for BaseDaoTest. /// [TestFixture] public abstract class BaseDaoTest { /// /// A daoManager /// protected static DaoManager daoManager = null; protected static string ScriptDirectory = null; private ManualResetEvent _startEvent = new ManualResetEvent(false); private ManualResetEvent _stopEvent = new ManualResetEvent(false); /// /// Constructor /// static BaseDaoTest() { ScriptDirectory = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Resources.ApplicationBase, ".."), ".."), "Scripts"), ConfigurationSettings.AppSettings["database"]) + Path.DirectorySeparatorChar; } /// /// Run a sql batch for the datasource. /// /// The datasource. /// The sql batch protected static void InitScript(DataSource datasource, string script) { ScriptRunner runner = new ScriptRunner(); runner.RunScript(datasource, script); } #region Dao statement tests /// /// Verify that DaoManager.GetDao("Account") /// return an object that implemetent the interface IAccountDao. /// [Test] public void TestGetDao() { Type type = typeof (IAccountDao); IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Assert.IsNotNull(accountDao); Assert.IsTrue(type.IsInstanceOfType(accountDao)); } /// /// Test Open connection with a connection string /// [Test] public void TestOpenConnection() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Account account = NewAccount(); try { daoManager.OpenConnection(daoManager.LocalDataSource.ConnectionString); accountDao.Create(account); account = accountDao.GetAccountById(1001); } catch (Exception e) { // Ignore Console.WriteLine("TestCreateAccount, error cause : " + e.Message); } finally { daoManager.CloseConnection(); } Assert.IsNotNull(account); Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress); } /// /// Test CreateAccount /// [Test] public void TestCreateAccount() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Account account = NewAccount(); try { daoManager.OpenConnection(); accountDao.Create(account); account = accountDao.GetAccountById(1001); } catch (Exception e) { // Ignore Console.WriteLine("TestCreateAccount, error cause : " + e.Message); } finally { daoManager.CloseConnection(); } Assert.IsNotNull(account); Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress); } /// /// Test CreateAccount /// [Test] public void TestCreateAccountExplicitOpenSession() { IAccountDao accountDao = daoManager[typeof (IAccountDao)] as IAccountDao; Account account = NewAccount(); try { accountDao.Create(account); account = accountDao.GetAccountById(1001); } catch (Exception e) { // Ignore Console.WriteLine("TestCreateAccount, error cause : " + e.Message); } finally { } Assert.IsNotNull(account); Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress); } /// /// Test Transaction Rollback /// [Test] public void TestTransactionRollback() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); Account account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); account2.EmailAddress = "someotherAddress@somewhere.com"; try { daoManager.BeginTransaction(); accountDao.Create(account); accountDao.Update(account2); throw new Exception("BOOM!"); //daoManager.CommitTransaction(); } catch { daoManager.RollBackTransaction(); } finally { } daoManager.OpenConnection(); account = accountDao.GetAccountById(account.Id); account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); Assert.IsNull(account); Assert.AreEqual("Joe.Dalton@somewhere.com", account2.EmailAddress); } /// /// Test Transaction Commit /// [Test] public void TestTransactionCommit() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); Account account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); account2.EmailAddress = "someotherAddress@somewhere.com"; try { daoManager.BeginTransaction(); accountDao.Create(account); accountDao.Update(account2); daoManager.CommitTransaction(); } finally { // Nothing } daoManager.OpenConnection(); account = accountDao.GetAccountById(account.Id); account2 = accountDao.GetAccountById(1); daoManager.CloseConnection(); Assert.IsNotNull(account); Assert.AreEqual("someotherAddress@somewhere.com", account2.EmailAddress); } /// /// Test Delete /// [Test] public void TestDeleteAccount() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; Account account = NewAccount(); daoManager.OpenConnection(); accountDao.Create(account); account = accountDao.GetAccountById(1001); Assert.IsNotNull(account); Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress); accountDao.Delete(account); account = accountDao.GetAccountById(1001); Assert.IsNull(account); daoManager.CloseConnection(); } /// /// Test Using syntax on daoManager.OpenConnection /// [Test] public void TestUsingConnection() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; using (IDalSession session = daoManager.OpenConnection()) { Account account = NewAccount(); accountDao.Create(account); } // compiler will call Dispose on DaoSession } /// /// Test Test Using syntax on daoManager.BeginTransaction /// [Test] public void TestUsingTransaction() { IAccountDao accountDao = (IAccountDao) daoManager[typeof (IAccountDao)]; using (IDalSession session = daoManager.BeginTransaction()) { Account account = NewAccount(); Account account2 = accountDao.GetAccountById(1); account2.EmailAddress = "someotherAddress@somewhere.com"; accountDao.Create(account); accountDao.Update(account2); session.Complete(); // Commit } // compiler will call Dispose on IDalSession } #endregion #region Thread test [Test] public void TestCommonUsageMultiThread() { const int threadCount = 10; Thread[] threads = new Thread[threadCount]; for (int i = 0; i < threadCount; i++) { threads[i] = new Thread(new ThreadStart(ExecuteMethodUntilSignal)); threads[i].Start(); } _startEvent.Set(); Thread.CurrentThread.Join(1*2000); _stopEvent.Set(); } public void ExecuteMethodUntilSignal() { _startEvent.WaitOne(int.MaxValue, false); IAccountDao accountDao = daoManager[typeof (IAccountDao)] as IAccountDao; while (!_stopEvent.WaitOne(1, false)) { Assert.IsFalse(daoManager.IsDaoSessionStarted()); Account account = account = accountDao.GetAccountById(1); Assert.IsFalse(daoManager.IsDaoSessionStarted()); Assert.AreEqual(1, account.Id, "account.Id"); Assert.AreEqual("Joe", account.FirstName, "account.FirstName"); Assert.AreEqual("Dalton", account.LastName, "account.LastName"); } } #endregion /// /// Create a new account with id = 1001 /// /// An account protected Account NewAccount() { Account account = new Account(); account.Id = 1001; account.FirstName = "Calamity"; account.LastName = "Jane"; account.EmailAddress = "Calamity.Jane@somewhere.com"; return account; } } }