public void SetMasterConfiguration()
        {
            var mixinConfiguration = new MixinConfiguration();

            MixinConfiguration.SetMasterConfiguration(mixinConfiguration);
            Assert.That(MixinConfiguration.GetMasterConfiguration(), Is.SameAs(mixinConfiguration));
        }
        public void GetResourceManager_ChangingTheMasterConfigurationOnADifferentThreadIsRecognizedOnOtherThreads_CacheIsNotResetForCurrentFieldValue()
        {
            var backup = MixinConfiguration.GetMasterConfiguration();

            try
            {
                var typeInformation1     = TypeAdapter.Create(typeof(ClassWithoutMultiLingualResourcesAttributes));
                var typeInformation2     = TypeAdapter.Create(typeof(InheritedClassWithoutMultiLingualResourcesAttributes));
                var resourceManagerCache =
                    (ICache <ITypeInformation, IResourceManager>)PrivateInvoke.GetNonPublicField(_globalizationService, "_resourceManagerCache");
                IResourceManager outValue;

                var newMasterConfiguration = MixinConfiguration.BuildNew().BuildConfiguration();

                Dev.Null = _globalizationService.GetResourceManager(typeInformation1);

                Assert.That(resourceManagerCache.TryGetValue(typeInformation1, out outValue), Is.True);
                Assert.That(outValue, Is.Not.Null);
                Assert.That(resourceManagerCache.TryGetValue(typeInformation2, out outValue), Is.False);
                Assert.That(outValue, Is.Null);

                var task = Task.Run(
                    () =>
                {
                    MixinConfiguration.SetMasterConfiguration(newMasterConfiguration);
                    MixinConfiguration.SetActiveConfiguration(newMasterConfiguration);

                    // Populate the cache.
                    Dev.Null = _globalizationService.GetResourceManager(typeInformation1);
                    Dev.Null = _globalizationService.GetResourceManager(typeInformation2);

                    Assert.That(resourceManagerCache.TryGetValue(typeInformation1, out outValue), Is.True);
                    Assert.That(outValue, Is.Not.Null);
                    Assert.That(resourceManagerCache.TryGetValue(typeInformation2, out outValue), Is.True);
                    Assert.That(outValue, Is.Not.Null);
                });

                task.Wait();

                MixinConfiguration.SetActiveConfiguration(newMasterConfiguration);

                // Getting a ResourceManager does not reset the cache because this already happened on other thread.
                Dev.Null = _globalizationService.GetResourceManager(typeInformation1);

                Assert.That(resourceManagerCache.TryGetValue(typeInformation1, out outValue), Is.True);
                Assert.That(outValue, Is.Not.Null);
                Assert.That(resourceManagerCache.TryGetValue(typeInformation2, out outValue), Is.True);
                Assert.That(outValue, Is.Not.Null);
            }
            finally
            {
                MixinConfiguration.SetMasterConfiguration(backup);
                MixinConfiguration.SetActiveConfiguration(backup);
            }
        }
        public void GetMasterConfiguration_Default()
        {
            var oldMasterConfiguration = MixinConfiguration.GetMasterConfiguration();

            MixinConfiguration.SetMasterConfiguration(null);
            var newMasterConfiguration = MixinConfiguration.GetMasterConfiguration();

            Assert.That(newMasterConfiguration, Is.Not.Null);
            Assert.That(newMasterConfiguration, Is.Not.SameAs(oldMasterConfiguration));
            Assert.That(newMasterConfiguration.GetContext(typeof(BaseType1)), Is.Not.Null);
        }
Exemplo n.º 4
0
        public void MasterConfigurationIsCopiedByNewThreads()
        {
            var oldMasterConfiguration = MixinConfiguration.GetMasterConfiguration();

            try
            {
                var newMasterConfiguration = new MixinConfiguration();
                MixinConfiguration.SetMasterConfiguration(newMasterConfiguration);

                ThreadRunner.Run(() => Assert.That(MixinConfiguration.ActiveConfiguration, Is.SameAs(newMasterConfiguration)));
            }
            finally
            {
                MixinConfiguration.SetMasterConfiguration(oldMasterConfiguration);
            }
        }
Exemplo n.º 5
0
        public IResourceManager GetResourceManager(ITypeInformation typeInformation)
        {
            ArgumentUtility.CheckNotNull("typeInformation", typeInformation);

            var masterConfiguration = MixinConfiguration.GetMasterConfiguration();

            if (masterConfiguration != MixinConfiguration.ActiveConfiguration)
            {
                return(GetResourceManagerFromType(typeInformation));
            }

            // During normal operation, the lock-statement is cheap enough as to not matter when accessing the ResourceManager.
            lock (_mixinConfigurationLockObject)
            {
                if (_mixinConfiguration != masterConfiguration)
                {
                    _resourceManagerCache.Clear();
                    _mixinConfiguration = masterConfiguration;
                }
            }

            return(_resourceManagerCache.GetOrCreateValue(typeInformation, GetResourceManagerFromType));
        }
 public void SetUp()
 {
     _oldMasterConfiguration = MixinConfiguration.GetMasterConfiguration();
     _oldActiveConfiguration = MixinConfiguration.ActiveConfiguration;
 }