コード例 #1
0
        public void RemoveTypeMappingsGeneric_RemoveAMapping()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>(),
                TypeMapping.Create <IConfigManager, ConfigManager>(),
                TypeMapping.Create <IHierarchicalConfig, HierarchicalConfig>()
            };

            var config = new ServiceLocationConfigData(typeMappings);
            var bag    = new BIPropertyBag();

            BSPFarm.SetLocal();
            var cfgMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (key, propertyBag) => true,
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => bag,
            };

            cfgMgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, propetyBag) => config);
            cfgMgr.BehaveAsDefaultValue();

            //Act
            var target = new ServiceLocatorConfig(cfgMgr);

            target.RemoveTypeMappings <ILogger>();
            List <TypeMapping> registeredTypeMappings =
                target.GetTypeMappings().ToList();

            //Assert
            Assert.AreEqual(2, registeredTypeMappings.Count);
            Assert.AreSame(typeMappings[1], registeredTypeMappings[0]);
            Assert.AreSame(typeMappings[2], registeredTypeMappings[1]);
        }
コード例 #2
0
        public void RegisterTypeMapping_SaveNewMappingWithSite()
        {
            var    mappings   = new List <TypeMapping>();
            string savedkey   = null;
            object savedValue = null;
            var    configData = new ServiceLocationConfigData(mappings);

            BSPFarm.SetLocal();
            const string expectedKey = "Microsoft.Practices.SharePoint.Common.TypeMappings";
            var          web         = new MSPWeb();
            var          propBag     = new BIPropertyBag();

            var mgr = new SIConfigManager()
            {
                SetInPropertyBagStringObjectIPropertyBag = (key, value, bag) =>
                {
                    savedkey   = key;
                    savedValue = value;
                },

                ContainsKeyInPropertyBagStringIPropertyBag = (key, bag) =>
                {
                    if (key == expectedKey)
                    {
                        return(true);
                    }
                    return(false);
                },
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (level) => propBag,
                SetWebSPWeb = (w) => { },
            };

            mgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, bag) =>
            {
                if (key == expectedKey)
                {
                    return(configData);
                }
                return(null);
            });

            var configSite = new MSPSite()
            {
                RootWebGet = () => web
            };

            var target = new ServiceLocatorConfig(mgr);

            target.Site = configSite;

            //act
            target.RegisterTypeMapping <ISomething, Something>();

            //assert
            Assert.IsNotNull(savedValue);
            Assert.AreEqual(expectedKey, savedkey);
        }
コード例 #3
0
        private void SetTypeMappingsList(List <TypeMapping> typeMappings)
        {
            // We need to deal with low level serialization to break the dependency between service location and configuration manager, otherwise config mgr and hierarchical config can't be overridden.
            var propertyBag = GetPropertyBag();

            if (propertyBag != null)
            {
                var configData = new ServiceLocationConfigData(typeMappings);
                configData.LastUpdate = DateTime.Now;
                Manager.SetInPropertyBag(GetConfigKey(), configData, propertyBag);
            }
            else
            {
                throw new InvalidOperationException("Resources.ContextMissingSetTypeMappingList");
            }
        }
コード例 #4
0
        public void RemoveTypeMapping_RemoveAMapping()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                new TypeMapping()
                {
                    FromAssembly = "1"
                },
                new TypeMapping()
                {
                    FromAssembly = "2"
                },
                new TypeMapping()
                {
                    FromAssembly = "3"
                }
            };
            var config = new ServiceLocationConfigData(typeMappings);
            var bag    = new BIPropertyBag();

            BSPFarm.SetLocal();
            var cfgMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (key, propertyBag) => true,
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => bag,
            };

            cfgMgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, propetyBag) => config);
            cfgMgr.BehaveAsDefaultValue();

            //Act
            var target = new ServiceLocatorConfig(cfgMgr);

            target.RemoveTypeMapping(typeMappings[0]);
            List <TypeMapping> registeredTypeMappings =
                target.GetTypeMappings().ToList();

            //Assert
            Assert.AreEqual(2, registeredTypeMappings.Count);
            Assert.AreSame(typeMappings[1], registeredTypeMappings[0]);
            Assert.AreSame(typeMappings[2], registeredTypeMappings[1]);
        }
コード例 #5
0
        private ServiceLocationConfigData GetConfigData()
        {
            // We need to deal with low level seralization to break the dependency between service location and  // configuration manager, otherwise config mgr and hierarchical config can't be overridden.
            var propertyBag = GetPropertyBag();
            ServiceLocationConfigData configData = null;

            // In some cases this will be null, like when attempting to load farm service locator config from the sandbox without the proxy installed.
            if (propertyBag != null)
            {
                configData = Manager.GetFromPropertyBag <ServiceLocationConfigData>(GetConfigKey(), propertyBag);
            }
            if (configData == null)
            {
                configData = new ServiceLocationConfigData()
                {
                    LastUpdate = null
                }
            }
            ;
            return(configData);
        }
コード例 #6
0
        public void Deerialize_XmlSerializerFailure_ThrowsConfigurationException()
        {
            //Arrange
            MXmlSerializerFactory.AllInstances.CreateSerializerType = (instance, type) => { throw new TypeLoadException("error"); };
            var  target     = new ConfigSettingSerializer();
            var  testConfig = new ServiceLocationConfigData();
            bool expectedExceptionThrown = false;

            //Act
            try
            {
                target.Deserialize(typeof(ServiceLocationConfigData), "foobar");
            }
            catch (ConfigurationException)
            {
                expectedExceptionThrown = true;
            }

            //Assert caught by expected exception
            Assert.IsTrue(expectedExceptionThrown);
        }
コード例 #7
0
        public void RegisterTypeMapping_RegisterMappingWithAKey()
        {
            //Arrange
            BSPFarm.SetLocal();
            List <TypeMapping> typeMappings = new List <TypeMapping>();
            var configData = new ServiceLocationConfigData();
            var propBag    = new BIPropertyBag();

            int savedCnt  = 0;
            var configMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (s, propertybag) => typeMappings != null,
                SetInPropertyBagStringObjectIPropertyBag   = (s, obj, propertybag) =>
                {
                    savedCnt++;
                },
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => propBag,
            };

            configMgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, propertybag) => configData);

            //Act
            var target = new ServiceLocatorConfig(configMgr as IConfigManager);

            target.RegisterTypeMapping <ISomething, Something>("key1");
            target.RegisterTypeMapping <ISomething, Something>("key2");

            //Assert
            Assert.IsTrue(configData.Count == 2);
            Assert.IsTrue(savedCnt == 2);
            TypeMapping mapping1 = configData[0];
            TypeMapping mapping2 = configData[1];

            Assert.AreEqual("key1", configData[0].Key);
            Assert.AreEqual("key2", configData[1].Key);
        }
コード例 #8
0
        public void GetTypeMappings_GetAllMappings()
        {
            //Arrange
            var setMappings = new List <TypeMapping> {
                new TypeMapping {
                    FromAssembly = "1"
                },
                new TypeMapping {
                    FromAssembly = "2"
                },
                new TypeMapping {
                    FromAssembly = "3"
                }
            };
            var configData = new ServiceLocationConfigData(setMappings);
            var bag        = new BIPropertyBag();

            BSPFarm.SetLocal();

            var cfgMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (strValue, propertyBag) => true,
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => bag
            };

            cfgMgr.BehaveAsDefaultValue();
            cfgMgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((strValue, property) => configData);

            //Act
            var target = new ServiceLocatorConfig(cfgMgr);
            IEnumerable <TypeMapping> registeredTypeMappings = target.GetTypeMappings();

            //Assert
            Assert.AreEqual(3, registeredTypeMappings.Count());
        }