AbstractPlatformTransactionManagerTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Transaction » Support » 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 » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Transaction » Support » AbstractPlatformTransactionManagerTests.cs
using System;
using NUnit.Framework;

namespace Spring.Transaction.Support{
  [TestFixture]
  public class AbstractPlatformTransactionManagerTests
  {
    private MockTxnPlatformMgrAbstract _mockTxnMgr;
    
    [SetUp]
    public void Init()
    {
      _mockTxnMgr = new MockTxnPlatformMgrAbstract();
      if ( TransactionSynchronizationManager.SynchronizationActive ) 
      {
        TransactionSynchronizationManager.ClearSynchronization();
      }
    }
    [TearDown]
    public void Destroy()
    {
      _mockTxnMgr.Verify();
      _mockTxnMgr = null;
      if ( TransactionSynchronizationManager.SynchronizationActive ) 
      {
        TransactionSynchronizationManager.ClearSynchronization();
      }
    }
    [Test]
    public void VanillaProperties()
    {
      Assert.AreEqual( TransactionSynchronizationState.Always, _mockTxnMgr.TransactionSynchronization);  
      Assert.IsTrue(!_mockTxnMgr.NestedTransactionsAllowed);
      Assert.IsTrue(!_mockTxnMgr.RollbackOnCommitFailure);
      
      _mockTxnMgr.NestedTransactionsAllowed = true;
      _mockTxnMgr.RollbackOnCommitFailure = true;
      _mockTxnMgr.TransactionSynchronization = TransactionSynchronizationState.OnActualTransaction;
      
      Assert.AreEqual( TransactionSynchronizationState.OnActualTransaction, _mockTxnMgr.TransactionSynchronization);  
      Assert.IsTrue(_mockTxnMgr.NestedTransactionsAllowed);
      Assert.IsTrue(_mockTxnMgr.RollbackOnCommitFailure);
    }
    [Test]
    [ExpectedException(typeof(InvalidTimeoutException), ExpectedMessage="Invalid transaction timeout")]
    public void DefinitionInvalidTimeoutException()
    {
      MockTxnDefinition def = new MockTxnDefinition();  
      def.TransactionTimeout = -1000;
      _mockTxnMgr.GetTransaction( def );
    }
    [Test]
    [ExpectedException(typeof(IllegalTransactionStateException), ExpectedMessage="Transaction propagation 'mandatory' but no existing transaction found")]
    public void DefinitionInvalidPropagationState()
    {
      MockTxnDefinition def = new MockTxnDefinition();  
      def.PropagationBehavior = TransactionPropagation.Mandatory;
      _mockTxnMgr.GetTransaction( def );
    }

    [Test]
    [ExpectedException(typeof(IllegalTransactionStateException), ExpectedMessage="Transaction propagation 'never' but existing transaction found.")]
    public void NeverPropagateState()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Never;
      setGeneralGetTransactionExpectations();
      _mockTxnMgr.GetTransaction(def);
    }
    [Test]
    [ExpectedException(typeof(NestedTransactionNotSupportedException), ExpectedMessage="Transaction manager does not allow nested transactions by default - specify 'NestedTransactionsAllowed' property with value 'true'")]
    public void NoNestedTransactionsAllowed()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Nested;
      setGeneralGetTransactionExpectations();
      _mockTxnMgr.GetTransaction(def);
    }
    [Test]
    public void TransactionSuspendedSuccessfully()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.NotSupported;
      def.ReadOnly = false;
      setGeneralGetTransactionExpectations();

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.IsNull( status.Transaction );
      Assert.IsTrue( !status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNotNull( status.SuspendedResources);
    }
    [Test]
    public void TransactionCreatedSuccessfully()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.RequiresNew;
      def.ReadOnly = false;

      setGeneralGetTransactionExpectations();

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.AreEqual( _mockTxnMgr.Transaction, status.Transaction );
      Assert.IsTrue( status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNotNull( status.SuspendedResources);
    }
    [Test]
    public void NestedTransactionSuccessfully()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Nested;
      def.ReadOnly = false;

      setGeneralGetTransactionExpectations();
      _mockTxnMgr.SetExpectedCalls( "DoBegin", 1 );
      _mockTxnMgr.Savepoints = false;
      _mockTxnMgr.NestedTransactionsAllowed = true;

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.AreEqual( _mockTxnMgr.Transaction, status.Transaction );
      Assert.IsTrue( status.IsNewTransaction );
      Assert.AreEqual( true, status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }

    [Test]
    public void NestedTransactionWithSavepoint()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Nested;
      def.ReadOnly = false;
      setVanillaGetTransactionExpectations();
      _mockTxnMgr.SetTransaction( new MyMockTxnObjectSavepointMgr());
      _mockTxnMgr.SetExpectedCalls("DoBegin", 0);
      _mockTxnMgr.Savepoints = true;
      _mockTxnMgr.NestedTransactionsAllowed = true;

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.AreEqual( _mockTxnMgr.Transaction, status.Transaction );
      Assert.IsFalse( status.IsNewTransaction );
      Assert.IsFalse( status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }
    [Test]
    public void DefaultPropagationBehavior()
    {
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Required;
      def.ReadOnly = true;
      setGeneralGetTransactionExpectations();

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.AreEqual( _mockTxnMgr.Transaction, status.Transaction );
      Assert.IsTrue( !status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }
    [Test]
    public void DefaultPropagationBehaviorWithNullDefinition()
    {
      setGeneralGetTransactionExpectations();

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(null);  
      Assert.AreEqual( _mockTxnMgr.Transaction, status.Transaction );
      Assert.IsTrue( !status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }
    [Test]
    public void DefaultNoExistingTransaction()
    {
      setVanillaGetTransactionExpectations();
      _mockTxnMgr.SetExpectedCalls( "DoBegin", 1 );

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(null);  
      Assert.IsNotNull( status.Transaction );
      Assert.IsTrue( status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( !status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }
    [Test]
    public void DefaultBehaviorDefaultPropagationNoExistingTransaction()
    {
      setVanillaGetTransactionExpectations();
      _mockTxnMgr.SetExpectedCalls( "DoBegin", 0 );
      
      MockTxnDefinition def = new MockTxnDefinition();
      def.PropagationBehavior = TransactionPropagation.Never;
      def.ReadOnly = true;

      DefaultTransactionStatus status = (DefaultTransactionStatus)_mockTxnMgr.GetTransaction(def);  
      Assert.IsNull( status.Transaction );
      Assert.IsTrue( !status.IsNewTransaction );
      Assert.IsTrue( status.NewSynchronization );
      Assert.IsTrue( status.ReadOnly );
      Assert.IsNull( status.SuspendedResources);
    }
    private void setGeneralGetTransactionExpectations()
    {
      _mockTxnMgr.SetTransaction( new object() );
      setVanillaGetTransactionExpectations();
    }

    private void setVanillaGetTransactionExpectations()
    {
      _mockTxnMgr.SetExpectedCalls( "DoGetTransaction", 1);
      _mockTxnMgr.SetExpectedCalls( "IsExistingTransaction", 1);
    }

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