Hi Henrik,

I'm looking forward to check/test the file transaction support that
you added to the services transaction, but as Roelof noticed, seems
that you forgot the test cases. Can you recreate a new patch file,
with the test cases?

Cheers,
Henry Conceição



On Tue, Jun 16, 2009 at 6:42 AM, Roelof Blom<[email protected]> wrote:
> Please attach the patch as a file.
>
> On Tue, Jun 16, 2009 at 11:25 AM, Henrik Feldt <[email protected]> wrote:
>>
>> [TestFixture]
>>
>> public class FileTransactionWithTXManagerTests : TestFixtureSetupBase
>>
>> {
>>
>> private string _DirPath;
>>
>>
>>
>> // just if I'm curious and want to see that the file exists with my own
>> eyes :p
>>
>> private bool _DeleteAtEnd;
>>
>> private string _FilePath;
>>
>>
>>
>> [SetUp]
>>
>> public void Setup()
>>
>> {
>>
>>            _DirPath = "../../Transactions/".CombineAssert("tmp");
>>
>>            _FilePath = _DirPath.Combine("test.txt");
>>
>>
>>
>>            if (File.Exists(_FilePath))
>>
>>                       File.Delete(_FilePath);
>>
>>
>>
>>            _DeleteAtEnd = true;
>>
>> }
>>
>>
>>
>> [TearDown]
>>
>> public void TearDown()
>>
>> {
>>
>>            if (_DeleteAtEnd && Directory.Exists(_DirPath))
>>
>>                       Directory.Delete(_DirPath, true);
>>
>> }
>>
>>
>>
>> [Test]
>>
>> public void TransactionResources_AreDisposed()
>>
>> {
>>
>>            var txM = _Container.Resolve<ITransactionManager>();
>>
>>            var t = txM.CreateTransaction(TransactionMode.Requires,
>> IsolationMode.Unspecified);
>>
>>            var resource = new TestResource();
>>
>>            t.Enlist(resource);
>>
>>            t.Begin();
>>
>>            // lalala
>>
>>            t.Rollback();
>>
>>            txM.Dispose(t);
>>
>>
>>
>>            Assert.That(resource.wasDisposed);
>>
>> }
>>
>>
>>
>> private class TestResource : IResource, IDisposable
>>
>> {
>>
>>            public bool wasDisposed;
>>
>>
>>
>>            public void Start()
>>
>>            {
>>
>>            }
>>
>>
>>
>>            public void Commit()
>>
>>            {
>>
>>            }
>>
>>
>>
>>            public void Rollback()
>>
>>            {
>>
>>            }
>>
>>
>>
>>            public void Dispose()
>>
>>            {
>>
>>                       wasDisposed = true;
>>
>>            }
>>
>> }
>>
>>
>>
>> [Test]
>>
>> public void NestedFileTransaction_CanBeCommitted()
>>
>> {
>>
>>            var txM = _Container.Resolve<ITransactionManager>();
>>
>>            Assert.That(txM.CurrentTransaction, Is.Null);
>>
>>
>>
>>            var stdTx = txM.CreateTransaction(TransactionMode.Requires,
>> IsolationMode.Unspecified);
>>
>>            stdTx.Begin();
>>
>>
>>
>>            Assert.That(txM.CurrentTransaction, Is.Not.Null);
>>
>>            Assert.That(txM.CurrentTransaction, Is.EqualTo(stdTx));
>>
>>
>>
>>            // invocation.Proceed() in interceptor
>>
>>
>>
>>            var childTx = txM.CreateTransaction(TransactionMode.Requires,
>> IsolationMode.Unspecified);
>>
>>            Assert.That(childTx,
>> Is.InstanceOfType(typeof(ChildTransaction)));
>>
>>            Assert.That(txM.CurrentTransaction, Is.EqualTo(childTx),
>>
>>                       "Now that we have created a child, it's the current
>> tx.");
>>
>>
>>
>>            var txF = new FileTransaction();
>>
>>            childTx.Enlist(new FileResourceAdapter(txF));
>>
>>            childTx.Begin();
>>
>>
>>
>>            txF.WriteAllText(_FilePath, "Hello world");
>>
>>
>>
>>            childTx.Commit();
>>
>>            stdTx.Commit();
>>
>>
>>
>>            Assert.That(File.Exists(_FilePath));
>>
>>            Assert.That(File.ReadAllLines(_FilePath)[0], Is.EqualTo("Hello
>> world"));
>>
>>
>>
>>            // first we need to dispose the child transaction.
>>
>>            txM.Dispose(childTx);
>>
>>
>>
>>            // now we can dispose the main transaction.
>>
>>            txM.Dispose(stdTx);
>>
>>
>>
>>            Assert.That(txF.Status,
>> Is.EqualTo(TransactionStatus.Committed));
>>
>>            Assert.That(txF.IsDisposed);
>>
>> }
>>
>>
>>
>> [Test]
>>
>> public void
>> Container_InjectsTransactions_IfTransactionInjectAttribute_is_set()
>>
>> {
>>
>>            _Container.AddComponent("AClass", typeof (ISomething), typeof
>> (AClass));
>>
>>
>>
>>            var something = _Container.Resolve<ISomething>();
>>
>>
>>
>>            Assert.That(something, Is.Not.Null);
>>
>>
>>
>>            something.A(null);
>>
>>            something.B(null);
>>
>> }
>>
>>
>>
>> [Transactional]
>>
>> public class AClass : ISomething
>>
>> {
>>
>>            [Transaction]
>>
>>            public void A(ITransaction tx)
>>
>>            {
>>
>>                       Assert.That(tx, Is.Null);
>>
>>            }
>>
>>
>>
>>            [Transaction, InjectTransaction]
>>
>>            public void B(ITransaction tx)
>>
>>            {
>>
>>                       Assert.That(tx, Is.Not.Null);
>>
>>            }
>>
>> }
>>
>>
>>
>> public interface ISomething
>>
>> {
>>
>>            void A(ITransaction tx);
>>
>>            void B(ITransaction tx);
>>
>> }
>>
>> }
>>
>>
>>
>>            [TestFixture]
>>
>>            public class FileTransactionTests
>>
>>            {
>>
>>                       private string dllPath;
>>
>>                       private string testFixturePath;
>>
>>                       private List<string> filesCreated = new
>> List<string>();
>>
>>
>>
>>                       [TestFixtureSetUp]
>>
>>                       public void Setup()
>>
>>                       {
>>
>>                                  dllPath = Environment.CurrentDirectory;
>>
>>                                  testFixturePath = dllPath.Combine(
>> "..\\..\\Kernel" );
>>
>>                       }
>>
>>
>>
>>                       [TearDown]
>>
>>                       public void RemoveAllCreatedFiles()
>>
>>                       {
>>
>>                                  foreach (var filePath in filesCreated)
>>
>>                                  {
>>
>>                                             if (File.Exists(filePath))
>>
>>
>> File.Delete(filePath);
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [SetUp]
>>
>>                       public void CleanOutListEtc()
>>
>>                       {
>>
>>                                  filesCreated.Clear();
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CTorTests()
>>
>>                       {
>>
>>                                  var t = new FileTransaction();
>>
>>                                  Assert.That(t.Status,
>> Is.EqualTo(TransactionStatus.NoTransaction));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CreateFileTranscationally_Commit()
>>
>>                       {
>>
>>                                  var filepath =
>> testFixturePath.CombineAssert("temp").Combine("test");
>>
>>
>>
>>                                  Assert.That(!File.Exists(filepath), "We
>> must clean out the file after every test.");
>>
>>
>>
>>                                  filesCreated.Add(filepath);
>>
>>
>>
>>                                  using (var tx = new
>> FileTransaction("Commit TX"))
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>                                             tx.WriteAllText(filepath,
>> "Transactioned file.");
>>
>>                                             tx.Commit();
>>
>>
>>
>>                                             Assert.That(tx.Status ==
>> TransactionStatus.Committed);
>>
>>                                  }
>>
>>
>>
>>                                  Assert.That(File.Exists(filepath), "The
>> file should exists after the transaction.");
>>
>>
>> Assert.That(File.ReadAllLines(filepath)[0], Is.EqualTo("Transactioned
>> file."));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CreateFileAndReplaceContents()
>>
>>                       {
>>
>>                                  var filePath =
>> testFixturePath.CombineAssert("temp")
>>
>>                                             .Combine("temp__");
>>
>>                                  filesCreated.Add(filePath);
>>
>>
>>
>>                                  // simply write something to to file.
>>
>>                                  using (var wr =
>> File.CreateText(filePath))
>>
>>                                             wr.WriteLine("Hello");
>>
>>
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>
>>
>>                                             using ( var fs = (tx as
>> IFileAdapter).Create(filePath) )
>>
>>                                             {
>>
>>                                                       var str = new
>> UTF8Encoding().GetBytes("Goodbye");
>>
>>                                                       fs.Write(str, 0,
>> str.Length);
>>
>>                                                       fs.Flush();
>>
>>                                             }
>>
>>
>>
>>                                             tx.Commit();
>>
>>                                  }
>>
>>
>>
>>
>> Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Goodbye"));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CreateFileTransactionally_Rollback()
>>
>>                       {
>>
>>                                  var filePath =
>> testFixturePath.CombineAssert("temp")
>>
>>                                             .Combine("temp2");
>>
>>                                  filesCreated.Add(filePath);
>>
>>
>>
>>                                  // simply write something to to file.
>>
>>                                  using (var wr =
>> File.CreateText(filePath))
>>
>>                                             wr.WriteLine("Hello");
>>
>>
>>
>>                                  using (var tx = new
>> FileTransaction("Rollback tx"))
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>
>>
>>                                             using (var fs =
>> tx.Open(filePath, FileMode.Truncate))
>>
>>                                             {
>>
>>                                                       var str = new
>> UTF8Encoding().GetBytes("Goodbye");
>>
>>                                                       fs.Write(str, 0,
>> str.Length);
>>
>>                                                       fs.Flush();
>>
>>                                             }
>>
>>
>>
>>                                             tx.Rollback();
>>
>>                                  }
>>
>>
>>
>>
>> Assert.That(File.ReadAllLines(filePath)[0], Is.EqualTo("Hello"));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void
>> Using_TransactionScope_IsDistributed_AlsoTestingStatusWhenRolledBack()
>>
>>                       {
>>
>>                                  using (new TransactionScope())
>>
>>                                  {
>>
>>                                             using (var tx = new
>> FileTransaction())
>>
>>                                             {
>>
>>                                                       tx.Begin();
>>
>>
>>
>>
>> Assert.That(tx.DistributedTransaction);
>>
>>
>>
>>                                                       tx.Rollback();
>>
>>
>> Assert.That(tx.IsRollbackOnlySet);
>>
>>
>> Assert.That(tx.Status, Is.EqualTo(TransactionStatus.RolledBack));
>>
>>                                             }
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CannotCommitAfterSettingRollbackOnly()
>>
>>                       {
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>                                             tx.SetRollbackOnly();
>>
>>                                             try
>>
>>                                             {
>>
>>                                                       tx.Commit();
>>
>>                                                       Assert.Fail("Could
>> commit when rollback only was set.");
>>
>>                                             }
>>
>>                                             catch (TransactionException)
>>
>>                                             {
>>
>>                                             }
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void FailingResource_TxStillRolledBack()
>>
>>                       {
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Enlist(new R());
>>
>>                                             tx.Begin();
>>
>>                                             try
>>
>>                                             {
>>
>>                                                       try
>>
>>                                                       {
>>
>>
>> tx.Rollback();
>>
>>
>> Assert.Fail("Tests is wrong or the transaction doesn't rollback
>> resources.");
>>
>>                                                       }
>>
>>                                                       catch
>> (ApplicationException)
>>
>>                                                       {
>>
>>                                                        }
>>
>>
>>
>>
>> Assert.That(tx.Status == TransactionStatus.RolledBack);
>>
>>                                             }
>>
>>                                             catch
>> (RollbackResourceException rex)
>>
>>                                             {
>>
>>                                                       // good.
>>
>>
>> Assert.That(rex.FailedResources[0], Is.InstanceOfType(typeof(R)));
>>
>>                                             }
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void
>> WhenCallingBeginTwice_WeSimplyReturn_Also_TestForRollbackedState()
>>
>>                       {
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             Assert.That(tx.Status,
>> Is.EqualTo(TransactionStatus.NoTransaction));
>>
>>                                             tx.Begin();
>>
>>                                             Assert.That(tx.Status,
>> Is.EqualTo(TransactionStatus.Active));
>>
>>
>> Assert.That(tx.DistributedTransaction, Is.False);
>>
>>                                             tx.Begin();
>>
>>
>> Assert.That(tx.DistributedTransaction, Is.False, "Starting the same
>> transaction twice should make no difference.");
>>
>>                                             Assert.That(tx.Status,
>> Is.EqualTo(TransactionStatus.Active));
>>
>>                                             tx.Commit();
>>
>>                                             Assert.That(tx.Status,
>> Is.EqualTo(TransactionStatus.Committed));
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       #region Resource
>>
>>
>>
>>                       private class R : IResource
>>
>>                       {
>>
>>                                  public void Start()
>>
>>                                  {
>>
>>                                  }
>>
>>
>>
>>                                  public void Commit()
>>
>>                                  {
>>
>>                                  }
>>
>>
>>
>>                                  public void Rollback()
>>
>>                                  {
>>
>>                                             throw new
>> ApplicationException("Expected.");
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       #endregion
>>
>>
>>
>>                       [Test,
>> ExpectedException(typeof(TransactionException))]
>>
>>                       public void InvalidStateOnCreate_Throws()
>>
>>                       {
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             (tx as
>> IDirectoryAdapter).Create("lol");
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       // Directories
>>
>>
>>
>>                       [Test]
>>
>>                       public void CanCreateAndFindDirectoryWithinTx()
>>
>>                       {
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>                                             Assert.That((tx as
>> IDirectoryAdapter).Exists("something"), Is.Not.True);
>>
>>                                             (tx as
>> IDirectoryAdapter).Create("something");
>>
>>                                             Assert.That((tx as
>> IDirectoryAdapter).Exists("something"));
>>
>>                                             tx.Rollback();
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void
>> CreatingFolder_InTransaction_AndCommitting_MeansExistsAfter()
>>
>>                       {
>>
>>                                  var directoryPath = "testing";
>>
>>
>> Assert.That(Directory.Exists(directoryPath), Is.False);
>>
>>
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>                                             (tx as
>> IDirectoryAdapter).Create(directoryPath);
>>
>>                                             tx.Commit();
>>
>>                                  }
>>
>>
>>
>>
>> Assert.That(Directory.Exists(directoryPath));
>>
>>
>>
>>                                  Directory.Delete(directoryPath);
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void NoCommit_MeansNoDirectory()
>>
>>                       {
>>
>>                                  var directoryPath = "testing";
>>
>>
>> Assert.That(Directory.Exists(directoryPath), Is.False);
>>
>>
>>
>>                                  using (var tx = new FileTransaction())
>>
>>                                  {
>>
>>                                             tx.Begin();
>>
>>                                             (tx as
>> IDirectoryAdapter).Create(directoryPath);
>>
>>                                  }
>>
>>
>>
>>
>> Assert.That(!Directory.Exists(directoryPath));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void NonExistentDir()
>>
>>                       {
>>
>>                                  using ( var t = new FileTransaction())
>>
>>                                  {
>>
>>                                             t.Begin();
>>
>>                                             var dir = (t as
>> IDirectoryAdapter);
>>
>>
>> Assert.IsFalse(dir.Exists("/hahaha"));
>>
>>
>> Assert.IsFalse(dir.Exists("another_non_existent"));
>>
>>                                             dir.Create("existing");
>>
>>
>> Assert.IsTrue(dir.Exists("existing"));
>>
>>                                  }
>>
>>                                  // no commit
>>
>>
>> Assert.IsFalse(Directory.Exists("existing"));
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void TestDeleteRecursive()
>>
>>                       {
>>
>>                                  // 1. Create directory
>>
>>                                  var pr = dllPath.Combine("testing");
>>
>>                                  Directory.CreateDirectory(pr);
>>
>>
>> Directory.CreateDirectory(pr.Combine("one"));
>>
>>
>> Directory.CreateDirectory(pr.Combine("two"));
>>
>>
>> Directory.CreateDirectory(pr.Combine("three"));
>>
>>
>>
>>
>> File.WriteAllLines(pr.Combine("one").Combine("fileone"), new[] { "Hello
>> world", "second line" });
>>
>>
>> File.WriteAllLines(pr.Combine("one").Combine("filetwo"), new[] { "two",
>> "second line" });
>>
>>
>> File.WriteAllLines(pr.Combine("two").Combine("filethree"), new[] { "three",
>> "second line" });
>>
>>
>>
>>                                  using (var t = new FileTransaction())
>>
>>                                  {
>>
>>                                             t.Begin();
>>
>>                                             Assert.IsTrue((t as
>> IDirectoryAdapter).Delete(pr, true));
>>
>>                                             t.Commit();
>>
>>                                  }
>>
>>                       }
>>
>>
>>
>>                       [Test]
>>
>>                       public void CanMove_FileOrDirectory()
>>
>>                       {
>>
>>                                  // TODO: implement using the for tx
>> kernel...
>>
>>                       }
>>
>>
>>
>>
>>
>> And a whole host of tests for the map util and path util as well, but I’ll
>> do those as you commit it.
>>
>>
>>
>> Cheers
>>
>> Henrik
>>
>>
>>
>>
>>
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Roelof Blom
>> Sent: den 16 juni 2009 09:57
>> To: <a href="mailto:castle-project-de...@googlegroup

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Development List" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-devel?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to