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 description for TransactionTest. /// [TestFixture] public class TransactionTest: BaseTest { #region SetUp & TearDown /// /// SetUp /// [SetUp] public void Init() { InitScript( sqlMap.DataSource, ScriptDirectory + "account-init.sql" ); } /// /// TearDown /// [TearDown] public void Dispose() { /* ... */ } #endregion #region Transaction tests /// /// Test BeginTransaction, CommitTransaction /// [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); } /// /// Test that nested BeginTransaction throw an exception /// [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); } /// /// Test that CommitTransaction without BeginTransaction trow an exception /// [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); } /// /// Test a RollBack Transaction. /// [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 /// /// Test usage of auto commit for an insert /// [Test] public void TestAutoCommitInsert() { Account account = NewAccount6(); sqlMap.Insert("InsertAccountViaParameterMap", account); account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 6); AssertAccount6(account); } /// /// Test usage of auto commit for a query /// [Test] public void TestAutoCommitQuery() { Account account = (Account) sqlMap.QueryForObject("GetAccountNullableEmail", 1); AssertAccount1(account); } #endregion } }