public void GetCurrent_WithContext_UsesSiteMappings()
        {
            //Arrange
            BSPFarm.SetLocal();
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };
            var siteTypeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SBaseLogger>()
            };
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            sharepointLocator.SiteTypeMappings = siteTypeMappings;

            var site = new MSPSite()
            {
                IDGet = () => TestsConstants.TestGuid,
            };

            var context = new MSPContext
            {
                SiteGet = () => site,
            };

            MSPContext.CurrentGet = () => context;

            //Act
            var target = sharepointLocator.GetCurrent().GetInstance <ILogger>();

            // Assert
            Assert.IsInstanceOfType(target, typeof(SBaseLogger));
        }
Пример #2
0
        public void GetSiteCacheInterval_WithoutConfiguredFarmValue_ReturnsNegativeOne()
        {
            //Arrange
            int    expected    = -1;
            string expectedKey = "Microsoft.Practices.SharePoint.Common.SiteLocatorCacheInterval";
            var    bag         = new BIPropertyBag();
            SPFarm farm        = BSPFarm.SetLocal();

            var context = new SIApplicationContextProvider();

            context.GetCurrentAppDomainFriendlyName = () => "FullTrust";
            context.GetSPFarmLocal = () => farm;
            SharePointEnvironment.ApplicationContextProvider = context;

            var configMgr = new SIConfigManager();

            configMgr.ContainsKeyInPropertyBagStringIPropertyBag = (key, propertyBag) =>
            {
                if (key == expectedKey)
                {
                    return(false);
                }
                return(true);
            };
            configMgr.GetPropertyBagConfigLevel = (cfgLevel) => bag;

            var config = new ServiceLocatorConfig(configMgr);

            //Act
            int target = config.GetSiteCacheInterval();

            //Assert
            Assert.AreEqual(expected, target);
        }
        public void GetCurrent_LoadsSiteTypeMappings()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };
            var siteTypeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SBaseLogger>()
            };

            BSPFarm.SetLocal();
            var expectedSite = new BSPSite();

            expectedSite.ID = TestsConstants.TestGuid;

            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            sharepointLocator.SiteTypeMappings = siteTypeMappings;

            //Act
            var target       = sharepointLocator.GetCurrent(expectedSite);
            var targetLogger = target.GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger));
        }
Пример #4
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]);
        }
        public void GetCurrent_AddNewFarmMappingAlreadyDefinedInSiteUsesSite()
        {
            //Setup
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };

            BSPFarm.SetLocal();
            var expectedSite = new BSPSite {
                ID = TestsConstants.TestGuid
            };

            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = new List <TypeMapping>();
            sharepointLocator.SiteTypeMappings = typeMappings;

            var target      = sharepointLocator.GetCurrent(expectedSite); // get current to make sure it is in memory
            var farmLocator = sharepointLocator.GetCurrent() as ActivatingServiceLocator;

            //Act
            farmLocator.RegisterTypeMapping <ILogger, SBaseLogger>();
            var targetLogger = target.GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(targetLogger, typeof(SharePointLogger));
        }
        public void GetCurrent_SiteUsesCachedInstance()
        {
            //Setup
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };

            BSPFarm.SetLocal();
            var expectedSite = new BSPSite {
                ID = TestsConstants.TestGuid
            };

            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings      = new List <TypeMapping>();
            sharepointLocator.SiteTypeMappings      = typeMappings;
            sharepointLocator.SiteLastUpdatedRetVal = DateTime.Now;

            var target = sharepointLocator.GetCurrent(expectedSite);        // get current to make sure it is in memory

            Assert.IsTrue(sharepointLocator.SiteGotTypeMappingsFromConfig); //getting from config and populating cache

            sharepointLocator.SiteGotTypeMappingsFromConfig = false;        // reset getting config...

            //Act
            sharepointLocator.GetCurrent(expectedSite);


            //Assert
            Assert.IsFalse(sharepointLocator.SiteGotTypeMappingsFromConfig);
        }
        public void GetCurrent_SiteTimeoutReloadDoesntDropProgrammaticOverride()
        {
            //Setup
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };

            BSPFarm.SetLocal();
            var expectedSite = new BSPSite {
                ID = TestsConstants.TestGuid
            };
            var time = new DateTime(2010, 5, 30);

            var sharepointLocator = new TestableSharePointServiceLocator
            {
                FarmTypeMappings = new List <TypeMapping>(),
                SiteTypeMappings = typeMappings
            };

            MDateTime.NowGet = () => time;
            var target = sharepointLocator.GetCurrent(expectedSite);     // get current to make sure it is in memory

            time = time.AddSeconds(SharePointServiceLocator.SiteCachingTimeoutInSeconds + 1);
            sharepointLocator.SiteLastUpdatedRetVal = time;

            //Act
            var initialLogger = target.GetInstance <ILogger>();

            ((ActivatingServiceLocator)target).RegisterTypeMapping <ILogger, SBaseLogger>();
            var targetLogger = sharepointLocator.GetCurrent(expectedSite).GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(initialLogger, typeof(SharePointLogger));
            Assert.IsInstanceOfType(targetLogger, typeof(SBaseLogger));
        }
Пример #8
0
        public void RegisterTypeMapping_SetANewMappingForExistingMapping()
        {
            //Arrange
            List <TypeMapping> mappings = null;
            var propertyBag             = new BIPropertyBag();

            BSPFarm.SetLocal();

            var configMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (stringValue, farm) => false,
                SetInPropertyBagStringObjectIPropertyBag   = (strValue, obj, farm) => mappings = obj as List <TypeMapping>,
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => propertyBag
            };

            configMgr.BehaveAsDefaultValue();

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

            target.RegisterTypeMapping <ISomething, Something>();
            target.RegisterTypeMapping <ISomething, SomethingElse>();
            TypeMapping mapping = mappings.First();

            //Assert
            Assert.IsTrue(mapping.ToType.Contains("SomethingElse"));
        }
        public BSPConfiguredFarm()
        {
            BSPFarm.SetLocal();
            fss = new FarmSettingStore();

            mfarmpo = new MSPPersistedObject(BSPFarm.Local);
            mfarmpo.GetChildString <FarmSettingStore>((s) => fss);
        }
Пример #10
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);
        }
        public void Execute_TracesMessage()
        {
            //Arrange
            var  args       = new TracingOperationArgs();
            Guid createGuid = Guid.Empty;

            args.Message  = TestsConstants.TestGuidName;
            args.EventId  = -99;
            args.Category = TestsConstants.AreasCategories;
            args.Severity = (int)SandboxTraceSeverity.High;
            args.SiteID   = TestsConstants.TestGuid;

            SharePointServiceLocator.ReplaceCurrentServiceLocator(new ActivatingServiceLocator());
            ((ActivatingServiceLocator)SharePointServiceLocator.GetCurrent())
            .RegisterTypeMapping <ILogger, TestTraceLogger>(InstantiationType.AsSingleton);

            var operation = new TracingOperation();

            BSPFarm.SetLocal();
            MSPSite site;
            BSPWeb  bweb = new BSPWeb();
            var     mweb = new MSPWeb(bweb)
            {
                NameGet = () => "foo.bar",
            };


            MSPSite.ConstructorGuid = (instance, g) =>
            {
                site = new MSPSite(instance)
                {
                    Dispose    = () => { },
                    IDGet      = () => TestsConstants.TestGuid,
                    RootWebGet = () => mweb
                };

                createGuid = g;
            };


            //Act
            object target = operation.Execute(args);

            //Assert
            var logger = SharePointServiceLocator.GetCurrent().GetInstance <ILogger>() as TestTraceLogger;

            Assert.IsNotInstanceOfType(target, typeof(Exception));
            Assert.AreEqual(TestsConstants.TestGuid, createGuid);
            Assert.IsTrue(logger.Message.Contains(args.Message));
            Assert.AreEqual(logger.Category, TestsConstants.AreasCategories);
            Assert.AreEqual(logger.EventId, args.EventId);
            Assert.AreEqual(logger.Severity, SandboxTraceSeverity.High);
        }
        public void UnRegisterSucceeds()
        {
            //Arrange
            SPService target = null;

            BSPFarm.SetLocal();
            MSPDiagnosticsServiceBase.GetLocal <DiagnosticsService>(() => new DiagnosticsService());
            MSPService.AllInstances.Delete = (b) => target = b;

            //Act
            DiagnosticsService.Unregister();

            //Assert
            Assert.IsNotNull(target);
            Assert.IsTrue(target.GetType().Equals(typeof(DiagnosticsService)));
        }
        public void GetCurrent_ServiceLocatorIsLoadedWithTypes()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ISomething, Something>("key")
            };
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            BSPFarm.SetLocal();

            //Act
            var target = sharepointLocator.GetCurrent() as ActivatingServiceLocator;

            //Assert
            Assert.IsInstanceOfType(SharePointServiceLocator.GetCurrent(), typeof(ActivatingServiceLocator));
            Assert.IsTrue(target.IsTypeRegistered <ISomething>());
        }
        public void GetCurrent_CanOverrideDefaultTypemapping()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SBaseLogger>()
            };

            BSPFarm.SetLocal();
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;

            //Act
            var target = sharepointLocator.GetCurrent().GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(target, typeof(SBaseLogger));
        }
        public void Execute_WithWrongProxyArgsType_ReturnsArgumentException()
        {
            //Arrange
            var args = new LoggingOperationArgs();

            args.Message  = "foobar";
            args.EventId  = -1;
            args.Category = "foobar";
            args.Severity = (int)SandboxEventSeverity.Error;
            var operation = new TracingOperation();

            BSPFarm.SetLocal();

            //Act
            object target = operation.Execute(args);

            //Assert
            Assert.IsInstanceOfType(target, typeof(ArgumentException));
        }
        public void Execute_WithNoMessageInProxyArgs_ReturnsNullArgumentException()
        {
            //Arrange
            var args = new TracingOperationArgs();

            args.Message  = null;
            args.EventId  = -1;
            args.Category = "foobar";
            args.Severity = (int)SandboxTraceSeverity.High;
            var operation = new TracingOperation();

            BSPFarm.SetLocal();

            //Act
            object target = operation.Execute(args);

            //Assert
            Assert.IsInstanceOfType(target, typeof(ArgumentNullException));
        }
        public void GetCurrent_CanRegisterDifferentServiceLocator()
        {
            //Arrange
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <IServiceLocatorFactory, BIServiceLocatorFactory>()
            };

            BSPFarm.SetLocal();
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;


            //Act
            IServiceLocator target = sharepointLocator.GetCurrent();

            //Assert
            Assert.IsInstanceOfType(target, typeof(SIServiceLocator));
        }
Пример #18
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]);
        }
        public void GetCurrent_LoadsFarmTypeMappings()
        {
            //Setup
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };

            BSPFarm.SetLocal();

            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;

            //Act
            var target       = sharepointLocator.GetCurrent();
            var targetLogger = target.GetInstance <ILogger>();

            //Assert
            Assert.IsInstanceOfType(targetLogger, typeof(SharePointLogger));
        }
Пример #20
0
        public void GetSiteCacheInterval_WithConfiguredFarmValue_ReturnsValue()
        {
            //Arrange
            int    expected    = 30;
            string expectedKey = "Microsoft.Practices.SharePoint.Common.SiteLocatorCacheInterval";
            var    bag         = new BIPropertyBag();

            BSPFarm.SetLocal();

            var configMgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (key, propertyBag) =>
                {
                    if (key == expectedKey)
                    {
                        return(true);
                    }
                    return(false);
                },
                CanAccessFarmConfigGet    = () => true,
                GetPropertyBagConfigLevel = (configlevel) => bag,
            };

            configMgr.GetFromPropertyBagStringIPropertyBag <int>((key, propetyBag) =>
            {
                if (key == expectedKey)
                {
                    return(expected);
                }
                return(-1);
            });

            var config = new ServiceLocatorConfig(configMgr);

            //Act
            int target = config.GetSiteCacheInterval();

            //Assert
            Assert.AreEqual(expected, target);
        }
Пример #21
0
        public void RegisterTypeMapping_SaveNewMapping()
        {
            // Arrange
            object value      = null;
            var    rootConfig = new BIPropertyBag();

            BSPFarm.SetLocal();

            var mgr = new SIConfigManager
            {
                ContainsKeyInPropertyBagStringIPropertyBag = (stringValue, propertyBag) => false,
                SetInPropertyBagStringObjectIPropertyBag   = (strValue, obj, propertyBag) => value = obj as List <TypeMapping>,
                GetPropertyBagConfigLevel = (level) => rootConfig,
                CanAccessFarmConfigGet    = () => true
            };

            mgr.GetFromPropertyBagStringIPropertyBag <ServiceLocationConfigData>((key, bag) => null);

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

            target.RegisterTypeMapping <ISomething, Something>();
            var typeMappings = value as List <TypeMapping>;


            //Assert
            Assert.IsNotNull(typeMappings);
            Assert.IsTrue(typeMappings.Count >= 1);
            TypeMapping mapping = typeMappings[0];

            Assert.AreEqual("Microsoft.Practices.SharePoint.Common.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
                            , mapping.FromAssembly);
            Assert.AreEqual("Microsoft.Practices.SharePoint.Common.Tests.ServiceLocation.ISomething, Microsoft.Practices.SharePoint.Common.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
                            , mapping.FromType);
            Assert.AreEqual("Microsoft.Practices.SharePoint.Common.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
                            , mapping.ToAssembly);
            Assert.AreEqual("Microsoft.Practices.SharePoint.Common.Tests.ServiceLocation.Something, Microsoft.Practices.SharePoint.Common.Tests, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
                            , mapping.ToType);
        }
        public void GetCurrent_WithoutContext_UsesFarmMappings()
        {
            //Arrange
            BSPFarm.SetLocal();
            var typeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SharePointLogger>()
            };
            var siteTypeMappings = new List <TypeMapping> {
                TypeMapping.Create <ILogger, SBaseLogger>()
            };
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = typeMappings;
            sharepointLocator.SiteTypeMappings = siteTypeMappings;


            //Act
            var target = sharepointLocator.GetCurrent().GetInstance <ILogger>();

            // Assert
            Assert.IsInstanceOfType(target, typeof(SharePointLogger));
        }
Пример #23
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);
        }
        public void Current_AttemptLoadingFromCommonServiceLocator_ThrowsErrorMessage()
        {
            //Arrange
            var farm = BSPFarm.SetLocal();
            var sharepointLocator = new TestableSharePointServiceLocator();

            sharepointLocator.FarmTypeMappings = null;
            bool expectedException = false;

            var sl = sharepointLocator.GetCurrent();

            //Act
            try
            {
                var sl1 = ServiceLocator.Current;
            }
            catch (NotSupportedException)
            {
                expectedException = true;
            }

            // Assert
            Assert.IsTrue(expectedException);
        }
Пример #25
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());
        }