daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Linq;
  2.  
  3. using Crimson.ForTesting;
  4. using Crimson.Infrastructure.Configuration;
  5. using Crimson.Infrastructure.Persistence;
  6.  
  7. using FluentNHibernate.Cfg;
  8. using FluentNHibernate.Mapping;
  9.  
  10. using Machine.Specifications;
  11. using Machine.Specifications.Utility;
  12.  
  13. using NHibernate;
  14.  
  15. using Rhino.Mocks;
  16.  
  17. namespace Crimson.Infrastructure.Tests.Persistence
  18. {
  19.     public class MappedClass
  20.     {
  21.         protected MappedClass()
  22.         {
  23.         }
  24.  
  25.         public virtual int Id
  26.         {
  27.             get;
  28.             set;
  29.         }
  30.     }
  31.  
  32.     public class MappedClassMap : ClassMap<MappedClass>
  33.     {
  34.         public MappedClassMap()
  35.         {
  36.             Id(x => x.Id);
  37.         }
  38.     }
  39.  
  40.     [Subject(typeof(NHibernateSessionFactory))]
  41.     public class When_the_NHibernate_session_factory_is_created
  42.     {
  43.         static IPersistenceConfiguration PersistenceConfiguration;
  44.         static INHibernatePersistenceModel PersistenceModel;
  45.         static NHibernateSessionFactory Factory;
  46.         static INHibernateInitializationAware[] Initializers;
  47.  
  48.         Establish context = () =>
  49.             {
  50.                 PersistenceConfiguration = MockRepository.GenerateStub<IPersistenceConfiguration>();
  51.                 PersistenceConfiguration
  52.                     .Stub(x => x.GetConfiguration())
  53.                     .Return(new SQLiteInMemoryDatabaseConfiguration().GetConfiguration());
  54.  
  55.                 PersistenceModel = MockRepository.GenerateStub<INHibernatePersistenceModel>();
  56.                 PersistenceModel
  57.                     .Stub(x => x.AddMappings(null))
  58.                     .IgnoreArguments()
  59.                     .WhenCalled(x =>
  60.                         {
  61.                             MappingConfiguration config = (MappingConfiguration) x.Arguments.First();
  62.                             config.FluentMappings.Add<MappedClassMap>();
  63.                         });
  64.  
  65.                 Initializers = new[]
  66.                                {
  67.                                 MockRepository.GenerateStub<INHibernateInitializationAware>(),
  68.                                 MockRepository.GenerateStub<INHibernateInitializationAware>()
  69.                                };
  70.             };
  71.  
  72.         Because of = () => { new NHibernateSessionFactory(PersistenceConfiguration, PersistenceModel, Initializers); };
  73.  
  74.         It should_retrieve_the_persistence_configuration =
  75.             () => PersistenceConfiguration.AssertWasCalled(x => x.GetConfiguration());
  76.  
  77.         It should_add_mappings_from_the_persistence_model =
  78.             () => PersistenceModel.AssertWasCalled(x => x.AddMappings(Arg<MappingConfiguration>.Is.NotNull));
  79.  
  80.         It should_invoke_the_initializers_before_initialization =
  81.             () => Initializers.Each(x => x.AssertWasCalled(i => i.BeforeInitialization()));
  82.  
  83.         It should_invoke_the_initializers_while_configuring =
  84.             () => Initializers.Each(x => x.AssertWasCalled(i => i.Configuring(Arg<NHibernate.Cfg.Configuration>.Is.NotNull),
  85.                                                            // First call: by the NHSF, second call by FNH.                                          
  86.                                                            o => o.Repeat.Twice()));
  87.  
  88.         It should_invoke_the_initializers_with_the_actual_configuration =
  89.             () => Initializers.Each(x => x.AssertWasCalled(i => i.Configured(Arg<NHibernate.Cfg.Configuration>.Is.NotNull)));
  90.  
  91.         It should_invoke_the_initializers_with_the_session_factory =
  92.             () => Initializers.Each(x => x.AssertWasCalled(i => i.Initialized(Arg<NHibernate.Cfg.Configuration>.Is.NotNull,
  93.                                                                               Arg<ISessionFactory>.Is.NotNull)));
  94.     }
  95.  
  96.     [Subject(typeof(NHibernateSessionFactory))]
  97.     public class When_a_NHibernate_session_created
  98.     {
  99.         static IPersistenceConfiguration PersistenceConfiguration;
  100.         static INHibernatePersistenceModel PersistenceModel;
  101.         static ISession Session;
  102.         static NHibernateSessionFactory Factory;
  103.  
  104.         Establish context = () =>
  105.             {
  106.                 PersistenceConfiguration = MockRepository.GenerateStub<IPersistenceConfiguration>();
  107.                 PersistenceConfiguration
  108.                     .Stub(x => x.GetConfiguration())
  109.                     .Return(new SQLiteInMemoryDatabaseConfiguration().GetConfiguration());
  110.  
  111.                 PersistenceModel = MockRepository.GenerateStub<INHibernatePersistenceModel>();
  112.                 PersistenceModel
  113.                     .Stub(x => x.AddMappings(null))
  114.                     .IgnoreArguments()
  115.                     .WhenCalled(x =>
  116.                         {
  117.                             MappingConfiguration config = (MappingConfiguration) x.Arguments.First();
  118.                             config.FluentMappings.Add<MappedClassMap>();
  119.                         });
  120.  
  121.                 Factory = new NHibernateSessionFactory(PersistenceConfiguration,
  122.                                                        PersistenceModel,
  123.                                                        new[] { MockRepository.GenerateStub<INHibernateInitializationAware>() });
  124.             };
  125.  
  126.         Because of = () => { Session = Factory.CreateSession(); };
  127.  
  128.         It should_be_able_to_create_a_session =
  129.             () => Session.ShouldNotBeNull();
  130.    
  131.         It should_create_a_session_that_flushes_on_commit =
  132.             () => Session.FlushMode.ShouldEqual(FlushMode.Commit);
  133.     }
  134. }
RAW Paste Data
Top