User: mzywitza
Date: 2010/01/12 12:46 PM

Added:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/
  ScopedConversationTests.cs

Modified:
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
  Castle.ActiveRecord.Tests-vs2008.csproj
 /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/
  ConversationScenarioTest.cs
 /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Scopes/
  IConversation.cs, ScopedConversation.cs

Log:
 IConversation update.

File Changes:

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/
=============================================================

File [modified]: Castle.ActiveRecord.Tests-vs2008.csproj
Delta lines: +90 -42
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ConversationScenarioTest.cs
   2010-01-12 15:35:00 UTC (rev 6646)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ConversationScenarioTest.cs
   2010-01-12 19:46:52 UTC (rev 6647)
@@ -61,26 +61,6 @@
             conversation.Dispose();
         }
 
-       private void ArrangeRecords()
-       {
-               BlogLazy blog = new BlogLazy()
-                                       {
-                                               Author = "Markus",
-                                               Name = "Conversations"
-                                       };
-               PostLazy post = new PostLazy()
-                                       {
-                                               Blog = blog,
-                                               Category = "Scenario",
-                                               Title = "The Convesration is 
here",
-                                               Contents = "A new way for AR in 
fat clients",
-                                               Created = new DateTime(2010, 1, 
1),
-                                               Published = true
-                                       };
-               blog.Save();
-               post.Save();
-       }
-
        [Test]
                public void CanCancelConversations()
                {
@@ -104,38 +84,106 @@
                }
                        Assert.That(BlogLazy.FindAll().First().Author, 
Is.EqualTo("Markus"));
                }
-    }
 
-       [TestFixture]
-       public class ScopedConversationTests : NUnitInMemoryTest
-       {
-               public override Type[] GetTypes()
+               [Test]
+               public void CanSetFlushModeToNever()
                {
-                       return new[] { typeof(BlogLazy), typeof(PostLazy) };
+                       ArrangeRecords();
+
+                       using (var conversation = new 
ScopedConversation(ConversationFlushMode.Explicit))
+                       {
+                               BlogLazy blog;
+                               using (new ConversationalScope(conversation))
+                               {
+                                       blog = BlogLazy.FindAll().First();
+                                       blog.Author = "Anonymous";
+                                       blog.Save();
+                                       BlogLazy.FindAll(); // Triggers 
flushing if allowed
+                               }
+
+                               Assert.That(blog.Author, 
Is.EqualTo("Anonymous"));
+
+                               // Outside any ConversationalScope 
session-per-request is used
+                               Assert.That(BlogLazy.FindAll().First().Author, 
Is.EqualTo("Markus"));
+
+                               conversation.Flush();
+                       }
+
+                       Assert.That(BlogLazy.FindAll().First().Author, 
Is.EqualTo("Anonymous"));
                }
-               
+
                [Test]
-               public void SessionsAreKeptThroughoutTheConversation()
+               public void CanSetFlushModeToOnClose()
                {
-                       IScopeConversation conversation = new 
ScopedConversation();
-                       ISession session = null;
+                       ArrangeRecords();
 
-                       using (new ConversationalScope(conversation))
+                       using (var conversation = new 
ScopedConversation(ConversationFlushMode.OnClose))
                        {
-                               BlogLazy.FindAll();
-                               session = BlogLazy.Holder.CreateSession(typeof 
(BlogLazy));
-                       }
+                               BlogLazy blog;
+                               using (new ConversationalScope(conversation))
+                               {
+                                       blog = BlogLazy.FindAll().First();
+                                       blog.Author = "Anonymous";
+                                       blog.Save();
+                                       BlogLazy.FindAll(); // Triggers 
flushing if allowed
+                               }
 
-                       Assert.That(session.IsOpen);
+                               Assert.That(blog.Author, 
Is.EqualTo("Anonymous"));
 
-                       using (new ConversationalScope(conversation))
-                       {
-                               BlogLazy.FindAll();
-                               
Assert.That(BlogLazy.Holder.CreateSession(typeof(BlogLazy)), 
Is.SameAs(session));
+                               // Outside any ConversationalScope 
session-per-request is used
+                               Assert.That(BlogLazy.FindAll().First().Author, 
Is.EqualTo("Markus"));
+
+                               // conversation.Flush(); // Only needed when 
set to explicit
                        }
 
-                       conversation.Dispose();
-                       Assert.That(session.IsOpen, Is.False);
+                       Assert.That(BlogLazy.FindAll().First().Author, 
Is.EqualTo("Anonymous"));
                }
-       }
+
+       [Test]
+       public void CanRestartAConversationWithFreshSessions()
+       {
+               ISession s1, s2;
+               using (var c = new ScopedConversation())
+               {
+                       using (new ConversationalScope(c))
+                       {
+                               BlogLazy.FindAll();
+                               s1 = BlogLazy.Holder.CreateSession(typeof 
(BlogLazy));
+                       }
+                               
+                               c.Restart();
+
+                               using (new ConversationalScope(c))
+                               {
+                                       BlogLazy.FindAll();
+                                       s2 = 
BlogLazy.Holder.CreateSession(typeof(BlogLazy));
+                               }                       
+
+                               Assert.That(s1, Is.Not.SameAs(s2));
+                       Assert.That(s1.IsOpen, Is.False);
+                       Assert.That(s2.IsOpen, Is.True);
+               }
+       }
+
+
+       private void ArrangeRecords()
+       {
+               BlogLazy blog = new BlogLazy()
+                                       {
+                                               Author = "Markus",
+                                               Name = "Conversations"
+                                       };
+               PostLazy post = new PostLazy()
+                                       {
+                                               Blog = blog,
+                                               Category = "Scenario",
+                                               Title = "The Convesration is 
here",
+                                               Contents = "A new way for AR in 
fat clients",
+                                               Created = new DateTime(2010, 1, 
1),
+                                               Published = true
+                                       };
+               blog.Save();
+               post.Save();
+       }
+    }

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/
==========================================================================

File [modified]: ConversationScenarioTest.cs
Delta lines: +142 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ScopedConversationTests.cs
                            (rev 0)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Conversation/ScopedConversationTests.cs
    2010-01-12 19:46:52 UTC (rev 6647)
@@ -0,0 +1,142 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+// 
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// 
+//     http://www.apache.org/licenses/LICENSE-2.0
+// 
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+namespace Castle.ActiveRecord.Tests.Conversation
+{
+       using System;
+       using Framework;
+       using NHibernate;
+       using NUnit.Framework;
+
+       [TestFixture]
+       public class ScopedConversationTests : NUnitInMemoryTest
+       {
+               public override Type[] GetTypes()
+               {
+                       return new[] { typeof(BlogLazy), typeof(PostLazy) };
+               }
+               
+               [Test]
+               public void SessionsAreKeptThroughoutTheConversation()
+               {
+                       IScopeConversation conversation = new 
ScopedConversation();
+                       ISession session = null;
+
+                       using (new ConversationalScope(conversation))
+                       {
+                               BlogLazy.FindAll();
+                               session = BlogLazy.Holder.CreateSession(typeof 
(BlogLazy));
+                       }
+
+                       Assert.That(session.IsOpen);
+
+                       using (new ConversationalScope(conversation))
+                       {
+                               BlogLazy.FindAll();
+                               
Assert.That(BlogLazy.Holder.CreateSession(typeof(BlogLazy)), 
Is.SameAs(session));
+                       }
+
+                       conversation.Dispose();
+                       Assert.That(session.IsOpen, Is.False);
+               }
+       
+               [Test]
+               public void ThrowsReasonableErrorMessageWhenUsedAfterCancel()
+               {
+                       using (var conversation = new ScopedConversation())
+                       {
+                               conversation.Cancel();
+
+                               var ex = 
Assert.Throws<ActiveRecordException>(() =>
+                                                                               
{
+                                                                               
        using (new ConversationalScope(conversation))
+                                                                               
                BlogLazy.FindAll();
+                                                                               
}
+                                       );
+
+                               Assert.That(ex.Message.Contains("cancel"));
+                               
Assert.That(ex.Message.Contains("ConversationalScope"));
+                               Assert.That(ex.Message.Contains("session"));
+                               Assert.That(ex.Message.Contains("request"));
+                       }
+               }
+
+               [Test]
+               public void CannotFlushAfterCancel()
+               {
+                       using (var conv = new 
ScopedConversation(ConversationFlushMode.Explicit))
+                       {
+                               conv.Cancel();
+                               Assert.Throws<ActiveRecordException>(() => { 
conv.Flush(); });
+                       }
+               }
+
+               [Test]
+               public void CanSetFlushModeAfterCreation()
+               {
+                       using (var conv=new ScopedConversation())
+                       {
+                               conv.FlushMode = ConversationFlushMode.Explicit;
+                               Assert.That(conv.FlushMode, 
Is.EqualTo(ConversationFlushMode.Explicit));
+                       }
+               }
+
+               [Test]
+               public void CannotSetFlushModeAfterUsingTheConversation()
+               {
+                       using (var c = new ScopedConversation())
+                       {
+                               using (new ConversationalScope(c))
+                               {
+                                       BlogLazy.FindAll();
+                               }
+
+                               var ex = Assert.Throws<ActiveRecordException>(
+                                       
()=>c.FlushMode=ConversationFlushMode.Explicit);
+
+                               Assert.That(ex.Message, Is.Not.Null
+                                       .And.Contains("FlushMode")
+                                       .And.Contains("set")
+                                       .And.Contains("after")
+                                       .And.Contains("session"));
+                       }
+               }
+               [Test]
+               public void CanCheckWhetherAConversationWasCanceled()
+               {
+                       using (var c = new ScopedConversation())
+                       {
+                               Assert.That(c.Canceled, Is.False);
+                               c.Cancel();
+                               Assert.That(c.Canceled);
+                       }
+               }
+
+               [Test]
+               public void CanRestartAConversation()
+               {
+                       using (var c = new ScopedConversation())
+                       {
+                               Assert.That(c.Canceled, Is.False);
+                               c.Cancel();
+                               Assert.That(c.Canceled);
+                               c.Restart();
+                               Assert.That(c.Canceled, Is.False);
+                       }
+               }
+
+
+
+       }
+}

File [added]: ScopedConversationTests.cs
Delta lines: +0 -0
===================================================================

Directory: /ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Scopes/
========================================================================

File [modified]: IConversation.cs
Delta lines: +81 -11
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Scopes/ScopedConversation.cs
   2010-01-12 15:35:00 UTC (rev 6646)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord/Framework/Scopes/ScopedConversation.cs
   2010-01-12 19:46:52 UTC (rev 6647)
@@ -27,24 +27,49 @@
        /// </summary>
        public class ScopedConversation : IScopeConversation
        {
+               /// <summary>
+               /// Creates a conversation with <see 
cref="ConversationFlushMode.Automatic"/>.
+               /// </summary>
+               public ScopedConversation() : 
this(ConversationFlushMode.Automatic)
+               {
+               }
+
+               /// <summary>
+               /// Creates a conversation with the chosen flush mode.
+               /// </summary>
+               /// <param name="mode">The flush mode to use</param>
+               public ScopedConversation(ConversationFlushMode mode)
+               {
+                       flushMode = mode;
+               }
+
+               private ConversationFlushMode flushMode;
+
                private bool canceled = false;
+               private Dictionary<ISessionFactory, ISession> openedSessions = 
new Dictionary<ISessionFactory,ISession>();
 
                /// <inheritDoc />
                public void Dispose()
                {
-                       //todo: Add possibility to rollback on failure
+                       ClearSessions();
+               }
+
+               private void ClearSessions()
+               {
                        foreach (var session in openedSessions.Values)
                        {
                                if (canceled)
                                        session.Transaction.Rollback();
                                else
                                {
+                                       if (flushMode != 
ConversationFlushMode.Explicit)
+                                               session.Flush();
                                        session.Transaction.Commit();
-                                       session.Flush();
                                }
 
                                session.Dispose();
                        }
+                       openedSessions.Clear();
                }
 
                /// <inheritDoc />
@@ -54,30 +79,75 @@
                }
 
                /// <inheritDoc />
+               public void Flush()
+               {
+                       AssertNotCanceled();
+                       foreach (var session in openedSessions.Values)
+                       {
+                               session.Flush();
+                       }
+               }
+
+               /// <inheritDoc />
+               public void Restart()
+               {
+                       ClearSessions();
+                       canceled = false;
+               }
+
+               /// <inheritDoc />
+               public ConversationFlushMode FlushMode
+               {
+                       get { return flushMode; }
+                       set
+                       {
+                               if (openedSessions.Count > 0)
+                                       throw new ActiveRecordException(
+                                               "The FlushMode cannot be set 
after the "+
+                                               "conversation was used for the 
first time. "+
+                                               "A session was already opened, 
setting the "+
+                                               "FlushMode must be done before 
any sessions "+
+                                               "are opened.");
+                               flushMode = value;
+                       }
+               }
+
+               /// <inheritDoc />
+               public bool Canceled
+               {
+                       get { return canceled; }
+               }
+
+               /// <inheritDoc />
                public ISession GetSession(ISessionFactory factory, 
IInterceptor interceptor)
                {
+                       AssertNotCanceled();
+                       if (!openedSessions.ContainsKey(factory))
+                               CreateSession(factory, interceptor);
+                       return openedSessions[factory];
+               }
+
+               private void AssertNotCanceled()
+               {
                        if (canceled)
                        {
                                throw new ActiveRecordException(
                                        "A session was requested from a 
conversation that has "+
-                    "been already canceled. Please check that after the "+
+                                       "been already canceled. Please check 
that after the "+
                                        "cancellation of a conversation no more 
"+
-                    "ConversationalScopes are opened using it.");
+                                       "ConversationalScopes are opened using 
it.");
                        }
-                       if (!openedSessions.ContainsKey(factory))
-                               CreateSession(factory, interceptor);
-                       return openedSessions[factory];
                }
 
                private void CreateSession(ISessionFactory factory, 
IInterceptor interceptor)
                {
                        var session = factory.OpenSession(interceptor);
                        session.BeginTransaction();
-                       // todo: allow to configure this
-                       session.FlushMode = FlushMode.Auto;
+                       session.FlushMode =
+                               flushMode == ConversationFlushMode.Automatic ? 
NHibernate.FlushMode.Auto :
+                               flushMode == ConversationFlushMode.OnClose ? 
NHibernate.FlushMode.Commit :
+                               NHibernate.FlushMode.Never;
                        openedSessions.Add(factory,session);
                }
-
-               private Dictionary<ISessionFactory, ISession> openedSessions = 
new Dictionary<ISessionFactory,ISession>();
        }
 }

File [modified]: ScopedConversation.cs
Delta lines: +1 -0
===================================================================

--- 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-12 15:35:00 UTC (rev 6646)
+++ 
ActiveRecord/trunk/src/Castle.ActiveRecord.Tests/Castle.ActiveRecord.Tests-vs2008.csproj
    2010-01-12 19:46:52 UTC (rev 6647)
@@ -338,6 +338,7 @@
     <Compile Include="Config\ConfigureTests.cs" />
     <Compile Include="Config\StorageConfigurationTests.cs" />
     <Compile Include="Conversation\ConversationScenarioTest.cs" />
+    <Compile Include="Conversation\ScopedConversationTests.cs" />
     <Compile Include="DefaultConfigurationsTestCase.cs" />
     <Compile Include="FetchTestCase.cs" />

-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Commits" 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-commits?hl=en.


Reply via email to