TransactionTest.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » Common » Test » NUnit » CommonTests » Transaction » 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 » Common » Test » NUnit » CommonTests » Transaction » TransactionTest.cs

using System;

using IBatisNet.Common.Transaction;

using IBatisNet.Common.Test.Domain;

using NUnit.Framework;

namespace IBatisNet.Common.Test.NUnit.CommonTests.Transaction{
  /// <summary>
  /// Summary description for TransactionTest.
  /// </summary>
  [TestFixture] 
  [Category("MTS")]
  public class TransactionTest : BaseTest
  {
    #region SetUp & TearDown
    /// <summary>
    /// SetUp
    /// </summary>
    [SetUp] 
    public void SetUp() 
    {
      InitSqlMap();
      InitScript( sqlMap.DataSource, ScriptDirectory + "account-init.sql" );
    }

    /// <summary>
    /// TearDown
    /// </summary>
    [TearDown] 
    public void Dispose()
    { 
    } 
    #endregion

    #region Transaction Tests
    /// <summary>
    /// Test Simple call to TransactionScope with a commit
    /// </summary>
    [Test] 
    public void SimpleTransaction() 
    {
      Account account = null;

      using (TransactionScope tx = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName ="transaction changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        tx.Complete(); // Commit
      }

      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("transaction changed", account.FirstName);
    }

    /// <summary>
    /// Test Simple call to TransactionScope with a roolback
    /// </summary>
    [Test] 
    public void SimpleRollback() 
    {
      Account account = null;

      using (TransactionScope tx = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName ="transaction changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        //tx.Complete(); // RollBack
      }

      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("Joe", account.FirstName);
    }

    /// <summary>
    /// Test Nested TransactionScope
    /// </summary>
    [Test] 
    public void NestedTransactionScope()
    {
      Account account = null;

      using (TransactionScope tx1 = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName ="transaction1 changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        account = null;

        using (TransactionScope tx2 = new TransactionScope())
        {
          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
          Assert.AreEqual("transaction1 changed", account.FirstName);
        
          account.FirstName ="transaction2 changed";
          sqlMap.Update("UpdateAccountViaParameterMap", account);

          tx2.Complete(); // Commit
        }

        tx1.Complete(); // Commit
      }
      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("transaction2 changed", account.FirstName);

    }

    /// <summary>
    /// Test Nested TransactionScope
    /// </summary>
    [Test] 
    public void NestedCommitWithRollBack()
    {
      Account account = null;

      using (TransactionScope tx1 = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName = "transaction1 changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        account = null;

        using (TransactionScope tx2 = new TransactionScope())
        {
          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
          Assert.AreEqual("transaction1 changed", account.FirstName);
        
          account.FirstName = "transaction2 changed";
          sqlMap.Update("UpdateAccountViaParameterMap", account);

          //tx2.Complete(); // RollBack
        }

        tx1.Complete(); // Commit

        Assert.AreEqual(false, tx1.IsVoteCommit);
      }
      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("Joe", account.FirstName);
    }

    /// <summary>
    /// Test Nested TransactionScope
    /// </summary>
    [Test] 
    public void NestedRollbackWithCommit()
    {
      Account account = null;

      using (TransactionScope tx1 = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName = "transaction1 changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        account = null;

        using (TransactionScope tx2 = new TransactionScope())
        {
          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
          Assert.AreEqual("transaction1 changed", account.FirstName);
        
          account.FirstName = "transaction2 changed";
          sqlMap.Update("UpdateAccountViaParameterMap", account);

          tx2.Complete(); // Commit

          Assert.AreEqual(true, tx2.IsVoteCommit);
        }

        //tx1.Complete(); // RollBack 
      }
      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("Joe", account.FirstName);
    }

    [Test]
    public void NestedRollbacks() 
    {
      Account account = null;

      using (TransactionScope tx1 = new TransactionScope())
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName = "transaction1 changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        account = null;

        using (TransactionScope tx2 = new TransactionScope())
        {
          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
          Assert.AreEqual("transaction1 changed", account.FirstName);
        
          account.FirstName = "transaction2 changed";
          sqlMap.Update("UpdateAccountViaParameterMap", account);

          //tx2.Complete(); // RollBack
        }

        //tx1.Complete(); // RollBack 

        Assert.AreEqual(false, tx1.IsVoteCommit);
      }
      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("Joe", account.FirstName);
    }

    [Test]
    public void NestedTransactionScopeWithDifferentOption() 
    {
      Account account = null;

      using (TransactionScope tx1 = new TransactionScope(TransactionScopeOptions.Required))
      {
        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
        AssertAccount1(account);
        
        account.FirstName = "transaction1 changed";
        sqlMap.Update("UpdateAccountViaParameterMap", account);

        account = null;

        using (TransactionScope tx2 = new TransactionScope(TransactionScopeOptions.RequiresNew))
        {
          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
          Assert.AreEqual("transaction1 changed", account.FirstName);
        
          account.FirstName = "transaction2 changed";
          sqlMap.Update("UpdateAccountViaParameterMap", account);

          //tx2.Complete(); // RollBack, rem tx1 will be rollback
        }

        tx1.Complete(); 

        Assert.AreEqual(false, tx1.IsVoteCommit);
      }
      account = null;
      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;

      Assert.AreEqual("Joe", account.FirstName);
    }

//    [Test]
//    public void NestedTransactionScopeWithDifferentOption2() 
//    {
//      Account account = null;
//
//      using (TransactionScope tx1 = new TransactionScope(TransactionScopeOptions.Required))
//      {
//        account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
//        AssertAccount1(account);
//        
//        account.FirstName = "transaction1 changed";
//        sqlMap.Update("UpdateAccountViaParameterMap", account);
//
//        account = null;
//
//        using (TransactionScope tx2 = new TransactionScope(TransactionScopeOptions.RequiresNew))
//        {
//          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
//          Assert.AreEqual("Joe", account.FirstName);
//        
//          account.FirstName = "transaction2 changed";
//          sqlMap.Update("UpdateAccountViaParameterMap", account);
//
//          tx2.Consistent = false; // RollBack
//
//          Assert.AreEqual(false, tx2.IsVoteCommit);
//        }
//
//        using (TransactionScope tx2 = new TransactionScope(TransactionScopeOptions.RequiresNew))
//        {
//          account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
//          Assert.AreEqual("Joe", account.FirstName);
//        
//          tx2.Consistent = false; // RollBack
//
//          Assert.AreEqual(false, tx2.IsVoteCommit);
//        }
//
//        tx1.Consistent = false; // RollBack 
//
//        Assert.AreEqual(false, tx1.IsVoteCommit);
//      }
//      account = null;
//      account = sqlMap.QueryForObject("GetAccountViaColumnName", 1) as Account;
//
//      Assert.AreEqual("Joe", account.FirstName);
//    }
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.