TransactionTest.cs :  » Persistence-Frameworks » iBATIS.NET » IBatisNet » DataMapper » Test » NUnit » SqlMapTests » 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 » DataMapper » Test » NUnit » SqlMapTests » TransactionTest.cs
using System;
using System.Collections;
using System.Configuration;

using NUnit.Framework;

using IBatisNet.DataMapper.Exceptions;

using IBatisNet.DataMapper.Test;
using IBatisNet.DataMapper.Test.Domain;

namespace IBatisNet.DataMapper.Test.NUnit.SqlMapTests{
  /// <summary>
  /// Summary description for TransactionTest.
  /// </summary>
  [TestFixture] 
  public class TransactionTest: BaseTest
  {

    #region SetUp & TearDown

    /// <summary>
    /// SetUp 
    /// </summary>
    [SetUp] 
    public void Init() 
    {
      InitScript( sqlMap.DataSource, ScriptDirectory + "account-init.sql" );
    }

    /// <summary>
    /// TearDown
    /// </summary>
    [TearDown] 
    public void Dispose()
    { /* ... */ } 

    #endregion

    #region Transaction tests

        /// <summary>
        /// Test IsTransactionStart
        /// </summary>
        [Test]
        public void TestIsTransactionStartProperty()
        {
            Account account = NewAccount6();

            sqlMap.BeginTransaction();
            sqlMap.Insert("InsertAccountViaParameterMap", account);
            InsertNewAccount();
            sqlMap.CommitTransaction();

        }

        public void InsertNewAccount()
        {
            Account account = NewAccount6();
            account.Id = 7;
            bool existingTransaction = (sqlMap.LocalSession != null && !sqlMap.LocalSession.IsTransactionStart);

            if (existingTransaction)
            {
                 sqlMap.BeginTransaction();
            }
            sqlMap.Insert("InsertAccountViaParameterMap", account);
            if (existingTransaction)
            {
                sqlMap.CommitTransaction();
            }
        }

    /// <summary>
    /// Test BeginTransaction, CommitTransaction
    /// </summary>
    [Test] 
    public void TestBeginCommitTransaction() 
    {
      Account account = NewAccount6();

      try 
      {
        sqlMap.BeginTransaction();
        sqlMap.Insert("InsertAccountViaParameterMap", account);
        sqlMap.CommitTransaction();
      } 
      finally 
      {
      }

      // This will use autocommit...
      account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6);

      AssertAccount6(account);
    }

    /// <summary>
    /// Test that nested BeginTransaction throw an exception
    /// </summary>
    [Test] 
    public void TestTransactionAlreadyStarted() 
    {
      Account account = NewAccount6();
      bool exceptionThrownAsExpected = false;

      try 
      {
        sqlMap.BeginTransaction();
        sqlMap.Insert("InsertAccountViaParameterMap", account);

        try 
        {
          sqlMap.BeginTransaction(); // transaction already started
        } 
        catch (DataMapperException e) 
        {
          exceptionThrownAsExpected = true;
          Console.WriteLine ( "Test TransactionAlreadyStarted " + e.Message );
        }
        sqlMap.CommitTransaction();
      } 
      finally 
      {
      }

      // This will use autocommit...
      account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6);
      AssertAccount6(account);
      Assert.IsTrue(exceptionThrownAsExpected);
    }

    /// <summary>
    /// Test that CommitTransaction without BeginTransaction trow an exception
    /// </summary>
    [Test]
    public void TestNoTransactionStarted() 
    {
      Account account = NewAccount6();
      bool exceptionThrownAsExpected = false;

      sqlMap.Insert("InsertAccountViaParameterMap", account);

      try 
      {
        sqlMap.CommitTransaction(); // No transaction started
      } 
      catch (DataMapperException e) 
      {
        exceptionThrownAsExpected = true;
        Console.WriteLine ( "Test NoTransactionStarted " + e.Message );
      }

      // This will use autocommit...
      Assert.IsTrue(exceptionThrownAsExpected);
      account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6);
      AssertAccount6(account);
    }

    /// <summary>
    /// Test a RollBack Transaction.
    /// </summary>
    [Test] 
    public void TestBeginRollbackTransaction() 
    {
      Account account = NewAccount6();

      try 
      {
        sqlMap.BeginTransaction();
        sqlMap.Insert("InsertAccountViaParameterMap", account);
      } 
      finally 
      {
        sqlMap.RollBackTransaction();
      }

      // This will use autocommit...
      account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6);
      Assert.IsNull(account);
    }

    #endregion

    #region AutoCommit tests

    /// <summary>
    /// Test usage of auto commit for an insert
    /// </summary>
    [Test] 
    public void TestAutoCommitInsert() 
    {
      Account account = NewAccount6();

      sqlMap.Insert("InsertAccountViaParameterMap", account);

      account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6);
      AssertAccount6(account);
    }

    /// <summary>
    /// Test usage of auto commit for a query
    /// </summary>
    [Test] 
    public void TestAutoCommitQuery() 
    {
      Account account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 1);

      AssertAccount1(account);
    }

    #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.