MutipleDaoTest.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » DataAccess » Test » NUnit » DaoTests » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » iBATIS.NET 
iBATIS.NET » IBatisNet » DataAccess » Test » NUnit » DaoTests » MutipleDaoTest.cs
using System;
using System.Configuration;
using IBatisNet.Common.Transaction;
using IBatisNet.DataAccess.Configuration;
using IBatisNet.DataAccess.Test.Dao.Interfaces;
using IBatisNet.DataAccess.Test.Domain;
using NUnit.Framework;

namespace IBatisNet.DataAccess.Test.NUnit.DaoTests{
  /// <summary>
  /// Summary description for MutipleDaoTest.
  /// </summary>
  [TestFixture] 
  public class MutipleDaoTest: BaseDaoTest
  {
    /// <summary>
    /// An other daoManager
    /// </summary>
    protected DaoManager daoManager2 = null;

    /// <summary>
    /// Initialisation
    /// </summary>
    [SetUp] 
    public void SetUp() 
    {
      DomDaoManagerBuilder builder = new DomDaoManagerBuilder();
      builder.Configure( "dao"+ "_" + ConfigurationSettings.AppSettings["database"] + "_"
        + ConfigurationSettings.AppSettings["providerType"] + ".config" );

      daoManager = DaoManager.GetInstance("SqlMapDao");
      daoManager2 = DaoManager.GetInstance("NHibernateDao");
      
      InitScript( daoManager.LocalDataSource, ScriptDirectory + "account-init.sql" );
      InitScript( daoManager2.LocalDataSource, ScriptDirectory + "user-init.sql" );
    }


    /// <summary>
    /// Test Create user
    /// </summary>
    [Test] 
    [Category("NHibernate")]
    public void TestCreateUser () 
    {
      IUserDao userDao = (IUserDao)daoManager2[typeof(IUserDao)];

      User newUser = new User();
      newUser.Id = "joe_cool";
      newUser.UserName = "Joseph Cool";
      newUser.Password = "abc123";
      newUser.EmailAddress = "joe@cool.com";
      newUser.LastLogon = DateTime.Now;

      try
      {
        daoManager2.OpenConnection();
        userDao.Create(newUser);
      }
      catch(Exception e)
      {
        // Ignore
        Console.WriteLine("TestCreateUser, error cause : "+e.Message);
      }
      finally
      {
        daoManager2.CloseConnection();
      }

      DateTime stamp = DateTime.Now;
      User joeCool = null;
      try
      {
        // open another session to retrieve the just inserted user
        daoManager2.OpenConnection();

        //The User object you get back is live! 
        joeCool = userDao.Load("joe_cool");

        Assert.IsNotNull(joeCool);
        Assert.AreEqual("Joseph Cool", joeCool.UserName);
      
        //Change its properties and it will get persisted to the database on Close. 
        // set Joe Cool's Last Login property
        joeCool.LastLogon = stamp;      
      }
      catch(Exception e)
      {
        // Ignore
        Console.WriteLine("TestCreateUser, error cause : "+e.Message);
      }
      finally
      {
        // flush the changes from the Session to the Database
        daoManager2.CloseConnection();
      }

      daoManager2.OpenConnection();
      //The User object you get back is live! 
      joeCool = userDao.Load("joe_cool");
      daoManager2.CloseConnection();

      Assert.IsNotNull(joeCool);
      Assert.AreEqual("Joseph Cool", joeCool.UserName);
      Assert.AreEqual(stamp.ToString(), joeCool.LastLogon.ToString());
    }


    /// <summary>
    /// Test mutiple dao in TransactionScope
    /// </summary>
    [Test] 
    [Category("MTS")]
    [Category("NHibernate")]
    public void TestUsingTransactionScope () 
    {
      Account account = NewAccount();
      IAccountDao accountDao = daoManager[typeof(IAccountDao)] as IAccountDao;
      IUserDao userDao = daoManager2[typeof(IUserDao)] as IUserDao;
      DateTime stamp = DateTime.Now.AddDays(2);
      User joeCool = null;

      daoManager.OpenConnection();
      accountDao.Create(account);
      daoManager.CloseConnection();

      User newUser = new User();
      newUser.Id = "joe_cool";
      newUser.UserName = "Joseph Cool";
      newUser.Password = "abc123";
      newUser.EmailAddress = "joe@cool.com";
      newUser.LastLogon = DateTime.Now;

      daoManager2.OpenConnection();
      userDao.Create(newUser);
      daoManager2.CloseConnection();

      using (TransactionScope tx = new TransactionScope())
      {
        daoManager.OpenConnection();
        account = accountDao.GetAccountById(1001);
        account.FirstName = "TestTransactionScope";
        accountDao.Update(account);
        daoManager.CloseConnection();

        daoManager2.OpenConnection();
        joeCool = userDao.Load("joe_cool");
        joeCool.LastLogon = stamp;
        daoManager2.CloseConnection();

        //tx.Complete(); // not call complte --> RollBack
      }

      //----------------------------------------
      daoManager.OpenConnection();
      account = accountDao.GetAccountById(1001);
      daoManager.CloseConnection();      
      
      Assert.IsNotNull(account);
      Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress);
      Assert.IsFalse( "TestTransactionScope"==account.FirstName );

      //----------------
      daoManager2.OpenConnection();
      joeCool = userDao.Load("joe_cool");
      daoManager2.CloseConnection();

      Assert.IsNotNull(joeCool);
      Assert.AreEqual("Joseph Cool", joeCool.UserName);
      Assert.IsFalse( stamp.ToString()==joeCool.LastLogon.ToString() );
    }



    /// <summary>
    /// Test Create user
    /// </summary>
    [Test] 
    [Category("NHibernate")]
    public void TestNestedDao()
    {
      IAccountDao accountDao = daoManager[typeof(IAccountDao)] as IAccountDao;
      IUserDao userDao = daoManager2[typeof(IUserDao)] as IUserDao;
      DateTime stamp = DateTime.Now.AddDays(2);
      User joeCool = null;
      User newUser = new User();
      Account account = NewAccount();

      newUser.Id = "joe_cool";
      newUser.UserName = "Joseph Cool";
      newUser.Password = "abc123";
      newUser.EmailAddress = "joe@cool.com";
      newUser.LastLogon = DateTime.Now;

      daoManager.OpenConnection();
      daoManager2.OpenConnection();
      accountDao.Create(account);
      userDao.Create(newUser);
      daoManager.CloseConnection();
      daoManager2.CloseConnection();

      account = null;
      daoManager.OpenConnection();
      account = accountDao.GetAccountById(1001);
      daoManager.CloseConnection();
      Assert.IsNotNull(account);
      Assert.AreEqual("Calamity.Jane@somewhere.com", account.EmailAddress);

      daoManager2.OpenConnection();
      joeCool = userDao.Load("joe_cool");
      daoManager2.CloseConnection();

      Assert.IsNotNull(joeCool);
      Assert.AreEqual("Joseph Cool", joeCool.UserName);
    }

  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.